|
| 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 org.apache.cloudstack.storage.image; |
| 18 | + |
| 19 | +import com.cloud.agent.api.to.DataStoreTO; |
| 20 | +import com.cloud.storage.VMTemplateStorageResourceAssoc; |
| 21 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 22 | +import junit.framework.TestCase; |
| 23 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 24 | +import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; |
| 25 | +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo; |
| 26 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
| 27 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; |
| 28 | +import org.apache.cloudstack.storage.to.TemplateObjectTO; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.InjectMocks; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.mockito.Spy; |
| 36 | +import org.mockito.junit.MockitoJUnitRunner; |
| 37 | + |
| 38 | +@RunWith(MockitoJUnitRunner.class) |
| 39 | +public class SecondaryStorageServiceImplTest extends TestCase { |
| 40 | + |
| 41 | + @Spy |
| 42 | + @InjectMocks |
| 43 | + private SecondaryStorageServiceImpl secondaryStorageService; |
| 44 | + |
| 45 | + @Mock |
| 46 | + TemplateDataStoreDao templateDataStoreDaoMock; |
| 47 | + |
| 48 | + @Mock |
| 49 | + TemplateDataStoreVO templateDataStoreVoMock; |
| 50 | + |
| 51 | + @Mock |
| 52 | + TemplateInfo templateInfoMock; |
| 53 | + |
| 54 | + @Mock |
| 55 | + TemplateObjectTO templateObjectToMock; |
| 56 | + |
| 57 | + @Mock |
| 58 | + DataStore dataStoreMock; |
| 59 | + |
| 60 | + @Mock |
| 61 | + DataStoreTO dataStoreToMock; |
| 62 | + |
| 63 | + private void prepareForTemplateIsOnDestinationTests() { |
| 64 | + long dataStoreId = 1; |
| 65 | + long templateId = 2; |
| 66 | + |
| 67 | + Mockito.when(dataStoreMock.getId()).thenReturn(dataStoreId); |
| 68 | + Mockito.when(dataStoreMock.getTO()).thenReturn(dataStoreToMock); |
| 69 | + Mockito.when(templateInfoMock.getId()).thenReturn(templateId); |
| 70 | + Mockito.when(templateInfoMock.getTO()).thenReturn(templateObjectToMock); |
| 71 | + Mockito.doReturn(templateDataStoreVoMock).when(templateDataStoreDaoMock).findByStoreTemplate(dataStoreId, templateId); |
| 72 | + Mockito.when(templateDataStoreVoMock.getState()).thenReturn(ObjectInDataStoreStateMachine.State.Ready); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void templateIsOnDestinationTestReturnsFalseWhenTemplateStoreRefDoesNotExist() { |
| 77 | + prepareForTemplateIsOnDestinationTests(); |
| 78 | + Mockito.doReturn(null).when(templateDataStoreDaoMock).findByStoreTemplate(Mockito.anyLong(), Mockito.anyLong()); |
| 79 | + |
| 80 | + boolean result = secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 81 | + |
| 82 | + Assert.assertFalse(result); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void templateIsOnDestinationTestReturnsTrueWhenTemplateIsReady() { |
| 87 | + prepareForTemplateIsOnDestinationTests(); |
| 88 | + |
| 89 | + boolean result = secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 90 | + |
| 91 | + Assert.assertTrue(result); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void templateIsOnDestinationTestReturnsFalseWhenTemplateIsNotReady() { |
| 96 | + prepareForTemplateIsOnDestinationTests(); |
| 97 | + Mockito.when(templateDataStoreVoMock.getState()).thenReturn(ObjectInDataStoreStateMachine.State.Creating); |
| 98 | + |
| 99 | + boolean result = secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 100 | + |
| 101 | + Assert.assertFalse(result); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void templateIsOnDestinationTestReturnsTrueIfTemplateIsDownloadedSuccessfully() { |
| 106 | + prepareForTemplateIsOnDestinationTests(); |
| 107 | + Mockito.when(templateDataStoreVoMock.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS); |
| 108 | + Mockito.doAnswer(I -> Mockito.when(templateDataStoreVoMock.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOADED)).when(secondaryStorageService).waitForTemplateDownload(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString()); |
| 109 | + |
| 110 | + boolean result = secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 111 | + |
| 112 | + Assert.assertTrue(result); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void templateIsOnDestinationTestReturnsFalseIfTemplateIsNotDownloadedSuccessfully() { |
| 117 | + prepareForTemplateIsOnDestinationTests(); |
| 118 | + Mockito.when(templateDataStoreVoMock.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS); |
| 119 | + Mockito.doAnswer(I -> { |
| 120 | + Mockito.when(templateDataStoreVoMock.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR); |
| 121 | + Mockito.when(templateDataStoreVoMock.getState()).thenReturn(ObjectInDataStoreStateMachine.State.Failed); |
| 122 | + return "mocked download fail"; |
| 123 | + }).when(secondaryStorageService).waitForTemplateDownload(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString()); |
| 124 | + |
| 125 | + boolean result = secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 126 | + |
| 127 | + Assert.assertFalse(result); |
| 128 | + } |
| 129 | + |
| 130 | + @Test(expected = CloudRuntimeException.class) |
| 131 | + public void templateIsOnDestinationTestThrowsExceptionIfDownloadTimesOut() { |
| 132 | + prepareForTemplateIsOnDestinationTests(); |
| 133 | + Mockito.when(templateDataStoreVoMock.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS); |
| 134 | + Mockito.doReturn(0L).when(secondaryStorageService).getTemplateDownloadTimeout(); |
| 135 | + |
| 136 | + secondaryStorageService.templateIsOnDestination(templateInfoMock, dataStoreMock); |
| 137 | + } |
| 138 | +} |
0 commit comments