From 3ab8537b8b328bb769c1656cfc57858f411d5bb8 Mon Sep 17 00:00:00 2001 From: Murat Kabilov Date: Fri, 26 May 2017 18:21:17 +0200 Subject: [PATCH] util test --- pkg/util/util_test.go | 118 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 pkg/util/util_test.go diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go new file mode 100644 index 000000000..ddf42760f --- /dev/null +++ b/pkg/util/util_test.go @@ -0,0 +1,118 @@ +package util + +import ( + "fmt" + "reflect" + "testing" + + "k8s.io/client-go/pkg/api/v1" + + "github.com/zalando-incubator/postgres-operator/pkg/spec" +) + +var pgUsers = []struct { + in spec.PgUser + out string +}{{spec.PgUser{ + Name: "test", + Password: "password", + Flags: []string{}, + MemberOf: []string{}}, + "md587f77988ccb5aa917c93201ba314fcd4"}, + {spec.PgUser{ + Name: "test", + Password: "md592f413f3974bdf3799bb6fecb5f9f2c6", + Flags: []string{}, + MemberOf: []string{}}, + "md592f413f3974bdf3799bb6fecb5f9f2c6"}} + +var prettyTest = []struct { + in interface{} + out string +}{ + {pgUsers, `[{{test password [] []} md587f77988ccb5aa917c93201ba314fcd4} {{test md592f413f3974bdf3799bb6fecb5f9f2c6 [] []} md592f413f3974bdf3799bb6fecb5f9f2c6}]`}, +} + +var prettyDiffTest = []struct { + inA interface{} + inB interface{} + out string +}{ + {[]int{1, 2, 3, 4}, []int{1, 2, 3}, "[]int[4] != []int[3]"}, + {[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}, ""}, +} + +var substractTest = []struct { + inA []string + inB []string + out []string + outEqual bool +}{ + {[]string{"a", "b", "c", "d"}, []string{"a", "b", "c", "d"}, []string{}, true}, + {[]string{"a", "b", "c", "d"}, []string{"a", "bb", "c", "d"}, []string{"b"}, false}, +} + +func TestRandomPassword(t *testing.T) { + const pwdLength = 10 + pwd := RandomPassword(pwdLength) + if a := len(pwd); a != pwdLength { + t.Errorf("Password length expected: %d, got: %d", pwdLength, a) + } +} + +func TestNameFromMeta(t *testing.T) { + meta := v1.ObjectMeta{ + Name: "testcluster", + Namespace: "default", + } + + expected := spec.NamespacedName{ + Name: "testcluster", + Namespace: "default", + } + + actual := NameFromMeta(meta) + if actual != expected { + t.Errorf("NameFromMeta expected: %#v, got: %#v", expected, actual) + } +} + +func TestPGUserPassword(t *testing.T) { + for _, tt := range pgUsers { + pwd := PGUserPassword(tt.in) + if pwd != tt.out { + t.Errorf("PgUserPassword expected: %s, got: %s", tt.out, pwd) + } + } +} + +func TestPretty(t *testing.T) { + for _, tt := range prettyTest { + if actual := Pretty(tt.in); fmt.Sprintf("%v", actual) != tt.out { + t.Errorf("Pretty expected: %s, got: %s", tt.out, actual) + } + } +} + +func TestPrettyDiff(t *testing.T) { + for _, tt := range prettyDiffTest { + if actual := PrettyDiff(tt.inA, tt.inB); actual != tt.out { + t.Errorf("PrettyDiff expected: %s, got: %s", tt.out, actual) + } + } +} + +func TestSubstractSlices(t *testing.T) { + for _, tt := range substractTest { + actualRes, actualEqual := SubstractStringSlices(tt.inA, tt.inB) + if actualEqual != tt.outEqual { + t.Errorf("SubstractStringSlices expected equal: %t, got: %t", tt.outEqual, actualEqual) + } + + if len(actualRes) == 0 && len(tt.out) == 0 { + continue + } else if !reflect.DeepEqual(actualRes, tt.out) { + t.Errorf("SubstractStringSlices expected res: %v, got: %v", tt.out, actualRes) + } + } +}