From 8b7e0ee7b010432071f2dd882e43381eaf1d2656 Mon Sep 17 00:00:00 2001 From: Srijan-SS02 Date: Sun, 7 Jul 2024 10:22:23 +0530 Subject: [PATCH 1/5] installed bats --- .gitmodules | 9 +++++++++ test/libs/bats | 1 + test/libs/bats-assert | 1 + test/libs/bats-support | 1 + 4 files changed, 12 insertions(+) create mode 100644 .gitmodules create mode 160000 test/libs/bats create mode 160000 test/libs/bats-assert create mode 160000 test/libs/bats-support 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/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 From c5207b85408b080f365003978f957c61462f17b1 Mon Sep 17 00:00:00 2001 From: Srijan-SS02 Date: Sun, 7 Jul 2024 10:25:32 +0530 Subject: [PATCH 2/5] added test for enable_force_recreate --- test/test.bats | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 test/test.bats diff --git a/test/test.bats b/test/test.bats new file mode 100755 index 0000000..c9e68c3 --- /dev/null +++ b/test/test.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env ./test/libs/bats/bin/bats +load 'libs/bats-support/load' +load 'libs/bats-assert/load' + +# Integration test running against the actual Makefile and deployment scripts +@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" +} From 03be40445a867c9faa43aaafebbf4d453d33ff85 Mon Sep 17 00:00:00 2001 From: Srijan-SS02 Date: Mon, 8 Jul 2024 13:48:15 +0530 Subject: [PATCH 3/5] added test for all the flags --- test/test.bats | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/test/test.bats b/test/test.bats index c9e68c3..5b82813 100755 --- a/test/test.bats +++ b/test/test.bats @@ -2,7 +2,6 @@ load 'libs/bats-support/load' load 'libs/bats-assert/load' -# Integration test running against the actual Makefile and deployment scripts @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 @@ -16,3 +15,63 @@ load 'libs/bats-assert/load' 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 From e8be4348e3d5b131629f059930cc6a888a872d96 Mon Sep 17 00:00:00 2001 From: Srijan-SS02 Date: Mon, 8 Jul 2024 13:59:37 +0530 Subject: [PATCH 4/5] updated readme.md for test Signed-off-by: Srijan-SS02 --- readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) 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) From b68404d861475a29ac5786cce08897050e93538a Mon Sep 17 00:00:00 2001 From: Srijan-SS02 Date: Fri, 12 Jul 2024 14:30:57 +0530 Subject: [PATCH 5/5] added mock test and Makfile.mock --- test/Makefile.mock | 14 ++++++++++++++ test/mock_test.bats | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/Makefile.mock create mode 100755 test/mock_test.bats 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/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