From 580faf2e64694339bcd885d8bb41639e67b9f0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20GLON?= Date: Tue, 28 Nov 2023 11:14:53 +0100 Subject: [PATCH] Add unit test --- .../oscmachine_vm_controller_unit_test.go | 133 +++++++++++++++++- 1 file changed, 131 insertions(+), 2 deletions(-) diff --git a/controllers/oscmachine_vm_controller_unit_test.go b/controllers/oscmachine_vm_controller_unit_test.go index 0babe130a..12fa16820 100644 --- a/controllers/oscmachine_vm_controller_unit_test.go +++ b/controllers/oscmachine_vm_controller_unit_test.go @@ -221,6 +221,50 @@ var ( }, }, } + defaultVmInitializeWithPublicIp = infrastructurev1beta1.OscMachineSpec{ + Node: infrastructurev1beta1.OscNode{ + Volumes: []*infrastructurev1beta1.OscVolume{ + { + Name: "test-volume", + Iops: 1000, + Size: 50, + VolumeType: "io1", + SubregionName: "eu-west-2a", + }, + }, + Vm: infrastructurev1beta1.OscVm{ + ClusterName: "test-cluster", + Name: "test-vm", + ImageId: "ami-00000000", + Role: "controlplane", + DeviceName: "/dev/sda1", + RootDisk: infrastructurev1beta1.OscRootDisk{ + RootDiskSize: 30, + RootDiskIops: 1500, + RootDiskType: "gp2", + }, + KeypairName: "rke", + SubregionName: "eu-west-2a", + SubnetName: "test-subnet", + LoadBalancerName: "test-loadbalancer", + PublicIpName: "test-publicip", + VmType: "tinav3.c2r4p2", + Replica: 1, + SecurityGroupNames: []infrastructurev1beta1.OscSecurityGroupElement{ + { + Name: "test-securitygroup", + }, + }, + PrivateIps: []infrastructurev1beta1.OscPrivateIpElement{ + { + Name: "test-privateip", + PrivateIp: "10.0.0.17", + }, + }, + }, + }, + } + defaultMultiVmInitialize = infrastructurev1beta1.OscMachineSpec{ Node: infrastructurev1beta1.OscNode{ Volumes: []*infrastructurev1beta1.OscVolume{ @@ -314,6 +358,52 @@ var ( }, }, } + defaultVmReconcileWithDedicatedIp = infrastructurev1beta1.OscMachineSpec{ + Node: infrastructurev1beta1.OscNode{ + Volumes: []*infrastructurev1beta1.OscVolume{ + { + Name: "test-volume", + Iops: 1000, + Size: 50, + VolumeType: "io1", + SubregionName: "eu-west-2a", + ResourceId: "volume-test-volume-uid", + }, + }, + Vm: infrastructurev1beta1.OscVm{ + ClusterName: "test-cluster", + Name: "test-vm", + ImageId: "ami-00000000", + Role: "controlplane", + DeviceName: "/dev/xvdb", + KeypairName: "rke", + RootDisk: infrastructurev1beta1.OscRootDisk{ + + RootDiskSize: 30, + RootDiskIops: 1500, + RootDiskType: "io1", + }, + SubregionName: "eu-west-2a", + SubnetName: "test-subnet", + LoadBalancerName: "test-loadbalancer", + VmType: "tinav3.c2r4p2", + ResourceId: "i-test-vm-uid", + PublicIp: true, + Replica: 1, + SecurityGroupNames: []infrastructurev1beta1.OscSecurityGroupElement{ + { + Name: "test-securitygroup", + }, + }, + PrivateIps: []infrastructurev1beta1.OscPrivateIpElement{ + { + Name: "test-privateip", + PrivateIp: "10.0.0.17", + }, + }, + }, + }, + } ) // SetupWithVmMock set vmMock with clusterScope, machineScope and oscmachine @@ -3299,6 +3389,7 @@ func TestReconcileVmResourceId(t *testing.T) { expLinkPublicIpFound bool expSecurityGroupFound bool expLoadBalancerSecurityGroupFound bool + expCreatePublicIpFound bool expReadTagErr error expReconcileVmErr error }{ @@ -3313,6 +3404,7 @@ func TestReconcileVmResourceId(t *testing.T) { expSecurityGroupFound: true, expLoadBalancerSecurityGroupFound: true, expTagFound: false, + expCreatePublicIpFound: false, expReadTagErr: nil, expReconcileVmErr: fmt.Errorf("test-volume-uid does not exist"), }, @@ -3327,11 +3419,12 @@ func TestReconcileVmResourceId(t *testing.T) { expSecurityGroupFound: true, expLoadBalancerSecurityGroupFound: true, expTagFound: false, + expCreatePublicIpFound: false, expReadTagErr: nil, expReconcileVmErr: fmt.Errorf("test-subnet-uid does not exist"), }, { - name: "PublicIp does not exist ", + name: "PublicIp does not exist on clusterScope", clusterSpec: defaultVmClusterInitialize, machineSpec: defaultVmInitialize, expVolumeFound: true, @@ -3341,6 +3434,7 @@ func TestReconcileVmResourceId(t *testing.T) { expSecurityGroupFound: true, expLoadBalancerSecurityGroupFound: true, expTagFound: false, + expCreatePublicIpFound: false, expReadTagErr: nil, expReconcileVmErr: fmt.Errorf("test-publicip-uid does not exist"), }, @@ -3355,6 +3449,7 @@ func TestReconcileVmResourceId(t *testing.T) { expSecurityGroupFound: false, expLoadBalancerSecurityGroupFound: false, expTagFound: false, + expCreatePublicIpFound: false, expReadTagErr: nil, expReconcileVmErr: fmt.Errorf("test-securitygroup-uid does not exist"), }, @@ -3369,6 +3464,7 @@ func TestReconcileVmResourceId(t *testing.T) { expSecurityGroupFound: true, expLoadBalancerSecurityGroupFound: true, expTagFound: true, + expCreatePublicIpFound: false, expReadTagErr: fmt.Errorf("ReadTag generic error"), expReconcileVmErr: fmt.Errorf("ReadTag generic error Can not get tag for OscMachine test-system/test-osc"), }, @@ -3477,6 +3573,7 @@ func TestReconcileDeleteVm(t *testing.T) { expDeleteInboundSecurityGroupRuleFound bool expDeleteOutboundSecurityGroupRuleFound bool expListMachine bool + expDeleteDedicatedPublicIpFound bool expUnlinkLoadBalancerBackendMachineErr error expDeleteInboundSecurityGroupRuleErr error expDeleteOutboundSecurityGroupRuleErr error @@ -3494,6 +3591,24 @@ func TestReconcileDeleteVm(t *testing.T) { expDeleteInboundSecurityGroupRuleFound: true, expDeleteOutboundSecurityGroupRuleFound: true, expListMachine: false, + expDeleteDedicatedPublicIpFound: false, + expUnlinkLoadBalancerBackendMachineErr: nil, + expDeleteInboundSecurityGroupRuleErr: nil, + expDeleteOutboundSecurityGroupRuleErr: nil, + expSecurityGroupRuleFound: true, + expDeleteVmErr: nil, + expGetVmFound: true, + expGetVmErr: nil, + expCheckUnlinkPublicIpErr: nil, + expReconcileDeleteVmErr: nil, + }, + { + name: "delete vm with publicIp", + clusterSpec: defaultVmClusterReconcile, + machineSpec: defaultVmReconcileWithDedicatedIp, + expDeleteInboundSecurityGroupRuleFound: true, + expDeleteOutboundSecurityGroupRuleFound: true, + expDeleteDedicatedPublicIpFound: true, expUnlinkLoadBalancerBackendMachineErr: nil, expDeleteInboundSecurityGroupRuleErr: nil, expDeleteOutboundSecurityGroupRuleErr: nil, @@ -3527,7 +3642,7 @@ func TestReconcileDeleteVm(t *testing.T) { machineSpec: defaultVmReconcile, expDeleteInboundSecurityGroupRuleFound: true, expDeleteOutboundSecurityGroupRuleFound: true, - expListMachine: false, + expDeleteDedicatedPublicIpFound: false, expUnlinkLoadBalancerBackendMachineErr: nil, expSecurityGroupRuleFound: true, expDeleteVmErr: fmt.Errorf("DeleteVm generic error"), @@ -3560,6 +3675,14 @@ func TestReconcileDeleteVm(t *testing.T) { securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId securityGroupIds = append(securityGroupIds, securityGroupId) } + + if vtc.expDeleteDedicatedPublicIpFound { + publicIpName := machineScope.GetName() + "-publicIp" + machineScope.OscMachine.Spec.Node.Vm.PublicIpName = publicIpName + vtc.machineSpec.Node.Vm.PublicIpName = publicIpName + machineScope.GetPublicIpIdRef().ResourceMap = map[string]string{publicIpName + "-uid": "eipassoc-" + publicIpName + "-uid"} + } + publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" linkPublicIpId := "eipassoc-" + publicIpName linkPublicIpRef := machineScope.GetLinkPublicIpRef() @@ -3640,6 +3763,12 @@ func TestReconcileDeleteVm(t *testing.T) { DeleteSecurityGroupRule(gomock.Eq(associateSecurityGroupId), gomock.Eq("Outbound"), gomock.Eq(ipProtocol), "", gomock.Eq(securityGroupIds[0]), gomock.Eq(fromPortRange), gomock.Eq(toPortRange)). Return(vtc.expDeleteOutboundSecurityGroupRuleErr) } + if vtc.expDeleteDedicatedPublicIpFound { + mockOscPublicIpInterface. + EXPECT(). + DeletePublicIp(gomock.Eq("eipassoc-test-osc-publicIp-uid")). + Return(nil) + } if vtc.expDeleteInboundSecurityGroupRuleFound { mockOscSecurityGroupInterface.