From e18e56b62c0054c4d5a245a186429f017325b435 Mon Sep 17 00:00:00 2001 From: saima-s Date: Thu, 29 Aug 2024 17:35:57 +0530 Subject: [PATCH 01/14] add code coverage.yaml --- .github/workflows/code-coverage.yaml | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/code-coverage.yaml diff --git a/.github/workflows/code-coverage.yaml b/.github/workflows/code-coverage.yaml new file mode 100644 index 0000000000..cdbad6c925 --- /dev/null +++ b/.github/workflows/code-coverage.yaml @@ -0,0 +1,31 @@ +name: Run tests and upload coverage + +on: + pull_request: + push: + branches: [ master ] + +jobs: + test: + name: Run tests and collect coverage + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v5 + + - name: Install dependencies + run: go mod download + + - name: Run tests + run: go test -coverprofile=coverage.txt + + - name: Upload results to Codecov + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: coverage.txt \ No newline at end of file From 2d3c2143f0d8ec03f4b211a5615960f9e0eff62d Mon Sep 17 00:00:00 2001 From: saima-s Date: Thu, 29 Aug 2024 17:59:39 +0530 Subject: [PATCH 02/14] added code cov in main.yaml --- .github/workflows/code-coverage.yaml | 31 ---------------------------- .github/workflows/main.yaml | 5 +++++ 2 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 .github/workflows/code-coverage.yaml diff --git a/.github/workflows/code-coverage.yaml b/.github/workflows/code-coverage.yaml deleted file mode 100644 index cdbad6c925..0000000000 --- a/.github/workflows/code-coverage.yaml +++ /dev/null @@ -1,31 +0,0 @@ -name: Run tests and upload coverage - -on: - pull_request: - push: - branches: [ master ] - -jobs: - test: - name: Run tests and collect coverage - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Set up Go - uses: actions/setup-go@v5 - - - name: Install dependencies - run: go mod download - - - name: Run tests - run: go test -coverprofile=coverage.txt - - - name: Upload results to Codecov - uses: codecov/codecov-action@v4 - with: - token: ${{ secrets.CODECOV_TOKEN }} - files: coverage.txt \ No newline at end of file diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 7fdcd0bdbd..14df577a37 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -88,6 +88,11 @@ jobs: make install-minio if: matrix.testSuite == 'test' - run: make ${{ matrix.testSuite }} + - name: Upload results to Codecov + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} + build: runs-on: ubuntu-20.04 needs: gomod From d1e1c4e9250723f64bda3489fb677e064638cfe2 Mon Sep 17 00:00:00 2001 From: saima-s Date: Thu, 29 Aug 2024 18:37:47 +0530 Subject: [PATCH 03/14] collect code coverage in text file --- .github/workflows/main.yaml | 1 + build/test.sh | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 14df577a37..8aac944cb8 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -92,6 +92,7 @@ jobs: uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} + files: coverage.txt build: runs-on: ubuntu-20.04 diff --git a/build/test.sh b/build/test.sh index 58410d46b4..396aeaccee 100755 --- a/build/test.sh +++ b/build/test.sh @@ -87,6 +87,7 @@ check_dependencies echo "Running tests:" go test -v ${TARGETS} -list . go test -v -installsuffix "static" ${TARGETS} -check.v ${GOCHECK_FILTER} +go test -coverprofile=coverage.txt ./... echo echo "PASS" From e3fccbd1484d0dc8230d5d417a256592222502b4 Mon Sep 17 00:00:00 2001 From: saima-s Date: Thu, 29 Aug 2024 23:04:18 +0530 Subject: [PATCH 04/14] test coverage increase to get report --- pkg/function/create_csi_snapshot_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/function/create_csi_snapshot_test.go b/pkg/function/create_csi_snapshot_test.go index 8bcfb101ac..1856daf8b5 100644 --- a/pkg/function/create_csi_snapshot_test.go +++ b/pkg/function/create_csi_snapshot_test.go @@ -16,6 +16,8 @@ package function import ( "context" + "fmt" + "k8s.io/apimachinery/pkg/util/rand" . "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" @@ -121,3 +123,20 @@ func getPVCManifest(pvcName, storageClassName string) *corev1.PersistentVolumeCl }, } } + +func (testSuite *CreateCSISnapshotTestSuite) TestDefaultSnapshotName(c *C) { + for _, tc := range []struct { + pvcName string + len int + expectedSnapshotName string + }{ + { + pvcName: pvcName, + len: 2, + expectedSnapshotName: fmt.Sprintf("%s-snapshot-%s", pvcName, rand.String(2)), + }, + } { + gotSnapshotName := defaultSnapshotName(tc.pvcName, tc.len) + c.Assert(gotSnapshotName[:len(tc.pvcName)], Equals, tc.expectedSnapshotName[:len(tc.pvcName)]) + } +} From 54734fd35d6d0b6996a93645e9a3c4c8e4aea5db Mon Sep 17 00:00:00 2001 From: saima-s Date: Fri, 30 Aug 2024 13:42:54 +0530 Subject: [PATCH 05/14] test coverage increase to get report --- go.mod | 4 ++++ go.sum | 3 ++- pkg/kancontroller/kancontroller_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 pkg/kancontroller/kancontroller_test.go diff --git a/go.mod b/go.mod index 5d3f0a76e8..36a41018f5 100644 --- a/go.mod +++ b/go.mod @@ -237,3 +237,7 @@ require ( sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 ) + +require github.com/stretchr/testify v1.9.0 + +require github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 10c3b11f31..c8b736cfca 100644 --- a/go.sum +++ b/go.sum @@ -525,8 +525,9 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pkg/kancontroller/kancontroller_test.go b/pkg/kancontroller/kancontroller_test.go new file mode 100644 index 0000000000..d1bf28c3f3 --- /dev/null +++ b/pkg/kancontroller/kancontroller_test.go @@ -0,0 +1,25 @@ +package kancontroller + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestMetricsEnabled(t *testing.T) { + // Test case 1: Environment variable is not set + os.Unsetenv(kanisterMetricsEnv) + assert.False(t, metricsEnabled(), "Expected metricsEnabled() to return false when environment variable is not set") + + // Test case 2: Environment variable is set but has invalid value + os.Setenv(kanisterMetricsEnv, "invalid") + assert.False(t, metricsEnabled(), "Expected metricsEnabled() to return false when environment variable is set to an invalid boolean") + + // Test case 3: Environment variable is set to "true" + os.Setenv(kanisterMetricsEnv, "true") + assert.True(t, metricsEnabled(), "Expected metricsEnabled() to return true when environment variable is set to 'true'") + + // Test case 4: Environment variable is set to "false" + os.Setenv(kanisterMetricsEnv, "false") + assert.False(t, metricsEnabled(), "Expected metricsEnabled() to return false when environment variable is set to 'false'") +} From 6ac4311722f8534fb8d75d3f36c911805f47fdee Mon Sep 17 00:00:00 2001 From: saima-s Date: Sun, 1 Sep 2024 11:33:48 +0530 Subject: [PATCH 06/14] test coverage increase to get report --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 8aac944cb8..efbf680379 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -88,7 +88,7 @@ jobs: make install-minio if: matrix.testSuite == 'test' - run: make ${{ matrix.testSuite }} - - name: Upload results to Codecov + - name: Upload coverage results to Codecov uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} From c5f2183973875d84f93ee87ff8951c847e4a8c8e Mon Sep 17 00:00:00 2001 From: saima-s Date: Sun, 1 Sep 2024 19:12:27 +0530 Subject: [PATCH 07/14] wip --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index efbf680379..7e169d4039 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -5,7 +5,7 @@ on: push: branches: - master - pull_request: + pull_request: [master] jobs: ## Make sure go.mod and go.sum files are up-to-date with the code From 1390c952d449b20663211487df2f6fb7e098c70b Mon Sep 17 00:00:00 2001 From: saima-s Date: Sun, 1 Sep 2024 19:17:13 +0530 Subject: [PATCH 08/14] wip --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 7e169d4039..efbf680379 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -5,7 +5,7 @@ on: push: branches: - master - pull_request: [master] + pull_request: jobs: ## Make sure go.mod and go.sum files are up-to-date with the code From 78a2bcd856f9afdbcb0ab721da46eb7585269dc0 Mon Sep 17 00:00:00 2001 From: saima-s Date: Sun, 1 Sep 2024 19:18:17 +0530 Subject: [PATCH 09/14] wip --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index efbf680379..6a6271ddaf 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -89,7 +89,7 @@ jobs: if: matrix.testSuite == 'test' - run: make ${{ matrix.testSuite }} - name: Upload coverage results to Codecov - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.txt From 37a4196e571b610e9ddf84b598e9728eb3931f5d Mon Sep 17 00:00:00 2001 From: saima-s Date: Mon, 2 Sep 2024 10:44:08 +0530 Subject: [PATCH 10/14] update codecov version --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 6a6271ddaf..efbf680379 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -89,7 +89,7 @@ jobs: if: matrix.testSuite == 'test' - run: make ${{ matrix.testSuite }} - name: Upload coverage results to Codecov - uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.txt From d5d77a5e6d4cc1f8577460cb756d968d069b2077 Mon Sep 17 00:00:00 2001 From: saima-s Date: Mon, 2 Sep 2024 10:57:18 +0530 Subject: [PATCH 11/14] add codecov yaml for custom PR comment --- codecov.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 codecov.yaml diff --git a/codecov.yaml b/codecov.yaml new file mode 100644 index 0000000000..f247b55739 --- /dev/null +++ b/codecov.yaml @@ -0,0 +1,4 @@ +comment: + require_changes: false + require_head: false # default true + require_base: false # default true \ No newline at end of file From a26fb565865c350613d8683b47fa6c4045bbcde4 Mon Sep 17 00:00:00 2001 From: saima-s Date: Mon, 2 Sep 2024 10:58:36 +0530 Subject: [PATCH 12/14] custom PR comment --- codecov.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/codecov.yaml b/codecov.yaml index f247b55739..54cf8da1b2 100644 --- a/codecov.yaml +++ b/codecov.yaml @@ -1,4 +1,7 @@ comment: require_changes: false require_head: false # default true - require_base: false # default true \ No newline at end of file + require_base: false # default true + layout: " diff, flags, files" + behavior: default + hide_project_coverage: false \ No newline at end of file From 4b53d71f0c32e1d174891e86057b6e14eaa1b28d Mon Sep 17 00:00:00 2001 From: saima-s Date: Mon, 2 Sep 2024 11:25:46 +0530 Subject: [PATCH 13/14] Add coverage PR comment --- .github/workflows/main.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index efbf680379..12ec1a0be3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -93,6 +93,12 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.txt + - name: Add Coverage PR Comment + uses: marocchino/sticky-pull-request-comment@v2 + if: github.event_name == 'pull_request' + with: + recreate: true + path: code-coverage-results.md build: runs-on: ubuntu-20.04 From 78f8cea95d303eea18205c511e46ab6f8db0e414 Mon Sep 17 00:00:00 2001 From: saima-s Date: Mon, 2 Sep 2024 11:26:33 +0530 Subject: [PATCH 14/14] Add coverage summary --- .github/workflows/main.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 12ec1a0be3..7ca2179c68 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -99,6 +99,9 @@ jobs: with: recreate: true path: code-coverage-results.md + - name: Code Coverage Summary + uses: irongut/CodeCoverageSummary@v1.3.0 + build: runs-on: ubuntu-20.04