From 440691956525964006fd4f6db14e464c93c5723d Mon Sep 17 00:00:00 2001 From: RoyUP9 <87927115+RoyUP9@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:04:00 +0300 Subject: [PATCH] added test workflow, added test for contains func (#184) --- .github/workflows/test.yaml | 22 ++++++++++ Makefile | 2 + README.md | 1 + cli/Makefile | 3 ++ cli/mizu/sliceUtils_test.go | 82 +++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 .github/workflows/test.yaml create mode 100644 cli/mizu/sliceUtils_test.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 000000000..dc4af4dca --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,22 @@ +name: test +on: + pull_request: + branches: + - 'develop' + - 'main' +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.x + uses: actions/setup-go@v2 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Test + run: make test + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v2 diff --git a/Makefile b/Makefile index bb85ff8ea..a3515ff83 100644 --- a/Makefile +++ b/Makefile @@ -65,3 +65,5 @@ clean-cli: ## Clean CLI. clean-docker: @(echo "DOCKER cleanup - NOT IMPLEMENTED YET " ) +test: ## Run tests. + @echo "running cli tests"; cd cli && $(MAKE) test diff --git a/README.md b/README.md index cc3f053ae..0f5b0d30b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ![Mizu: The API Traffic Viewer for Kubernetes](assets/mizu-logo.svg) + # The API Traffic Viewer for Kubernetes A simple-yet-powerful API traffic viewer for Kubernetes to help you troubleshoot and debug your microservices. Think TCPDump and Chrome Dev Tools combined. diff --git a/cli/Makefile b/cli/Makefile index b4841ba0c..065abcd3d 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -39,3 +39,6 @@ build-all: ## Build for all supported platforms. clean: ## Clean all build artifacts. go clean rm -rf ./bin/* + +test: ## Run cli tests. + @go test ./... -race -coverprofile=coverage.out -covermode=atomic diff --git a/cli/mizu/sliceUtils_test.go b/cli/mizu/sliceUtils_test.go new file mode 100644 index 000000000..f7f6529a1 --- /dev/null +++ b/cli/mizu/sliceUtils_test.go @@ -0,0 +1,82 @@ +package mizu_test + +import ( + "github.com/up9inc/mizu/cli/mizu" + "testing" +) + +func TestContainsExists(t *testing.T) { + tests := []struct { + slice []string + containsValue string + expected bool + }{ + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "apple", expected: true}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "orange", expected: true}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "banana", expected: true}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "grapes", expected: true}, + } + + for _, test := range tests { + actual := mizu.Contains(test.slice, test.containsValue) + if actual != test.expected { + t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual) + } + } +} + +func TestContainsNotExists(t *testing.T) { + tests := []struct { + slice []string + containsValue string + expected bool + }{ + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "cat", expected: false}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "dog", expected: false}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "apples", expected: false}, + {slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "rapes", expected: false}, + } + + for _, test := range tests { + actual := mizu.Contains(test.slice, test.containsValue) + if actual != test.expected { + t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual) + } + } +} + +func TestContainsEmptySlice(t *testing.T) { + tests := []struct { + slice []string + containsValue string + expected bool + }{ + {slice: []string{}, containsValue: "cat", expected: false}, + {slice: []string{}, containsValue: "dog", expected: false}, + } + + for _, test := range tests { + actual := mizu.Contains(test.slice, test.containsValue) + if actual != test.expected { + t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual) + } + } +} + +func TestContainsNilSlice(t *testing.T) { + tests := []struct { + slice []string + containsValue string + expected bool + }{ + {slice: nil, containsValue: "cat", expected: false}, + {slice: nil, containsValue: "dog", expected: false}, + } + + for _, test := range tests { + actual := mizu.Contains(test.slice, test.containsValue) + if actual != test.expected { + t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual) + } + } +}