From a3132dafeca8d1dfc46f25265caa0b109b172657 Mon Sep 17 00:00:00 2001 From: Toni Finger Date: Fri, 22 Dec 2023 10:06:09 +0100 Subject: [PATCH] Restructure e2e-framework To give a suggestion on how to structure/name tests using the e2e-framework I restructured the code example. Signed-off-by: Toni Finger --- .../Makefile | 14 +++--- .../plugin.yaml | 1 - .../scs_k8s_tests/main_test.go | 41 ++++++++++-------- ...ample_test.go => scs_0200_example_test.go} | 8 ++-- ...ple_test_2.go => scs_0201_example_test.go} | 43 ++++++++++++------- 5 files changed, 61 insertions(+), 46 deletions(-) rename Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/{k8s_example_test.go => scs_0200_example_test.go} (93%) rename Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/{k8s_example_test_2.go => scs_0201_example_test.go} (66%) diff --git a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/Makefile b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/Makefile index e33c6e820..9339c3b0f 100644 --- a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/Makefile +++ b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/Makefile @@ -15,8 +15,8 @@ KIND_CLUSTER ="testcluster" PLUGIN_NAME="plugin" PLUGIN_FILE="${PLUGIN_NAME}.yaml" -SONO_WAIT = 2 -SONO_TIMEOUT = 10 +#~ SONO_WAIT = 1 +#~ SONO_TIMEOUT = 60 KUBERNETES_SERVICE_HOST=127.0.0.1 KUBERNETES_SERVICE_PORT=34743 @@ -72,16 +72,18 @@ dev-build: container-init dev-go: @echo "[go]" @echo "[KubernetesService] ${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}" - mkdir ./build + @rm -rf ./build || true + @mkdir ./build go test -c -o ./build ./... - #go test -c -o ./build ./... --args --skip-labels="type=pod-list" - go tool test2json ./build -test.v +# go test -c -o ./build ./... --args --skip-labels="type=pod-list" +# go tool test2json ./build -test.v dev-run: @echo "[run-test]" @echo "sonobuoy run --plugin ${PLUGIN_FILE} --wait=${SONO_WAIT} --timeout=${SONO_TIMEOUT}" - @sonobuoy run --plugin ${PLUGIN_FILE} --wait=${SONO_WAIT} --timeout=${SONO_TIMEOUT} +#~ @sonobuoy run --plugin ${PLUGIN_FILE} --wait=${SONO_WAIT} --timeout=${SONO_TIMEOUT} + @sonobuoy run --plugin ${PLUGIN_FILE} @sonobuoy status diff --git a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/plugin.yaml b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/plugin.yaml index 6be857557..8b6d33248 100644 --- a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/plugin.yaml +++ b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/plugin.yaml @@ -2,7 +2,6 @@ sonobuoy-config: driver: Job plugin-name: scsconformance result-format: gojson - #source_url: https://raw.githubusercontent.com/vmware-tanzu/sonobuoy-plugins/main/examples/e2e-skeleton/plugin.yaml description: An easy to start from project for making K8s aware tests. spec: command: diff --git a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/main_test.go b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/main_test.go index 70ccd5995..d99972f28 100644 --- a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/main_test.go +++ b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/main_test.go @@ -1,19 +1,3 @@ -/* -Copyright 2021 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - package scs_k8s_tests import ( @@ -55,27 +39,46 @@ func TestMain(m *testing.M) { }) testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { + 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) 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.Finish( + // Teardown func: delete kind cluster + func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { + + fmt.Println("Finished go test suite") + //~ if err := ???; err != nil{ + //~ return ctx, err + //~ } + return ctx, nil + }, + ) + os.Exit(testenv.Run(m)) } diff --git a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test.go b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0200_example_test.go similarity index 93% rename from Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test.go rename to Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0200_example_test.go index 0ca28678a..e56b17832 100644 --- a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test.go +++ b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0200_example_test.go @@ -25,7 +25,7 @@ import ( ) -func TestDummyIn(t *testing.T) { +func Test_scs_0201_TestDummyIn(t *testing.T) { fmt.Println("DEBUG: dummy test") testvar := 5 if testvar != 3 { @@ -37,7 +37,7 @@ func TestDummyIn(t *testing.T) { -func TestListPods(t *testing.T) { +func Test_scs_0201_TestListPods(t *testing.T) { f := features.New("pod list"). Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { var pods corev1.PodList @@ -57,7 +57,7 @@ func TestListPods(t *testing.T) { -func TestListPodsFailing(t *testing.T) { +func scs_0201_TestListPodsFailing(t *testing.T) { f := features.New("pod list"). //~ WithLabel("type","pod-list") Assess("pods from kube-test-a", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { @@ -77,7 +77,7 @@ func TestListPodsFailing(t *testing.T) { } -func TestLongTest(t *testing.T) { +func scs_0201_TestLongTest(t *testing.T) { f := features.New("pod list"). Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { var pods corev1.PodList diff --git a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test_2.go b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0201_example_test.go similarity index 66% rename from Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test_2.go rename to Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0201_example_test.go index 3c50388c2..aa1a1c866 100644 --- a/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/k8s_example_test_2.go +++ b/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests/scs_0201_example_test.go @@ -1,31 +1,41 @@ -/* - Copyright 2021 The Kubernetes Authors. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - package scs_k8s_tests import ( "context" "testing" "time" - + "fmt" plugin_helper "github.com/vmware-tanzu/sonobuoy-plugins/plugin-helper" corev1 "k8s.io/api/core/v1" "sigs.k8s.io/e2e-framework/pkg/envconf" "sigs.k8s.io/e2e-framework/pkg/features" ) -func TestListPodsB(t *testing.T) { + +func Test_scs_0200_sonobuoy_pass(t *testing.T) { + fmt.Println("Test a passing test") + testvar := 5 + if testvar != 5 { + + t.Errorf("testvar = %d; want 5", testvar) + + } +} + +func Test_scs_0200_sonobuoy_fail(t *testing.T) { + fmt.Println("Test a failing test") + testvar := 5 + if testvar != 3 { + + t.Errorf("testvar = %d; want 3", testvar) + + } +} + + +func scs_0200_sonobuoy_TestListPods(t *testing.T) { f := features.New("pod list"). + WithLabel("type", "pod-count"). Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { var pods corev1.PodList err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods) @@ -42,8 +52,9 @@ func TestListPodsB(t *testing.T) { testenv.Test(t, f.Feature()) } -func TestLongTestB(t *testing.T) { +func scs_0200_sonobuoy_TestListPods_Long(t *testing.T) { f := features.New("pod list"). + WithLabel("type", "progress"). Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { var pods corev1.PodList err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods)