Skip to content

Commit 4fd09cb

Browse files
committed
CLOUDSTACK-4324: delete snapshot_store_ref, if create snapshot failed on primary storage
1 parent d89667e commit 4fd09cb

7 files changed

Lines changed: 433 additions & 9 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider;
22+
import org.apache.cloudstack.storage.datastore.provider.CloudStackPrimaryDataStoreProviderImpl;
23+
import org.apache.cloudstack.storage.datastore.type.DataStoreType;
24+
import org.mockito.Mockito;
25+
import org.springframework.context.annotation.Bean;
26+
27+
import java.util.HashSet;
28+
import java.util.Set;
29+
30+
public class FakeDriverTestConfiguration extends ChildTestConfiguration{
31+
@Bean
32+
public CloudStackPrimaryDataStoreProviderImpl dataStoreProvider() {
33+
CloudStackPrimaryDataStoreProviderImpl provider = Mockito.mock(CloudStackPrimaryDataStoreProviderImpl.class);
34+
35+
return provider;
36+
}
37+
38+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.agent.api.to.DataStoreTO;
22+
import com.cloud.agent.api.to.DataTO;
23+
import org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo;
24+
import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult;
25+
import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult;
26+
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
27+
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
28+
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver;
29+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
30+
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
31+
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
32+
import org.apache.cloudstack.storage.command.CommandResult;
33+
import org.apache.cloudstack.storage.command.CreateObjectAnswer;
34+
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
35+
36+
import java.util.UUID;
37+
38+
public class FakePrimaryDataStoreDriver implements PrimaryDataStoreDriver {
39+
boolean snapshotResult = true;
40+
@Override
41+
public ChapInfo getChapInfo(VolumeInfo volumeInfo) {
42+
return null; //To change body of implemented methods use File | Settings | File Templates.
43+
}
44+
45+
@Override
46+
public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCmdResult> callback) {
47+
CreateCmdResult result = new CreateCmdResult(null, null);
48+
if (snapshotResult) {
49+
SnapshotObjectTO newSnap = new SnapshotObjectTO();
50+
newSnap.setPath(UUID.randomUUID().toString());
51+
52+
CreateObjectAnswer answer = new CreateObjectAnswer(newSnap);
53+
result.setAnswer(answer);
54+
} else {
55+
result.setResult("Failed to create snapshot");
56+
}
57+
callback.complete(result);
58+
return;
59+
}
60+
61+
public void makeTakeSnapshotSucceed(boolean success) {
62+
snapshotResult = success;
63+
}
64+
65+
@Override
66+
public void revertSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CommandResult> callback) {
67+
//To change body of implemented methods use File | Settings | File Templates.
68+
}
69+
70+
@Override
71+
public DataTO getTO(DataObject data) {
72+
return null; //To change body of implemented methods use File | Settings | File Templates.
73+
}
74+
75+
@Override
76+
public DataStoreTO getStoreTO(DataStore store) {
77+
return null; //To change body of implemented methods use File | Settings | File Templates.
78+
}
79+
80+
@Override
81+
public void createAsync(DataStore store, DataObject data, AsyncCompletionCallback<CreateCmdResult> callback) {
82+
//To change body of implemented methods use File | Settings | File Templates.
83+
}
84+
85+
@Override
86+
public void deleteAsync(DataStore store, DataObject data, AsyncCompletionCallback<CommandResult> callback) {
87+
CommandResult result = new CommandResult();
88+
result.setSuccess(true);
89+
callback.complete(result);
90+
return;
91+
}
92+
93+
@Override
94+
public void copyAsync(DataObject srcdata, DataObject destData, AsyncCompletionCallback<CopyCommandResult> callback) {
95+
//To change body of implemented methods use File | Settings | File Templates.
96+
}
97+
98+
@Override
99+
public boolean canCopy(DataObject srcData, DataObject destData) {
100+
return false; //To change body of implemented methods use File | Settings | File Templates.
101+
}
102+
103+
@Override
104+
public void resize(DataObject data, AsyncCompletionCallback<CreateCmdResult> callback) {
105+
//To change body of implemented methods use File | Settings | File Templates.
106+
}
107+
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

Comments
 (0)