mirror of https://github.com/k3s-io/k3s
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.pull/6/head
parent
2110f72e4c
commit
4f035181cb
|
@ -38,44 +38,74 @@ func TestUsingEmptyTokenFails(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTokenValidationFailures(t *testing.T) {
|
||||
invalidTokens := []string{
|
||||
"1234567890123456789012",
|
||||
"12345.1234567890123456",
|
||||
".1234567890123456",
|
||||
"123456.1234567890.123456",
|
||||
var tests = []struct {
|
||||
t string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
t: "1234567890123456789012",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
t: "12345.1234567890123456",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
t: ".1234567890123456",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
t: "123456.1234567890.123456",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, token := range invalidTokens {
|
||||
s := newSecretsWithToken(token)
|
||||
for _, rt := range tests {
|
||||
s := newSecretsWithToken(rt.t)
|
||||
_, err := UseGivenTokenIfValid(s)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("UseGivenTokenIfValid did not return an error for this invalid token: [%s]", token)
|
||||
if (err == nil) != rt.expected {
|
||||
t.Errorf(
|
||||
"failed UseGivenTokenIfValid and did not return an error for this invalid token: [%s]",
|
||||
rt.t,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidTokenPopulatesSecrets(t *testing.T) {
|
||||
s := newSecretsWithToken("123456.0123456789AbCdEf")
|
||||
expectedToken := []byte("0123456789abcdef")
|
||||
expectedTokenID := "123456"
|
||||
expectedBearerToken := "0123456789abcdef"
|
||||
var tests = []struct {
|
||||
token string
|
||||
expectedToken []byte
|
||||
expectedTokenID string
|
||||
expectedBearerToken string
|
||||
}{
|
||||
{
|
||||
token: "123456.0123456789AbCdEf",
|
||||
expectedToken: []byte("0123456789abcdef"),
|
||||
expectedTokenID: "123456",
|
||||
expectedBearerToken: "0123456789abcdef",
|
||||
},
|
||||
}
|
||||
|
||||
given, err := UseGivenTokenIfValid(s)
|
||||
if err != nil {
|
||||
t.Errorf("UseGivenTokenIfValid gave an error for a valid token: %v", err)
|
||||
}
|
||||
if !given {
|
||||
t.Error("UseGivenTokenIfValid returned given = false when given a valid token")
|
||||
}
|
||||
if s.TokenID != expectedTokenID {
|
||||
t.Errorf("UseGivenTokenIfValid did not populate the TokenID correctly; expected [%s] but got [%s]", expectedTokenID, s.TokenID)
|
||||
}
|
||||
if s.BearerToken != expectedBearerToken {
|
||||
t.Errorf("UseGivenTokenIfValid did not populate the BearerToken correctly; expected [%s] but got [%s]", expectedBearerToken, s.BearerToken)
|
||||
}
|
||||
if !bytes.Equal(s.Token, expectedToken) {
|
||||
t.Errorf("UseGivenTokenIfValid did not populate the Token correctly; expected %v but got %v", expectedToken, s.Token)
|
||||
for _, rt := range tests {
|
||||
s := newSecretsWithToken(rt.token)
|
||||
|
||||
given, err := UseGivenTokenIfValid(s)
|
||||
if err != nil {
|
||||
t.Errorf("UseGivenTokenIfValid gave an error for a valid token: %v", err)
|
||||
}
|
||||
if !given {
|
||||
t.Error("UseGivenTokenIfValid returned given = false when given a valid token")
|
||||
}
|
||||
if s.TokenID != rt.expectedTokenID {
|
||||
t.Errorf("UseGivenTokenIfValid did not populate the TokenID correctly; expected [%s] but got [%s]", rt.expectedTokenID, s.TokenID)
|
||||
}
|
||||
if s.BearerToken != rt.expectedBearerToken {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue