diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..dedb624 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "test/libs/bats"] + path = test/libs/bats + url = https://github.com/sstephenson/bats +[submodule "test/libs/bats-assert"] + path = test/libs/bats-assert + url = https://github.com/ztombol/bats-assert +[submodule "test/libs/bats-support"] + path = test/libs/bats-support + url = https://github.com/ztombol/bats-support diff --git a/readme.md b/readme.md index c3b86b0..eea38a5 100644 --- a/readme.md +++ b/readme.md @@ -54,6 +54,17 @@ In general every project requires observability, ci/cd pipelines, environment ma 1. Go the *Actions* tab and open *Deploy Service* Action from the left bar 2. Click on *Run workflow* and provide environment (this should be same as you used while setting up Action) and the service name you want to deploy + +## Running tests +1. Before running tests, update the git submodules + ``` +git submodule init +git submodule update +``` +2. Command to run test +``` +test/test_file_name.bats +``` ## Developer Documentaion 1. [Onboarding a service](./docs/onboarding.md) diff --git a/test/Makefile.mock b/test/Makefile.mock new file mode 100644 index 0000000..f6d9183 --- /dev/null +++ b/test/Makefile.mock @@ -0,0 +1,14 @@ +# Makefile.mock +FORCE_RECREATE_FLAG := $(if $(filter 1,$(ENABLE_FORCE_RECREATE)),--force-recreate,) +REMOVE_ORPHANS_FLAG := $(if $(or $(services),$(filter 1, $(DISABLE_REMOVE_ORPHANS))),,--remove-orphans) +REMOVE_ANSI_FLAG := $(if $(filter 1,$(DISABLE_ANSI)),,--ansi never) + +DOCKER_COMPOSE_COMMAND=echo "Mock Docker Compose Command: docker compose $(REMOVE_ANSI_FLAG) -p bhasai" + +deploy: $(if $(filter 1,$(ENABLE_GIT_PULL)),git-pull,) $(if $(filter 1,$(DISABLE_PULL)),,pull build) reload-caddy + $(DOCKER_COMPOSE_COMMAND) up -d $(FORCE_RECREATE_FLAG) $(REMOVE_ORPHANS_FLAG) ${services} + +git-pull: + @echo "Mock: git pull" + +.PHONY: deploy reload-caddy pull build git-pull diff --git a/test/libs/bats b/test/libs/bats new file mode 160000 index 0000000..0360811 --- /dev/null +++ b/test/libs/bats @@ -0,0 +1 @@ +Subproject commit 03608115df2071fff4eaaff1605768c275e5f81f diff --git a/test/libs/bats-assert b/test/libs/bats-assert new file mode 160000 index 0000000..9f88b42 --- /dev/null +++ b/test/libs/bats-assert @@ -0,0 +1 @@ +Subproject commit 9f88b4207da750093baabc4e3f41bf68f0dd3630 diff --git a/test/libs/bats-support b/test/libs/bats-support new file mode 160000 index 0000000..004e707 --- /dev/null +++ b/test/libs/bats-support @@ -0,0 +1 @@ +Subproject commit 004e707638eedd62e0481e8cdc9223ad471f12ee diff --git a/test/mock_test.bats b/test/mock_test.bats new file mode 100755 index 0000000..da1b236 --- /dev/null +++ b/test/mock_test.bats @@ -0,0 +1,19 @@ +#!/usr/bin/env ./test/libs/bats/bin/bats +load 'libs/bats-support/load' +load 'libs/bats-assert/load' + +setup() { + export MAKEFILE="./test/Makefile.mock" +} + +@test "Deploy with ENABLE_FORCE_RECREATE set to 1" { + run make -f $MAKEFILE deploy ENABLE_FORCE_RECREATE=1 + [ "$status" -eq 0 ] + assert_output --partial "--force-recreate" +} + +@test "Deploy with ENABLE_FORCE_RECREATE set to 0" { + run make -f $MAKEFILE deploy ENABLE_FORCE_RECREATE=0 + [ "$status" -eq 0 ] + refute_output --partial "--force-recreate" +} \ No newline at end of file diff --git a/test/test.bats b/test/test.bats new file mode 100755 index 0000000..5b82813 --- /dev/null +++ b/test/test.bats @@ -0,0 +1,77 @@ +#!/usr/bin/env ./test/libs/bats/bin/bats +load 'libs/bats-support/load' +load 'libs/bats-assert/load' + +@test "Deploy with ENABLE_FORCE_RECREATE set to 1" { + run make -f Makefile deploy ENABLE_FORCE_RECREATE=1 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + assert_output --partial "--force-recreate" +} + +@test "Deploy with ENABLE_FORCE_RECREATE set to 0" { + run make -f Makefile deploy ENABLE_FORCE_RECREATE=0 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + refute_output --partial "--force-recreate" +} + + +@test "Deploy with DISABLE_ANSI set to 1" { + run make -f Makefile deploy DISABLE_ANSI=1 + [ "$status" -eq 0 ] + echo "status code: $status" + refute_output --partial "--ansi" +} + +@test "Deploy with DISABLE_ANSI set to 0" { + run make -f Makefile deploy DISABLE_ANSI=0 + [ "$status" -eq 0 ] + echo "status code: $status" + assert_output --partial "--ansi" +} + + +@test "Deploy with DISABLE_REMOVE_ORPHANS set to 1" { + run make -f Makefile deploy DISABLE_REMOVE_ORPHANS=1 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + refute_output --partial "--remove-orphans" +} + +@test "Deploy with DISABLE_REMOVE_ORPHANS set to 0" { + run make -f Makefile deploy DISABLE_REMOVE_ORPHANS=0 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + assert_output --partial "--remove-orphans" +} + + +@test "Deploy with ENABLE_GIT_PULL set to 1" { + run make -f Makefile deploy ENABLE_GIT_PULL=1 + [ "$status" -eq 0 ] + echo "status code: $status" + assert_output --partial "git pull" +} + +@test "Deploy with ENABLE_GIT_PULL set to 0" { + run make -f Makefile deploy ENABLE_GIT_PULL=0 + [ "$status" -eq 0 ] + echo "status code: $status" + refute_output --partial "git pull" +} + + +@test "Deploy with DISABLE_PULL set to 1" { + run make -f Makefile deploy DISABLE_PULL=1 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + refute_output --partial "docker compose --ansi never -p bhasai pull" +} + +@test "Deploy with DISABLE_PULL set to 0" { + run make -f Makefile deploy DISABLE_PULL=0 + [ "$status" -eq 0 ] # Check if make deploy command succeeded + echo "status code: $status" + assert_output --partial "docker compose --ansi never -p bhasai pull" +} \ No newline at end of file