Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golang linter #771

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/lint-golang.yml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 13 additions & 4 deletions Tests/kaas/kaas-sonobuoy-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
146 changes: 76 additions & 70 deletions Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ 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"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"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
Expand All @@ -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))
Expand Down Expand Up @@ -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())
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading