Skip to content

Commit

Permalink
add more tests and fix code path
Browse files Browse the repository at this point in the history
  • Loading branch information
lubedacht committed Jul 3, 2024
1 parent ef477b3 commit b93b735
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/ionoscloudcluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ var _ = Describe("IonosCloudCluster", func() {
Expect(k8sClient.Create(context.Background(), cluster)).
Should(MatchError(ContainSubstring("credentialsRef.name must be provided")))
})
It("should not allow creating clusters with empty location", func() {
cluster := defaultCluster()
cluster.Spec.Location = ""
Expect(k8sClient.Create(context.Background(), cluster)).
Should(MatchError(ContainSubstring(" spec.location in body should be at least 1 chars long")))
})
})

Context("Update", func() {
Expand Down
8 changes: 4 additions & 4 deletions internal/service/cloud/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (s *Service) ReconcileControlPlaneEndpointDeletion(
s.getControlPlaneEndpointIPBlock,
s.getLatestControlPlaneEndpointIPBlockCreationRequest,
)

if ignoreErrUserSetIPNotFound(ignoreNotFound(err)) != nil {
return false, err
}
// NOTE(gfariasalves): we ignore the error if it is a "user set IP not found" error, because it doesn't matter here.
// This error is only relevant when we are trying to create a new IP block. If it shows up here, it means that:
// a) this IP block was created by the user, and they have deleted it, or,
Expand All @@ -111,10 +115,6 @@ func (s *Service) ReconcileControlPlaneEndpointDeletion(
return false, nil
}

if ignoreErrUserSetIPNotFound(ignoreNotFound(err)) != nil {
return false, err
}

// NOTE: this check covers the case where customers have set the control plane endpoint IP themselves.
// If this is the case we don't request for the deletion of the IP block.
if ipBlock != nil && ptr.Deref(ipBlock.GetProperties().GetName(), unknownValue) != s.controlPlaneEndpointIPBlockName(cs) {
Expand Down
18 changes: 18 additions & 0 deletions internal/service/cloud/ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ func (s *ipBlockTestSuite) TestReconcileControlPlaneEndpointDeletionRequestNewDe
s.Equal(exampleRequestPath, s.clusterScope.IonosCluster.Status.CurrentClusterRequest.RequestPath)
}

func (s *ipBlockTestSuite) TestReconcileControlPlaneEndpointDeletionErrorOnDuplicateIPBlock() {
block := exampleIPBlock()
dup := exampleIPBlock()
s.mockListIPBlocksCall().Return(&sdk.IpBlocks{Items: &[]sdk.IpBlock{
*block,
*dup,
}}, nil).Once()
requeue, err := s.service.ReconcileControlPlaneEndpointDeletion(s.ctx, s.clusterScope)
s.False(requeue)
s.Error(err)
}

func (s *ipBlockTestSuite) TestReconcileFailoverIPBlockDeletion() {
s.infraMachine.Spec.FailoverIP = ptr.To(infrav1.CloudResourceConfigAuto)
ipBlock := exampleIPBlockWithName(s.service.failoverIPBlockName(s.machineScope))
Expand Down Expand Up @@ -472,6 +484,12 @@ func (s *ipBlockTestSuite) TestReconcileFailoverIPBlockDeletionDeletionFinished(
s.Nil(s.machineScope.IonosMachine.Status.CurrentRequest)
}

func (s *ipBlockTestSuite) TestReconcileFailoverIPBlockDeletionShouldSkipDeletion() {
requeue, err := s.service.ReconcileFailoverIPBlockDeletion(s.ctx, s.machineScope)
s.NoError(err)
s.False(requeue)
s.Nil(s.machineScope.IonosMachine.Status.CurrentRequest)
}
func (s *ipBlockTestSuite) mockDeleteIPBlockCall() *clienttest.MockClient_DeleteIPBlock_Call {
return s.ionosClient.EXPECT().DeleteIPBlock(s.ctx, exampleIPBlockID)
}
Expand Down
45 changes: 39 additions & 6 deletions internal/service/cloud/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func setControlPlaneLabel(ctx context.Context, k8sClient client.Client, machine
return k8sClient.Update(ctx, machine)
}

func (s *lanSuite) reconcileIPFailoverDeletion(testServer *sdk.Server, testLAN sdk.Lan) {
func (s *lanSuite) reconcileIPFailoverDeletion(testServer *sdk.Server, testLAN sdk.Lan) (requeue bool, err error) {
s.mockGetServerCall(exampleServerID).Return(testServer, nil).Once()
s.mockListLANsCall().Return(&sdk.Lans{Items: &[]sdk.Lan{testLAN}}, nil).Once()
s.setupSuccessfulLANPatchMocks()
Expand All @@ -384,9 +384,7 @@ func (s *lanSuite) reconcileIPFailoverDeletion(testServer *sdk.Server, testLAN s
}

s.mockPatchLANCall(props).Return(exampleRequestPath, nil).Once()
s.assertSuccessfulDeletion(
s.service.ReconcileIPFailoverDeletion(s.ctx, s.machineScope),
)
return s.service.ReconcileIPFailoverDeletion(s.ctx, s.machineScope)
}

func (s *lanSuite) TestReconcileIPFailoverDeletionWorker() {
Expand All @@ -409,7 +407,7 @@ func (s *lanSuite) TestReconcileIPFailoverDeletionWorker() {
NicUuid: ptr.To(exampleNICID),
}}

s.reconcileIPFailoverDeletion(testServer, testLAN)
s.assertSuccessfulDeletion(s.reconcileIPFailoverDeletion(testServer, testLAN))
}

func (s *lanSuite) TestReconcileIPFailoverDeletionControlPlane() {
Expand All @@ -430,7 +428,7 @@ func (s *lanSuite) TestReconcileIPFailoverDeletionControlPlane() {
NicUuid: ptr.To(exampleNICID),
}}

s.reconcileIPFailoverDeletion(testServer, testLAN)
s.assertSuccessfulDeletion(s.reconcileIPFailoverDeletion(testServer, testLAN))
}

func (s *lanSuite) TestReconcileIPFailoverDeletionControlPlaneSwitchNIC() {
Expand Down Expand Up @@ -484,6 +482,41 @@ func (s *lanSuite) TestReconcileIPFailoverDeletionControlPlaneSwitchNIC() {
)
}

func (s *lanSuite) TestReconcileFailoverDeletionWorkerNoSwap() {
const deploymentLabel = "test-deployment"
labels := s.infraMachine.GetLabels()
labels[clusterv1.MachineDeploymentNameLabel] = deploymentLabel
s.infraMachine.SetLabels(labels)

s.infraMachine.Spec.FailoverIP = ptr.To(exampleWorkerFailoverIP)

secondaryMachine := s.infraMachine.DeepCopy()
secondaryMachine.SetName("test-machine-2")
secondaryMachine.SetResourceVersion("")
secondaryMachine.SetCreationTimestamp(metav1.NewTime(time.Now()))
testServer := s.defaultServer(s.infraMachine, exampleDHCPIP, exampleWorkerFailoverIP)

s.NoError(s.k8sClient.Create(s.ctx, secondaryMachine))
s.NoError(s.k8sClient.Update(s.ctx, s.infraMachine))

testLAN := s.exampleLAN()
testLAN.Properties.IpFailover = &[]sdk.IPFailover{{
Ip: ptr.To(exampleArbitraryIP),
NicUuid: ptr.To(arbitraryNICID),
}, {
Ip: ptr.To(exampleWorkerFailoverIP),
NicUuid: ptr.To(exampleSecondaryNICID),
}}

s.mockGetServerCall(exampleServerID).Return(testServer, nil).Once()
s.mockListLANsCall().Return(&sdk.Lans{Items: &[]sdk.Lan{testLAN}}, nil).Once()
s.mockGetLANPatchRequestCall().Return(nil, nil).Once()
requeue, err := s.service.ReconcileIPFailoverDeletion(s.ctx, s.machineScope)

s.NoError(err)
s.False(requeue)
}

func (s *lanSuite) setupSuccessfulLANPatchMocks() {
s.T().Helper()
patchRequest := s.examplePatchRequest(sdk.RequestStatusDone)
Expand Down
42 changes: 40 additions & 2 deletions internal/service/cloud/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cloud
import (
"context"
"fmt"
"github.com/google/uuid"
"net/http"
"path"
"strconv"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (s *ServiceTestSuite) SetupTest() {
Spec: clusterv1.MachineSpec{
ClusterName: s.capiCluster.Name,
Version: ptr.To("v1.26.12"),
ProviderID: ptr.To("ionos://dd426c63-cd1d-4c02-aca3-13b4a27c2ebf"),
ProviderID: ptr.To("ionos://" + exampleServerID),
},
}
s.infraMachine = &infrav1.IonosCloudMachine{
Expand All @@ -150,7 +151,7 @@ func (s *ServiceTestSuite) SetupTest() {
},
},
Spec: infrav1.IonosCloudMachineSpec{
ProviderID: ptr.To("ionos://dd426c63-cd1d-4c02-aca3-13b4a27c2ebf"),
ProviderID: ptr.To("ionos://" + exampleServerID),
DatacenterID: "ccf27092-34e8-499e-a2f5-2bdee9d34a12",
NumCores: 2,
AvailabilityZone: infrav1.AvailabilityZoneAuto,
Expand Down Expand Up @@ -339,6 +340,39 @@ func (s *ServiceTestSuite) defaultServerComponents() (sdk.ServerProperties, sdk.
return props, entities
}

func (s *ServiceTestSuite) newIonosCloudMachine(name string) *infrav1.IonosCloudMachine {

Check failure on line 343 in internal/service/cloud/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

func `(*ServiceTestSuite).newIonosCloudMachine` is unused (unused)
providerID := uuid.New().String()
return &infrav1.IonosCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Name: name,
Labels: map[string]string{
clusterv1.ClusterNameLabel: s.capiCluster.Name,
clusterv1.MachineDeploymentNameLabel: "test-md",
},
},
Spec: infrav1.IonosCloudMachineSpec{
ProviderID: ptr.To("ionos://" + providerID),
DatacenterID: "ccf27092-34e8-499e-a2f5-2bdee9d34a12",
NumCores: 2,
AvailabilityZone: infrav1.AvailabilityZoneAuto,
MemoryMB: 4096,
CPUFamily: ptr.To("AMD_OPTERON"),
Disk: &infrav1.Volume{
Name: name + "-hdd",
DiskType: infrav1.VolumeDiskTypeHDD,
SizeGB: 20,
AvailabilityZone: infrav1.AvailabilityZoneAuto,
Image: &infrav1.ImageSpec{
ID: "3e3e3e3e-3e3e-3e3e-3e3e-3e3e3e3e3e3e",
},
},
Type: infrav1.ServerTypeEnterprise,
},
Status: infrav1.IonosCloudMachineStatus{},
}
}

func (s *ServiceTestSuite) mockGetIPBlocksRequestsPostCall() *clienttest.MockClient_GetRequests_Call {
return s.ionosClient.EXPECT().GetRequests(s.ctx, http.MethodPost, ipBlocksPath)
}
Expand Down Expand Up @@ -370,3 +404,7 @@ func (s *ServiceTestSuite) mockGetServerCall(serverID string) *clienttest.MockCl
func (s *ServiceTestSuite) mockListLANsCall() *clienttest.MockClient_ListLANs_Call {
return s.ionosClient.EXPECT().ListLANs(s.ctx, s.machineScope.DatacenterID())
}

type ionosCloudMachineParams struct {

Check failure on line 408 in internal/service/cloud/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

type `ionosCloudMachineParams` is unused (unused)
name string
}

0 comments on commit b93b735

Please sign in to comment.