Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏃Improve unit test coverage for KCP #2862

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions controlplane/kubeadm/internal/kubeadm_config_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ kubernetesVersion: v1.16.1
delete(kubeadmConfigNoKey.Data, clusterConfigurationKey)

kubeadmConfigBadData := kconf.DeepCopy()
kubeadmConfigBadData.Data[clusterConfigurationKey] = `foobar`
kubeadmConfigBadData.Data[clusterConfigurationKey] = `something`
wfernandes marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
Expand Down Expand Up @@ -94,6 +94,7 @@ kubernetesVersion: v1.16.1
})
}
}

func Test_kubeadmConfig_RemoveAPIEndpoint(t *testing.T) {
g := NewWithT(t)
original := &corev1.ConfigMap{
Expand Down Expand Up @@ -378,40 +379,49 @@ scheduler: {}`,
func TestUpdateImageRepository(t *testing.T) {

tests := []struct {
name string
clusterConfigurationValue string
imageRepository string
expected string
expectErr error
name string
data map[string]string
imageRepository string
expected string
expectErr error
}{
{
name: "it should set the values, if they were empty",
clusterConfigurationValue: `
data: map[string]string{
clusterConfigurationKey: `
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
imageRepository: k8s.gcr.io
`,
`},
imageRepository: "example.com/k8s",
expected: "example.com/k8s",
},
{
name: "it shouldn't write empty strings",
clusterConfigurationValue: `
data: map[string]string{
clusterConfigurationKey: `
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
imageRepository: k8s.gcr.io
`,
`},
imageRepository: "",
expected: "k8s.gcr.io",
},
{
name: "it should error if it's not a valid k8s object",
clusterConfigurationValue: `
data: map[string]string{
clusterConfigurationKey: `
imageRepository: "cool"
`,
`},
imageRepository: "example.com/k8s",
expectErr: errors.New("Object 'Kind' is missing"),
},
{
name: "returns an error if config map doesn't have the cluster config data key",
data: map[string]string{},
imageRepository: "example.com/k8s",
expectErr: errors.New("unable to find \"ClusterConfiguration\" key in kubeadm ConfigMap"),
},
}

for _, test := range tests {
Expand All @@ -420,9 +430,7 @@ imageRepository: "cool"

kconfig := &kubeadmConfig{
ConfigMap: &corev1.ConfigMap{
Data: map[string]string{
clusterConfigurationKey: test.clusterConfigurationValue,
},
Data: test.data,
},
}

Expand Down
79 changes: 79 additions & 0 deletions controlplane/kubeadm/internal/workload_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,85 @@ kubernetesVersion: v1.16.1
}
}

func TestUpdateImageRepositoryInKubeadmConfigMap(t *testing.T) {
kubeadmConfig := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: kubeadmConfigKey,
Namespace: metav1.NamespaceSystem,
},
Data: map[string]string{
clusterConfigurationKey: `
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
imageRepository: k8s.gcr.io
`,
},
}

kubeadmConfigNoKey := kubeadmConfig.DeepCopy()
delete(kubeadmConfigNoKey.Data, clusterConfigurationKey)

kubeadmConfigBadData := kubeadmConfig.DeepCopy()
kubeadmConfigBadData.Data[clusterConfigurationKey] = `foobar`

g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())
tests := []struct {
name string
imageRepository string
objs []runtime.Object
expectErr bool
}{
{
name: "updates the config map",
imageRepository: "myspecialrepo.io",
objs: []runtime.Object{kubeadmConfig},
expectErr: false,
},
{
name: "returns error if cannot find config map",
expectErr: true,
},
{
name: "returns error if config has bad data",
objs: []runtime.Object{kubeadmConfigBadData},
imageRepository: "myspecialrepo.io",
expectErr: true,
},
{
name: "returns error if config doesn't have cluster config key",
objs: []runtime.Object{kubeadmConfigNoKey},
imageRepository: "myspecialrepo.io",
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
fakeClient := fake.NewFakeClientWithScheme(scheme, tt.objs...)
w := &Workload{
Client: fakeClient,
}
ctx := context.TODO()
err := w.UpdateImageRepositoryInKubeadmConfigMap(ctx, tt.imageRepository)
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
var actualConfig corev1.ConfigMap
g.Expect(w.Client.Get(
ctx,
ctrlclient.ObjectKey{Name: kubeadmConfigKey, Namespace: metav1.NamespaceSystem},
&actualConfig,
)).To(Succeed())
g.Expect(actualConfig.Data[clusterConfigurationKey]).To(ContainSubstring(tt.imageRepository))
})
}
}

func TestClusterStatus(t *testing.T) {
node1 := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Expand Down