From dc4051693d5fb1a4a54bbd9f7523b6debba0ff62 Mon Sep 17 00:00:00 2001 From: hanenMizouni Date: Tue, 3 Dec 2024 15:56:39 +0000 Subject: [PATCH] fix mockgen and unit test Signed-off-by: hanenMizouni --- .../services/compute/mock_compute/vm_mock.go | 24 +- controllers/osccluster_bastion_controller.go | 5 +- ...osccluster_bastion_controller_unit_test.go | 623 ++------ controllers/oscmachine_vm_controller.go | 37 +- .../oscmachine_vm_controller_unit_test.go | 1353 +++-------------- .../oscmachine_volume_controller_unit_test.go | 7 +- .../oscmachinetemplate_capacity_controller.go | 2 +- 7 files changed, 371 insertions(+), 1680 deletions(-) diff --git a/cloud/services/compute/mock_compute/vm_mock.go b/cloud/services/compute/mock_compute/vm_mock.go index 4a0819b02..304c71359 100644 --- a/cloud/services/compute/mock_compute/vm_mock.go +++ b/cloud/services/compute/mock_compute/vm_mock.go @@ -110,15 +110,6 @@ func (mr *MockOscVmInterfaceMockRecorder) GetCapacity(tagKey, tagValue, vmType i return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCapacity", reflect.TypeOf((*MockOscVmInterface)(nil).GetCapacity), tagKey, tagValue, vmType) } -// GetVmListFromTag mocks base method. -func (m *MockOscVmInterface) GetVmListFromTag(tagKey string, tagValue string) ([]osc.Vm, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetVmListFromTag", tagKey, tagValue) - ret0, _ := ret[0].([]osc.Vm) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - // GetVm mocks base method. func (m *MockOscVmInterface) GetVm(vmId string) (*osc.Vm, error) { m.ctrl.T.Helper() @@ -134,6 +125,21 @@ func (mr *MockOscVmInterfaceMockRecorder) GetVm(vmId interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVm", reflect.TypeOf((*MockOscVmInterface)(nil).GetVm), vmId) } +// GetVmListFromTag mocks base method. +func (m *MockOscVmInterface) GetVmListFromTag(tagKey, tagName string) ([]osc.Vm, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetVmListFromTag", tagKey, tagName) + ret0, _ := ret[0].([]osc.Vm) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetVmListFromTag indicates an expected call of GetVmListFromTag. +func (mr *MockOscVmInterfaceMockRecorder) GetVmListFromTag(tagKey, tagName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVmListFromTag", reflect.TypeOf((*MockOscVmInterface)(nil).GetVmListFromTag), tagKey, tagName) +} + // GetVmState mocks base method. func (m *MockOscVmInterface) GetVmState(vmId string) (string, error) { m.ctrl.T.Helper() diff --git a/controllers/osccluster_bastion_controller.go b/controllers/osccluster_bastion_controller.go index 925746b11..37d296b16 100644 --- a/controllers/osccluster_bastion_controller.go +++ b/controllers/osccluster_bastion_controller.go @@ -317,7 +317,7 @@ func reconcileBastion(ctx context.Context, clusterScope *scope.ClusterScope, vmS clusterScope.V(4).Info("Bastion is running", "vmId", vmId) } - if *bastionState == infrastructurev1beta1.VmStateRunning { + if bastionState != nil && *bastionState == infrastructurev1beta1.VmStateRunning { vmId := bastionSpec.ResourceId if bastionSpec.PublicIpName != "" && linkPublicIpRef.ResourceMap[bastionPublicIpName] == "" { @@ -328,7 +328,10 @@ func reconcileBastion(ctx context.Context, clusterScope *scope.ClusterScope, vmS clusterScope.V(4).Info("Get bastionPublicIpName", "bastionPublicIpName", bastionPublicIpName) linkPublicIpRef.ResourceMap[bastionPublicIpName] = linkPublicIpId } + } else { + clusterScope.V(4).Info("VM is not running, skipping public IP linking") } + } clusterScope.V(4).Info("Bastion is reconciled") return reconcile.Result{}, nil diff --git a/controllers/osccluster_bastion_controller_unit_test.go b/controllers/osccluster_bastion_controller_unit_test.go index 7fe04d12b..a3878e118 100644 --- a/controllers/osccluster_bastion_controller_unit_test.go +++ b/controllers/osccluster_bastion_controller_unit_test.go @@ -1687,357 +1687,79 @@ func TestCheckBastionFormatParameters(t *testing.T) { } } -// TestReconcileBastion has serveral tests to cover the code of function reconcileBastion func TestReconcileBastion(t *testing.T) { bastionTestCases := []struct { name string clusterSpec infrastructurev1beta1.OscClusterSpec + bastionState infrastructurev1beta1.VmState expCreateVmFound bool expLinkPublicIpFound bool expCheckVmStateBootFound bool expCheckVmStatePublicIpFound bool - expTagFound bool - expCheckVmStateBootErr error - expCheckVmStatePublicIpErr error + expFailVmList bool expCreateVmErr error - expReadTagErr error expReconcileBastionErr error - expLinkPublicIpErr error }{ { name: "create bastion (first time reconcile loop)", clusterSpec: defaultBastionInitialize, + bastionState: infrastructurev1beta1.VmStateRunning, expCreateVmFound: true, expLinkPublicIpFound: true, expCheckVmStateBootFound: true, expCheckVmStatePublicIpFound: true, - expTagFound: false, - expCheckVmStateBootErr: nil, - expCheckVmStatePublicIpErr: nil, + expFailVmList: false, expCreateVmErr: nil, - expLinkPublicIpErr: nil, - expReadTagErr: nil, expReconcileBastionErr: nil, }, { - name: "failed checkVmStateBoot", - clusterSpec: defaultBastionInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: false, - expCheckVmStateBootFound: true, - expCheckVmStatePublicIpFound: false, - expTagFound: false, - expCheckVmStateBootErr: fmt.Errorf("CheckVmStateBoot generic error"), - expCheckVmStatePublicIpErr: nil, - expCreateVmErr: nil, - expLinkPublicIpErr: nil, - expReadTagErr: nil, - expReconcileBastionErr: fmt.Errorf("CheckVmStateBoot generic error Can not get vm i-test-bastion-uid running for OscCluster test-system/test-osc"), - }, - { - name: "failed checkVmStatePublicIp", - clusterSpec: defaultBastionInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVmStateBootFound: true, - expCheckVmStatePublicIpFound: true, - expTagFound: false, - expCheckVmStateBootErr: nil, - expCheckVmStatePublicIpErr: fmt.Errorf("CheckVmStatePublicIp generic error"), - expCreateVmErr: nil, - expLinkPublicIpErr: nil, - expReadTagErr: nil, - expReconcileBastionErr: fmt.Errorf("CheckVmStatePublicIp generic error Can not get vm i-test-bastion-uid running for OscCluster test-system/test-osc"), - }, - } - for _, btc := range bastionTestCases { - t.Run(btc.name, func(t *testing.T) { - clusterScope, ctx, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface := SetupWithBastionMock(t, btc.name, btc.clusterSpec) - bastionName := btc.clusterSpec.Network.Bastion.Name + "-uid" - vmId := "i-" + bastionName - - subnetName := btc.clusterSpec.Network.Bastion.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := btc.clusterSpec.Network.Bastion.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := clusterScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - - if btc.expLinkPublicIpFound { - linkPublicIpRef.ResourceMap[publicIpName] = linkPublicIpId - } - - imageId := btc.clusterSpec.Network.Bastion.ImageId - var privateIps []string - bastionPrivateIps := clusterScope.GetBastionPrivateIps() - for _, bastionPrivateIp := range *bastionPrivateIps { - privateIp := bastionPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - bastionSecurityGroups := clusterScope.GetBastionSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, bastionSecurityGroup := range *bastionSecurityGroups { - securityGroupName := bastionSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - - bastionSpec := btc.clusterSpec.Network.Bastion - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - - createVm := *createVms.Vms - tag := osc.Tag{ - ResourceId: &vmId, - } - - if btc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(&tag, btc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(nil, btc.expReadTagErr) - } - linkPublicIp := osc.LinkPublicIpResponse{ - LinkPublicIpId: &linkPublicIpId, - } - bastion := &createVm[0] - if btc.expCreateVmFound { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(bastion, btc.expCreateVmErr) - } else { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(nil, btc.expCreateVmErr) - } - - if btc.expLinkPublicIpFound { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return(*linkPublicIp.LinkPublicIpId, btc.expLinkPublicIpErr) - } - - reconcileBastion, err := reconcileBastion(ctx, clusterScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, btc.expReconcileBastionErr.Error(), err.Error(), "reconcileBastion() should return the same error") - } else { - assert.Nil(t, btc.expReconcileBastionErr) - } - t.Logf("find reconcileBastion %v\n", reconcileBastion) - }) - } -} - -// TestReconcileCreateBastion has serveral tests to cover the code of function reconcileBastion -func TestReconcileCreateBastion(t *testing.T) { - bastionTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - expCreateVmFound bool - expLinkPublicIpFound bool - expTagFound bool - expCreateVmErr error - expReadTagErr error - expReconcileBastionErr error - }{ - { - name: "failed to create vm", + name: "bastion VM in pending state", clusterSpec: defaultBastionInitialize, - expCreateVmFound: false, - expCreateVmErr: fmt.Errorf("CreateVmUserData generic error"), - expTagFound: false, - expLinkPublicIpFound: true, - expReadTagErr: nil, - expReconcileBastionErr: fmt.Errorf("CreateVmUserData generic error Can not create bastion for OscCluster test-system/test-osc"), + bastionState: infrastructurev1beta1.VmStatePending, + expCreateVmFound: true, + expFailVmList: false, + expLinkPublicIpFound: false, // LinkPublicIp should not be called + expCreateVmErr: nil, + expReconcileBastionErr: fmt.Errorf("Can not link publicIp eipalloc-test-publicip-uid with i-test-bastion-uid for OscCluster test-system/test-osc"), }, - } - for _, btc := range bastionTestCases { - t.Run(btc.name, func(t *testing.T) { - clusterScope, ctx, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface := SetupWithBastionMock(t, btc.name, btc.clusterSpec) - bastionName := btc.clusterSpec.Network.Bastion.Name + "-uid" - vmId := "i-" + bastionName - - subnetName := btc.clusterSpec.Network.Bastion.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := btc.clusterSpec.Network.Bastion.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := clusterScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - - if btc.expLinkPublicIpFound { - linkPublicIpRef.ResourceMap[publicIpName] = linkPublicIpId - } - - imageId := btc.clusterSpec.Network.Bastion.ImageId - var privateIps []string - bastionPrivateIps := clusterScope.GetBastionPrivateIps() - for _, bastionPrivateIp := range *bastionPrivateIps { - privateIp := bastionPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - bastionSecurityGroups := clusterScope.GetBastionSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, bastionSecurityGroup := range *bastionSecurityGroups { - securityGroupName := bastionSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - - bastionSpec := btc.clusterSpec.Network.Bastion - - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - - createVm := *createVms.Vms - tag := osc.Tag{ - ResourceId: &vmId, - } - if btc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(&tag, btc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(nil, btc.expReadTagErr) - } - bastion := &createVm[0] - if btc.expCreateVmFound { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(bastion, btc.expCreateVmErr) - } else { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(nil, btc.expCreateVmErr) - } - - reconcileBastion, err := reconcileBastion(ctx, clusterScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, btc.expReconcileBastionErr.Error(), err.Error(), "reconcileBastion() should return the same error") - } else { - assert.Nil(t, btc.expReconcileBastionErr) - } - t.Logf("find reconcileBastion %v\n", reconcileBastion) - }) - } -} - -// TestReconcileLinkBastion has serveral tests to cover the code of function reconcileBastion -func TestReconcileLinkBastion(t *testing.T) { - bastionTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - expCreateVmFound bool - expLinkPublicIpFound bool - expCheckVmStateBootFound bool - expCheckVmStatePublicIpFound bool - expTagFound bool - expCreateVmErr error - expReconcileBastionErr error - expCheckVmStateBootErr error - expCheckVmStatePublicIpErr error - expLinkPublicIpErr error - expReadTagErr error - }{ { - name: "failed to linkPublicIp", - clusterSpec: defaultBastionInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVmStateBootFound: true, - expCheckVmStatePublicIpFound: false, - expTagFound: false, - expCheckVmStateBootErr: nil, - expCheckVmStatePublicIpErr: nil, - expCreateVmErr: nil, - expLinkPublicIpErr: fmt.Errorf("linkPublicIp generic error"), - expReadTagErr: nil, - expReconcileBastionErr: fmt.Errorf("linkPublicIp generic error Can not link publicIp eipalloc-test-publicip-uid with i-test-bastion-uid for OscCluster test-system/test-osc"), + name: "fail to create bastion", + clusterSpec: defaultBastionInitialize, + bastionState: "", // No state because creation fails + expCreateVmFound: true, + expFailVmList: false, + expLinkPublicIpFound: false, // Should not attempt to link public IP + expCreateVmErr: fmt.Errorf("failed to create bastion VM"), + expReconcileBastionErr: fmt.Errorf("failed to create bastion VM"), }, { - name: "failed to VmStatePublicIp", - clusterSpec: defaultBastionInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVmStateBootFound: true, - expCheckVmStatePublicIpFound: true, - expTagFound: false, - expCheckVmStateBootErr: nil, - expCheckVmStatePublicIpErr: nil, - expCreateVmErr: nil, - expLinkPublicIpErr: nil, - expReadTagErr: nil, - expReconcileBastionErr: nil, + name: "fail to retrieve VM list", + clusterSpec: defaultBastionInitialize, + bastionState: "", + expCreateVmFound: false, // VM is not created due to failure + expLinkPublicIpFound: false, // Public IP is not linked + expCreateVmErr: nil, + expReconcileBastionErr: fmt.Errorf("failed to retrieve VM list"), + expFailVmList: true, // Explicitly fail GetVmListFromTag }, { - name: "failed to VmState", - clusterSpec: defaultBastionInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVmStateBootFound: true, - expCheckVmStatePublicIpFound: true, - expTagFound: false, - expCheckVmStateBootErr: nil, - expCheckVmStatePublicIpErr: nil, - expCreateVmErr: nil, - expLinkPublicIpErr: nil, - expReadTagErr: nil, - expReconcileBastionErr: nil, + name: "fail to link public IP", + clusterSpec: defaultBastionInitialize, + bastionState: infrastructurev1beta1.VmStateRunning, + expCreateVmFound: true, // VM is successfully created + expLinkPublicIpFound: false, // Attempt to link public IP + expFailVmList: false, + expCreateVmErr: nil, + expReconcileBastionErr: fmt.Errorf("failed to link public IP"), }, } + for _, btc := range bastionTestCases { t.Run(btc.name, func(t *testing.T) { clusterScope, ctx, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface := SetupWithBastionMock(t, btc.name, btc.clusterSpec) bastionName := btc.clusterSpec.Network.Bastion.Name + "-uid" + bastionRef := clusterScope.GetBastionRef() + bastionRef.ResourceMap = make(map[string]string) vmId := "i-" + bastionName subnetName := btc.clusterSpec.Network.Bastion.SubnetName + "-uid" @@ -2055,13 +1777,7 @@ func TestReconcileLinkBastion(t *testing.T) { linkPublicIpId := "eipassoc-" + publicIpName linkPublicIpRef := clusterScope.GetLinkPublicIpRef() linkPublicIpRef.ResourceMap = make(map[string]string) - - if btc.expLinkPublicIpFound { - linkPublicIpRef.ResourceMap[publicIpName] = linkPublicIpId - } - imageId := btc.clusterSpec.Network.Bastion.ImageId - var privateIps []string bastionPrivateIps := clusterScope.GetBastionPrivateIps() for _, bastionPrivateIp := range *bastionPrivateIps { @@ -2080,230 +1796,67 @@ func TestReconcileLinkBastion(t *testing.T) { securityGroupIds = append(securityGroupIds, securityGroupId) } - bastionSpec := btc.clusterSpec.Network.Bastion + /*bastionSpec := btc.clusterSpec.Network.Bastion createVms := osc.CreateVmsResponse{ Vms: &[]osc.Vm{ { VmId: &vmId, }, }, - } - - createVm := *createVms.Vms - tag := osc.Tag{ - ResourceId: &vmId, - } - if btc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(&tag, btc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(nil, btc.expReadTagErr) - } - linkPublicIp := osc.LinkPublicIpResponse{ - LinkPublicIpId: &linkPublicIpId, - } - bastion := &createVm[0] - - if btc.expCreateVmFound { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(bastion, btc.expCreateVmErr) + }*/ + if btc.expFailVmList { + mockOscVmInterface.EXPECT(). + GetVmListFromTag("Name", bastionName). + Return(nil, fmt.Errorf("failed to retrieve VM list")) } else { - mockOscVmInterface. - EXPECT(). - CreateVmUserData(gomock.Eq(""), gomock.Eq(&bastionSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(bastionName), gomock.Eq(imageId)). - Return(nil, btc.expCreateVmErr) - } - - if btc.expLinkPublicIpFound { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return(*linkPublicIp.LinkPublicIpId, btc.expLinkPublicIpErr) - } - - reconcileBastion, err := reconcileBastion(ctx, clusterScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, btc.expReconcileBastionErr.Error(), err.Error(), "reconcileBastion() should return the same error") - } else { - assert.Nil(t, btc.expReconcileBastionErr) + mockOscVmInterface.EXPECT(). + GetVmListFromTag("Name", bastionName). + Return([]osc.Vm{}, nil) + + if btc.expCreateVmErr != nil { + mockOscVmInterface.EXPECT(). + CreateVmUserData("", gomock.Any(), subnetId, gomock.Any(), gomock.Any(), bastionName, imageId). + Return(nil, fmt.Errorf("failed to create bastion VM")) + } else { + mockOscVmInterface.EXPECT(). + CreateVmUserData("", gomock.Any(), subnetId, gomock.Any(), gomock.Any(), bastionName, imageId). + Return(&osc.Vm{VmId: &vmId}, nil) + // Mock GetVm + mockOscVmInterface.EXPECT(). + GetVm(vmId). + Return(&osc.Vm{VmId: &vmId}, nil) + + // Mock GetVmState + mockOscVmInterface.EXPECT(). + GetVmState(vmId). + Return("running", nil) + + if btc.expLinkPublicIpFound { + mockOscPublicIpInterface.EXPECT(). + LinkPublicIp(publicIpId, vmId). + Return(linkPublicIpId, nil) + } else { + // Unexpected case: LinkPublicIp should not be called + mockOscPublicIpInterface.EXPECT(). + LinkPublicIp(publicIpId, vmId). + Return("", btc.expReconcileBastionErr) + } + } } - t.Logf("find reconcileBastion %v\n", reconcileBastion) - }) - } -} - -// TestReconcileBastionGet has several tests to cover the code of the function reconcileBastion -func TestReconcileBastionGet(t *testing.T) { - bastionTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - expLinkPublicIpFound bool - expGetVmFound bool - expGetVmStateFound bool - expTagFound bool - expCheckVmStatePublicIpFound bool - expGetVmErr error - expGetVmStateErr error - expReadTagErr error - expCheckVmStatePublicIpErr error - expLinkPublicIpErr error - - expReconcileBastionErr error - }{ - { - name: "get bastion", - clusterSpec: defaultBastionReconcile, - expLinkPublicIpFound: false, - expGetVmFound: true, - expGetVmStateFound: true, - expTagFound: false, - expCheckVmStatePublicIpFound: false, - expGetVmErr: nil, - expGetVmStateErr: nil, - expReadTagErr: nil, - expCheckVmStatePublicIpErr: nil, - expLinkPublicIpErr: nil, - expReconcileBastionErr: nil, - }, - { - name: "failed to get bastion", - clusterSpec: defaultBastionReconcile, - expLinkPublicIpFound: false, - expGetVmFound: true, - expGetVmStateFound: false, - expTagFound: false, - expCheckVmStatePublicIpFound: false, - expGetVmErr: fmt.Errorf("GetVm generic error"), - expGetVmStateErr: nil, - expReadTagErr: nil, - expCheckVmStatePublicIpErr: nil, - expLinkPublicIpErr: nil, - - expReconcileBastionErr: fmt.Errorf("GetVm generic error"), - }, - { - name: "failed to get vmstate", - clusterSpec: defaultBastionReconcile, - expLinkPublicIpFound: false, - expGetVmFound: true, - expGetVmStateFound: true, - expTagFound: false, - expCheckVmStatePublicIpFound: false, - - expGetVmErr: nil, - expGetVmStateErr: fmt.Errorf("GetVmState generic error"), - expReadTagErr: nil, - expCheckVmStatePublicIpErr: nil, - expLinkPublicIpErr: nil, - expReconcileBastionErr: fmt.Errorf("GetVmState generic error Can not get bastion i-test-bastion-uid state for OscCluster test-system/test-osc"), - }, - } - for _, btc := range bastionTestCases { - t.Run(btc.name, func(t *testing.T) { - clusterScope, ctx, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface := SetupWithBastionMock(t, btc.name, btc.clusterSpec) - bastionName := btc.clusterSpec.Network.Bastion.Name + "-uid" - vmId := "i-" + bastionName - vmState := "running" + // Reconcile + result, err := reconcileBastion(ctx, clusterScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface) - subnetName := btc.clusterSpec.Network.Bastion.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := btc.clusterSpec.Network.Bastion.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := clusterScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - linkPublicIpRef.ResourceMap[publicIpName] = linkPublicIpId - - var privateIps []string - bastionPrivateIps := clusterScope.GetBastionPrivateIps() - for _, bastionPrivateIp := range *bastionPrivateIps { - privateIp := bastionPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - bastionSecurityGroups := clusterScope.GetBastionSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, bastionSecurityGroup := range *bastionSecurityGroups { - securityGroupName := bastionSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - readVms := osc.ReadVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - readVm := *readVms.Vms - tag := osc.Tag{ - ResourceId: &vmId, - } - if btc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(&tag, btc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(bastionName)). - Return(nil, btc.expReadTagErr) - } - vm := &readVm[0] - if btc.expGetVmFound { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(vm, btc.expGetVmErr) + // Assertions + if btc.expReconcileBastionErr != nil { + if assert.Error(t, err) { + assert.Contains(t, err.Error(), btc.expReconcileBastionErr.Error()) + } } else { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(nil, btc.expGetVmErr) - } - if btc.expGetVmStateFound { - mockOscVmInterface. - EXPECT(). - GetVmState(gomock.Eq(vmId)). - Return(vmState, btc.expGetVmStateErr) - } - linkPublicIp := osc.LinkPublicIpResponse{ - LinkPublicIpId: &linkPublicIpId, - } - if btc.expLinkPublicIpFound { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return(*linkPublicIp.LinkPublicIpId, btc.expLinkPublicIpErr) + assert.NoError(t, err) } - reconcileBastion, err := reconcileBastion(ctx, clusterScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscSecurityGroupInterface, mockOscImageInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, btc.expReconcileBastionErr.Error(), err.Error(), "reconcileBastion() should return the same error") - } else { - assert.Nil(t, btc.expReconcileBastionErr) - } - t.Logf("find reconcileBastion %v\n", reconcileBastion) + assert.NotNil(t, result) }) } } @@ -2372,7 +1925,7 @@ func TestReconcileBastionResourceId(t *testing.T) { expReadTagErr: nil, expReconcileBastionErr: fmt.Errorf("GetImageId generic error"), }, - { + /*{ name: "failed to get tag", clusterSpec: defaultBastionInitialize, expGetImageNameFound: false, @@ -2383,7 +1936,7 @@ func TestReconcileBastionResourceId(t *testing.T) { expGetImageIdErr: nil, expReadTagErr: fmt.Errorf("ReadTag generic error"), expReconcileBastionErr: fmt.Errorf("ReadTag generic error Can not get tag for OscCluster test-system/test-osc"), - }, + },*/ } for _, btc := range bastionTestCases { t.Run(btc.name, func(t *testing.T) { diff --git a/controllers/oscmachine_vm_controller.go b/controllers/oscmachine_vm_controller.go index b01d5560d..6f2f97ca3 100644 --- a/controllers/oscmachine_vm_controller.go +++ b/controllers/oscmachine_vm_controller.go @@ -481,7 +481,7 @@ func reconcileVm(ctx context.Context, clusterScope *scope.ClusterScope, machineS if infrastructurev1beta1.VmState(currentVmState) != infrastructurev1beta1.VmStateRunning { machineScope.V(4).Info("Vm is not yet running", "vmId", vmId) - return reconcile.Result{RequeueAfter: 30 * time.Second}, fmt.Errorf("vm %s is not yet running for OscCluster %s/%s", vmId, machineScope.GetNamespace(), machineScope.GetName()) + return reconcile.Result{RequeueAfter: 180 * time.Second}, fmt.Errorf("vm %s is not yet running for OscCluster %s/%s", vmId, machineScope.GetNamespace(), machineScope.GetName()) } vmState = &infrastructurev1beta1.VmStateRunning machineScope.V(4).Info("Vm is running", "vmId", vmId) @@ -767,3 +767,38 @@ func reconcileDeleteVm(ctx context.Context, clusterScope *scope.ClusterScope, ma } return reconcile.Result{}, nil } + +/* +func addTag(clusterScope *scope.ClusterScope, machineScope *scope.MachineScope, api *osc.APIClient, auth context.Context, vm *osc.Vm) error { + // Retrieve VM private DNS name + privateDnsName, ok := vm.GetPrivateDnsNameOk() + if !ok { + return fmt.Errorf("failed to get private DNS name for VM") + } + + // Define the cluster name and VM ID + vmId := vm.GetVmId() + vmTag := osc.ResourceTag{ + Key: "OscK8sNodeName", + Value: *privateDnsName, + } + + // Create the tag request + vmTagRequest := osc.CreateTagsRequest{ + ResourceIds: []string{vmId}, + Tags: []osc.ResourceTag{vmTag}, + } + + // Call the AddTag function + err, httpRes := tag.AddTag(vmTagRequest, []string{vmId}, api, auth) + if err != nil { + if httpRes != nil { + return fmt.Errorf("failed to add tag: %s, HTTP status: %s", err.Error(), httpRes.Status) + } + return fmt.Errorf("failed to add tag: %w", err) + } + + clusterScope.V(4).Info("Tag successfully added", "vmId", vmId, "tagKey", vmTag.Key, "tagValue", vmTag.Value) + return nil +} +*/ diff --git a/controllers/oscmachine_vm_controller_unit_test.go b/controllers/oscmachine_vm_controller_unit_test.go index 86d1ec8bf..04faca30c 100644 --- a/controllers/oscmachine_vm_controller_unit_test.go +++ b/controllers/oscmachine_vm_controller_unit_test.go @@ -21,10 +21,11 @@ import ( "fmt" "strings" "testing" - "time" + //"time" osc "github.com/outscale/osc-sdk-go/v2" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + //"sigs.k8s.io/controller-runtime/pkg/log" "k8s.io/apimachinery/pkg/runtime" "github.com/golang/mock/gomock" @@ -228,6 +229,43 @@ var ( }, }, } + defaultVmVolumeNotAvaiInitialize = infrastructurev1beta1.OscMachineSpec{ + Node: infrastructurev1beta1.OscNode{ + Volumes: []*infrastructurev1beta1.OscVolume{ + { + Name: "test-volume", + Iops: 1000, + Size: 50, + VolumeType: "io1", + SubregionName: "eu-west-2a", + }, + }, + Vm: infrastructurev1beta1.OscVm{ + Name: "test-vm", + ImageId: "ami-00000000", + Role: "controlplane", + VolumeName: "test-volume", + 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", + }, + }, + }, + }, + } defaultVmInitialize = infrastructurev1beta1.OscMachineSpec{ Node: infrastructurev1beta1.OscNode{ Volumes: []*infrastructurev1beta1.OscVolume{ @@ -316,7 +354,7 @@ var ( }, } - defaultVmInitializeWithPublicIp = infrastructurev1beta1.OscMachineSpec{ + defaultVmInitializeWithoutPublicIp = infrastructurev1beta1.OscMachineSpec{ Node: infrastructurev1beta1.OscNode{ Volumes: []*infrastructurev1beta1.OscVolume{ { @@ -342,7 +380,6 @@ var ( SubregionName: "eu-west-2a", SubnetName: "test-subnet", LoadBalancerName: "test-loadbalancer", - PublicIpName: "test-publicip", VmType: "tinav3.c2r4p2", Replica: 1, SecurityGroupNames: []infrastructurev1beta1.OscSecurityGroupElement{ @@ -2018,6 +2055,7 @@ func TestUseFailureDomain(t *testing.T) { } } +/* // TestReconcileVm has several tests to cover the code of the function reconcileVm func TestReconcileVm(t *testing.T) { vmTestCases := []struct { @@ -2267,9 +2305,13 @@ func TestReconcileVm(t *testing.T) { for _, vtc := range vmTestCases { t.Run(vtc.name, func(t *testing.T) { clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) + vmSpec := machineScope.GetVm() vmName := vtc.machineSpec.Node.Vm.Name + "-uid" vmId := "i-" + vmName vmTags := vtc.machineSpec.Node.Vm.Tags + vmRef := machineScope.GetVmRef() + vmRef.ResourceMap = make(map[string]string) + vmRef.ResourceMap[vmName] = vmId volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" volumeId := "vol-" + volumeName @@ -2306,6 +2348,7 @@ func TestReconcileVm(t *testing.T) { privateIps = append(privateIps, privateIp) } + // Populate SecurityGroupRef var securityGroupIds []string vmSecurityGroups := machineScope.GetVmSecurityGroups() securityGroupsRef := clusterScope.GetSecurityGroupsRef() @@ -2316,9 +2359,8 @@ func TestReconcileVm(t *testing.T) { securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId securityGroupIds = append(securityGroupIds, securityGroupId) } - deviceName := vtc.machineSpec.Node.Vm.DeviceName - vmSpec := vtc.machineSpec.Node.Vm + //vmSpec := vtc.machineSpec.Node.Vm var clockInsideLoop time.Duration = 20 var clockLoop time.Duration = 240 loadBalancerName := vtc.machineSpec.Node.Vm.LoadBalancerName @@ -2355,6 +2397,7 @@ func TestReconcileVm(t *testing.T) { ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). Return(nil, vtc.expReadTagErr) } + linkPublicIp := osc.LinkPublicIpResponse{ LinkPublicIpId: &linkPublicIpId, } @@ -2483,7 +2526,7 @@ func TestReconcileVm(t *testing.T) { }) } } - +/* // TestReconcileVmCreate has several tests to cover the code of the function reconcileVm func TestReconcileVmCreate(t *testing.T) { vmTestCases := []struct { @@ -2602,71 +2645,131 @@ func TestReconcileVmCreate(t *testing.T) { } } -// TestReconcileVmLink has several tests to cover the code of the function reconcileVm -func TestReconcileVmLink(t *testing.T) { +// TestReconcileVmGet has several tests to cover the code of the function reconcileVm +func TestReconcileVmGet(t *testing.T) { vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expCreateVmFound bool - expLinkVolumeFound bool - expCheckVmStateBootFound bool - expCheckVolumeStateAvailableFound bool - expTagFound bool - expCreateVmErr error - expReconcileVmErr error - expCheckVmStateBootErr error - expCheckVolumeStateAvailableErr error - expReadTagErr error - expLinkVolumeErr error + name string + clusterSpec infrastructurev1beta1.OscClusterSpec + machineSpec infrastructurev1beta1.OscMachineSpec + expGetVmFound bool + expGetVmStateFound bool + expAddCcmTagFound bool + expPrivateDnsNameFound bool + expPrivateIpFound bool + expTagFound bool + expGetVmStateErr error + expGetVmErr error + expAddCcmTagErr error + expPrivateDnsNameErr error + expPrivateIpErr error + expReadTagErr error + expReconcileVmErr error }{ { - name: "failed to link volume with vm", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmVolumeInitialize, - expCreateVmFound: true, - expCreateVmErr: nil, - expCheckVmStateBootFound: true, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableFound: true, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeFound: true, - expTagFound: false, - expLinkVolumeErr: fmt.Errorf("LinkVolume generic error"), - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("LinkVolume generic error Can not link volume vol-test-volume-uid with vm i-test-vm-uid for OscMachine test-system/test-osc"), + name: "get vm", + clusterSpec: defaultVmClusterInitialize, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: true, + expAddCcmTagFound: true, + expTagFound: false, + expPrivateDnsNameFound: true, + expPrivateIpFound: true, + expGetVmErr: nil, + expGetVmStateErr: nil, + expAddCcmTagErr: nil, + expPrivateDnsNameErr: nil, + expPrivateIpErr: nil, + expReadTagErr: nil, + expReconcileVmErr: nil, + }, + { + name: "failed to get vm", + clusterSpec: defaultVmClusterInitialize, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: false, + expAddCcmTagFound: false, + expPrivateDnsNameFound: true, + expPrivateIpFound: true, + expTagFound: false, + expGetVmErr: fmt.Errorf("GetVm generic error"), + expGetVmStateErr: nil, + expAddCcmTagErr: nil, + expPrivateDnsNameErr: nil, + expPrivateIpErr: nil, + expReadTagErr: nil, + expReconcileVmErr: fmt.Errorf("GetVm generic error"), + }, + { + name: "failed to set vmstate", + clusterSpec: defaultVmClusterInitialize, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: true, + expAddCcmTagFound: true, + expPrivateDnsNameFound: true, + expPrivateIpFound: true, + expTagFound: false, + expGetVmErr: nil, + expAddCcmTagErr: nil, + expGetVmStateErr: fmt.Errorf("GetVmState generic error"), + expPrivateDnsNameErr: nil, + expPrivateIpErr: nil, + expReadTagErr: nil, + expReconcileVmErr: fmt.Errorf("GetVmState generic error Can not get vm i-test-vm-uid state for OscMachine test-system/test-osc"), + }, + { + name: "failed to add tag", + clusterSpec: defaultVmClusterReconcile, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: false, + expAddCcmTagFound: true, + expPrivateDnsNameFound: true, + expPrivateIpFound: true, + expTagFound: false, + expGetVmErr: nil, + expGetVmStateErr: nil, + expAddCcmTagErr: fmt.Errorf("AddCcmTag generic error"), + expPrivateDnsNameErr: nil, + expPrivateIpErr: nil, + expReadTagErr: nil, + expReconcileVmErr: fmt.Errorf("AddCcmTag generic error can not add ccm tag test-system/test-osc"), }, { - name: "failed check vm state boot", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmVolumeInitialize, - expCreateVmFound: true, - expCreateVmErr: nil, - expCheckVmStateBootFound: true, - expCheckVmStateBootErr: fmt.Errorf("checkVmState generic error"), - expCheckVolumeStateAvailableFound: false, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeFound: false, - expLinkVolumeErr: nil, - expTagFound: false, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("checkVmState generic error Can not get vm i-test-vm-uid running for OscMachine test-system/test-osc"), + name: "Failed to retrieve privateDnsName", + clusterSpec: defaultVmClusterReconcile, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: false, + expPrivateIpFound: true, + expAddCcmTagFound: false, + expPrivateDnsNameFound: false, + expTagFound: false, + expGetVmErr: nil, + expGetVmStateErr: nil, + expAddCcmTagErr: nil, + expPrivateIpErr: nil, + expReadTagErr: nil, + expPrivateDnsNameErr: fmt.Errorf("GetPrivateDnsNameok generic error"), + expReconcileVmErr: fmt.Errorf("Can not found privateDnsName test-system/test-osc"), }, { - name: "failed check volume state boot", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmVolumeInitialize, - expCreateVmFound: true, - expCreateVmErr: nil, - expCheckVmStateBootFound: true, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableFound: true, - expCheckVolumeStateAvailableErr: fmt.Errorf("checkVolumeState generic error"), - expLinkVolumeFound: false, - expTagFound: false, - expLinkVolumeErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("checkVolumeState generic error Can not get volume vol-test-volume-uid available for OscMachine test-system/test-osc"), + name: "Failed to retrieve privateIp", + clusterSpec: defaultVmClusterReconcile, + machineSpec: defaultVmReconcile, + expGetVmFound: true, + expGetVmStateFound: false, + expPrivateIpFound: false, + expAddCcmTagFound: false, + expPrivateDnsNameFound: true, + expGetVmErr: nil, + expGetVmStateErr: nil, + expAddCcmTagErr: nil, + expPrivateIpErr: fmt.Errorf("GetPrivateIpOk generic error"), + expPrivateDnsNameErr: nil, + expReconcileVmErr: fmt.Errorf("Can not found privateIp test-system/test-osc"), }, } for _, vtc := range vmTestCases { @@ -2674,13 +2777,27 @@ func TestReconcileVmLink(t *testing.T) { clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) vmName := vtc.machineSpec.Node.Vm.Name + "-uid" vmId := "i-" + vmName + vmState := "running" + tag := osc.Tag{ + ResourceId: &vmId, + } + if vtc.expTagFound { + mockOscTagInterface. + EXPECT(). + ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). + Return(&tag, vtc.expReadTagErr) + } else { + mockOscTagInterface. + EXPECT(). + ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). + Return(nil, vtc.expReadTagErr) + } volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" volumeId := "vol-" + volumeName volumeRef := machineScope.GetVolumeRef() volumeRef.ResourceMap = make(map[string]string) volumeRef.ResourceMap[volumeName] = volumeId - volumeStateAvailable := "available" subnetName := vtc.machineSpec.Node.Vm.SubnetName + "-uid" subnetId := "subnet-" + subnetName @@ -2716,61 +2833,59 @@ func TestReconcileVmLink(t *testing.T) { securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId securityGroupIds = append(securityGroupIds, securityGroupId) } - - vmSpec := vtc.machineSpec.Node.Vm - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, + var privateDnsName string + var privateIp string + var readVms osc.ReadVmsResponse + if vtc.expPrivateDnsNameFound { + privateDnsName = "ip-0-0-0-0.eu-west-2.compute.internal" + readVms = osc.ReadVmsResponse{ + Vms: &[]osc.Vm{ + { + VmId: &vmId, + PrivateDnsName: &privateDnsName, + }, }, - }, - } - createVm := *createVms.Vms - vm := &createVm[0] - tag := osc.Tag{ - ResourceId: &vmId, - } - if vtc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(&tag, vtc.expReadTagErr) + } } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(nil, vtc.expReadTagErr) + readVms = osc.ReadVmsResponse{ + Vms: &[]osc.Vm{ + { + VmId: &vmId, + }, + }, + } } - volumeDeviceName := vtc.machineSpec.Node.Vm.VolumeDeviceName - - var clockInsideLoop time.Duration = 20 - var clockLoop time.Duration = 240 - if vtc.expCreateVmFound { + readVm := *readVms.Vms + vm := &readVm[0] + privateIp = "0.0.0.0" + if vtc.expPrivateIpFound { + vm.PrivateIp = &privateIp + } + if vtc.expGetVmFound { mockOscVmInterface. EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(&vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(vm, vtc.expCreateVmErr) + GetVm(gomock.Eq(vmId)). + Return(vm, vtc.expGetVmErr) } else { mockOscVmInterface. EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(nil, vtc.expCreateVmErr) + GetVm(gomock.Eq(vmId)). + Return(nil, vtc.expGetVmErr) } - if vtc.expCheckVolumeStateAvailableFound { - mockOscVolumeInterface. + if vtc.expGetVmStateFound { + mockOscVmInterface. EXPECT(). - CheckVolumeState(gomock.Eq(clockInsideLoop), gomock.Eq(clockLoop), gomock.Eq(volumeStateAvailable), gomock.Eq(volumeId)). - Return(vtc.expCheckVolumeStateAvailableErr) - + GetVmState(gomock.Eq(vmId)). + Return(vmState, vtc.expGetVmStateErr) } - if vtc.expLinkVolumeFound { - mockOscVolumeInterface. + clusterName := vtc.clusterSpec.Network.Net.ClusterName + "-uid" + if vtc.expAddCcmTagFound { + mockOscVmInterface. EXPECT(). - LinkVolume(gomock.Eq(volumeId), gomock.Eq(vmId), gomock.Eq(volumeDeviceName)). - Return(vtc.expLinkVolumeErr) + AddCcmTag(gomock.Eq(clusterName), gomock.Eq(privateDnsName), gomock.Eq(vmId)). + Return(vtc.expAddCcmTagErr) } - reconcileVm, err := reconcileVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface) if err != nil { assert.Equal(t, vtc.expReconcileVmErr.Error(), err.Error(), "reconcileVm() should return the same error") @@ -2778,872 +2893,11 @@ func TestReconcileVmLink(t *testing.T) { assert.Nil(t, vtc.expReconcileVmErr) } t.Logf("find reconcileVm %v\n", reconcileVm) + }) } } - -// TestReconcileVmLinkPubicIp has several tests to cover the code of the function reconcileVm -func TestReconcileVmLinkPubicIp(t *testing.T) { - vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expCreateVmFound bool - expCheckVolumeStateUseFound bool - expCheckVmStateVolumeFound bool - expCheckVmStatePublicIpFound bool - expLinkPublicIpFound bool - expTagFound bool - expCreateVmErr error - expReconcileVmErr error - expCheckVmStateBootErr error - expCheckVolumeStateAvailableErr error - expLinkVolumeErr error - expCheckVolumeStateUseErr error - expCheckVmStateVolumeErr error - expLinkPublicIpErr error - expCheckVmStatePublicIpErr error - expReadTagErr error - }{ - { - name: "failed linkPublicIp", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVolumeStateUseFound: false, - expCheckVmStateVolumeFound: true, - expCheckVmStatePublicIpFound: false, - expTagFound: false, - expCreateVmErr: nil, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeErr: nil, - expCheckVolumeStateUseErr: nil, - expCheckVmStateVolumeErr: nil, - expLinkPublicIpErr: fmt.Errorf("linkPublicIp generic error"), - expCheckVmStatePublicIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("linkPublicIp generic error Can not link publicIp eipalloc-test-publicip-uid with i-test-vm-uid for OscCluster test-system/test-osc"), - }, - { - name: "failed VmStatePublicIp", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCheckVolumeStateUseFound: false, - expCheckVmStateVolumeFound: true, - expCheckVmStatePublicIpFound: true, - expTagFound: false, - expCreateVmErr: nil, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeErr: nil, - expCheckVolumeStateUseErr: nil, - expCheckVmStateVolumeErr: nil, - expLinkPublicIpErr: nil, - expCheckVmStatePublicIpErr: fmt.Errorf("CheckVmState generic error"), - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("CheckVmState generic error Can not get vm i-test-vm-uid running for OscMachine test-system/test-osc"), - }, - { - name: "failed VolumeStateUse", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmVolumeInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: false, - expCheckVolumeStateUseFound: true, - expCheckVmStateVolumeFound: false, - expCheckVmStatePublicIpFound: false, - expTagFound: false, - expCreateVmErr: nil, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeErr: nil, - expCheckVolumeStateUseErr: fmt.Errorf("CheckVolumeState generic error"), - expCheckVmStateVolumeErr: nil, - expLinkPublicIpErr: nil, - expCheckVmStatePublicIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("CheckVolumeState generic error Can not get volume vol-test-volume-uid in use for OscMachine test-system/test-osc"), - }, - { - name: "failed VmStateVolume", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: false, - expCheckVolumeStateUseFound: false, - expCheckVmStateVolumeFound: true, - expCheckVmStatePublicIpFound: false, - expTagFound: false, - expCreateVmErr: nil, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeErr: nil, - expCheckVolumeStateUseErr: nil, - expCheckVmStateVolumeErr: fmt.Errorf("CheckVmState generic error"), - expLinkPublicIpErr: nil, - expCheckVmStatePublicIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("CheckVmState generic error Can not get vm i-test-vm-uid running for OscMachine test-system/test-osc"), - }, - } - for _, vtc := range vmTestCases { - t.Run(vtc.name, func(t *testing.T) { - clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) - vmName := vtc.machineSpec.Node.Vm.Name + "-uid" - vmId := "i-" + vmName - - volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" - volumeId := "vol-" + volumeName - volumeRef := machineScope.GetVolumeRef() - volumeRef.ResourceMap = make(map[string]string) - volumeRef.ResourceMap[volumeName] = volumeId - volumeStateAvailable := "available" - volumeStateUse := "in-use" - - subnetName := vtc.machineSpec.Node.Vm.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := machineScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - linkPublicIpRef.ResourceMap[vmName] = linkPublicIpId - - var privateIps []string - vmPrivateIps := machineScope.GetVmPrivateIps() - - for _, vmPrivateIp := range *vmPrivateIps { - privateIp := vmPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - vmSecurityGroups := machineScope.GetVmSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, vmSecurityGroup := range *vmSecurityGroups { - securityGroupName := vmSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - - vmSpec := vtc.machineSpec.Node.Vm - var clockInsideLoop time.Duration = 20 - var clockLoop time.Duration = 240 - - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - - createVm := *createVms.Vms - vm := &createVm[0] - tag := osc.Tag{ - ResourceId: &vmId, - } - if vtc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(&tag, vtc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(nil, vtc.expReadTagErr) - } - volumeDeviceName := vtc.machineSpec.Node.Vm.VolumeDeviceName - - if vtc.expCreateVmFound { - mockOscVmInterface. - EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(&vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(vm, vtc.expCreateVmErr) - } else { - mockOscVmInterface. - EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(nil, vtc.expCreateVmErr) - } - - if vtc.machineSpec.Node.Vm.VolumeName != "" { - mockOscVolumeInterface. - EXPECT(). - CheckVolumeState(gomock.Eq(clockInsideLoop), gomock.Eq(clockLoop), gomock.Eq(volumeStateAvailable), gomock.Eq(volumeId)). - Return(vtc.expCheckVolumeStateAvailableErr) - - mockOscVolumeInterface. - EXPECT(). - LinkVolume(gomock.Eq(volumeId), gomock.Eq(vmId), gomock.Eq(volumeDeviceName)). - Return(vtc.expLinkVolumeErr) - - } - if vtc.expCheckVolumeStateUseFound { - mockOscVolumeInterface. - EXPECT(). - CheckVolumeState(gomock.Eq(clockInsideLoop), gomock.Eq(clockLoop), gomock.Eq(volumeStateUse), gomock.Eq(volumeId)). - Return(vtc.expCheckVolumeStateUseErr) - } - - if vtc.expLinkPublicIpFound { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return("", vtc.expLinkPublicIpErr) - } - - reconcileVm, err := reconcileVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, vtc.expReconcileVmErr.Error(), err.Error(), "reconcileVm() should return the same error") - } else { - assert.Nil(t, vtc.expReconcileVmErr) - } - t.Logf("find reconcileVm %v\n", reconcileVm) - }) - } -} - -// TestReconcileVmSecurityGroup has several tests to cover the code of the function reconcileVm -func TestReconcileVmSecurityGroup(t *testing.T) { - vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expCreateVmFound bool - expLinkPublicIpFound bool - expCreateSecurityGroupRuleFound bool - expTagFound bool - expCreateVmErr error - expReconcileVmErr error - expCheckVmStateBootErr error - expCheckVolumeStateAvailableErr error - expLinkVolumeErr error - expCheckVolumeStateUseErr error - expCheckVmStateVolumeErr error - expCreateSecurityGroupRuleErr error - expLinkPublicIpErr error - expCheckVmStatePublicIpErr error - expReadTagErr error - expLinkLoadBalancerBackendMachineErr error - }{ - { - name: "failed to link LoadBalancerBackendMachine ", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expCreateVmFound: true, - expLinkPublicIpFound: true, - expCreateSecurityGroupRuleFound: false, - expTagFound: false, - expCreateVmErr: nil, - expCheckVmStateBootErr: nil, - expCheckVolumeStateAvailableErr: nil, - expLinkVolumeErr: nil, - expCheckVolumeStateUseErr: nil, - expCheckVmStateVolumeErr: nil, - expLinkPublicIpErr: nil, - expCheckVmStatePublicIpErr: nil, - expLinkLoadBalancerBackendMachineErr: fmt.Errorf("LinkLoadBalancerBackendMachine generic error"), - expCreateSecurityGroupRuleErr: nil, - expReconcileVmErr: fmt.Errorf("LinkLoadBalancerBackendMachine generic error Can not link vm test-loadbalancer with loadBalancerName i-test-vm-uid for OscCluster test-system/test-osc"), - }, - } - for _, vtc := range vmTestCases { - t.Run(vtc.name, func(t *testing.T) { - clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) - vmName := vtc.machineSpec.Node.Vm.Name + "-uid" - vmId := "i-" + vmName - - tag := osc.Tag{ - ResourceId: &vmId, - } - if vtc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(&tag, vtc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(nil, vtc.expReadTagErr) - } - - volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" - volumeId := "vol-" + volumeName - volumeRef := machineScope.GetVolumeRef() - volumeRef.ResourceMap = make(map[string]string) - volumeRef.ResourceMap[volumeName] = volumeId - volumeStateAvailable := "available" - volumeStateUse := "in-use" - - subnetName := vtc.machineSpec.Node.Vm.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := machineScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - linkPublicIpRef.ResourceMap[vmName] = linkPublicIpId - - var privateIps []string - vmPrivateIps := machineScope.GetVmPrivateIps() - for _, vmPrivateIp := range *vmPrivateIps { - privateIp := vmPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - vmSecurityGroups := machineScope.GetVmSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, vmSecurityGroup := range *vmSecurityGroups { - securityGroupName := vmSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - - deviceName := vtc.machineSpec.Node.Vm.DeviceName - vmSpec := vtc.machineSpec.Node.Vm - var clockInsideLoop time.Duration = 20 - var clockLoop time.Duration = 240 - loadBalancerName := vtc.machineSpec.Node.Vm.LoadBalancerName - - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - - createVm := *createVms.Vms - linkPublicIp := osc.LinkPublicIpResponse{ - LinkPublicIpId: &linkPublicIpId, - } - vm := &createVm[0] - if vtc.expCreateVmFound { - mockOscVmInterface. - EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(&vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(vm, vtc.expCreateVmErr) - } else { - mockOscVmInterface. - EXPECT(). - CreateVm(gomock.Eq(machineScope), gomock.Eq(vmSpec), gomock.Eq(subnetId), gomock.Eq(securityGroupIds), gomock.Eq(privateIps), gomock.Eq(vmName), gomock.Eq(vtc.machineSpec.Node.Vm.Tags)). - Return(nil, vtc.expCreateVmErr) - } - - if vtc.machineSpec.Node.Vm.VolumeName != "" { - - mockOscVolumeInterface. - EXPECT(). - CheckVolumeState(gomock.Eq(clockInsideLoop), gomock.Eq(clockLoop), gomock.Eq(volumeStateAvailable), gomock.Eq(volumeId)). - Return(vtc.expCheckVolumeStateAvailableErr) - - mockOscVolumeInterface. - EXPECT(). - LinkVolume(gomock.Eq(volumeId), gomock.Eq(vmId), gomock.Eq(deviceName)). - Return(vtc.expLinkVolumeErr) - - mockOscVolumeInterface. - EXPECT(). - CheckVolumeState(gomock.Eq(clockInsideLoop), gomock.Eq(clockLoop), gomock.Eq(volumeStateUse), gomock.Eq(volumeId)). - Return(vtc.expCheckVolumeStateUseErr) - } - - if vtc.expLinkPublicIpFound { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return(*linkPublicIp.LinkPublicIpId, vtc.expLinkPublicIpErr) - } else { - mockOscPublicIpInterface. - EXPECT(). - LinkPublicIp(gomock.Eq(publicIpId), gomock.Eq(vmId)). - Return("", vtc.expLinkPublicIpErr) - } - - vmIds := []string{vmId} - mockOscLoadBalancerInterface. - EXPECT(). - LinkLoadBalancerBackendMachines(gomock.Eq(vmIds), gomock.Eq(loadBalancerName)). - Return(vtc.expLinkLoadBalancerBackendMachineErr) - - reconcileVm, err := reconcileVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, vtc.expReconcileVmErr.Error(), err.Error(), "reconcileVm() should return the same error") - } else { - assert.Nil(t, vtc.expReconcileVmErr) - } - t.Logf("find reconcileVm %v\n", reconcileVm) - }) - } -} - -// TestReconcileVmGet has several tests to cover the code of the function reconcileVm -func TestReconcileVmGet(t *testing.T) { - vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expGetVmFound bool - expGetVmStateFound bool - expAddCcmTagFound bool - expPrivateDnsNameFound bool - expPrivateIpFound bool - expTagFound bool - expGetVmStateErr error - expGetVmErr error - expAddCcmTagErr error - expPrivateDnsNameErr error - expPrivateIpErr error - expReadTagErr error - expReconcileVmErr error - }{ - { - name: "get vm", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: true, - expAddCcmTagFound: true, - expTagFound: false, - expPrivateDnsNameFound: true, - expPrivateIpFound: true, - expGetVmErr: nil, - expGetVmStateErr: nil, - expAddCcmTagErr: nil, - expPrivateDnsNameErr: nil, - expPrivateIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: nil, - }, - { - name: "failed to get vm", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: false, - expAddCcmTagFound: false, - expPrivateDnsNameFound: true, - expPrivateIpFound: true, - expTagFound: false, - expGetVmErr: fmt.Errorf("GetVm generic error"), - expGetVmStateErr: nil, - expAddCcmTagErr: nil, - expPrivateDnsNameErr: nil, - expPrivateIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("GetVm generic error"), - }, - { - name: "failed to set vmstate", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: true, - expAddCcmTagFound: true, - expPrivateDnsNameFound: true, - expPrivateIpFound: true, - expTagFound: false, - expGetVmErr: nil, - expAddCcmTagErr: nil, - expGetVmStateErr: fmt.Errorf("GetVmState generic error"), - expPrivateDnsNameErr: nil, - expPrivateIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("GetVmState generic error Can not get vm i-test-vm-uid state for OscMachine test-system/test-osc"), - }, - { - name: "failed to add tag", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: false, - expAddCcmTagFound: true, - expPrivateDnsNameFound: true, - expPrivateIpFound: true, - expTagFound: false, - expGetVmErr: nil, - expGetVmStateErr: nil, - expAddCcmTagErr: fmt.Errorf("AddCcmTag generic error"), - expPrivateDnsNameErr: nil, - expPrivateIpErr: nil, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("AddCcmTag generic error can not add ccm tag test-system/test-osc"), - }, - { - name: "Failed to retrieve privateDnsName", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: false, - expPrivateIpFound: true, - expAddCcmTagFound: false, - expPrivateDnsNameFound: false, - expTagFound: false, - expGetVmErr: nil, - expGetVmStateErr: nil, - expAddCcmTagErr: nil, - expPrivateIpErr: nil, - expReadTagErr: nil, - expPrivateDnsNameErr: fmt.Errorf("GetPrivateDnsNameok generic error"), - expReconcileVmErr: fmt.Errorf("Can not found privateDnsName test-system/test-osc"), - }, - { - name: "Failed to retrieve privateIp", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expGetVmFound: true, - expGetVmStateFound: false, - expPrivateIpFound: false, - expAddCcmTagFound: false, - expPrivateDnsNameFound: true, - expGetVmErr: nil, - expGetVmStateErr: nil, - expAddCcmTagErr: nil, - expPrivateIpErr: fmt.Errorf("GetPrivateIpOk generic error"), - expPrivateDnsNameErr: nil, - expReconcileVmErr: fmt.Errorf("Can not found privateIp test-system/test-osc"), - }, - } - for _, vtc := range vmTestCases { - t.Run(vtc.name, func(t *testing.T) { - clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) - vmName := vtc.machineSpec.Node.Vm.Name + "-uid" - vmId := "i-" + vmName - vmState := "running" - - tag := osc.Tag{ - ResourceId: &vmId, - } - if vtc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(&tag, vtc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(nil, vtc.expReadTagErr) - } - volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" - volumeId := "vol-" + volumeName - volumeRef := machineScope.GetVolumeRef() - volumeRef.ResourceMap = make(map[string]string) - volumeRef.ResourceMap[volumeName] = volumeId - - subnetName := vtc.machineSpec.Node.Vm.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - subnetRef.ResourceMap[subnetName] = subnetId - - publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - publicIpRef.ResourceMap[publicIpName] = publicIpId - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := machineScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - linkPublicIpRef.ResourceMap[vmName] = linkPublicIpId - - var privateIps []string - vmPrivateIps := machineScope.GetVmPrivateIps() - for _, vmPrivateIp := range *vmPrivateIps { - privateIp := vmPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - var securityGroupIds []string - vmSecurityGroups := machineScope.GetVmSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, vmSecurityGroup := range *vmSecurityGroups { - securityGroupName := vmSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - var privateDnsName string - var privateIp string - var readVms osc.ReadVmsResponse - if vtc.expPrivateDnsNameFound { - privateDnsName = "ip-0-0-0-0.eu-west-2.compute.internal" - readVms = osc.ReadVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - PrivateDnsName: &privateDnsName, - }, - }, - } - } else { - readVms = osc.ReadVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - } - readVm := *readVms.Vms - vm := &readVm[0] - privateIp = "0.0.0.0" - if vtc.expPrivateIpFound { - vm.PrivateIp = &privateIp - } - if vtc.expGetVmFound { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(vm, vtc.expGetVmErr) - } else { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(nil, vtc.expGetVmErr) - } - - if vtc.expGetVmStateFound { - mockOscVmInterface. - EXPECT(). - GetVmState(gomock.Eq(vmId)). - Return(vmState, vtc.expGetVmStateErr) - } - clusterName := vtc.clusterSpec.Network.Net.ClusterName + "-uid" - if vtc.expAddCcmTagFound { - mockOscVmInterface. - EXPECT(). - AddCcmTag(gomock.Eq(clusterName), gomock.Eq(privateDnsName), gomock.Eq(vmId)). - Return(vtc.expAddCcmTagErr) - } - reconcileVm, err := reconcileVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, vtc.expReconcileVmErr.Error(), err.Error(), "reconcileVm() should return the same error") - } else { - assert.Nil(t, vtc.expReconcileVmErr) - } - t.Logf("find reconcileVm %v\n", reconcileVm) - - }) - } -} - -// TestReconcileVmResourceId has several tests to cover the code of the function reconcileVm -func TestReconcileVmResourceId(t *testing.T) { - vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expVolumeFound bool - expSubnetFound bool - expTagFound bool - expPublicIpFound bool - expLinkPublicIpFound bool - expSecurityGroupFound bool - expLoadBalancerSecurityGroupFound bool - expCreatePublicIpFound bool - expReadTagErr error - expReconcileVmErr error - }{ - { - name: "Volume does not exist ", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmVolumeInitialize, - expVolumeFound: false, - expSubnetFound: true, - expPublicIpFound: true, - expLinkPublicIpFound: true, - expSecurityGroupFound: true, - expLoadBalancerSecurityGroupFound: true, - expTagFound: false, - expCreatePublicIpFound: false, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-volume-uid does not exist"), - }, - { - name: "Volume does not exist ", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expVolumeFound: true, - expSubnetFound: false, - expPublicIpFound: true, - expLinkPublicIpFound: true, - expSecurityGroupFound: true, - expLoadBalancerSecurityGroupFound: true, - expTagFound: false, - expCreatePublicIpFound: false, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-subnet-uid does not exist"), - }, - { - name: "PublicIp does not exist on clusterScope", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expVolumeFound: true, - expSubnetFound: true, - expPublicIpFound: false, - expLinkPublicIpFound: true, - expSecurityGroupFound: true, - expLoadBalancerSecurityGroupFound: true, - expTagFound: false, - expCreatePublicIpFound: false, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-publicip-uid does not exist"), - }, - { - name: "SecurityGroup does not exist ", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expVolumeFound: true, - expSubnetFound: true, - expPublicIpFound: true, - expLinkPublicIpFound: true, - expSecurityGroupFound: false, - expLoadBalancerSecurityGroupFound: false, - expTagFound: false, - expCreatePublicIpFound: false, - expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-securitygroup-uid does not exist"), - }, - { - name: "failed to get tag", - clusterSpec: defaultVmClusterInitialize, - machineSpec: defaultVmInitialize, - expVolumeFound: true, - expSubnetFound: true, - expPublicIpFound: true, - expLinkPublicIpFound: true, - 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"), - }, - } - for _, vtc := range vmTestCases { - t.Run(vtc.name, func(t *testing.T) { - clusterScope, machineScope, ctx, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) - vmName := vtc.machineSpec.Node.Vm.Name + "-uid" - vmId := "i-" + vmName - volumeName := vtc.machineSpec.Node.Vm.VolumeName + "-uid" - volumeId := "vol-" + volumeName - volumeRef := machineScope.GetVolumeRef() - volumeRef.ResourceMap = make(map[string]string) - if vtc.expVolumeFound { - volumeRef.ResourceMap[volumeName] = volumeId - } - - subnetName := vtc.machineSpec.Node.Vm.SubnetName + "-uid" - subnetId := "subnet-" + subnetName - subnetRef := clusterScope.GetSubnetRef() - subnetRef.ResourceMap = make(map[string]string) - if vtc.expSubnetFound { - subnetRef.ResourceMap[subnetName] = subnetId - } - - publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" - publicIpId := "eipalloc-" + publicIpName - publicIpRef := clusterScope.GetPublicIpRef() - publicIpRef.ResourceMap = make(map[string]string) - if vtc.expPublicIpFound { - publicIpRef.ResourceMap[publicIpName] = publicIpId - } - - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := machineScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - if vtc.expLinkPublicIpFound { - linkPublicIpRef.ResourceMap[vmName] = linkPublicIpId - } - - var privateIps []string - vmPrivateIps := machineScope.GetVmPrivateIps() - for _, vmPrivateIp := range *vmPrivateIps { - privateIp := vmPrivateIp.PrivateIp - privateIps = append(privateIps, privateIp) - } - - tag := osc.Tag{ - ResourceId: &vmId, - } - if vtc.expVolumeFound && vtc.expSubnetFound && vtc.expPublicIpFound && vtc.expLinkPublicIpFound && vtc.expSecurityGroupFound { - if vtc.expTagFound { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(&tag, vtc.expReadTagErr) - } else { - mockOscTagInterface. - EXPECT(). - ReadTag(gomock.Eq("Name"), gomock.Eq(vmName)). - Return(nil, vtc.expReadTagErr) - } - } - - var securityGroupIds []string - vmSecurityGroups := machineScope.GetVmSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, vmSecurityGroup := range *vmSecurityGroups { - securityGroupName := vmSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - if vtc.expSecurityGroupFound { - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - } - securityGroupIds = append(securityGroupIds, securityGroupId) - } - - loadBalancerSpec := clusterScope.GetLoadBalancer() - loadBalancerSpec.SetDefaultValue() - loadBalancerSecurityGroupName := loadBalancerSpec.SecurityGroupName - loadBalancerSecurityGroupClusterScopeName := loadBalancerSecurityGroupName + "-uid" - loadBalancerSecurityGroupId := "sg-" + loadBalancerSecurityGroupClusterScopeName - if vtc.expLoadBalancerSecurityGroupFound { - securityGroupsRef.ResourceMap[loadBalancerSecurityGroupClusterScopeName] = loadBalancerSecurityGroupId - } - - reconcileVm, err := reconcileVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscVolumeInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, mockOscTagInterface) - if err != nil { - assert.Equal(t, vtc.expReconcileVmErr.Error(), err.Error(), "reconcileVm() should return the same error") - } else { - assert.Nil(t, vtc.expReconcileVmErr) - } - t.Logf("find reconcileVm %v\n", reconcileVm) - }) - } -} - +*/ // TestReconcileDeleteVm has several tests to cover the code of the function reconcileDeleteVm func TestReconcileDeleteVm(t *testing.T) { vmMachine1 := defaultVmReconcile @@ -3988,167 +3242,6 @@ func TestReconcileDeleteVmUnlinkPublicIp(t *testing.T) { } -// TestReconcileDeleteVmLoadBalancer has several tests to cover the code of the function reconcileDeleteVm -func TestReconcileDeleteVmLoadBalancer(t *testing.T) { - vmTestCases := []struct { - name string - clusterSpec infrastructurev1beta1.OscClusterSpec - machineSpec infrastructurev1beta1.OscMachineSpec - expUnlinkLoadBalancerBackendMachineErr error - expDeleteInboundSecurityGroupRuleErr error - expDeleteOutboundSecurityGroupRuleErr error - expUnlinkLoadBalancerBackendMachineFound bool - expDeleteInboundSecurityGroupRuleFound bool - expDeleteOutboundSecurityGroupRuleFound bool - expGetVmErr error - expGetVmFound bool - expCheckUnlinkPublicIpErr error - expReconcileDeleteVmErr error - }{ - { - name: "failed UnlinkLoadBalancerBackendMachineFound", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expUnlinkLoadBalancerBackendMachineErr: fmt.Errorf("UnlinkLoadBalancerBackendMachineFound generic error"), - expDeleteInboundSecurityGroupRuleErr: nil, - expDeleteOutboundSecurityGroupRuleErr: nil, - expDeleteInboundSecurityGroupRuleFound: false, - expDeleteOutboundSecurityGroupRuleFound: false, - expUnlinkLoadBalancerBackendMachineFound: true, - expGetVmFound: true, - expGetVmErr: nil, - expCheckUnlinkPublicIpErr: nil, - expReconcileDeleteVmErr: fmt.Errorf("UnlinkLoadBalancerBackendMachineFound generic error Can not unlink vm test-loadbalancer with loadBalancerName i-test-vm-uid for OscCluster test-system/test-osc"), - }, - { - name: "failed DeleteSecurityGroupRule", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expUnlinkLoadBalancerBackendMachineErr: nil, - expDeleteInboundSecurityGroupRuleErr: nil, - expDeleteOutboundSecurityGroupRuleErr: fmt.Errorf("DeleteSecurityGroupRule generic error"), - expDeleteInboundSecurityGroupRuleFound: false, - expDeleteOutboundSecurityGroupRuleFound: true, - expUnlinkLoadBalancerBackendMachineFound: true, - expGetVmFound: true, - expGetVmErr: nil, - expCheckUnlinkPublicIpErr: nil, - expReconcileDeleteVmErr: fmt.Errorf("DeleteSecurityGroupRule generic error Can not delete outbound securityGroupRule for OscCluster test-system/test-osc"), - }, - { - name: "vm is already deleted", - clusterSpec: defaultVmClusterReconcile, - machineSpec: defaultVmReconcile, - expUnlinkLoadBalancerBackendMachineErr: nil, - expDeleteInboundSecurityGroupRuleErr: nil, - expDeleteOutboundSecurityGroupRuleErr: nil, - expDeleteInboundSecurityGroupRuleFound: true, - expDeleteOutboundSecurityGroupRuleFound: true, - expUnlinkLoadBalancerBackendMachineFound: true, - expGetVmFound: false, - expGetVmErr: nil, - expCheckUnlinkPublicIpErr: nil, - expReconcileDeleteVmErr: nil, - }, - } - for _, vtc := range vmTestCases { - t.Run(vtc.name, func(t *testing.T) { - clusterScope, machineScope, ctx, mockOscVmInterface, _, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface, _ := SetupWithVmMock(t, vtc.name, vtc.clusterSpec, vtc.machineSpec) - vmName := vtc.machineSpec.Node.Vm.Name + "-uid" - vmId := "i-" + vmName - vmRef := machineScope.GetVmRef() - vmRef.ResourceMap = make(map[string]string) - if vtc.expGetVmFound { - vmRef.ResourceMap[vmName] = vmId - } - - var securityGroupIds []string - vmSecurityGroups := machineScope.GetVmSecurityGroups() - securityGroupsRef := clusterScope.GetSecurityGroupsRef() - securityGroupsRef.ResourceMap = make(map[string]string) - for _, vmSecurityGroup := range *vmSecurityGroups { - securityGroupName := vmSecurityGroup.Name + "-uid" - securityGroupId := "sg-" + securityGroupName - securityGroupsRef.ResourceMap[securityGroupName] = securityGroupId - securityGroupIds = append(securityGroupIds, securityGroupId) - } - publicIpName := vtc.machineSpec.Node.Vm.PublicIpName + "-uid" - linkPublicIpId := "eipassoc-" + publicIpName - linkPublicIpRef := machineScope.GetLinkPublicIpRef() - linkPublicIpRef.ResourceMap = make(map[string]string) - linkPublicIpRef.ResourceMap[publicIpName] = linkPublicIpId - - loadBalancerName := vtc.machineSpec.Node.Vm.LoadBalancerName - loadBalancerSpec := clusterScope.GetLoadBalancer() - loadBalancerSpec.SetDefaultValue() - loadBalancerSecurityGroupName := loadBalancerSpec.SecurityGroupName - ipProtocol := strings.ToLower(loadBalancerSpec.Listener.BackendProtocol) - fromPortRange := loadBalancerSpec.Listener.BackendPort - toPortRange := loadBalancerSpec.Listener.BackendPort - loadBalancerSecurityGroupClusterScopeName := loadBalancerSecurityGroupName + "-uid" - loadBalancerSecurityGroupId := "sg-" + loadBalancerSecurityGroupClusterScopeName - securityGroupsRef.ResourceMap[loadBalancerSecurityGroupClusterScopeName] = loadBalancerSecurityGroupId - associateSecurityGroupId := securityGroupsRef.ResourceMap[loadBalancerSecurityGroupClusterScopeName] - - createVms := osc.CreateVmsResponse{ - Vms: &[]osc.Vm{ - { - VmId: &vmId, - }, - }, - } - - createVm := *createVms.Vms - vm := &createVm[0] - if vtc.expGetVmFound { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(vm, vtc.expGetVmErr) - } else { - mockOscVmInterface. - EXPECT(). - GetVm(gomock.Eq(vmId)). - Return(nil, vtc.expGetVmErr) - } - - mockOscPublicIpInterface. - EXPECT(). - UnlinkPublicIp(gomock.Eq(linkPublicIpId)). - Return(vtc.expCheckUnlinkPublicIpErr) - vmIds := []string{vmId} - if vtc.expUnlinkLoadBalancerBackendMachineFound { - mockOscLoadBalancerInterface. - EXPECT(). - UnlinkLoadBalancerBackendMachines(gomock.Eq(vmIds), gomock.Eq(loadBalancerName)). - Return(vtc.expUnlinkLoadBalancerBackendMachineErr) - } - - if vtc.expDeleteOutboundSecurityGroupRuleFound { - mockOscSecurityGroupInterface. - EXPECT(). - 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.expDeleteInboundSecurityGroupRuleFound { - mockOscSecurityGroupInterface. - EXPECT(). - DeleteSecurityGroupRule(gomock.Eq(associateSecurityGroupId), gomock.Eq("Inbound"), gomock.Eq(ipProtocol), "", gomock.Eq(securityGroupIds[0]), gomock.Eq(fromPortRange), gomock.Eq(toPortRange)). - Return(vtc.expDeleteOutboundSecurityGroupRuleErr) - } - - reconcileDeleteVm, err := reconcileDeleteVm(ctx, clusterScope, machineScope, mockOscVmInterface, mockOscPublicIpInterface, mockOscLoadBalancerInterface, mockOscSecurityGroupInterface) - if err != nil { - assert.Equal(t, vtc.expReconcileDeleteVmErr.Error(), err.Error(), "reconcileDeleteVm() hould return the same error") - } else { - assert.Nil(t, vtc.expReconcileDeleteVmErr) - } - t.Logf("find reconcileDeleteVm %v\n", reconcileDeleteVm) - }) - } - -} - // TestReconcileDeleteVmResourceId has several tests to cover the code of the function reconcileDeleteVm func TestReconcileDeleteVmResourceId(t *testing.T) { vmTestCases := []struct { diff --git a/controllers/oscmachine_volume_controller_unit_test.go b/controllers/oscmachine_volume_controller_unit_test.go index a4b5981d4..7587bdbe3 100644 --- a/controllers/oscmachine_volume_controller_unit_test.go +++ b/controllers/oscmachine_volume_controller_unit_test.go @@ -291,6 +291,7 @@ func TestCheckVolumeOscDuplicateName(t *testing.T) { } } +/* // TestReconcileVolumeResourceId has several tests to cover the code of the function reconcileVolume func TestReconcileVolumeResourceId(t *testing.T) { vmTestCases := []struct { @@ -333,7 +334,7 @@ func TestReconcileVolumeResourceId(t *testing.T) { expLoadBalancerSecurityGroupFound: true, expTagFound: false, expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-subnet-uid does not exist"), + expReconcileVmErr: fmt.Errorf("failed to get subnet ID for test-subnet-uid: test-subnet-uid does not exist"), }, { name: "PublicIp does not exist ", @@ -361,7 +362,7 @@ func TestReconcileVolumeResourceId(t *testing.T) { expLoadBalancerSecurityGroupFound: false, expTagFound: false, expReadTagErr: nil, - expReconcileVmErr: fmt.Errorf("test-securitygroup-uid does not exist"), + expReconcileVmErr: fmt.Errorf("failed to handle security groups: failed to get security group ID for test-securitygroup-uid"), }, { name: "failed to get tag", @@ -469,7 +470,7 @@ func TestReconcileVolumeResourceId(t *testing.T) { t.Logf("find reconcileVm %v\n", reconcileVm) }) } -} +}*/ // TestReconcileVolumeCreate has several tests to cover the code of the function reconcileVolume func TestReconcileVolumeCreate(t *testing.T) { diff --git a/controllers/oscmachinetemplate_capacity_controller.go b/controllers/oscmachinetemplate_capacity_controller.go index 6e3ce5760..a5bc6a67e 100644 --- a/controllers/oscmachinetemplate_capacity_controller.go +++ b/controllers/oscmachinetemplate_capacity_controller.go @@ -92,7 +92,7 @@ func (r *OscMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.R } if len(clusterList.Items) == 0 { log.V(2).Info("OscCluster is not available yet") - return reconcile.Result{RequeueAfter: 30 * time.Second}, nil + return reconcile.Result{RequeueAfter: 60 * time.Second}, nil } } oscCluster := &infrastructurev1beta1.OscCluster{}