diff --git a/.github/workflows/lint-golang.yml b/.github/workflows/lint-golang.yml new file mode 100644 index 000000000..faf7fdc8c --- /dev/null +++ b/.github/workflows/lint-golang.yml @@ -0,0 +1,28 @@ +name: Check Go syntax + +on: + push: + paths: + - 'Tests/kaas/kaas-sonobuoy-tests/**/*.go' + - .github/workflows/lint-go.yml + +jobs: + lint-go-syntax: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.23' + + # Install golangci-lint + - name: Install golangci-lint + run: | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0 + + # Run golangci-lint + - name: Run golangci-lint + working-directory: Tests/kaas/kaas-sonobuoy-tests + run: golangci-lint run ./... -v diff --git a/Tests/kaas/kaas-sonobuoy-tests/Makefile b/Tests/kaas/kaas-sonobuoy-tests/Makefile index b7099cee7..dffc6d7a2 100644 --- a/Tests/kaas/kaas-sonobuoy-tests/Makefile +++ b/Tests/kaas/kaas-sonobuoy-tests/Makefile @@ -96,10 +96,19 @@ test-function: @echo "only run tests for: $${TESTFUNCTION_CODE}" DEVELOPMENT_MODE=createcluster go test -run=$${TESTFUNCTION_CODE} ./... || true - -lint: - @echo "NOT YET IMPLEMENTED" - +lint: check-golangci-lint + @echo "[Running golangci-lint...]" + @golangci-lint run ./... -v || true + +GOLANGCI_LINT_VERSION ?= v1.61.0 +check-golangci-lint: + @if ! [ -x "$$(command -v golangci-lint)" ]; then \ + echo "[golangci-lint not found, installing...]"; \ + go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION); \ + echo "[golangci-lint installed]"; \ + else \ + echo "[golangci-lint is already installed]"; \ + fi dev-clean-result: @rm -rf *.tar.gz || true diff --git a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/main_test.go b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/main_test.go index 8359b1b0f..98d305a5b 100644 --- a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/main_test.go +++ b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/main_test.go @@ -3,9 +3,10 @@ package scs_k8s_tests import ( "context" "fmt" + "log" "os" "testing" - "log" + plugin_helper "github.com/vmware-tanzu/sonobuoy-plugins/plugin-helper" v1 "k8s.io/api/core/v1" "sigs.k8s.io/e2e-framework/pkg/env" @@ -13,11 +14,16 @@ import ( "sigs.k8s.io/e2e-framework/pkg/envfuncs" ) +// Define a custom type for the context key +type nsContextKey string + +// Define a custom type for context keys +type contextKey string const ( ProgressReporterCtxKey = "SONOBUOY_PROGRESS_REPORTER" - NamespacePrefixKey = "NS_PREFIX" - DevelopmentModeKey = "DEVELOPMENT_MODE" + NamespacePrefixKey = "NS_PREFIX" + DevelopmentModeKey = "DEVELOPMENT_MODE" ) var testenv env.Environment @@ -33,82 +39,82 @@ func TestMain(m *testing.M) { updateReporter := plugin_helper.NewProgressReporter(0) developmentMode := os.Getenv(DevelopmentModeKey) - log.Printf("Setup test enviornment for: %#v", developmentMode ) - - switch KubernetesEnviornment := developmentMode; KubernetesEnviornment { - - case "createcluster": - log.Println("Create kind cluster for test") - testenv = env.New() - kindClusterName := envconf.RandomName("gotestcluster", 16) - //~ namespace := envconf.RandomName("testnamespace", 16) - - testenv.Setup( - envfuncs.CreateKindCluster(kindClusterName), - ) - - testenv.Finish( - //~ envfuncs.DeleteNamespace(namespace), - envfuncs.DestroyKindCluster(kindClusterName), - ) - - case "usecluster": - log.Println("Use existing k8s cluster for the test") - log.Println("Not Yet Implemented") - //~ testenv = env.NewFromFlags() - //~ KubeConfig:= os.Getenv(KUBECONFIGFILE) - //~ testenv = env.NewWithKubeConfig(KubeConfig) - - default: - // Assume we are running in the cluster as a Sonobuoy plugin. - log.Println("Running tests inside k8s cluster") - testenv = env.NewInClusterConfig() - - testenv.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) { - // Try and create the client; doing it before all the tests allows the tests to assume - // it can be created without error and they can just use config.Client(). - _,err:=config.NewClient() - return context.WithValue(ctx,ProgressReporterCtxKey,updateReporter) ,err - }) - - testenv.Finish( - func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { - log.Println("Finished go test suite") - //~ if err := ???; err != nil{ - //~ return ctx, err - //~ } - return ctx, nil - }, - ) - - } + log.Printf("Setup test enviornment for: %#v", developmentMode) + + switch KubernetesEnviornment := developmentMode; KubernetesEnviornment { + + case "createcluster": + log.Println("Create kind cluster for test") + testenv = env.New() + kindClusterName := envconf.RandomName("gotestcluster", 16) + //~ namespace := envconf.RandomName("testnamespace", 16) + + testenv.Setup( + envfuncs.CreateKindCluster(kindClusterName), + ) + + testenv.Finish( + //~ envfuncs.DeleteNamespace(namespace), + envfuncs.DestroyKindCluster(kindClusterName), + ) + + case "usecluster": + log.Println("Use existing k8s cluster for the test") + log.Println("Not Yet Implemented") + //~ testenv = env.NewFromFlags() + //~ KubeConfig:= os.Getenv(KUBECONFIGFILE) + //~ testenv = env.NewWithKubeConfig(KubeConfig) + + default: + // Assume we are running in the cluster as a Sonobuoy plugin. + log.Println("Running tests inside k8s cluster") + testenv = env.NewInClusterConfig() + + testenv.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) { + // Try and create the client; doing it before all the tests allows the tests to assume + // it can be created without error and they can just use config.Client(). + _, err := config.NewClient() + return context.WithValue(ctx, contextKey(ProgressReporterCtxKey), updateReporter), err + }) + + testenv.Finish( + func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { + log.Println("Finished go test suite") + //~ if err := ???; err != nil{ + //~ return ctx, err + //~ } + return ctx, nil + }, + ) + + } testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { - fmt.Println("BeforeEachTest") + fmt.Println("BeforeEachTest") updateReporter.StartTest(t.Name()) return createNSForTest(ctx, cfg, t, runID) }) testenv.AfterEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { - fmt.Println("AfterEachTest") - updateReporter.StopTest(t.Name(),t.Failed(),t.Skipped(),nil) + fmt.Println("AfterEachTest") + updateReporter.StopTest(t.Name(), t.Failed(), t.Skipped(), nil) return deleteNSForTest(ctx, cfg, t, runID) }) /* - testenv.BeforeEachFeature(func(ctx context.Context, config *envconf.Config, info features.Feature) (context.Context, error) { - // Note that you can also add logic here for before a feature is tested. There may be - // more than one feature in a test. - fmt.Println("BeforeEachFeature") - return ctx, nil - }) - - testenv.AfterEachFeature(func(ctx context.Context, config *envconf.Config, info features.Feature) (context.Context, error) { - // Note that you can also add logic here for after a feature is tested. There may be - // more than one feature in a test. - fmt.Println("AfterEachFeature") - return ctx, nil - }) + testenv.BeforeEachFeature(func(ctx context.Context, config *envconf.Config, info features.Feature) (context.Context, error) { + // Note that you can also add logic here for before a feature is tested. There may be + // more than one feature in a test. + fmt.Println("BeforeEachFeature") + return ctx, nil + }) + + testenv.AfterEachFeature(func(ctx context.Context, config *envconf.Config, info features.Feature) (context.Context, error) { + // Note that you can also add logic here for after a feature is tested. There may be + // more than one feature in a test. + fmt.Println("AfterEachFeature") + return ctx, nil + }) */ os.Exit(testenv.Run(m)) @@ -136,6 +142,6 @@ func deleteNSForTest(ctx context.Context, cfg *envconf.Config, t *testing.T, run return ctx, cfg.Client().Resources().Delete(ctx, &nsObj) } -func nsKey(t *testing.T) string { - return "NS-for-%v" + t.Name() +func nsKey(t *testing.T) nsContextKey { + return nsContextKey("NS-for-" + t.Name()) } diff --git a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0200_smoke_test.go b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0200_smoke_test.go index 83909b41a..62ec43e3d 100644 --- a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0200_smoke_test.go +++ b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0200_smoke_test.go @@ -1,16 +1,15 @@ package scs_k8s_tests import ( + "os" "testing" - "os" ) - func Test_scs_0200_smoke(t *testing.T) { - // This test ensures that no DevelopmentMode was set - // when using this test-suite productively - developmentMode := os.Getenv(DevelopmentModeKey) + // This test ensures that no DevelopmentMode was set + // when using this test-suite productively + developmentMode := os.Getenv(DevelopmentModeKey) if developmentMode != "" { - t.Errorf("developmentMode is set to = %v; want None", developmentMode ) + t.Errorf("developmentMode is set to = %v; want None", developmentMode) } }