|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.kubernetes.cluster.actionworkers; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 22 | +import org.apache.cloudstack.context.CallContext; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.MockedStatic; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.mockito.junit.MockitoJUnitRunner; |
| 30 | + |
| 31 | +import com.cloud.kubernetes.cluster.KubernetesCluster; |
| 32 | +import com.cloud.kubernetes.cluster.KubernetesClusterManagerImpl; |
| 33 | +import com.cloud.kubernetes.cluster.dao.KubernetesClusterDao; |
| 34 | +import com.cloud.kubernetes.version.KubernetesSupportedVersion; |
| 35 | +import com.cloud.storage.Storage; |
| 36 | +import com.cloud.storage.VMTemplateVO; |
| 37 | +import com.cloud.storage.dao.VMTemplateDao; |
| 38 | +import com.cloud.template.TemplateApiService; |
| 39 | +import com.cloud.template.VirtualMachineTemplate; |
| 40 | +import com.cloud.uservm.UserVm; |
| 41 | +import com.cloud.vm.UserVmVO; |
| 42 | +import com.cloud.vm.VmIsoMapVO; |
| 43 | +import com.cloud.vm.dao.UserVmDao; |
| 44 | +import com.cloud.vm.dao.VmIsoMapDao; |
| 45 | + |
| 46 | +/** |
| 47 | + * Attaching the binaries ISO to the nodes of a cluster, some of which may still have an ISO attached. |
| 48 | + */ |
| 49 | +@RunWith(MockitoJUnitRunner.class) |
| 50 | +public class KubernetesClusterActionWorkerIsoTest { |
| 51 | + |
| 52 | + private static final long BINARIES_ISO_ID = 10L; |
| 53 | + private static final long OTHER_ISO_ID = 11L; |
| 54 | + private static final long VM_ID = 2L; |
| 55 | + |
| 56 | + @Mock |
| 57 | + KubernetesClusterManagerImpl kubernetesClusterManager; |
| 58 | + |
| 59 | + @Mock |
| 60 | + KubernetesCluster kubernetesCluster; |
| 61 | + |
| 62 | + @Mock |
| 63 | + TemplateApiService templateService; |
| 64 | + |
| 65 | + @Mock |
| 66 | + VMTemplateDao templateDao; |
| 67 | + |
| 68 | + @Mock |
| 69 | + UserVmDao userVmDao; |
| 70 | + |
| 71 | + @Mock |
| 72 | + VmIsoMapDao vmIsoMapDao; |
| 73 | + |
| 74 | + @Mock |
| 75 | + KubernetesSupportedVersion version; |
| 76 | + |
| 77 | + @Mock |
| 78 | + UserVm vm; |
| 79 | + |
| 80 | + KubernetesClusterActionWorker actionWorker; |
| 81 | + |
| 82 | + @Before |
| 83 | + public void setUp() { |
| 84 | + kubernetesClusterManager.kubernetesClusterDao = Mockito.mock(KubernetesClusterDao.class); |
| 85 | + actionWorker = new KubernetesClusterActionWorker(kubernetesCluster, kubernetesClusterManager); |
| 86 | + actionWorker.templateService = templateService; |
| 87 | + actionWorker.templateDao = templateDao; |
| 88 | + actionWorker.userVmDao = userVmDao; |
| 89 | + actionWorker.vmIsoMapDao = vmIsoMapDao; |
| 90 | + Mockito.when(version.getIsoId()).thenReturn(BINARIES_ISO_ID); |
| 91 | + VMTemplateVO iso = Mockito.mock(VMTemplateVO.class); |
| 92 | + Mockito.when(iso.getId()).thenReturn(BINARIES_ISO_ID); |
| 93 | + Mockito.when(iso.getFormat()).thenReturn(Storage.ImageFormat.ISO); |
| 94 | + Mockito.when(iso.getState()).thenReturn(VirtualMachineTemplate.State.Active); |
| 95 | + Mockito.when(templateDao.findById(BINARIES_ISO_ID)).thenReturn(iso); |
| 96 | + Mockito.when(vm.getId()).thenReturn(VM_ID); |
| 97 | + } |
| 98 | + |
| 99 | + private void attachBinariesIso(Long attachedIsoId, boolean binariesIsoInOtherCdrom) { |
| 100 | + UserVmVO userVm = Mockito.mock(UserVmVO.class); |
| 101 | + Mockito.when(userVm.getIsoId()).thenReturn(attachedIsoId); |
| 102 | + Mockito.when(userVmDao.findById(VM_ID)).thenReturn(userVm); |
| 103 | + Mockito.lenient().when(vmIsoMapDao.findByVmIdIsoId(VM_ID, BINARIES_ISO_ID)).thenReturn(binariesIsoInOtherCdrom ? Mockito.mock(VmIsoMapVO.class) : null); |
| 104 | + try (MockedStatic<CallContext> callContext = Mockito.mockStatic(CallContext.class)) { |
| 105 | + callContext.when(() -> CallContext.register(Mockito.any(), Mockito.any(ApiCommandResourceType.class))).thenReturn(Mockito.mock(CallContext.class)); |
| 106 | + actionWorker.attachIsoKubernetesVMs(List.of(vm), version); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testAttachIsoKubernetesVMsNoIsoAttached() { |
| 112 | + attachBinariesIso(null, false); |
| 113 | + Mockito.verify(templateService, Mockito.never()).detachIso(Mockito.anyLong(), Mockito.any(), Mockito.any()); |
| 114 | + Mockito.verify(templateService).attachIso(BINARIES_ISO_ID, VM_ID, true); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testAttachIsoKubernetesVMsBinariesIsoAlreadyAttached() { |
| 119 | + attachBinariesIso(BINARIES_ISO_ID, false); |
| 120 | + Mockito.verifyNoInteractions(templateService); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testAttachIsoKubernetesVMsBinariesIsoAttachedToOtherCdrom() { |
| 125 | + attachBinariesIso(OTHER_ISO_ID, true); |
| 126 | + Mockito.verifyNoInteractions(templateService); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testAttachIsoKubernetesVMsOtherIsoAttached() { |
| 131 | + attachBinariesIso(OTHER_ISO_ID, false); |
| 132 | + Mockito.verify(templateService).detachIso(VM_ID, OTHER_ISO_ID, true); |
| 133 | + Mockito.verify(templateService).attachIso(BINARIES_ISO_ID, VM_ID, true); |
| 134 | + } |
| 135 | +} |
0 commit comments