Skip to content

Commit

Permalink
Merge pull request #91520 from weijiehu/azureconfig
Browse files Browse the repository at this point in the history
Improves unittest CC for azure_config
  • Loading branch information
k8s-ci-robot committed May 30, 2020
2 parents 774c9a6 + f11ad05 commit acaa9ea
Showing 1 changed file with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func getTestCloudConfigTypeMergeConfigExpected() *Config {

func TestGetConfigFromSecret(t *testing.T) {
emptyConfig := &Config{}
badConfig := &Config{ResourceGroup: "DuplicateColumnsIncloud-config"}
tests := []struct {
name string
existingConfig *Config
Expand Down Expand Up @@ -129,6 +130,12 @@ func TestGetConfigFromSecret(t *testing.T) {
secretConfig: emptyConfig,
expectErr: true,
},
{
name: "Error should be reported when it failed to parse Azure cloud-config",
existingConfig: getTestCloudConfigTypeMergeConfig(),
secretConfig: badConfig,
expectErr: true,
},
}

for _, test := range tests {
Expand All @@ -147,13 +154,16 @@ func TestGetConfigFromSecret(t *testing.T) {
Namespace: "kube-system",
},
}
if test.secretConfig != emptyConfig {
if test.secretConfig != emptyConfig && test.secretConfig != badConfig {
secretData, err := yaml.Marshal(test.secretConfig)
assert.NoError(t, err, test.name)
secret.Data = map[string][]byte{
"cloud-config": secretData,
}
}
if test.secretConfig == badConfig {
secret.Data = map[string][]byte{"cloud-config": []byte(`unknown: "hello",unknown: "hello"`)}
}
_, err := az.KubeClient.CoreV1().Secrets(cloudConfigNamespace).Create(context.TODO(), secret, metav1.CreateOptions{})
assert.NoError(t, err, test.name)
}
Expand All @@ -169,3 +179,89 @@ func TestGetConfigFromSecret(t *testing.T) {
})
}
}

func TestInitializeCloudFromSecret(t *testing.T) {
emptyConfig := &Config{}
unknownConfigTypeConfig := getTestConfig()
unknownConfigTypeConfig.CloudConfigType = "UnknownConfigType"
tests := []struct {
name string
existingConfig *Config
secretConfig *Config
expected *Config
expectErr bool
}{
{
name: "Azure config shouldn't be override when cloud config type is file",
existingConfig: &Config{
ResourceGroup: "ResourceGroup1",
CloudConfigType: cloudConfigTypeFile,
},
secretConfig: getTestConfig(),
expected: nil,
},
{
name: "Azure config shouldn't be override when cloud config type is unknown",
existingConfig: &Config{
ResourceGroup: "ResourceGroup1",
CloudConfigType: "UnknownConfigType",
},
secretConfig: unknownConfigTypeConfig,
expected: nil,
},
{
name: "Azure config should be override when cloud config type is secret",
existingConfig: getTestCloudConfigTypeSecretConfig(),
secretConfig: getTestConfig(),
expected: getTestConfig(),
},
{
name: "Azure config should be override when cloud config type is merge",
existingConfig: getTestCloudConfigTypeMergeConfig(),
secretConfig: getTestConfig(),
expected: getTestCloudConfigTypeMergeConfigExpected(),
},
{
name: "Error should be reported when secret doesn't exists",
existingConfig: getTestCloudConfigTypeMergeConfig(),
expectErr: true,
},
{
name: "Error should be reported when secret exists but cloud-config data is not provided",
existingConfig: getTestCloudConfigTypeMergeConfig(),
secretConfig: emptyConfig,
expectErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
az := &Cloud{
KubeClient: fakeclient.NewSimpleClientset(),
}
if test.existingConfig != nil {
az.Config = *test.existingConfig
}
if test.secretConfig != nil {
secret := &v1.Secret{
Type: v1.SecretTypeOpaque,
ObjectMeta: metav1.ObjectMeta{
Name: "azure-cloud-provider",
Namespace: "kube-system",
},
}
if test.secretConfig != emptyConfig {
secretData, err := yaml.Marshal(test.secretConfig)
assert.NoError(t, err, test.name)
secret.Data = map[string][]byte{
"cloud-config": secretData,
}
}
_, err := az.KubeClient.CoreV1().Secrets(cloudConfigNamespace).Create(context.TODO(), secret, metav1.CreateOptions{})
assert.NoError(t, err, test.name)
}

az.InitializeCloudFromSecret()
})
}
}

0 comments on commit acaa9ea

Please sign in to comment.