Skip to content

Commit

Permalink
cicd runs unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalyd committed Jul 12, 2024
1 parent ea566ef commit aeb3d62
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
73 changes: 33 additions & 40 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
26 changes: 26 additions & 0 deletions qr-code-generator/main_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit aeb3d62

Please sign in to comment.