|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.kubernetes.version; |
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
19 | 22 | import java.util.UUID; |
20 | 23 |
|
21 | 24 | import org.apache.cloudstack.api.response.KubernetesSupportedVersionResponse; |
22 | 25 | import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; |
| 26 | +import org.apache.cloudstack.storage.datastore.db.ImageStoreDao; |
| 27 | +import org.apache.cloudstack.storage.datastore.db.ImageStoreVO; |
23 | 28 | import org.junit.Assert; |
24 | 29 | import org.junit.Test; |
25 | 30 | import org.junit.runner.RunWith; |
|
32 | 37 | import com.cloud.api.query.dao.TemplateJoinDao; |
33 | 38 | import com.cloud.api.query.vo.TemplateJoinVO; |
34 | 39 | import com.cloud.cpu.CPU; |
| 40 | +import com.cloud.dc.DataCenterVO; |
| 41 | +import com.cloud.dc.dao.DataCenterDao; |
| 42 | +import com.cloud.exception.InvalidParameterValueException; |
35 | 43 |
|
36 | 44 | @RunWith(MockitoJUnitRunner.class) |
37 | 45 | public class KubernetesVersionManagerImplTest { |
38 | 46 |
|
39 | 47 | @Mock |
40 | 48 | TemplateJoinDao templateJoinDao; |
41 | 49 |
|
| 50 | + @Mock |
| 51 | + ImageStoreDao imageStoreDao; |
| 52 | + |
| 53 | + @Mock |
| 54 | + DataCenterDao dataCenterDao; |
| 55 | + |
42 | 56 | @InjectMocks |
43 | 57 | KubernetesVersionManagerImpl kubernetesVersionManager = new KubernetesVersionManagerImpl(); |
44 | 58 |
|
@@ -72,4 +86,62 @@ public void testUpdateTemplateDetailsInKubernetesSupportedVersionResponseValidTe |
72 | 86 | response, true); |
73 | 87 | Assert.assertEquals(state.toString(), ReflectionTestUtils.getField(response, "isoState")); |
74 | 88 | } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testValidateImageStoreForZoneWithDirectDownload() { |
| 92 | + ReflectionTestUtils.invokeMethod(kubernetesVersionManager, "validateImageStoreForZone", 1L, true); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testValidateImageStoreForZoneWithValidZone() { |
| 97 | + Long zoneId = 1L; |
| 98 | + List<ImageStoreVO> imageStores = Collections.singletonList(Mockito.mock(ImageStoreVO.class)); |
| 99 | + Mockito.when(imageStoreDao.listStoresByZoneId(zoneId)).thenReturn(imageStores); |
| 100 | + |
| 101 | + ReflectionTestUtils.invokeMethod(kubernetesVersionManager, "validateImageStoreForZone", zoneId, false); |
| 102 | + } |
| 103 | + |
| 104 | + @Test(expected = InvalidParameterValueException.class) |
| 105 | + public void testValidateImageStoreForZoneWithNoImageStore() { |
| 106 | + Long zoneId = 1L; |
| 107 | + DataCenterVO zone = Mockito.mock(DataCenterVO.class); |
| 108 | + Mockito.when(zone.getName()).thenReturn("test-zone"); |
| 109 | + Mockito.when(dataCenterDao.findById(zoneId)).thenReturn(zone); |
| 110 | + Mockito.when(imageStoreDao.listStoresByZoneId(zoneId)).thenReturn(Collections.emptyList()); |
| 111 | + |
| 112 | + ReflectionTestUtils.invokeMethod(kubernetesVersionManager, "validateImageStoreForZone", zoneId, false); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testValidateImageStoreForAllZonesWithAllValid() { |
| 117 | + DataCenterVO zone1 = Mockito.mock(DataCenterVO.class); |
| 118 | + Mockito.when(zone1.getId()).thenReturn(1L); |
| 119 | + DataCenterVO zone2 = Mockito.mock(DataCenterVO.class); |
| 120 | + Mockito.when(zone2.getId()).thenReturn(2L); |
| 121 | + List<DataCenterVO> zones = Arrays.asList(zone1, zone2); |
| 122 | + Mockito.when(dataCenterDao.listAllZones()).thenReturn(zones); |
| 123 | + |
| 124 | + List<ImageStoreVO> imageStores = Collections.singletonList(Mockito.mock(ImageStoreVO.class)); |
| 125 | + Mockito.when(imageStoreDao.listStoresByZoneId(1L)).thenReturn(imageStores); |
| 126 | + Mockito.when(imageStoreDao.listStoresByZoneId(2L)).thenReturn(imageStores); |
| 127 | + |
| 128 | + ReflectionTestUtils.invokeMethod(kubernetesVersionManager, "validateImageStoreForZone", (Long) null, false); |
| 129 | + } |
| 130 | + |
| 131 | + @Test(expected = InvalidParameterValueException.class) |
| 132 | + public void testValidateImageStoreForAllZonesWithSomeMissingStorage() { |
| 133 | + DataCenterVO zone1 = Mockito.mock(DataCenterVO.class); |
| 134 | + Mockito.when(zone1.getId()).thenReturn(1L); |
| 135 | + DataCenterVO zone2 = Mockito.mock(DataCenterVO.class); |
| 136 | + Mockito.when(zone2.getId()).thenReturn(2L); |
| 137 | + Mockito.when(zone2.getName()).thenReturn("zone-without-storage"); |
| 138 | + List<DataCenterVO> zones = Arrays.asList(zone1, zone2); |
| 139 | + Mockito.when(dataCenterDao.listAllZones()).thenReturn(zones); |
| 140 | + |
| 141 | + List<ImageStoreVO> imageStores = Collections.singletonList(Mockito.mock(ImageStoreVO.class)); |
| 142 | + Mockito.when(imageStoreDao.listStoresByZoneId(1L)).thenReturn(imageStores); |
| 143 | + Mockito.when(imageStoreDao.listStoresByZoneId(2L)).thenReturn(Collections.emptyList()); |
| 144 | + |
| 145 | + ReflectionTestUtils.invokeMethod(kubernetesVersionManager, "validateImageStoreForZone", (Long) null, false); |
| 146 | + } |
75 | 147 | } |
0 commit comments