Skip to content

Commit

Permalink
fix tests in bastion_hosts_test.go file
Browse files Browse the repository at this point in the history
- Remove `create publicip fails` test in bastion host `Reconcile` test as we don't create public IP as part of bastion host `Reconcile`
- Remove `bastion successfully created with created public ip` test in bastion host `Reconcile` test as we don't create public IP as part of bastion host `Reconcile`
- Remove unnecessary / wrong method call expectations using mocks
  - `mPublicIP.CreateOrUpdate` method call expectations
  - `m.Delete(gomockinternal.AContext(), "my-rg", "my-bastionhost1")` expectation
- Fix the order of method call expectations using mocks
  - `mPublicIP.Get` gets called before `mSubnet.Get`
- Comment out `t.Parallel` temporarily with details about the removal of parallelization - tldr; enabling parallel tests seems to give wrong test results
- Fix the expected error messages

Signed-off-by: Karuppiah Natarajan <[email protected]>
  • Loading branch information
karuppiah7890 committed Oct 15, 2021
1 parent 4d1384e commit 002984f
Showing 1 changed file with 9 additions and 93 deletions.
102 changes: 9 additions & 93 deletions azure/services/bastionhosts/bastionhosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestReconcileBastionHosts(t *testing.T) {
}{
{
name: "fail to get subnets",
expectedError: "failed to get subnet: #: 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 @@ -71,65 +71,16 @@ func TestReconcileBastionHosts(t *testing.T) {
},
})
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: v1alpha4.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: 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")),
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 @@ -146,43 +97,9 @@ 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: "",
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")
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{})),
)
},
},
{
name: "bastion successfully created",
expectedError: "",
Expand All @@ -205,8 +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{}, 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 @@ -243,8 +160,8 @@ func TestReconcileBastionHosts(t *testing.T) {

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

t.Parallel()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down Expand Up @@ -327,7 +244,6 @@ 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")
},
},
{
Expand Down Expand Up @@ -357,8 +273,8 @@ func TestDeleteBastionHost(t *testing.T) {

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

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

0 comments on commit 002984f

Please sign in to comment.