Skip to content

Commit

Permalink
Merge pull request #4447 from mboersma/go-away-autorest
Browse files Browse the repository at this point in the history
Replace most usage of go-autorest in unit tests
  • Loading branch information
k8s-ci-robot authored Jan 10, 2024
2 parents 8b8cda3 + ce24d80 commit 3ed87c4
Show file tree
Hide file tree
Showing 22 changed files with 241 additions and 160 deletions.
10 changes: 0 additions & 10 deletions azure/scope/managedcontrolplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
aadpodv1 "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity/v1"
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1291,9 +1290,6 @@ func TestManagedControlPlaneScope_PrivateEndpointSpecs(t *testing.T) {
{
Name: "returns empty private endpoints list if no subnets are specified",
Input: ManagedControlPlaneScopeParams{
AzureClients: AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Expand All @@ -1318,9 +1314,6 @@ func TestManagedControlPlaneScope_PrivateEndpointSpecs(t *testing.T) {
{
Name: "returns empty private endpoints list if no private endpoints are specified",
Input: ManagedControlPlaneScopeParams{
AzureClients: AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Expand Down Expand Up @@ -1351,9 +1344,6 @@ func TestManagedControlPlaneScope_PrivateEndpointSpecs(t *testing.T) {
{
Name: "returns list of private endpoint specs if private endpoints are specified",
Input: ManagedControlPlaneScopeParams{
AzureClients: AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Expand Down
34 changes: 22 additions & 12 deletions azure/services/availabilitysets/availabilitysets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package availabilitysets

import (
"context"
"io"
"net/http"
"strconv"
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -62,9 +64,8 @@ var (
SKU: nil,
AdditionalTags: map[string]string{},
}
internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
parameterError = errors.Errorf("some error with parameters")
notFoundError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusNotFound}, "Not Found")
notFoundError = &azcore.ResponseError{StatusCode: http.StatusNotFound}
fakeSetWithVMs = armcompute.AvailabilitySet{
Properties: &armcompute.AvailabilitySetProperties{
VirtualMachines: []*armcompute.SubResource{
Expand All @@ -74,6 +75,15 @@ var (
}
)

func internalError() *azcore.ResponseError {
return &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
}

func TestReconcileAvailabilitySets(t *testing.T) {
testcases := []struct {
name string
Expand Down Expand Up @@ -114,8 +124,8 @@ func TestReconcileAvailabilitySets(t *testing.T) {
expect: func(s *mock_availabilitysets.MockAvailabilitySetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder) {
s.DefaultedAzureServiceReconcileTimeout().Return(reconciler.DefaultAzureServiceReconcileTimeout)
s.AvailabilitySetSpec().Return(&fakeSetSpec)
r.CreateOrUpdateResource(gomockinternal.AContext(), &fakeSetSpec, serviceName).Return(nil, internalError)
s.UpdatePutStatus(infrav1.AvailabilitySetReadyCondition, serviceName, internalError)
r.CreateOrUpdateResource(gomockinternal.AContext(), &fakeSetSpec, serviceName).Return(nil, internalError())
s.UpdatePutStatus(infrav1.AvailabilitySetReadyCondition, serviceName, internalError())
},
},
}
Expand All @@ -140,7 +150,7 @@ func TestReconcileAvailabilitySets(t *testing.T) {
err := s.Reconcile(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(err.Error()).To(ContainSubstring(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -214,13 +224,13 @@ func TestDeleteAvailabilitySets(t *testing.T) {
},
{
name: "error in getting availability set",
expectedError: "failed to get availability set test-as in resource group test-rg: #: Internal Server Error: StatusCode=500",
expectedError: "failed to get availability set test-as in resource group test-rg:.*#: Internal Server Error: StatusCode=500",
expect: func(s *mock_availabilitysets.MockAvailabilitySetScopeMockRecorder, m *mock_async.MockGetterMockRecorder, r *mock_async.MockReconcilerMockRecorder) {
s.AvailabilitySetSpec().Return(&fakeSetSpec)
gomock.InOrder(
s.DefaultedAzureServiceReconcileTimeout().Return(reconciler.DefaultAzureServiceReconcileTimeout),
m.Get(gomockinternal.AContext(), &fakeSetSpec).Return(nil, internalError),
s.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, gomockinternal.ErrStrEq("failed to get availability set test-as in resource group test-rg: #: Internal Server Error: StatusCode=500")),
m.Get(gomockinternal.AContext(), &fakeSetSpec).Return(nil, internalError()),
s.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, gomockinternal.ErrStrEq("failed to get availability set test-as in resource group test-rg: "+internalError().Error())),
)
},
},
Expand All @@ -244,8 +254,8 @@ func TestDeleteAvailabilitySets(t *testing.T) {
gomock.InOrder(
s.DefaultedAzureServiceReconcileTimeout().Return(reconciler.DefaultAzureServiceReconcileTimeout),
m.Get(gomockinternal.AContext(), &fakeSetSpec).Return(armcompute.AvailabilitySet{}, nil),
r.DeleteResource(gomockinternal.AContext(), &fakeSetSpec, serviceName).Return(internalError),
s.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, internalError),
r.DeleteResource(gomockinternal.AContext(), &fakeSetSpec, serviceName).Return(internalError()),
s.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, internalError()),
)
},
},
Expand Down Expand Up @@ -273,7 +283,7 @@ func TestDeleteAvailabilitySets(t *testing.T) {
err := s.Delete(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(strings.ReplaceAll(err.Error(), "\n", "")).To(MatchRegexp(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down
13 changes: 10 additions & 3 deletions azure/services/disks/disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package disks

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
Expand All @@ -48,7 +50,12 @@ var (
&diskSpec2,
}

internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
internalError = &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
)

func TestDeleteDisk(t *testing.T) {
Expand Down Expand Up @@ -127,7 +134,7 @@ func TestDeleteDisk(t *testing.T) {
err := s.Delete(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(err.Error()).To(ContainSubstring(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down
19 changes: 13 additions & 6 deletions azure/services/inboundnatrules/inboundnatrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package inboundnatrules

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -69,7 +71,12 @@ var (
FrontendIPConfigurationID: ptr.To("frontend-ip-config-id-2"),
}

internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
internalError = &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
)

func getFakeNatSpecWithoutPort(spec InboundNatSpec) *InboundNatSpec {
Expand Down Expand Up @@ -150,7 +157,7 @@ func TestReconcileInboundNATRule(t *testing.T) {
},
{
name: "fail to get existing rules",
expectedError: "failed to get existing NAT rules: #: Internal Server Error: StatusCode=500",
expectedError: `failed to get existing NAT rules:.*#: Internal Server Error: StatusCode=500`,
expect: func(s *mock_inboundnatrules.MockInboundNatScopeMockRecorder,
m *mock_inboundnatrules.MockclientMockRecorder,
r *mock_async.MockReconcilerMockRecorder) {
Expand All @@ -159,7 +166,7 @@ func TestReconcileInboundNATRule(t *testing.T) {
s.APIServerLBName().AnyTimes().Return("my-lb")
s.InboundNatSpecs().Return([]azure.ResourceSpecGetter{&fakeNatSpec})
m.List(gomockinternal.AContext(), fakeGroupName, "my-lb").Return(nil, internalError)
s.UpdatePutStatus(infrav1.InboundNATRulesReadyCondition, serviceName, gomockinternal.ErrStrEq("failed to get existing NAT rules: #: Internal Server Error: StatusCode=500"))
s.UpdatePutStatus(infrav1.InboundNATRulesReadyCondition, serviceName, gomockinternal.ErrStrEq("failed to get existing NAT rules: "+internalError.Error()))
},
},
{
Expand Down Expand Up @@ -203,7 +210,7 @@ func TestReconcileInboundNATRule(t *testing.T) {
err := s.Reconcile(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(strings.ReplaceAll(err.Error(), "\n", "")).To(MatchRegexp(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -281,7 +288,7 @@ func TestDeleteNetworkInterface(t *testing.T) {
err := s.Delete(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(strings.ReplaceAll(err.Error(), "\n", "")).To(MatchRegexp(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down
15 changes: 11 additions & 4 deletions azure/services/loadbalancers/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package loadbalancers

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -102,7 +104,12 @@ var (
},
}

internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
internalError = &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
)

func TestReconcileLoadBalancer(t *testing.T) {
Expand Down Expand Up @@ -194,7 +201,7 @@ func TestReconcileLoadBalancer(t *testing.T) {
err := s.Reconcile(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(err.Error()).To(ContainSubstring(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -272,7 +279,7 @@ func TestDeleteLoadBalancer(t *testing.T) {
err := s.Delete(context.TODO())
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
g.Expect(err.Error()).To(ContainSubstring(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
Expand Down
11 changes: 9 additions & 2 deletions azure/services/networkinterfaces/networkinterfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package networkinterfaces
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -71,7 +73,12 @@ var (
SKU: &fakeSku,
IPConfigs: []IPConfig{{}, {}},
}
internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
internalError = &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
)

func TestReconcileNetworkInterface(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions azure/services/privatedns/privatedns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package privatedns

import (
"context"
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -92,7 +93,7 @@ var (

notDoneError = azure.NewOperationNotDoneError(&infrav1.Future{Type: "resourceType", ResourceGroup: resourceGroup, Name: "resourceName"})
errFake = errors.New("this is an error")
notFoundError = autorest.DetailedError{StatusCode: 404}
notFoundError = &azcore.ResponseError{StatusCode: http.StatusNotFound}
)

func TestReconcilePrivateDNS(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions azure/services/publicips/publicips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package publicips

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -113,7 +115,12 @@ var (
},
}

internalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
internalError = &azcore.ResponseError{
RawResponse: &http.Response{
Body: io.NopCloser(strings.NewReader("#: Internal Server Error: StatusCode=500")),
StatusCode: http.StatusInternalServerError,
},
}
)

func TestReconcilePublicIP(t *testing.T) {
Expand Down
Loading

0 comments on commit 3ed87c4

Please sign in to comment.