From f28db5a897aaa33efb2c6dae156bec0ff2497b39 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 23 Jan 2018 05:34:43 +0000 Subject: [PATCH] Add some more azure unit tests. --- pkg/cloudprovider/providers/azure/BUILD | 2 + .../providers/azure/azure_fakes.go | 4 +- .../azure/azure_storageaccount_test.go | 80 +++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 pkg/cloudprovider/providers/azure/azure_storageaccount_test.go diff --git a/pkg/cloudprovider/providers/azure/BUILD b/pkg/cloudprovider/providers/azure/BUILD index 5f57de43f82..378254f7dcc 100644 --- a/pkg/cloudprovider/providers/azure/BUILD +++ b/pkg/cloudprovider/providers/azure/BUILD @@ -69,6 +69,7 @@ go_test( "azure_loadbalancer_test.go", "azure_metrics_test.go", "azure_standard_test.go", + "azure_storageaccount_test.go", "azure_test.go", "azure_vmss_test.go", "azure_wrap_test.go", @@ -81,6 +82,7 @@ go_test( "//pkg/kubelet/apis:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/arm/compute:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/arm/network:go_default_library", + "//vendor/github.com/Azure/azure-sdk-for-go/arm/storage:go_default_library", "//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", diff --git a/pkg/cloudprovider/providers/azure/azure_fakes.go b/pkg/cloudprovider/providers/azure/azure_fakes.go index 72aca359194..4778cd86201 100644 --- a/pkg/cloudprovider/providers/azure/azure_fakes.go +++ b/pkg/cloudprovider/providers/azure/azure_fakes.go @@ -930,6 +930,8 @@ func (fRTC *fakeRouteTablesClient) Get(resourceGroupName string, routeTableName type fakeStorageAccountClient struct { mutex *sync.Mutex FakeStore map[string]map[string]storage.Account + Keys storage.AccountListKeysResult + Err error } func newFakeStorageAccountClient() *fakeStorageAccountClient { @@ -999,7 +1001,7 @@ func (fSAC *fakeStorageAccountClient) Delete(resourceGroupName string, accountNa } func (fSAC *fakeStorageAccountClient) ListKeys(resourceGroupName string, accountName string) (result storage.AccountListKeysResult, err error) { - return storage.AccountListKeysResult{}, nil + return fSAC.Keys, fSAC.Err } func (fSAC *fakeStorageAccountClient) ListByResourceGroup(resourceGroupName string) (result storage.AccountListResult, err error) { diff --git a/pkg/cloudprovider/providers/azure/azure_storageaccount_test.go b/pkg/cloudprovider/providers/azure/azure_storageaccount_test.go new file mode 100644 index 00000000000..86befc1ae8e --- /dev/null +++ b/pkg/cloudprovider/providers/azure/azure_storageaccount_test.go @@ -0,0 +1,80 @@ +/* +Copyright 2017 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 ( + "fmt" + "testing" + + "github.com/Azure/azure-sdk-for-go/arm/storage" +) + +func TestGetStorageAccessKeys(t *testing.T) { + cloud := &Cloud{} + fake := newFakeStorageAccountClient() + cloud.StorageAccountClient = fake + value := "foo bar" + + tests := []struct { + results storage.AccountListKeysResult + expectedKey string + expectErr bool + err error + }{ + {storage.AccountListKeysResult{}, "", true, nil}, + { + storage.AccountListKeysResult{ + Keys: &[]storage.AccountKey{ + {Value: &value}, + }, + }, + "bar", + false, + nil, + }, + { + storage.AccountListKeysResult{ + Keys: &[]storage.AccountKey{ + {}, + {Value: &value}, + }, + }, + "bar", + false, + nil, + }, + {storage.AccountListKeysResult{}, "", true, fmt.Errorf("test error")}, + } + + for _, test := range tests { + expectedKey := test.expectedKey + fake.Keys = test.results + fake.Err = test.err + key, err := cloud.getStorageAccesskey("acct") + if test.expectErr && err == nil { + t.Errorf("Unexpected non-error") + continue + } + if !test.expectErr && err != nil { + t.Errorf("Unexpected error: %v", err) + continue + } + if key != expectedKey { + t.Errorf("expected: %s, saw %s", expectedKey, key) + } + } +}