Skip to content

Commit

Permalink
Restructure e2e-framework
Browse files Browse the repository at this point in the history
To give a suggestion on how to structure/name tests using the e2e-framework
I restructured the code example.

Signed-off-by: Toni Finger <toni.finger@cloudandheat.com>
  • Loading branch information
tonifinger authored and cah-hbaum committed Jan 25, 2024
1 parent b6e7655 commit a3132da
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
14 changes: 8 additions & 6 deletions Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a3132da

Please sign in to comment.