diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 86ae1f9..2f2bb71 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,30 +1,32 @@ -name: CI/CD Pipeline - -on: - push: - branches: - - main - jobs: build: runs-on: ubuntu-latest - - services: - docker: - image: docker:latest - # options: --privileged - # ports: - # - 5432:5432 - # - 27017:27017 - # - 5000:5000 - # - 5001:5001 - # - 5002:5002 - # - 5003:5003 - steps: - name: Checkout code uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.22' + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: Run tests for QR Code Generator + working-directory: qr-code-generator + run: | + go test -v ./... + + - name: Install pytest + run: pip install pytest + + - name: Run tests for URL Shortener + working-directory: url-shortener + run: pytest test_url_shortener.py + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 @@ -34,28 +36,19 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Build Docker images + - name: Build and push API image run: | docker build -t docker.io/prajjwalyd/api:latest ./api - docker build -t docker.io/prajjwalyd/url-shortener:latest ./url-shortener - docker build -t docker.io/prajjwalyd/qr-code-generator:latest ./qr-code-generator - docker build -t docker.io/prajjwalyd/analytics:latest ./analytics - - - name: Start services - run: docker-compose up --build - - - name: Run integration tests - run: | - pip install requests - python test_integration.py - - - name: Tear down services - run: docker-compose down - - - name: Push Docker images - if: success() - run: | docker push docker.io/prajjwalyd/api:latest + - name: Build and push URL-Shortener image + run: | + docker build -t docker.io/prajjwalyd/url-shortener:latest ./url-shortener docker push docker.io/prajjwalyd/url-shortener:latest + - name: Build and push QR-Code-Generator image + run: | + docker build -t docker.io/prajjwalyd/qr-code-generator:latest ./qr-code-generator docker push docker.io/prajjwalyd/qr-code-generator:latest - docker push docker.io/prajjwalyd/analytics:latest + - name: Build and push Analytics image + run: | + docker build -t docker.io/prajjwalyd/analytics:latest ./analytics + docker push docker.io/prajjwalyd/analytics:latest \ No newline at end of file diff --git a/qr-code-generator/main_test.go b/qr-code-generator/main_test.go new file mode 100644 index 0000000..26f6343 --- /dev/null +++ b/qr-code-generator/main_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestGenerateQRCode(t *testing.T) { + req, err := http.NewRequest("GET", "/generate_qr?url=http://example.com", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(generateQRCode) + handler.ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) + } + + if ct := rr.Header().Get("Content-Type"); ct != "image/png" { + t.Errorf("handler returned wrong content type: got %v want %v", ct, "image/png") + } +}