Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests in bastion_hosts_test.go file #1754

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 22 additions & 106 deletions azure/services/bastionhosts/bastionhosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,84 +52,9 @@ func TestReconcileBastionHosts(t *testing.T) {
mSubnet *mock_subnets.MockClientMockRecorder,
mPublicIP *mock_publicips.MockClientMockRecorder)
}{
{
name: "fail to get subnets",
expectedError: "failed to get subnet: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
mPublicIP *mock_publicips.MockClientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.BastionSpec().Return(azure.BastionSpec{
AzureBastion: &azure.AzureBastionSpec{
Name: "my-bastion",
VNetName: "my-vnet",
SubnetSpec: v1beta1.SubnetSpec{
Name: "my-subnet",
},
PublicIPName: "my-publicip",
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").
Return(network.Subnet{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error"))
},
},
{
name: "fail to get publicip",
expectedError: "failed to get existing publicIP: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
mPublicIP *mock_publicips.MockClientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.BastionSpec().Return(azure.BastionSpec{
AzureBastion: &azure.AzureBastionSpec{
Name: "my-bastion",
VNetName: "my-vnet",
SubnetSpec: v1beta1.SubnetSpec{
Name: "my-subnet",
},
PublicIPName: "my-publicip",
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
gomock.InOrder(
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
)
},
},
{
name: "create publicip fails",
expectedError: "failed to create bastion publicIP: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
mPublicIP *mock_publicips.MockClientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.BastionSpec().Return(azure.BastionSpec{
AzureBastion: &azure.AzureBastionSpec{
Name: "my-bastion",
VNetName: "my-vnet",
SubnetSpec: v1beta1.SubnetSpec{
Name: "my-subnet",
},
PublicIPName: "my-publicip",
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
s.Location().AnyTimes().Return("fake-location")
gomock.InOrder(
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found")),
mPublicIP.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-publicip", gomock.AssignableToTypeOf(network.PublicIPAddress{})).Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
)
},
},
{
name: "fails to get a created publicip",
expectedError: "failed to get created publicIP: #: Internal Server Error: StatusCode=500",
expectedError: "error creating Azure Bastion: failed to get public IP for azure bastion: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -146,16 +71,12 @@ func TestReconcileBastionHosts(t *testing.T) {
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
s.Location().AnyTimes().Return("fake-location")
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil)
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found"))
mPublicIP.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-publicip", gomock.AssignableToTypeOf(network.PublicIPAddress{})).Return(nil)
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error"))
},
},
{
name: "bastion successfully created with created public ip",
expectedError: "",
name: "fail to get subnets",
expectedError: "error creating Azure Bastion: failed to get subnet for azure bastion: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -172,20 +93,16 @@ func TestReconcileBastionHosts(t *testing.T) {
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
s.Location().AnyTimes().Return("fake-location")
s.ClusterName().AnyTimes().Return("fake-cluster")
gomock.InOrder(
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found")),
mPublicIP.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-publicip", gomock.AssignableToTypeOf(network.PublicIPAddress{})).Return(nil),
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, nil),
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-bastion", gomock.AssignableToTypeOf(network.BastionHost{})),
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").
Return(network.Subnet{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
)
},
},
{
name: "bastion successfully created",
expectedError: "",
name: "fail to create a bastion",
expectedError: "error creating Azure Bastion: cannot create Azure Bastion: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -205,15 +122,15 @@ func TestReconcileBastionHosts(t *testing.T) {
s.Location().AnyTimes().Return("fake-location")
s.ClusterName().AnyTimes().Return("fake-cluster")
gomock.InOrder(
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, nil),
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-bastion", gomock.AssignableToTypeOf(network.BastionHost{})),
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-bastion", gomock.AssignableToTypeOf(network.BastionHost{})).Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
)
},
},
{
name: "fail to create a bastion",
expectedError: "error creating Azure Bastion: cannot create Azure Bastion: #: Internal Server Error: StatusCode=500",
name: "bastion successfully created",
expectedError: "",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -235,16 +152,16 @@ func TestReconcileBastionHosts(t *testing.T) {
gomock.InOrder(
mPublicIP.Get(gomockinternal.AContext(), "my-rg", "my-publicip").Return(network.PublicIPAddress{}, nil),
mSubnet.Get(gomockinternal.AContext(), "my-rg", "my-vnet", "my-subnet").Return(network.Subnet{}, nil),
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-bastion", gomock.AssignableToTypeOf(network.BastionHost{})).Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-bastion", gomock.AssignableToTypeOf(network.BastionHost{})),
)
},
},
}

for _, tc := range testcases {
tc := tc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

t.Parallel()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down Expand Up @@ -284,9 +201,8 @@ func TestDeleteBastionHost(t *testing.T) {
mPublicIP *mock_publicips.MockClientMockRecorder)
}{
{
name: "successfully delete an existing bastion host",

expectedError: "",
name: "bastion host deletion fails",
expectedError: "error deleting Azure Bastion: failed to delete Azure Bastion my-bastionhost in resource group my-rg: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -303,7 +219,8 @@ func TestDeleteBastionHost(t *testing.T) {
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost")
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost").
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error"))
},
},
{
Expand All @@ -327,12 +244,12 @@ func TestDeleteBastionHost(t *testing.T) {
s.ResourceGroup().AnyTimes().Return("my-rg")
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost").
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found"))
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost1")
},
},
{
name: "bastion host deletion fails",
expectedError: "error deleting Azure Bastion: failed to delete Azure Bastion my-bastionhost in resource group my-rg: #: Internal Server Error: StatusCode=500",
name: "successfully delete an existing bastion host",

expectedError: "",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
mSubnet *mock_subnets.MockClientMockRecorder,
Expand All @@ -349,16 +266,15 @@ func TestDeleteBastionHost(t *testing.T) {
},
})
s.ResourceGroup().AnyTimes().Return("my-rg")
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost").
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error"))
m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost")
},
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

t.Parallel()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down