Skip to content

Commit

Permalink
remove unncessary tests from bastion_hosts_test.go and modify some te…
Browse files Browse the repository at this point in the history
…sts according to the latest code with unncessary mocks removed or updated and expected errors updated

Signed-off-by: Karuppiah Natarajan <[email protected]>
  • Loading branch information
karuppiah7890 committed Oct 3, 2021
1 parent e62c522 commit 6df129e
Showing 1 changed file with 17 additions and 69 deletions.
86 changes: 17 additions & 69 deletions azure/services/bastionhosts/bastionhosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,7 @@ func TestReconcileBastionHosts(t *testing.T) {
}{
{
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: v1alpha4.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",
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 @@ -95,42 +72,15 @@ func TestReconcileBastionHosts(t *testing.T) {
})
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: v1alpha4.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")),
// TODO(karuppiah7890): Fix compile error
mPublicIP.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-publicip", gomock.AssignableToTypeOf(network.PublicIPAddress{})).Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")),
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{}, 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",
name: "fail to get publicip",
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 @@ -147,16 +97,11 @@ 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"))
// TODO(karuppiah7890): Fix compile error
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",
name: "bastion successfully created with already created public ip",
expectedError: "",
expect: func(s *mock_bastionhosts.MockBastionScopeMockRecorder,
m *mock_bastionhosts.MockclientMockRecorder,
Expand All @@ -177,11 +122,8 @@ 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{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found")),
// TODO(karuppiah7890): Fix compile error
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),
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{})),
)
},
Expand All @@ -208,8 +150,8 @@ 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),
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{})),
)
},
Expand Down Expand Up @@ -248,7 +190,10 @@ func TestReconcileBastionHosts(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

t.Parallel()
// TODO(karuppiah789): Make the tests parallel and still work in case of errors and success with no false results.
// Currently when tests run in parallel, if there's an error in the test it doesn't show up in the test result
// leading to a false result. Or it shows false errors due to some mix up happening when tests run in parallel.
// t.Parallel()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
scopeMock := mock_bastionhosts.NewMockBastionScope(mockCtrl)
Expand Down Expand Up @@ -362,7 +307,10 @@ func TestDeleteBastionHost(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

t.Parallel()
// TODO(karuppiah789): Make the tests parallel and still work in case of errors and success with no false results.
// Currently when tests run in parallel, if there's an error in the test it doesn't show up in the test result
// leading to a false result. Or it shows false errors due to some mix up happening when tests run in parallel.
// t.Parallel()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
scopeMock := mock_bastionhosts.NewMockBastionScope(mockCtrl)
Expand Down

0 comments on commit 6df129e

Please sign in to comment.