kubeadm: use t.Run in selfhosting and update phases

Used T.Run API for kubeadm tests in app/phases/selfhosting and
app/phases/update directories

This should improve testing output and make it more visible
which test is doing what.
This commit is contained in:
Ed Bartosh 2019-01-03 19:22:09 +02:00
parent 716b253963
commit 442098bdec
6 changed files with 300 additions and 239 deletions

View File

@ -27,11 +27,13 @@ import (
func TestMutatePodSpec(t *testing.T) {
var tests = []struct {
name string
component string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "mutate api server podspec",
component: kubeadmconstants.KubeAPIServer,
podSpec: &v1.PodSpec{
Containers: []v1.Container{
@ -73,6 +75,7 @@ func TestMutatePodSpec(t *testing.T) {
},
},
{
name: "mutate controller manager podspec",
component: kubeadmconstants.KubeControllerManager,
podSpec: &v1.PodSpec{},
expected: v1.PodSpec{
@ -86,6 +89,7 @@ func TestMutatePodSpec(t *testing.T) {
},
},
{
name: "mutate scheduler podspec",
component: kubeadmconstants.KubeScheduler,
podSpec: &v1.PodSpec{},
expected: v1.PodSpec{
@ -101,20 +105,24 @@ func TestMutatePodSpec(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
mutatePodSpec(GetDefaultMutators(), rt.component, rt.podSpec)
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed mutatePodSpec:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestAddNodeSelectorToPodSpec(t *testing.T) {
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "empty podspec",
podSpec: &v1.PodSpec{},
expected: v1.PodSpec{
NodeSelector: map[string]string{
@ -123,6 +131,7 @@ func TestAddNodeSelectorToPodSpec(t *testing.T) {
},
},
{
name: "podspec with a valid node selector",
podSpec: &v1.PodSpec{
NodeSelector: map[string]string{
"foo": "bar",
@ -138,20 +147,24 @@ func TestAddNodeSelectorToPodSpec(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
addNodeSelectorToPodSpec(rt.podSpec)
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed addNodeSelectorToPodSpec:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestSetMasterTolerationOnPodSpec(t *testing.T) {
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "empty podspec",
podSpec: &v1.PodSpec{},
expected: v1.PodSpec{
Tolerations: []v1.Toleration{
@ -160,6 +173,7 @@ func TestSetMasterTolerationOnPodSpec(t *testing.T) {
},
},
{
name: "podspec with a valid toleration",
podSpec: &v1.PodSpec{
Tolerations: []v1.Toleration{
{Key: "foo", Value: "bar"},
@ -175,26 +189,31 @@ func TestSetMasterTolerationOnPodSpec(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setMasterTolerationOnPodSpec(rt.podSpec)
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setMasterTolerationOnPodSpec:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestSetRightDNSPolicyOnPodSpec(t *testing.T) {
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "empty podspec",
podSpec: &v1.PodSpec{},
expected: v1.PodSpec{
DNSPolicy: v1.DNSClusterFirstWithHostNet,
},
},
{
name: "podspec with a v1.DNSClusterFirst policy",
podSpec: &v1.PodSpec{
DNSPolicy: v1.DNSClusterFirst,
},
@ -205,20 +224,24 @@ func TestSetRightDNSPolicyOnPodSpec(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setRightDNSPolicyOnPodSpec(rt.podSpec)
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setRightDNSPolicyOnPodSpec:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestSetHostIPOnPodSpec(t *testing.T) {
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "set HOST_IP env var on a podspec",
podSpec: &v1.PodSpec{
Containers: []v1.Container{
{
@ -254,21 +277,25 @@ func TestSetHostIPOnPodSpec(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setHostIPOnPodSpec(rt.podSpec)
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setHostIPOnPodSpec:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestSetSelfHostedVolumesForAPIServer(t *testing.T) {
hostPathDirectoryOrCreate := v1.HostPathDirectoryOrCreate
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "set selfhosted volumes for api server",
podSpec: &v1.PodSpec{
Containers: []v1.Container{
{
@ -346,6 +373,7 @@ func TestSetSelfHostedVolumesForAPIServer(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setSelfHostedVolumesForAPIServer(rt.podSpec)
sort.Strings(rt.podSpec.Containers[0].Command)
sort.Strings(rt.expected.Containers[0].Command)
@ -353,6 +381,7 @@ func TestSetSelfHostedVolumesForAPIServer(t *testing.T) {
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setSelfHostedVolumesForAPIServer:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
@ -360,10 +389,12 @@ func TestSetSelfHostedVolumesForControllerManager(t *testing.T) {
hostPathFileOrCreate := v1.HostPathFileOrCreate
hostPathDirectoryOrCreate := v1.HostPathDirectoryOrCreate
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "set selfhosted volumes for controller mananger",
podSpec: &v1.PodSpec{
Containers: []v1.Container{
{
@ -464,6 +495,7 @@ func TestSetSelfHostedVolumesForControllerManager(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setSelfHostedVolumesForControllerManager(rt.podSpec)
sort.Strings(rt.podSpec.Containers[0].Command)
sort.Strings(rt.expected.Containers[0].Command)
@ -471,16 +503,19 @@ func TestSetSelfHostedVolumesForControllerManager(t *testing.T) {
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setSelfHostedVolumesForControllerManager:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}
func TestSetSelfHostedVolumesForScheduler(t *testing.T) {
hostPathFileOrCreate := v1.HostPathFileOrCreate
var tests = []struct {
name string
podSpec *v1.PodSpec
expected v1.PodSpec
}{
{
name: "set selfhosted volumes for scheduler",
podSpec: &v1.PodSpec{
Containers: []v1.Container{
{
@ -534,6 +569,7 @@ func TestSetSelfHostedVolumesForScheduler(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
setSelfHostedVolumesForScheduler(rt.podSpec)
sort.Strings(rt.podSpec.Containers[0].Command)
sort.Strings(rt.expected.Containers[0].Command)
@ -541,5 +577,6 @@ func TestSetSelfHostedVolumesForScheduler(t *testing.T) {
if !reflect.DeepEqual(*rt.podSpec, rt.expected) {
t.Errorf("failed setSelfHostedVolumesForScheduler:\nexpected:\n%v\nsaw:\n%v", rt.expected, *rt.podSpec)
}
})
}
}

View File

@ -488,6 +488,7 @@ func TestBuildDaemonSet(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.component, func(t *testing.T) {
tempFile, err := createTempFileWithContent(rt.podBytes)
if err != nil {
t.Errorf("error creating tempfile with content:%v", err)
@ -508,21 +509,23 @@ func TestBuildDaemonSet(t *testing.T) {
if !bytes.Equal(dsBytes, rt.dsBytes) {
t.Errorf("failed TestBuildDaemonSet:\nexpected:\n%s\nsaw:\n%s", rt.dsBytes, dsBytes)
}
})
}
}
func TestLoadPodSpecFromFile(t *testing.T) {
tests := []struct {
name string
content string
expectError bool
}{
{
// No content
name: "no content",
content: "",
expectError: true,
},
{
// Good YAML
name: "valid YAML",
content: `
apiVersion: v1
kind: Pod
@ -535,7 +538,7 @@ spec:
expectError: false,
},
{
// Good JSON
name: "valid JSON",
content: `
{
"apiVersion": "v1",
@ -554,7 +557,7 @@ spec:
expectError: false,
},
{
// Bad PodSpec
name: "incorrect PodSpec",
content: `
apiVersion: v1
kind: Pod
@ -568,6 +571,7 @@ spec:
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
tempFile, err := createTempFileWithContent([]byte(rt.content))
if err != nil {
t.Errorf("error creating tempfile with content:%v", err)
@ -578,12 +582,15 @@ spec:
if (err != nil) != rt.expectError {
t.Errorf("failed TestLoadPodSpecFromFile:\nexpected error:\n%t\nsaw:\n%v", rt.expectError, err)
}
})
}
t.Run("empty file name", func(t *testing.T) {
_, err := loadPodSpecFromFile("")
if err == nil {
t.Error("unexpected success: loadPodSpecFromFile should return error when no file is given")
}
})
}
func createTempFileWithContent(content []byte) (string, error) {

View File

@ -781,18 +781,21 @@ func TestGetAvailableUpgrades(t *testing.T) {
func TestKubeletUpgrade(t *testing.T) {
tests := []struct {
name string
before map[string]uint16
after string
expected bool
}{
{ // upgrade available
{
name: "upgrade from v1.10.1 to v1.10.3 is available",
before: map[string]uint16{
"v1.10.1": 1,
},
after: "v1.10.3",
expected: true,
},
{ // upgrade available
{
name: "upgrade from v1.10.1 and v1.10.3/100 to v1.10.3 is available",
before: map[string]uint16{
"v1.10.1": 1,
"v1.10.3": 100,
@ -800,21 +803,24 @@ func TestKubeletUpgrade(t *testing.T) {
after: "v1.10.3",
expected: true,
},
{ // upgrade not available
{
name: "upgrade from v1.10.3 to v1.10.3 is not available",
before: map[string]uint16{
"v1.10.3": 1,
},
after: "v1.10.3",
expected: false,
},
{ // upgrade not available
{
name: "upgrade from v1.10.3/100 to v1.10.3 is not available",
before: map[string]uint16{
"v1.10.3": 100,
},
after: "v1.10.3",
expected: false,
},
{ // upgrade not available if we don't know anything about the earlier state
{
name: "upgrade is not available if we don't know anything about the earlier state",
before: map[string]uint16{},
after: "v1.10.3",
expected: false,
@ -822,7 +828,7 @@ func TestKubeletUpgrade(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
upgrade := Upgrade{
Before: ClusterState{
KubeletVersions: rt.before,
@ -835,6 +841,7 @@ func TestKubeletUpgrade(t *testing.T) {
if actual != rt.expected {
t.Errorf("failed TestKubeletUpgrade\n\texpected: %t\n\tgot: %t\n\ttest object: %v", rt.expected, actual, upgrade)
}
})
}
}
@ -883,10 +890,11 @@ func TestGetBranchFromVersion(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.version, func(t *testing.T) {
v := getBranchFromVersion(tc.version)
if v != tc.expectedVersion {
t.Errorf("expected version %s, got %s", tc.expectedVersion, v)
}
})
}
}

View File

@ -150,6 +150,7 @@ func TestShouldBackupAPIServerCertAndKey(t *testing.T) {
expected: true,
},
} {
t.Run(desc, func(t *testing.T) {
tmpdir := testutil.SetupTempDir(t)
defer os.RemoveAll(tmpdir)
cfg.CertificatesDir = tmpdir
@ -180,5 +181,6 @@ func TestShouldBackupAPIServerCertAndKey(t *testing.T) {
if shouldBackup != test.expected {
t.Fatalf("Test %s: shouldBackupAPIServerCertAndKey expected %v, got %v", desc, test.expected, shouldBackup)
}
})
}
}

View File

@ -108,26 +108,31 @@ func (p *goodPrepuller) DeleteFunc(component string) error {
func TestPrepullImagesInParallel(t *testing.T) {
tests := []struct {
name string
p Prepuller
timeout time.Duration
expectedErr bool
}{
{ // should error out; create failed
{
name: "should error out; create failed",
p: NewFailedCreatePrepuller(),
timeout: 10 * time.Second,
expectedErr: true,
},
{ // should error out; timeout exceeded
{
name: "should error out; timeout exceeded",
p: NewForeverWaitPrepuller(),
timeout: 10 * time.Second,
expectedErr: true,
},
{ // should error out; delete failed
{
name: "should error out; delete failed",
p: NewFailedDeletePrepuller(),
timeout: 10 * time.Second,
expectedErr: true,
},
{ // should work just fine
{
name: "should work just fine",
p: NewGoodPrepuller(),
timeout: 10 * time.Second,
expectedErr: false,
@ -135,7 +140,7 @@ func TestPrepullImagesInParallel(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
actualErr := PrepullImagesInParallel(rt.p, rt.timeout, append(constants.MasterComponents, constants.Etcd))
if (actualErr != nil) != rt.expectedErr {
t.Errorf(
@ -144,5 +149,6 @@ func TestPrepullImagesInParallel(t *testing.T) {
(actualErr != nil),
)
}
})
}
}

View File

@ -412,6 +412,7 @@ func TestStaticPodControlPlane(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.description, func(t *testing.T) {
waiter := NewFakeStaticPodWaiter(rt.waitErrsToReturn)
pathMgr, err := NewFakeStaticPodPathManager(rt.moveFileFunc)
if err != nil {
@ -521,7 +522,7 @@ func TestStaticPodControlPlane(t *testing.T) {
newHash,
)
}
return
})
}
}