From 1217395b5adc99a028e5cc8472ffd96b9887a2ca Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 27 Dec 2017 21:43:44 -0800 Subject: [PATCH] Add additional unit tests. --- pkg/cloudprovider/providers/azure/BUILD | 2 + .../providers/azure/azure_backoff.go | 4 +- .../providers/azure/azure_backoff_test.go | 148 ++++++++++++++++++ .../providers/azure/azure_standard_test.go | 92 +++++++++++ 4 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 pkg/cloudprovider/providers/azure/azure_backoff_test.go create mode 100644 pkg/cloudprovider/providers/azure/azure_standard_test.go diff --git a/pkg/cloudprovider/providers/azure/BUILD b/pkg/cloudprovider/providers/azure/BUILD index bd65300c2ed..5f57de43f82 100644 --- a/pkg/cloudprovider/providers/azure/BUILD +++ b/pkg/cloudprovider/providers/azure/BUILD @@ -64,9 +64,11 @@ go_library( go_test( name = "go_default_test", srcs = [ + "azure_backoff_test.go", "azure_cache_test.go", "azure_loadbalancer_test.go", "azure_metrics_test.go", + "azure_standard_test.go", "azure_test.go", "azure_vmss_test.go", "azure_wrap_test.go", diff --git a/pkg/cloudprovider/providers/azure/azure_backoff.go b/pkg/cloudprovider/providers/azure/azure_backoff.go index 9e4ee788d45..6d2bd98bc14 100644 --- a/pkg/cloudprovider/providers/azure/azure_backoff.go +++ b/pkg/cloudprovider/providers/azure/azure_backoff.go @@ -341,8 +341,8 @@ func processRetryResponse(resp autorest.Response, err error) (bool, error) { // suppress the error object so that backoff process continues return false, nil } - // Fall-through: stop periodic backoff, return error object from most recent request - return true, err + // Fall-through: stop periodic backoff + return true, nil } // shouldRetryAPIRequest determines if the response from an HTTP request suggests periodic retry behavior diff --git a/pkg/cloudprovider/providers/azure/azure_backoff_test.go b/pkg/cloudprovider/providers/azure/azure_backoff_test.go new file mode 100644 index 00000000000..3e60e2460d9 --- /dev/null +++ b/pkg/cloudprovider/providers/azure/azure_backoff_test.go @@ -0,0 +1,148 @@ +/* +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" + "net/http" + "testing" + + "github.com/Azure/go-autorest/autorest" +) + +func TestShouldRetry(t *testing.T) { + tests := []struct { + code int + err error + expected bool + }{ + { + code: http.StatusBadRequest, + expected: true, + }, + { + code: http.StatusInternalServerError, + expected: true, + }, + { + code: http.StatusOK, + err: fmt.Errorf("some error"), + expected: true, + }, + { + code: http.StatusOK, + expected: false, + }, + { + code: 399, + expected: false, + }, + } + + for _, test := range tests { + resp := autorest.Response{ + Response: &http.Response{ + StatusCode: test.code, + }, + } + res := shouldRetryAPIRequest(resp, test.err) + if res != test.expected { + t.Errorf("expected: %v, saw: %v", test.expected, res) + } + } +} + +func TestIsSuccessResponse(t *testing.T) { + tests := []struct { + code int + expected bool + }{ + { + code: http.StatusNotFound, + expected: false, + }, + { + code: http.StatusInternalServerError, + expected: false, + }, + { + code: http.StatusOK, + expected: true, + }, + } + + for _, test := range tests { + resp := autorest.Response{ + Response: &http.Response{ + StatusCode: test.code, + }, + } + res := isSuccessHTTPResponse(resp) + if res != test.expected { + t.Errorf("expected: %v, saw: %v", test.expected, res) + } + } +} + +func TestProcessRetryResponse(t *testing.T) { + tests := []struct { + code int + err error + stop bool + }{ + { + code: http.StatusBadRequest, + stop: false, + }, + { + code: http.StatusInternalServerError, + stop: false, + }, + { + code: http.StatusSeeOther, + err: fmt.Errorf("some error"), + stop: false, + }, + { + code: http.StatusSeeOther, + stop: true, + }, + { + code: http.StatusOK, + stop: true, + }, + { + code: 399, + stop: true, + }, + } + + for _, test := range tests { + resp := autorest.Response{ + Response: &http.Response{ + StatusCode: test.code, + }, + } + res, err := processRetryResponse(resp, test.err) + if res != test.stop { + t.Errorf("expected: %v, saw: %v", test.stop, res) + } + if err != nil { + t.Errorf("unexpected error: %v", err) + } + } +} diff --git a/pkg/cloudprovider/providers/azure/azure_standard_test.go b/pkg/cloudprovider/providers/azure/azure_standard_test.go new file mode 100644 index 00000000000..b8febcc8940 --- /dev/null +++ b/pkg/cloudprovider/providers/azure/azure_standard_test.go @@ -0,0 +1,92 @@ +/* +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 ( + "testing" + + "k8s.io/api/core/v1" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestIsMasterNode(t *testing.T) { + if isMasterNode(&v1.Node{}) { + t.Errorf("Empty node should not be master!") + } + if isMasterNode(&v1.Node{ + ObjectMeta: meta.ObjectMeta{ + Labels: map[string]string{ + nodeLabelRole: "worker", + }, + }, + }) { + t.Errorf("Node labelled 'workerk' should not be master!") + } + if !isMasterNode(&v1.Node{ + ObjectMeta: meta.ObjectMeta{ + Labels: map[string]string{ + nodeLabelRole: "master", + }, + }, + }) { + t.Errorf("Node should be master!") + } +} + +func TestGetLastSegment(t *testing.T) { + tests := []struct { + ID string + expected string + expectErr bool + }{ + { + ID: "", + expected: "", + expectErr: true, + }, + { + ID: "foo/", + expected: "", + expectErr: true, + }, + { + ID: "foo/bar", + expected: "bar", + expectErr: false, + }, + { + ID: "foo/bar/baz", + expected: "baz", + expectErr: false, + }, + } + + for _, test := range tests { + s, e := getLastSegment(test.ID) + if test.expectErr && e == nil { + t.Errorf("Expected err, but it was nil") + continue + } + if !test.expectErr && e != nil { + t.Errorf("Unexpected error: %v", e) + continue + } + if s != test.expected { + t.Errorf("expected: %s, got %s", test.expected, s) + } + } +}