Skip to content

Commit

Permalink
Move test cleanup function to defer (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
YrrepNoj authored Mar 15, 2022
1 parent 40c15fc commit 6801885
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 17 deletions.
5 changes: 3 additions & 2 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
kindcmd "sigs.k8s.io/kind/pkg/cmd"
)

// ZarfE2ETest Struct holding common fields most of the tests will utilize
type ZarfE2ETest struct {
zarfBinPath string

Expand Down Expand Up @@ -91,7 +92,7 @@ func (e2e *ZarfE2ETest) setUpKind() error {

// Set up a KinD cluster if necessary
e2e.provider = cluster.NewProvider(cluster.ProviderWithLogger(kindcmd.NewLogger()))
nodes, err := e2e.provider.ListNodes(e2e.clusterName)
nodes, _ := e2e.provider.ListNodes(e2e.clusterName)
if len(nodes) == 0 {
err = e2e.provider.Create(
e2e.clusterName,
Expand Down Expand Up @@ -273,7 +274,7 @@ func (e2e *ZarfE2ETest) checkIfClusterRunning() bool {

func (e2e *ZarfE2ETest) waitForHealthyCluster() error {
attempt := 0
for attempt < 10 {
for attempt < 15 {
pods, err := e2e.clientset.CoreV1().Pods("kube-system").List(context.TODO(), metav1.ListOptions{})
if err == nil && len(pods.Items) >= 0 {
allPodsHealthy := true
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/e2e_composability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestE2eExampleComposability(t *testing.T) {
defer e2e.cleanupAfterTest(t)

//run `zarf init`
output, err := e2e.execZarfCommand("init", "--confirm")
require.NoError(t, err, output)
Expand All @@ -33,6 +35,4 @@ func TestE2eExampleComposability(t *testing.T) {
// Validate the doom title in body.
assert.Contains(t, string(body), "Zarf needs games too")
assert.Equal(t, 200, resp.StatusCode)

e2e.cleanupAfterTest(t)
}
3 changes: 1 addition & 2 deletions test/e2e/e2e_data_injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestDataInjection(t *testing.T) {
defer e2e.cleanupAfterTest(t)

// run `zarf init`
output, err := e2e.execZarfCommand("init", "--confirm", "-l=trace")
Expand Down Expand Up @@ -40,6 +41,4 @@ func TestDataInjection(t *testing.T) {
}
assert.NoError(t, err)
assert.Contains(t, execStdOut, "this-is-an-example-file.txt")

e2e.cleanupAfterTest(t)
}
4 changes: 1 addition & 3 deletions test/e2e/e2e_example_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestE2eExampleGame(t *testing.T) {
defer e2e.cleanupAfterTest(t)

//run `zarf init`
output, err := e2e.execZarfCommand("init", "--confirm")
Expand All @@ -27,7 +28,4 @@ func TestE2eExampleGame(t *testing.T) {
resp, err := http.Get("http://127.0.0.1:22333?doom")
assert.NoError(t, err, resp)
assert.Equal(t, 200, resp.StatusCode)

// Clean up after this test (incase other tests cases will be run afterwards)
e2e.cleanupAfterTest(t)
}
5 changes: 2 additions & 3 deletions test/e2e/e2e_general_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGeneralCLI(t *testing.T) {
assert.NotEqual(t, output, "UnknownVersion", "Zarf version should not be the default value")

// Test for expected failure when given a bad componenet input
output, err = e2e.execZarfCommand("init", "--confirm", "--components=k3s,foo,logging")
_, err = e2e.execZarfCommand("init", "--confirm", "--components=k3s,foo,logging")
assert.Error(t, err)

// Test for expected failure when given invalid hostnames
Expand All @@ -57,6 +57,7 @@ func TestGeneralCLI(t *testing.T) {
}

func TestInitZarf(t *testing.T) {
defer e2e.cleanupAfterTest(t)

// Initialize Zarf for the next set of tests
// This also confirms that using the `--confirm` flags does not hang when not also specifying the `--components` flag
Expand All @@ -66,6 +67,4 @@ func TestInitZarf(t *testing.T) {
// Test that `zarf package deploy` gives an error if deploying a remote package without the --insecure or --shasum flags
output, err = e2e.execZarfCommand("package", "deploy", "https://zarf-examples.s3.amazonaws.com/zarf-package-appliance-demo-doom-20210125.tar.zst", "--confirm", "-l=trace")
assert.Error(t, err, output)

e2e.cleanupAfterTest(t)
}
3 changes: 1 addition & 2 deletions test/e2e/e2e_gitea_and_grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestGiteaAndGrafana(t *testing.T) {
defer e2e.cleanupAfterTest(t)

// run `zarf init`
output, err := e2e.execZarfCommand("init", "--components=gitops-service,logging", "--confirm")
Expand All @@ -31,6 +32,4 @@ func TestGiteaAndGrafana(t *testing.T) {
resp, err = http.Get("http://127.0.0.1:45002/monitor/login")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)

e2e.cleanupAfterTest(t)
}
6 changes: 3 additions & 3 deletions test/e2e/e2e_gitops_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func TestGitopsExample(t *testing.T) {
defer e2e.cleanupAfterTest(t)

// run `zarf init`
output, err := e2e.execZarfCommand("init", "--confirm", "--components=gitops-service")
require.NoError(t, err, output)
Expand Down Expand Up @@ -43,7 +45,7 @@ func TestGitopsExample(t *testing.T) {
expectedTag := "v0.15.0\n"
err = os.Chdir("mirror__github.com__defenseunicorns__zarf")
assert.NoError(t, err)
gitOutput, err = exec.Command("git", "tag").Output()
gitOutput, _ = exec.Command("git", "tag").Output()
assert.Equal(t, expectedTag, string(gitOutput), "Expected tag should match output")

// Check for correct commits
Expand All @@ -62,6 +64,4 @@ func TestGitopsExample(t *testing.T) {

err = os.Chdir("..")
assert.NoError(t, err, "unable to change directories back to blah blah blah")

e2e.cleanupAfterTest(t)
}

0 comments on commit 6801885

Please sign in to comment.