|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage.test; |
| 20 | + |
| 21 | +import com.cloud.hypervisor.Hypervisor; |
| 22 | +import com.cloud.storage.ScopeType; |
| 23 | +import com.cloud.storage.Snapshot; |
| 24 | +import com.cloud.storage.SnapshotVO; |
| 25 | +import com.cloud.storage.Storage; |
| 26 | +import com.cloud.storage.StoragePoolStatus; |
| 27 | +import com.cloud.storage.Volume; |
| 28 | +import com.cloud.storage.VolumeVO; |
| 29 | +import com.cloud.storage.dao.SnapshotDao; |
| 30 | +import com.cloud.storage.dao.VolumeDao; |
| 31 | +import com.cloud.utils.component.ComponentContext; |
| 32 | +import junit.framework.Assert; |
| 33 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 34 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager; |
| 35 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider; |
| 36 | +import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; |
| 37 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver; |
| 38 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreProvider; |
| 39 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory; |
| 40 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; |
| 41 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult; |
| 42 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService; |
| 43 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; |
| 44 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
| 45 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeService; |
| 46 | +import org.apache.cloudstack.storage.datastore.PrimaryDataStore; |
| 47 | +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
| 48 | +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao; |
| 49 | +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO; |
| 50 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 51 | +import org.junit.Before; |
| 52 | +import org.junit.Test; |
| 53 | +import org.junit.runner.RunWith; |
| 54 | +import org.mockito.Mockito; |
| 55 | +import org.springframework.test.context.ContextConfiguration; |
| 56 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 57 | + |
| 58 | +import javax.inject.Inject; |
| 59 | +import java.net.URI; |
| 60 | +import java.net.URISyntaxException; |
| 61 | +import java.util.HashSet; |
| 62 | +import java.util.Set; |
| 63 | +import java.util.UUID; |
| 64 | + |
| 65 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 66 | +@ContextConfiguration(locations = { "classpath:/fakeDriverTestContext.xml" }) |
| 67 | +public class SnapshotTestWithFakeData { |
| 68 | + @Inject |
| 69 | + SnapshotService snapshotService; |
| 70 | + @Inject |
| 71 | + SnapshotDao snapshotDao; |
| 72 | + @Inject |
| 73 | + PrimaryDataStoreDao primaryDataStoreDao; |
| 74 | + @Inject |
| 75 | + DataStoreManager dataStoreManager; |
| 76 | + @Inject |
| 77 | + SnapshotDataFactory snapshotDataFactory; |
| 78 | + @Inject |
| 79 | + PrimaryDataStoreProvider primaryDataStoreProvider; |
| 80 | + @Inject |
| 81 | + SnapshotDataStoreDao snapshotDataStoreDao; |
| 82 | + @Inject |
| 83 | + VolumeDao volumeDao; |
| 84 | + @Inject |
| 85 | + VolumeService volumeService; |
| 86 | + @Inject |
| 87 | + VolumeDataFactory volumeDataFactory; |
| 88 | + FakePrimaryDataStoreDriver driver = new FakePrimaryDataStoreDriver(); |
| 89 | + |
| 90 | + @Before |
| 91 | + public void setUp() { |
| 92 | + Mockito.when(primaryDataStoreProvider.configure(Mockito.anyMap())).thenReturn(true); |
| 93 | + Set<DataStoreProvider.DataStoreProviderType> types = new HashSet<DataStoreProvider.DataStoreProviderType>(); |
| 94 | + types.add(DataStoreProvider.DataStoreProviderType.PRIMARY); |
| 95 | + |
| 96 | + Mockito.when(primaryDataStoreProvider.getTypes()).thenReturn(types); |
| 97 | + Mockito.when(primaryDataStoreProvider.getName()).thenReturn(DataStoreProvider.DEFAULT_PRIMARY); |
| 98 | + Mockito.when(primaryDataStoreProvider.getDataStoreDriver()).thenReturn(driver); |
| 99 | + |
| 100 | + ComponentContext.initComponentsLifeCycle(); |
| 101 | + } |
| 102 | + private SnapshotVO createSnapshotInDb() { |
| 103 | + Snapshot.Type snapshotType = Snapshot.Type.MANUAL; |
| 104 | + SnapshotVO snapshotVO = new SnapshotVO(1, 2, 1, 1L, 1L, UUID.randomUUID() |
| 105 | + .toString(), (short) snapshotType.ordinal(), snapshotType.name(), 100, |
| 106 | + Hypervisor.HypervisorType.XenServer); |
| 107 | + return this.snapshotDao.persist(snapshotVO); |
| 108 | + } |
| 109 | + |
| 110 | + private VolumeInfo createVolume(Long templateId, DataStore store) { |
| 111 | + VolumeVO volume = new VolumeVO(Volume.Type.DATADISK, UUID.randomUUID().toString(), 1L, 1L, 1L, 1L, 1000, 0L, 0L, ""); |
| 112 | + ; |
| 113 | + volume.setPoolId(store.getId()); |
| 114 | + |
| 115 | + volume = volumeDao.persist(volume); |
| 116 | + VolumeInfo volumeInfo = volumeDataFactory.getVolume(volume.getId(), store); |
| 117 | + volumeInfo.stateTransit(Volume.Event.CreateRequested); |
| 118 | + volumeInfo.stateTransit(Volume.Event.OperationSucceeded); |
| 119 | + return volumeInfo; |
| 120 | + } |
| 121 | + private DataStore createDataStore() throws URISyntaxException { |
| 122 | + StoragePoolVO pool = new StoragePoolVO(); |
| 123 | + pool.setClusterId(1L); |
| 124 | + pool.setDataCenterId(1); |
| 125 | + URI uri = new URI("nfs://jfkdkf/fjdkfj"); |
| 126 | + pool.setHostAddress(uri.getHost()); |
| 127 | + pool.setPath(uri.getPath()); |
| 128 | + pool.setPort(0); |
| 129 | + pool.setName(UUID.randomUUID().toString()); |
| 130 | + pool.setUuid(UUID.randomUUID().toString()); |
| 131 | + pool.setStatus(StoragePoolStatus.Up); |
| 132 | + pool.setPoolType(Storage.StoragePoolType.NetworkFilesystem); |
| 133 | + pool.setPodId(1L); |
| 134 | + pool.setScope(ScopeType.CLUSTER); |
| 135 | + pool.setStorageProviderName(DataStoreProvider.DEFAULT_PRIMARY); |
| 136 | + pool = this.primaryDataStoreDao.persist(pool); |
| 137 | + DataStore store = this.dataStoreManager.getPrimaryDataStore(pool.getId()); |
| 138 | + return store; |
| 139 | + } |
| 140 | + @Test |
| 141 | + public void testTakeSnapshot() throws URISyntaxException { |
| 142 | + SnapshotVO snapshotVO = createSnapshotInDb(); |
| 143 | + DataStore store = createDataStore(); |
| 144 | + try { |
| 145 | + SnapshotInfo snapshotInfo = snapshotDataFactory.getSnapshot(snapshotVO.getId(), store); |
| 146 | + SnapshotResult result = snapshotService.takeSnapshot(snapshotInfo); |
| 147 | + Assert.assertTrue(result.isSuccess()); |
| 148 | + SnapshotDataStoreVO storeRef = snapshotDataStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshotVO.getId()); |
| 149 | + Assert.assertTrue(storeRef != null); |
| 150 | + Assert.assertTrue(storeRef.getState() == ObjectInDataStoreStateMachine.State.Ready); |
| 151 | + snapshotInfo = result.getSnashot(); |
| 152 | + boolean deletResult = snapshotService.deleteSnapshot(snapshotInfo); |
| 153 | + Assert.assertTrue(deletResult); |
| 154 | + snapshotDataStoreDao.expunge(storeRef.getId()); |
| 155 | + } finally { |
| 156 | + snapshotDao.expunge(snapshotVO.getId()); |
| 157 | + primaryDataStoreDao.remove(store.getId()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testTakeSnapshotWithFailed() throws URISyntaxException { |
| 163 | + SnapshotVO snapshotVO = createSnapshotInDb(); |
| 164 | + DataStore store = null; |
| 165 | + try { |
| 166 | + store = createDataStore(); |
| 167 | + FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver)store.getDriver(); |
| 168 | + dataStoreDriver.makeTakeSnapshotSucceed(false); |
| 169 | + SnapshotInfo snapshotInfo = snapshotDataFactory.getSnapshot(snapshotVO.getId(), store); |
| 170 | + SnapshotResult result = snapshotService.takeSnapshot(snapshotInfo); |
| 171 | + Assert.assertFalse(result.isSuccess()); |
| 172 | + SnapshotDataStoreVO storeRef = snapshotDataStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshotVO.getId()); |
| 173 | + Assert.assertTrue(storeRef == null); |
| 174 | + } finally { |
| 175 | + snapshotDao.expunge(snapshotVO.getId()); |
| 176 | + if (store != null) { |
| 177 | + primaryDataStoreDao.remove(store.getId()); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testTakeSnapshotFromVolume() throws URISyntaxException { |
| 184 | + DataStore store = createDataStore(); |
| 185 | + FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver)store.getDriver(); |
| 186 | + dataStoreDriver.makeTakeSnapshotSucceed(false); |
| 187 | + VolumeInfo volumeInfo = createVolume(1L, store); |
| 188 | + Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready); |
| 189 | + SnapshotInfo result = volumeService.takeSnapshot(volumeInfo); |
| 190 | + Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready); |
| 191 | + Assert.assertTrue(result == null); |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments