Skip to content

Commit

Permalink
Improve unit test coverage for KCP
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Fernandes committed Apr 3, 2020
1 parent 9fe8ad4 commit 9b909cf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
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`

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

0 comments on commit 9b909cf

Please sign in to comment.