Skip to content

Commit

Permalink
349 | Update tests to use testify/require
Browse files Browse the repository at this point in the history
  • Loading branch information
ShajithaMohammed committed Oct 20, 2021
1 parent 3bfdd4f commit dcd1332
Show file tree
Hide file tree
Showing 24 changed files with 175 additions and 332 deletions.
10 changes: 4 additions & 6 deletions test/e2e/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

uitest "github.com/cppforlife/go-cli-ui/ui/test"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -463,14 +464,11 @@ data:
respKubectl := corev1.ConfigMap{}

err := json.Unmarshal([]byte(out), &respKubectl)
if err != nil {
t.Fatalf("Expected to successfully unmarshal: %s", err)
}
require.NoErrorf(t, err, "Expected to successfully unmarshal")

_, versionedAnnExists := respKubectl.Annotations["kapp.k14s.io/versioned"]
if respKubectl.Kind != "ConfigMap" || respKubectl.Name != "config" || !versionedAnnExists {
t.Fatalf("Expected to have versioned ConfigMap resource")
}
isConditionsMet := respKubectl.Kind == "ConfigMap" || respKubectl.Name == "config" || versionedAnnExists
require.True(t, isConditionsMet, "Expected to have versioned ConfigMap resource")

kappOut, _ := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name, "--json"},
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml)})
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -41,9 +42,9 @@ func NewPresentClusterResource(kind, name, ns string, kubectl Kubectl) ClusterRe

func NewMissingClusterResource(t *testing.T, kind, name, ns string, kubectl Kubectl) {
_, err := kubectl.RunWithOpts([]string{"get", kind, name, "-n", ns, "-o", "yaml"}, RunOpts{AllowError: true})
if err == nil || !strings.Contains(err.Error(), "Error from server (NotFound)") {
t.Fatalf("Expected resource to not exist")
}
isConditionsMet := err != nil || strings.Contains(err.Error(), "Error from server (NotFound)")

require.True(t, isConditionsMet, "Expected resource to not exist")
}

func (r ClusterResource) UID() string {
Expand Down
15 changes: 5 additions & 10 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package e2e

import (
"reflect"
"testing"

"github.com/cppforlife/go-cli-ui/ui"
"github.com/stretchr/testify/require"
)

// validateChanges: common func used by multiple test cases for validation between actual and expected
Expand All @@ -19,13 +19,8 @@ func validateChanges(t *testing.T, respTable []ui.JSONUITableResp, expected []ma
delete(row, "age")
}

if !reflect.DeepEqual(respTable[0].Rows, expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", output)
}
if respTable[0].Notes[0] != notesOp {
t.Fatalf("Expected to see correct summary, but did not: '%s'", output)
}
if respTable[0].Notes[1] != notesWaitTo {
t.Fatalf("Expected to see correct summary, but did not: '%s'", output)
}
require.Exactlyf(t, expected, respTable[0].Rows, "Expected to see correct changes, but did not")
require.Equalf(t, notesOp, respTable[0].Notes[0], "Expected to see correct summary, but did not")
require.Equalf(t, notesWaitTo, respTable[0].Notes[1], "Expected to see correct summary, but did not")

}
98 changes: 31 additions & 67 deletions test/e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package e2e

import (
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -108,29 +107,22 @@ data:
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})

firstData := NewPresentClusterResource("configmap", "first", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"data"}))
if !reflect.DeepEqual(firstData, map[string]interface{}{"keep": "", "delete": ""}) {
t.Fatalf("Expected value to be correct: %#v", firstData)
}
require.Exactlyf(t, map[string]interface{}{"keep": "", "delete": ""}, firstData, "Expected value to be correct")

secondData := NewPresentClusterResource("configmap", "second", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"data"}))
if !reflect.DeepEqual(secondData, map[string]interface{}{"keep": "", "delete": ""}) {
t.Fatalf("Expected value to be correct: %#v", secondData)
}
require.Exactlyf(t, map[string]interface{}{"keep": "", "delete": ""}, secondData, "Expected value to be correct")
})

logger.Section("check rebases", func() {
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name},
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})

firstData := NewPresentClusterResource("configmap", "first", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"data"}))
if !reflect.DeepEqual(firstData, map[string]interface{}{"keep": "", "keep2": ""}) {
t.Fatalf("Expected value to be correct: %#v", firstData)
}
require.Exactlyf(t, map[string]interface{}{"keep": "", "keep2": ""}, firstData, "Expected value to be correct")

secondData := NewPresentClusterResource("configmap", "second", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"data"}))
if !reflect.DeepEqual(secondData, map[string]interface{}{"keep": "", "keep2": "", "delete": ""}) {
t.Fatalf("Expected value to be correct: %#v", secondData)
}
require.Exactlyf(t, map[string]interface{}{"keep": "", "keep2": "", "delete": ""}, secondData, "Expected value to be correct")

})
}

Expand Down Expand Up @@ -199,24 +191,17 @@ secrets:
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})

secrets := NewPresentClusterResource("serviceaccount", "test-sa-with-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{})
if len(secrets) != 2 {
t.Fatalf("Expected one set and one generated secret")
}
if !reflect.DeepEqual(secrets[0], map[string]interface{}{"name": "some-secret"}) {
t.Fatalf("Expected provided secret at idx0: %#v", secrets[0])
}

require.Len(t, secrets, 2, "Expected one set and one generated secret")
require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret")

generatedSecretName = secrets[1].(map[string]interface{})["name"].(string)
if !strings.HasPrefix(generatedSecretName, "test-sa-with-secrets-token-") {
t.Fatalf("Expected generated secret at idx1: %#v", secrets[1])
}
require.True(t, strings.HasPrefix(generatedSecretName, "test-sa-with-secrets-token-"), "Expected generated secret at idx1: %#v", secrets[1])

secrets = NewPresentClusterResource("serviceaccount", "test-sa-without-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{})
if len(secrets) != 1 {
t.Fatalf("Expected one set and one generated secret")
}
if !strings.HasPrefix(secrets[0].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-") {
t.Fatalf("Expected generated secret at idx0: %#v", secrets[0])
}

require.Len(t, secrets, 1, "Expected one set and one generated secret")
require.True(t, strings.HasPrefix(secrets[0].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-"), "Expected generated secret at idx0: %#v", secrets[0])
})

ensureDeploysWithNoChanges := func(yamlContent string) {
Expand All @@ -228,12 +213,8 @@ secrets:
resp := uitest.JSONUIFromBytes(t, []byte(out))
expected := []map[string]string{}

if !reflect.DeepEqual(resp.Tables[0].Rows, expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
if resp.Tables[0].Notes[0] != "Op: 0 create, 0 delete, 0 update, 0 noop" {
t.Fatalf("Expected to see correct summary, but did not: '%s'", out)
}
require.Exactlyf(t, expected, resp.Tables[0].Rows, "Expected to see correct changes, but did not")
require.Equalf(t, "Op: 0 create, 0 delete, 0 update, 0 noop", resp.Tables[0].Notes[0], "Expected to see correct summary, but did not")
})
}
}
Expand All @@ -245,18 +226,12 @@ secrets:
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})

secrets := NewPresentClusterResource("serviceaccount", "test-sa-with-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{})
if len(secrets) != 3 {
t.Fatalf("Expected one set and one generated secret")
}
if !reflect.DeepEqual(secrets[0], map[string]interface{}{"name": "some-secret"}) {
t.Fatalf("Expected provided secret at idx0: %#v", secrets[0])
}
if !reflect.DeepEqual(secrets[1], map[string]interface{}{"name": "new-some-secret"}) {
t.Fatalf("Expected provided secret at idx1: %#v", secrets[0])
}
if !reflect.DeepEqual(secrets[2], map[string]interface{}{"name": generatedSecretName}) {
t.Fatalf("Expected previous generated secret at idx2: %#v", secrets[1])
}

require.Len(t, secrets, 3, "Expected one set and one generated secret")
require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret at idx0")
require.Exactlyf(t, map[string]interface{}{"name": "new-some-secret"}, secrets[1], "Expected provided secret at idx1")
require.Exactlyf(t, map[string]interface{}{"name": generatedSecretName}, secrets[2], "Expected previous generated secret at idx2")

})

ensureDeploysWithNoChanges(yaml2)
Expand All @@ -266,23 +241,16 @@ secrets:
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml3)})

secrets := NewPresentClusterResource("serviceaccount", "test-sa-with-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{})
if len(secrets) != 1 {
t.Fatalf("Expected one set and one generated secret")
}
if !reflect.DeepEqual(secrets[0], map[string]interface{}{"name": generatedSecretName}) {
t.Fatalf("Expected previous generated secret at idx0: %#v", secrets[0])
}

require.Len(t, secrets, 1, "Expected one set and one generated secret")
require.Exactlyf(t, map[string]interface{}{"name": generatedSecretName}, secrets[0], "Expected previous generated secret at idx0")

secrets = NewPresentClusterResource("serviceaccount", "test-sa-without-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{})
if len(secrets) != 2 {
t.Fatalf("Expected one set and one generated secret")
}
if !reflect.DeepEqual(secrets[0], map[string]interface{}{"name": "some-secret"}) {
t.Fatalf("Expected provided secret at idx0: %#v", secrets[0])
}
if !strings.HasPrefix(secrets[1].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-") {
t.Fatalf("Expected generated secret at idx1: %#v", secrets[1])
}

require.True(t, strings.HasPrefix(secrets[1].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-"), "Expected generated secret at idx1: %#v", secrets[1])
require.Len(t, secrets, 2, "Expected one set and one generated secret")
require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret at idx0")

})

ensureDeploysWithNoChanges(yaml3)
Expand Down Expand Up @@ -438,12 +406,8 @@ data:
resp := uitest.JSONUIFromBytes(t, []byte(out))
expected := []map[string]string{}

if !reflect.DeepEqual(resp.Tables[0].Rows, expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
if resp.Tables[0].Notes[0] != "Op: 0 create, 0 delete, 0 update, 0 noop" {
t.Fatalf("Expected to see correct summary, but did not: '%s'", out)
}
require.Exactlyf(t, expected, resp.Tables[0].Rows, "Expected to see correct changes, but did not")
require.Equalf(t, "Op: 0 create, 0 delete, 0 update, 0 noop", resp.Tables[0].Notes[0], "Expected to see correct summary, but did not")

cm = NewPresentClusterResource("configmap", "test-cm", env.Namespace, kubectl)
data := cm.RawPath(ctlres.NewPathFromStrings([]string{"data"})).(map[string]interface{})
Expand Down
9 changes: 3 additions & 6 deletions test/e2e/create_fallback_on_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

uitest "github.com/cppforlife/go-cli-ui/ui/test"
"github.com/stretchr/testify/require"
)

func TestCreateFallbackOnUpdate(t *testing.T) {
Expand Down Expand Up @@ -68,9 +69,7 @@ imagePullSecrets:
_, err := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name},
RunOpts{AllowError: true, StdinReader: strings.NewReader(yamlNoCreateStrategy)})

if !strings.Contains(err.Error(), `serviceaccounts "default" already exists`) {
t.Fatalf("Expected serviceaccount to be already created, but error was: %s", err)
}
require.Containsf(t, err.Error(), `serviceaccounts "default" already exists`, "Expected serviceaccount to be already created, but error was: %s", err)

cleanUp()
})
Expand All @@ -86,8 +85,6 @@ imagePullSecrets:

resp := uitest.JSONUIFromBytes(t, []byte(out))

if len(resp.Tables[0].Rows) != 0 {
t.Fatalf("Expected to see no changes, but did not: '%s'", out)
}
require.Len(t, resp.Tables[0].Rows, 0, "Expected to see no changes, but did not")
})
}
7 changes: 3 additions & 4 deletions test/e2e/create_update_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package e2e

import (
"reflect"
"strings"
"testing"

ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/stretchr/testify/require"
)

func TestCreateUpdateDelete(t *testing.T) {
Expand Down Expand Up @@ -78,9 +78,8 @@ data:

config := NewPresentClusterResource("configmap", "redis-config", env.Namespace, kubectl)
val := config.RawPath(ctlres.NewPathFromStrings([]string{"data", "key"}))
if !reflect.DeepEqual(val, "value2") {
t.Fatalf("Expected value to be updated")
}

require.Exactlyf(t, "value2", val, "Expected value to be updated")

NewPresentClusterResource("configmap", "redis-config2", env.Namespace, kubectl)
})
Expand Down
Loading

0 comments on commit dcd1332

Please sign in to comment.