kubeadm: changed tests to be table driven

This change was to make tests found in tokens_test.go be table driven to
match other testing development that has been going on in kubeadm.
This commit is contained in:
Derek McQuay 2016-11-03 09:43:03 -07:00
parent 2110f72e4c
commit 4f035181cb

View File

@ -38,44 +38,74 @@ func TestUsingEmptyTokenFails(t *testing.T) {
} }
func TestTokenValidationFailures(t *testing.T) { func TestTokenValidationFailures(t *testing.T) {
invalidTokens := []string{ var tests = []struct {
"1234567890123456789012", t string
"12345.1234567890123456", expected bool
".1234567890123456", }{
"123456.1234567890.123456", {
t: "1234567890123456789012",
expected: false,
},
{
t: "12345.1234567890123456",
expected: false,
},
{
t: ".1234567890123456",
expected: false,
},
{
t: "123456.1234567890.123456",
expected: false,
},
} }
for _, token := range invalidTokens { for _, rt := range tests {
s := newSecretsWithToken(token) s := newSecretsWithToken(rt.t)
_, err := UseGivenTokenIfValid(s) _, err := UseGivenTokenIfValid(s)
if (err == nil) != rt.expected {
if err == nil { t.Errorf(
t.Errorf("UseGivenTokenIfValid did not return an error for this invalid token: [%s]", token) "failed UseGivenTokenIfValid and did not return an error for this invalid token: [%s]",
rt.t,
)
} }
} }
} }
func TestValidTokenPopulatesSecrets(t *testing.T) { func TestValidTokenPopulatesSecrets(t *testing.T) {
s := newSecretsWithToken("123456.0123456789AbCdEf") var tests = []struct {
expectedToken := []byte("0123456789abcdef") token string
expectedTokenID := "123456" expectedToken []byte
expectedBearerToken := "0123456789abcdef" expectedTokenID string
expectedBearerToken string
}{
{
token: "123456.0123456789AbCdEf",
expectedToken: []byte("0123456789abcdef"),
expectedTokenID: "123456",
expectedBearerToken: "0123456789abcdef",
},
}
given, err := UseGivenTokenIfValid(s) for _, rt := range tests {
if err != nil { s := newSecretsWithToken(rt.token)
t.Errorf("UseGivenTokenIfValid gave an error for a valid token: %v", err)
} given, err := UseGivenTokenIfValid(s)
if !given { if err != nil {
t.Error("UseGivenTokenIfValid returned given = false when given a valid token") t.Errorf("UseGivenTokenIfValid gave an error for a valid token: %v", err)
} }
if s.TokenID != expectedTokenID { if !given {
t.Errorf("UseGivenTokenIfValid did not populate the TokenID correctly; expected [%s] but got [%s]", expectedTokenID, s.TokenID) t.Error("UseGivenTokenIfValid returned given = false when given a valid token")
} }
if s.BearerToken != expectedBearerToken { if s.TokenID != rt.expectedTokenID {
t.Errorf("UseGivenTokenIfValid did not populate the BearerToken correctly; expected [%s] but got [%s]", expectedBearerToken, s.BearerToken) t.Errorf("UseGivenTokenIfValid did not populate the TokenID correctly; expected [%s] but got [%s]", rt.expectedTokenID, s.TokenID)
} }
if !bytes.Equal(s.Token, expectedToken) { if s.BearerToken != rt.expectedBearerToken {
t.Errorf("UseGivenTokenIfValid did not populate the Token correctly; expected %v but got %v", expectedToken, s.Token) t.Errorf("UseGivenTokenIfValid did not populate the BearerToken correctly; expected [%s] but got [%s]", rt.expectedBearerToken, s.BearerToken)
}
if !bytes.Equal(s.Token, rt.expectedToken) {
t.Errorf("UseGivenTokenIfValid did not populate the Token correctly; expected %v but got %v", rt.expectedToken, s.Token)
}
} }
} }