Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebglon authored and vincentBaer committed Dec 4, 2023
1 parent ac0a12a commit 580faf2
Showing 1 changed file with 131 additions and 2 deletions.
133 changes: 131 additions & 2 deletions controllers/oscmachine_vm_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3299,6 +3389,7 @@ func TestReconcileVmResourceId(t *testing.T) {
expLinkPublicIpFound bool
expSecurityGroupFound bool
expLoadBalancerSecurityGroupFound bool
expCreatePublicIpFound bool
expReadTagErr error
expReconcileVmErr error
}{
Expand All @@ -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"),
},
Expand All @@ -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,
Expand All @@ -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"),
},
Expand All @@ -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"),
},
Expand All @@ -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"),
},
Expand Down Expand Up @@ -3477,6 +3573,7 @@ func TestReconcileDeleteVm(t *testing.T) {
expDeleteInboundSecurityGroupRuleFound bool
expDeleteOutboundSecurityGroupRuleFound bool
expListMachine bool
expDeleteDedicatedPublicIpFound bool
expUnlinkLoadBalancerBackendMachineErr error
expDeleteInboundSecurityGroupRuleErr error
expDeleteOutboundSecurityGroupRuleErr error
Expand All @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 580faf2

Please sign in to comment.