Merge pull request #72445 from bart0sh/PR0052-app-apis-kubeadm-use-T.Run

kubeadm: use T.Run API in app/apis/kubeadm
This commit is contained in:
Kubernetes Prow Robot 2018-12-31 00:38:29 -08:00 committed by GitHub
commit 7946a9a327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 368 additions and 293 deletions

View File

@ -64,14 +64,16 @@ func TestToSecret(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual := rt.bt.ToSecret() t.Run(rt.bt.Token.ID, func(t *testing.T) {
if !reflect.DeepEqual(actual, rt.secret) { actual := rt.bt.ToSecret()
t.Errorf( if !reflect.DeepEqual(actual, rt.secret) {
"failed BootstrapToken.ToSecret():\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.secret, "failed BootstrapToken.ToSecret():\n\texpected: %v\n\t actual: %v",
actual, rt.secret,
) actual,
} )
}
})
} }
} }
@ -92,27 +94,31 @@ func TestBootstrapTokenToSecretRoundtrip(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := BootstrapTokenFromSecret(rt.bt.ToSecret()) t.Run(rt.bt.Token.ID, func(t *testing.T) {
if err != nil { actual, err := BootstrapTokenFromSecret(rt.bt.ToSecret())
t.Errorf("failed BootstrapToken to Secret roundtrip with error: %v", err) if err != nil {
} t.Errorf("failed BootstrapToken to Secret roundtrip with error: %v", err)
if !reflect.DeepEqual(actual, rt.bt) { }
t.Errorf( if !reflect.DeepEqual(actual, rt.bt) {
"failed BootstrapToken to Secret roundtrip:\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.bt, "failed BootstrapToken to Secret roundtrip:\n\texpected: %v\n\t actual: %v",
actual, rt.bt,
) actual,
} )
}
})
} }
} }
func TestEncodeTokenSecretData(t *testing.T) { func TestEncodeTokenSecretData(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
bt *BootstrapToken bt *BootstrapToken
data map[string][]byte data map[string][]byte
}{ }{
{ {
&BootstrapToken{ // the minimum amount of information needed to be specified "the minimum amount of information needed to be specified",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
}, },
map[string][]byte{ map[string][]byte{
@ -121,7 +127,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds description "adds description",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
Description: "foo", Description: "foo",
}, },
@ -132,7 +139,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds ttl "adds ttl",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
TTL: &metav1.Duration{ TTL: &metav1.Duration{
Duration: mustParseDuration("2h", t), Duration: mustParseDuration("2h", t),
@ -145,7 +153,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds expiration "adds expiration",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
Expires: &metav1.Time{ Expires: &metav1.Time{
Time: refTime, Time: refTime,
@ -158,7 +167,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds ttl and expiration, should favor expiration "adds ttl and expiration, should favor expiration",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
TTL: &metav1.Duration{ TTL: &metav1.Duration{
Duration: mustParseDuration("2h", t), Duration: mustParseDuration("2h", t),
@ -174,7 +184,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds usages "adds usages",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
Usages: []string{"authentication", "signing"}, Usages: []string{"authentication", "signing"},
}, },
@ -186,7 +197,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // adds groups "adds groups",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
Groups: []string{"system:bootstrappers", "system:bootstrappers:foo"}, Groups: []string{"system:bootstrappers", "system:bootstrappers:foo"},
}, },
@ -197,7 +209,8 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
}, },
{ {
&BootstrapToken{ // all together "all together",
&BootstrapToken{
Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}, Token: &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
Description: "foo", Description: "foo",
TTL: &metav1.Duration{ TTL: &metav1.Duration{
@ -221,14 +234,16 @@ func TestEncodeTokenSecretData(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual := encodeTokenSecretData(rt.bt, refTime) t.Run(rt.name, func(t *testing.T) {
if !reflect.DeepEqual(actual, rt.data) { actual := encodeTokenSecretData(rt.bt, refTime)
t.Errorf( if !reflect.DeepEqual(actual, rt.data) {
"failed encodeTokenSecretData:\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.data, "failed encodeTokenSecretData:\n\texpected: %v\n\t actual: %v",
actual, rt.data,
) actual,
} )
}
})
} }
} }
@ -242,12 +257,14 @@ func mustParseDuration(durationStr string, t *testing.T) time.Duration {
func TestBootstrapTokenFromSecret(t *testing.T) { func TestBootstrapTokenFromSecret(t *testing.T) {
var tests = []struct { var tests = []struct {
desc string
name string name string
data map[string][]byte data map[string][]byte
bt *BootstrapToken bt *BootstrapToken
expectedError bool expectedError bool
}{ }{
{ // minimum information {
"minimum information",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -258,7 +275,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // invalid token id {
"invalid token id",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdeF"), "token-id": []byte("abcdeF"),
@ -267,7 +285,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
nil, nil,
true, true,
}, },
{ // invalid secret naming {
"invalid secret naming",
"foo", "foo",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -276,7 +295,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
nil, nil,
true, true,
}, },
{ // invalid token secret {
"invalid token secret",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -285,7 +305,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
nil, nil,
true, true,
}, },
{ // adds description {
"adds description",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -298,7 +319,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // adds expiration {
"adds expiration",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -313,7 +335,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // invalid expiration {
"invalid expiration",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -323,7 +346,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
nil, nil,
true, true,
}, },
{ // adds usages {
"adds usages",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -337,7 +361,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // should ignore usages that aren't set to true {
"should ignore usages that aren't set to true",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -353,7 +378,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // adds groups {
"adds groups",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -366,7 +392,8 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
false, false,
}, },
{ // all fields set {
"all fields set",
"bootstrap-token-abcdef", "bootstrap-token-abcdef",
map[string][]byte{ map[string][]byte{
"token-id": []byte("abcdef"), "token-id": []byte("abcdef"),
@ -390,34 +417,36 @@ func TestBootstrapTokenFromSecret(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := BootstrapTokenFromSecret(&v1.Secret{ t.Run(rt.desc, func(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{ actual, err := BootstrapTokenFromSecret(&v1.Secret{
Name: rt.name, ObjectMeta: metav1.ObjectMeta{
Namespace: "kube-system", Name: rt.name,
}, Namespace: "kube-system",
Type: v1.SecretType("bootstrap.kubernetes.io/token"), },
Data: rt.data, Type: v1.SecretType("bootstrap.kubernetes.io/token"),
}) Data: rt.data,
if (err != nil) != rt.expectedError { })
t.Errorf( if (err != nil) != rt.expectedError {
"failed BootstrapTokenFromSecret\n\texpected error: %t\n\t actual error: %v",
rt.expectedError,
err,
)
} else {
if actual == nil && rt.bt == nil {
// if both pointers are nil, it's okay, just continue
continue
}
// If one of the pointers is defined but the other isn't, throw error. If both pointers are defined but unequal, throw error
if (actual == nil && rt.bt != nil) || (actual != nil && rt.bt == nil) || !reflect.DeepEqual(*actual, *rt.bt) {
t.Errorf( t.Errorf(
"failed BootstrapTokenFromSecret\n\texpected: %s\n\t actual: %s", "failed BootstrapTokenFromSecret\n\texpected error: %t\n\t actual error: %v",
jsonMarshal(rt.bt), rt.expectedError,
jsonMarshal(actual), err,
) )
} else {
if actual == nil && rt.bt == nil {
// if both pointers are nil, it's okay, just continue
return
}
// If one of the pointers is defined but the other isn't, throw error. If both pointers are defined but unequal, throw error
if (actual == nil && rt.bt != nil) || (actual != nil && rt.bt == nil) || !reflect.DeepEqual(*actual, *rt.bt) {
t.Errorf(
"failed BootstrapTokenFromSecret\n\texpected: %s\n\t actual: %s",
jsonMarshal(rt.bt),
jsonMarshal(actual),
)
}
} }
} })
} }
} }
@ -428,11 +457,13 @@ func jsonMarshal(bt *BootstrapToken) string {
func TestGetSecretString(t *testing.T) { func TestGetSecretString(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
secret *v1.Secret secret *v1.Secret
key string key string
expectedVal string expectedVal string
}{ }{
{ {
name: "existing key",
secret: &v1.Secret{ secret: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Data: map[string][]byte{ Data: map[string][]byte{
@ -443,6 +474,7 @@ func TestGetSecretString(t *testing.T) {
expectedVal: "bar", expectedVal: "bar",
}, },
{ {
name: "non-existing key",
secret: &v1.Secret{ secret: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Data: map[string][]byte{ Data: map[string][]byte{
@ -453,6 +485,7 @@ func TestGetSecretString(t *testing.T) {
expectedVal: "", expectedVal: "",
}, },
{ {
name: "no data",
secret: &v1.Secret{ secret: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "foo"},
}, },
@ -461,13 +494,15 @@ func TestGetSecretString(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual := getSecretString(rt.secret, rt.key) t.Run(rt.name, func(t *testing.T) {
if actual != rt.expectedVal { actual := getSecretString(rt.secret, rt.key)
t.Errorf( if actual != rt.expectedVal {
"failed getSecretString:\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expectedVal, "failed getSecretString:\n\texpected: %s\n\t actual: %s",
actual, rt.expectedVal,
) actual,
} )
}
})
} }
} }

View File

@ -34,17 +34,19 @@ func TestMarshalJSON(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`}, {BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`},
} }
for _, rt := range tests { for _, rt := range tests {
b, err := json.Marshal(rt.bts) t.Run(rt.bts.ID, func(t *testing.T) {
if err != nil { b, err := json.Marshal(rt.bts)
t.Fatalf("json.Marshal returned an unexpected error: %v", err) if err != nil {
} t.Fatalf("json.Marshal returned an unexpected error: %v", err)
if string(b) != rt.expected { }
t.Errorf( if string(b) != rt.expected {
"failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s",
string(b), rt.expected,
) string(b),
} )
}
})
} }
} }
@ -64,17 +66,19 @@ func TestUnmarshalJSON(t *testing.T) {
{`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false}, {`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false},
} }
for _, rt := range tests { for _, rt := range tests {
newbts := &BootstrapTokenString{} t.Run(rt.input, func(t *testing.T) {
err := json.Unmarshal([]byte(rt.input), newbts) newbts := &BootstrapTokenString{}
if (err != nil) != rt.expectedError { err := json.Unmarshal([]byte(rt.input), newbts)
t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err) if (err != nil) != rt.expectedError {
} else if !reflect.DeepEqual(rt.bts, newbts) { t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err)
t.Errorf( } else if !reflect.DeepEqual(rt.bts, newbts) {
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.bts, "failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v",
newbts, rt.bts,
) newbts,
} )
}
})
} }
} }
@ -87,9 +91,11 @@ func TestJSONRoundtrip(t *testing.T) {
{"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}}, {"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
if err := roundtrip(rt.input, rt.bts); err != nil { t.Run(rt.input, func(t *testing.T) {
t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err) if err := roundtrip(rt.input, rt.bts); err != nil {
} t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err)
}
})
} }
} }
@ -140,14 +146,16 @@ func TestTokenFromIDAndSecret(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"}, {BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"},
} }
for _, rt := range tests { for _, rt := range tests {
actual := rt.bts.String() t.Run(rt.bts.ID, func(t *testing.T) {
if actual != rt.expected { actual := rt.bts.String()
t.Errorf( if actual != rt.expected {
"failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s",
actual, rt.expected,
) actual,
} )
}
})
} }
} }
@ -175,22 +183,24 @@ func TestNewBootstrapTokenString(t *testing.T) {
{token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenString(rt.token) t.Run(rt.token, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenString(rt.token)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v",
rt.expectedError, rt.token,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v",
rt.bts, rt.token,
actual, rt.bts,
) actual,
} )
}
})
} }
} }
@ -215,23 +225,25 @@ func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
{id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret) t.Run(rt.id, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v",
rt.secret, rt.id,
rt.expectedError, rt.secret,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v",
rt.secret, rt.id,
rt.bts, rt.secret,
actual, rt.bts,
) actual,
} )
}
})
} }
} }

View File

@ -34,17 +34,19 @@ func TestMarshalJSON(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`}, {BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`},
} }
for _, rt := range tests { for _, rt := range tests {
b, err := json.Marshal(rt.bts) t.Run(rt.bts.ID, func(t *testing.T) {
if err != nil { b, err := json.Marshal(rt.bts)
t.Fatalf("json.Marshal returned an unexpected error: %v", err) if err != nil {
} t.Fatalf("json.Marshal returned an unexpected error: %v", err)
if string(b) != rt.expected { }
t.Errorf( if string(b) != rt.expected {
"failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s",
string(b), rt.expected,
) string(b),
} )
}
})
} }
} }
@ -64,17 +66,19 @@ func TestUnmarshalJSON(t *testing.T) {
{`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false}, {`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false},
} }
for _, rt := range tests { for _, rt := range tests {
newbts := &BootstrapTokenString{} t.Run(rt.input, func(t *testing.T) {
err := json.Unmarshal([]byte(rt.input), newbts) newbts := &BootstrapTokenString{}
if (err != nil) != rt.expectedError { err := json.Unmarshal([]byte(rt.input), newbts)
t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err) if (err != nil) != rt.expectedError {
} else if !reflect.DeepEqual(rt.bts, newbts) { t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err)
t.Errorf( } else if !reflect.DeepEqual(rt.bts, newbts) {
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.bts, "failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v",
newbts, rt.bts,
) newbts,
} )
}
})
} }
} }
@ -87,9 +91,11 @@ func TestJSONRoundtrip(t *testing.T) {
{"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}}, {"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
if err := roundtrip(rt.input, rt.bts); err != nil { t.Run(rt.input, func(t *testing.T) {
t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err) if err := roundtrip(rt.input, rt.bts); err != nil {
} t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err)
}
})
} }
} }
@ -140,14 +146,16 @@ func TestTokenFromIDAndSecret(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"}, {BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"},
} }
for _, rt := range tests { for _, rt := range tests {
actual := rt.bts.String() t.Run(rt.bts.ID, func(t *testing.T) {
if actual != rt.expected { actual := rt.bts.String()
t.Errorf( if actual != rt.expected {
"failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s",
actual, rt.expected,
) actual,
} )
}
})
} }
} }
@ -175,22 +183,24 @@ func TestNewBootstrapTokenString(t *testing.T) {
{token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenString(rt.token) t.Run(rt.token, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenString(rt.token)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v",
rt.expectedError, rt.token,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v",
rt.bts, rt.token,
actual, rt.bts,
) actual,
} )
}
})
} }
} }
@ -215,23 +225,25 @@ func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
{id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret) t.Run(rt.id, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v",
rt.secret, rt.id,
rt.expectedError, rt.secret,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v",
rt.secret, rt.id,
rt.bts, rt.secret,
actual, rt.bts,
) actual,
} )
}
})
} }
} }

View File

@ -46,13 +46,15 @@ func TestJoinConfigurationConversion(t *testing.T) {
expectedError: true, expectedError: true,
}, },
} }
for _, tc := range testcases { for name, tc := range testcases {
internal := &kubeadm.JoinConfiguration{} t.Run(name, func(t *testing.T) {
err := Convert_v1alpha3_JoinConfiguration_To_kubeadm_JoinConfiguration(tc.old, internal, nil) internal := &kubeadm.JoinConfiguration{}
if (err != nil) != tc.expectedError { err := Convert_v1alpha3_JoinConfiguration_To_kubeadm_JoinConfiguration(tc.old, internal, nil)
t.Errorf("ImageToImageMeta returned unexpected error: %v, saw: %v", tc.expectedError, (err != nil)) if (err != nil) != tc.expectedError {
return t.Errorf("ImageToImageMeta returned unexpected error: %v, saw: %v", tc.expectedError, (err != nil))
} return
}
})
} }
} }
@ -76,12 +78,14 @@ func TestInitConfigurationConversion(t *testing.T) {
expectedErr: true, expectedErr: true,
}, },
} }
for _, tc := range testcases { for name, tc := range testcases {
internal := &kubeadm.InitConfiguration{} t.Run(name, func(t *testing.T) {
err := Convert_v1alpha3_InitConfiguration_To_kubeadm_InitConfiguration(tc.old, internal, nil) internal := &kubeadm.InitConfiguration{}
if (err != nil) != tc.expectedErr { err := Convert_v1alpha3_InitConfiguration_To_kubeadm_InitConfiguration(tc.old, internal, nil)
t.Errorf("no error was expected but '%s' was found", err) if (err != nil) != tc.expectedErr {
} t.Errorf("no error was expected but '%s' was found", err)
}
})
} }
} }

View File

@ -34,17 +34,19 @@ func TestMarshalJSON(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`}, {BootstrapTokenString{ID: "h", Secret: "b"}, `"h.b"`},
} }
for _, rt := range tests { for _, rt := range tests {
b, err := json.Marshal(rt.bts) t.Run(rt.bts.ID, func(t *testing.T) {
if err != nil { b, err := json.Marshal(rt.bts)
t.Fatalf("json.Marshal returned an unexpected error: %v", err) if err != nil {
} t.Fatalf("json.Marshal returned an unexpected error: %v", err)
if string(b) != rt.expected { }
t.Errorf( if string(b) != rt.expected {
"failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s",
string(b), rt.expected,
) string(b),
} )
}
})
} }
} }
@ -64,17 +66,19 @@ func TestUnmarshalJSON(t *testing.T) {
{`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false}, {`"123456.aabbccddeeffgghh"`, &BootstrapTokenString{ID: "123456", Secret: "aabbccddeeffgghh"}, false},
} }
for _, rt := range tests { for _, rt := range tests {
newbts := &BootstrapTokenString{} t.Run(rt.input, func(t *testing.T) {
err := json.Unmarshal([]byte(rt.input), newbts) newbts := &BootstrapTokenString{}
if (err != nil) != rt.expectedError { err := json.Unmarshal([]byte(rt.input), newbts)
t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err) if (err != nil) != rt.expectedError {
} else if !reflect.DeepEqual(rt.bts, newbts) { t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err)
t.Errorf( } else if !reflect.DeepEqual(rt.bts, newbts) {
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.bts, "failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v",
newbts, rt.bts,
) newbts,
} )
}
})
} }
} }
@ -87,9 +91,11 @@ func TestJSONRoundtrip(t *testing.T) {
{"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}}, {"", &BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
if err := roundtrip(rt.input, rt.bts); err != nil { t.Run(rt.input, func(t *testing.T) {
t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err) if err := roundtrip(rt.input, rt.bts); err != nil {
} t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err)
}
})
} }
} }
@ -140,14 +146,16 @@ func TestTokenFromIDAndSecret(t *testing.T) {
{BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"}, {BootstrapTokenString{ID: "h", Secret: "b"}, "h.b"},
} }
for _, rt := range tests { for _, rt := range tests {
actual := rt.bts.String() t.Run(rt.bts.ID, func(t *testing.T) {
if actual != rt.expected { actual := rt.bts.String()
t.Errorf( if actual != rt.expected {
"failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s", t.Errorf(
rt.expected, "failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s",
actual, rt.expected,
) actual,
} )
}
})
} }
} }
@ -175,22 +183,24 @@ func TestNewBootstrapTokenString(t *testing.T) {
{token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {token: "123456.1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenString(rt.token) t.Run(rt.token, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenString(rt.token)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v",
rt.expectedError, rt.token,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.token, "failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v",
rt.bts, rt.token,
actual, rt.bts,
) actual,
} )
}
})
} }
} }
@ -215,23 +225,25 @@ func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
{id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}}, {id: "123456", secret: "1234560123456789", expectedError: false, bts: &BootstrapTokenString{ID: "123456", Secret: "1234560123456789"}},
} }
for _, rt := range tests { for _, rt := range tests {
actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret) t.Run(rt.id, func(t *testing.T) {
if (err != nil) != rt.expectedError { actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret)
t.Errorf( if (err != nil) != rt.expectedError {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v",
rt.secret, rt.id,
rt.expectedError, rt.secret,
err, rt.expectedError,
) err,
} else if !reflect.DeepEqual(actual, rt.bts) { )
t.Errorf( } else if !reflect.DeepEqual(actual, rt.bts) {
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v", t.Errorf(
rt.id, "failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v",
rt.secret, rt.id,
rt.bts, rt.secret,
actual, rt.bts,
) actual,
} )
}
})
} }
} }