kubeadm: added unit tests for util/tokens

pull/6/head
Derek McQuay 2016-10-20 10:52:19 -07:00 committed by Paulo Pires
parent 569da52204
commit 1bfa867088
No known key found for this signature in database
GPG Key ID: F3F6ED5C522EAA71
1 changed files with 85 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package util
import (
"bytes"
"strings"
"testing"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
@ -83,3 +84,87 @@ func newSecretsWithToken(token string) *kubeadmapi.Secrets {
s.GivenToken = token
return s
}
func TestGenerateToken(t *testing.T) {
var genTest = []struct {
s kubeadmapi.Secrets
l int
n int
}{
{kubeadmapi.Secrets{}, 2, 6},
}
for _, rt := range genTest {
GenerateToken(&rt.s)
givenToken := strings.Split(strings.ToLower(rt.s.GivenToken), ".")
if len(givenToken) != rt.l {
t.Errorf(
"failed GenerateToken:\n\texpected: %d\n\t actual: %d",
rt.l,
len(givenToken),
)
}
if len(givenToken[0]) != rt.n {
t.Errorf(
"failed GenerateToken:\n\texpected: %d\n\t actual: %d",
rt.l,
len(givenToken),
)
}
}
}
func TestUseGivenTokenIfValid(t *testing.T) {
var tokenTest = []struct {
s kubeadmapi.Secrets
expected bool
}{
{kubeadmapi.Secrets{GivenToken: ""}, false}, // GivenToken == ""
{kubeadmapi.Secrets{GivenToken: "noperiod"}, false}, // not 2-part '.' format
{kubeadmapi.Secrets{GivenToken: "abcd.a"}, false}, // len(tokenID) != 6
{kubeadmapi.Secrets{GivenToken: "abcdef.a"}, true},
}
for _, rt := range tokenTest {
actual, _ := UseGivenTokenIfValid(&rt.s)
if actual != rt.expected {
t.Errorf(
"failed UseGivenTokenIfValid:\n\texpected: %t\n\t actual: %t",
rt.expected,
actual,
)
}
}
}
func TestRandBytes(t *testing.T) {
var randTest = []struct {
r int
l int
expected error
}{
{0, 0, nil},
{1, 1, nil},
{2, 2, nil},
{3, 3, nil},
{100, 100, nil},
}
for _, rt := range randTest {
actual, _, err := RandBytes(rt.r)
if err != rt.expected {
t.Errorf(
"failed RandBytes:\n\texpected: %t\n\t actual: %t",
rt.expected,
err,
)
}
if len(actual) != rt.l {
t.Errorf(
"failed RandBytes:\n\texpected: %d\n\t actual: %d\n",
rt.l,
len(actual),
)
}
}
}