Add unit tests for getConfigFromSecret

This commit is contained in:
Pengfei Ni
2019-05-23 15:51:12 +08:00
parent 8f0e05fb6f
commit 9516ade5c4
4 changed files with 173 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ go_library(
"//vendor/github.com/Azure/go-autorest/autorest/adal:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest/azure:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library",
"//vendor/github.com/kardianos/osext:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/rubiojr/go-vhd/vhd:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
@@ -82,6 +83,7 @@ go_test(
srcs = [
"azure_backoff_test.go",
"azure_cache_test.go",
"azure_config_test.go",
"azure_controller_common_test.go",
"azure_instances_test.go",
"azure_loadbalancer_test.go",
@@ -102,6 +104,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//staging/src/k8s.io/cloud-provider:go_default_library",
"//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library",
@@ -112,6 +115,7 @@ go_test(
"//vendor/github.com/Azure/go-autorest/autorest:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
],
)

View File

@@ -0,0 +1,166 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakeclient "k8s.io/client-go/kubernetes/fake"
"k8s.io/legacy-cloud-providers/azure/auth"
"sigs.k8s.io/yaml"
"github.com/Azure/go-autorest/autorest/to"
"github.com/stretchr/testify/assert"
)
func getTestConfig() *Config {
return &Config{
AzureAuthConfig: auth.AzureAuthConfig{
TenantID: "TenantID",
SubscriptionID: "SubscriptionID",
AADClientID: "AADClientID",
AADClientSecret: "AADClientSecret",
},
ResourceGroup: "ResourceGroup",
RouteTableName: "RouteTableName",
RouteTableResourceGroup: "RouteTableResourceGroup",
Location: "Location",
SubnetName: "SubnetName",
VnetName: "VnetName",
PrimaryAvailabilitySetName: "PrimaryAvailabilitySetName",
PrimaryScaleSetName: "PrimaryScaleSetName",
LoadBalancerSku: "LoadBalancerSku",
ExcludeMasterFromStandardLB: to.BoolPtr(true),
}
}
func getTestMustOverrideConfig() *Config {
return &Config{
AzureAuthConfig: auth.AzureAuthConfig{
TenantID: "TenantID",
SubscriptionID: "SubscriptionID",
},
ResourceGroup: "ResourceGroup",
RouteTableName: "RouteTableName",
RouteTableResourceGroup: "RouteTableResourceGroup",
SecurityGroupName: "SecurityGroupName",
OverrideType: secretOverrideTypeMust,
}
}
func getTestCanOverrideConfig() *Config {
return &Config{
AzureAuthConfig: auth.AzureAuthConfig{
TenantID: "TenantID",
SubscriptionID: "SubscriptionID",
},
ResourceGroup: "ResourceGroup",
RouteTableName: "RouteTableName",
RouteTableResourceGroup: "RouteTableResourceGroup",
SecurityGroupName: "SecurityGroupName",
OverrideType: secretOverrideTypeCan,
}
}
func getTestCanOverrideConfigExpected() *Config {
config := getTestConfig()
config.SecurityGroupName = "SecurityGroupName"
config.OverrideType = secretOverrideTypeCan
return config
}
func TestGetConfigFromSecret(t *testing.T) {
emptyConfig := &Config{}
tests := []struct {
name string
existingConfig *Config
secretConfig *Config
expected *Config
expectErr bool
}{
{
name: "Azure config shouldn't be override when override type is no",
existingConfig: &Config{
ResourceGroup: "ResourceGroup1",
OverrideType: secretOverrideTypeNo,
},
secretConfig: getTestConfig(),
expected: nil,
},
{
name: "Azure config should be override when override type is must",
existingConfig: getTestMustOverrideConfig(),
secretConfig: getTestConfig(),
expected: getTestConfig(),
},
{
name: "Azure config should be override when override type is can",
existingConfig: getTestCanOverrideConfig(),
secretConfig: getTestConfig(),
expected: getTestCanOverrideConfigExpected(),
},
{
name: "Error should be reported when secret doesn't exists",
existingConfig: getTestCanOverrideConfig(),
expectErr: true,
},
{
name: "Error should be reported when secret exists but cloud-config data is not provided",
existingConfig: getTestCanOverrideConfig(),
secretConfig: emptyConfig,
expectErr: true,
},
}
for _, test := range tests {
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(secretNamespace).Create(secret)
assert.NoError(t, err, test.name)
}
real, err := az.getConfigFromSecret()
if test.expectErr {
assert.Error(t, err, test.name)
continue
}
assert.NoError(t, err, test.name)
assert.Equal(t, test.expected, real, test.name)
}
}

View File

@@ -11,6 +11,7 @@ require (
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20181220005116-f8e995905100
github.com/aws/aws-sdk-go v1.16.26
github.com/dnaeon/go-vcr v1.0.1 // indirect
github.com/kardianos/osext v0.0.0-20150410034420-8fef92e41e22
github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c // indirect
github.com/prometheus/client_golang v0.9.2
github.com/rubiojr/go-vhd v0.0.0-20160810183302-0bfd3b39853c

View File

@@ -48,6 +48,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be h1:AHimNtVIpiBjPUhEF5KNCkrUyqTSA5zWUl8sQ2bfGBE=
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/kardianos/osext v0.0.0-20150410034420-8fef92e41e22 h1:eLCQd4nxsC7sumkwNg4OiB6bGiD7I5l1MSfBAxmxkKQ=
github.com/kardianos/osext v0.0.0-20150410034420-8fef92e41e22/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c h1:N7uWGS2fTwH/4BwxbHiJZNAFTSJ5yPU0emHsQWvkxEY=
github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=