diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 7fdcd0bdbd..7ca2179c68 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -88,6 +88,21 @@ jobs: make install-minio if: matrix.testSuite == 'test' - run: make ${{ matrix.testSuite }} + - name: Upload coverage results to Codecov + uses: codecov/codecov-action@v4 + 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 + - name: Code Coverage Summary + uses: irongut/CodeCoverageSummary@v1.3.0 + + build: runs-on: ubuntu-20.04 needs: gomod 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" diff --git a/codecov.yaml b/codecov.yaml new file mode 100644 index 0000000000..54cf8da1b2 --- /dev/null +++ b/codecov.yaml @@ -0,0 +1,7 @@ +comment: + require_changes: false + require_head: false # default true + require_base: false # default true + layout: " diff, flags, files" + behavior: default + hide_project_coverage: false \ No newline at end of file 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/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)]) + } +} 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'") +}