Skip to content

Commit

Permalink
Delete k8s cluster resource in e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
MIBc committed Jan 6, 2020
1 parent 99d8b22 commit 5ad45fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (test *e2eTest) CreateTestNamespace(t *testing.T, namespace string) {
expectedOutputRegexp := fmt.Sprintf("namespace?.+%s.+created", namespace)
out, err := createNamespace(t, namespace, MaxRetries, logger)
if err != nil {
logger.Fatalf("Could not create namespace, giving up")
logger.Fatalf("Could not create namespace with error %v, giving up\n", err)
}

// check that last output indeed show created namespace
Expand Down Expand Up @@ -159,12 +159,12 @@ func createNamespace(t *testing.T, namespace string, maxRetries int, logger Logg
)

for retries < maxRetries {
out, err := kubectlCreateNamespace()
out, err = kubectlCreateNamespace()
if err == nil {
return out, nil
}
retries++
logger.Debugf("Could not create namespace, waiting %ds, and trying again: %d of %d\n", int(RetrySleepDuration.Seconds()), retries, maxRetries)
logger.Debugf("Could not create namespace with error %v, waiting %ds, and trying again: %d of %d\n", err, int(RetrySleepDuration.Seconds()), retries, maxRetries)
time.Sleep(RetrySleepDuration)
}

Expand Down
26 changes: 23 additions & 3 deletions test/e2e/source_apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func TestSourceApiServer(t *testing.T) {
t.Parallel()
test := NewE2eTest(t)
test.Setup(t)
defer test.Teardown(t)
defer func() {
test.deleteServiceAccountForApiserver(t, "testsa")
test.Teardown(t)
}()

test.setupServiceAccountForApiserver(t, "testsa")
test.serviceCreate(t, "testsvc0")
Expand Down Expand Up @@ -88,11 +91,28 @@ func (test *e2eTest) setupServiceAccountForApiserver(t *testing.T, name string)
}
_, err = kubectl.RunWithOpts([]string{"create", "clusterrole", "testsa-role", "--verb=get,list,watch", "--resource=events,namespaces"}, runOpts{})
if err != nil {
t.Fatalf(fmt.Sprintf("Error executing 'kubectl clusterrole testsa-role'. Error: %s", err.Error()))
t.Fatalf(fmt.Sprintf("Error executing 'kubectl create clusterrole testsa-role'. Error: %s", err.Error()))
}
_, err = kubectl.RunWithOpts([]string{"create", "clusterrolebinding", "testsa-binding", "--clusterrole=testsa-role", "--serviceaccount=" + test.kn.namespace + ":" + name}, runOpts{})
if err != nil {
t.Fatalf(fmt.Sprintf("Error executing 'kubectl clusterrolebinding testsa-binding'. Error: %s", err.Error()))
t.Fatalf(fmt.Sprintf("Error executing 'kubectl create clusterrolebinding testsa-binding'. Error: %s", err.Error()))
}
}

func (test *e2eTest) deleteServiceAccountForApiserver(t *testing.T, name string) {
kubectl := kubectl{t, Logger{}}

_, err := kubectl.RunWithOpts([]string{"delete", "serviceaccount", name, "--namespace", test.kn.namespace}, runOpts{})
if err != nil {
t.Fatalf(fmt.Sprintf("Error executing 'kubectl delete serviceaccount test-sa'. Error: %s", err.Error()))
}
_, err = kubectl.RunWithOpts([]string{"delete", "clusterrole", "testsa-role"}, runOpts{})
if err != nil {
t.Fatalf(fmt.Sprintf("Error executing 'kubectl delete clusterrole testsa-role'. Error: %s", err.Error()))
}
_, err = kubectl.RunWithOpts([]string{"delete", "clusterrolebinding", "testsa-binding"}, runOpts{})
if err != nil {
t.Fatalf(fmt.Sprintf("Error executing 'kubectl delete clusterrolebinding testsa-binding'. Error: %s", err.Error()))
}
}

Expand Down
1 change: 1 addition & 0 deletions test/e2e/tekton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

func TestTektonPipeline(t *testing.T) {
test := NewE2eTest(t)
defer test.Teardown(t)
test.Setup(t)

kubectl := kubectl{t, Logger{}}
Expand Down

0 comments on commit 5ad45fe

Please sign in to comment.