Skip to content

Commit 32caf90

Browse files
authored
engine-storage: fix errored template becomes active (#7485)
* engine-storage: fix errored template becomes active Fixes #7342 Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * test Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> --------- Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent c475549 commit 32caf90

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

engine/storage/image/src/main/java/org/apache/cloudstack/storage/image/TemplateServiceImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,23 @@ public void handleSysTemplateDownload(HypervisorType hostHyper, Long dcId) {
286286
}
287287
}
288288

289+
protected boolean isSkipTemplateStoreDownload(VMTemplateVO template, Long zoneId) {
290+
if (template.isPublicTemplate()) {
291+
return false;
292+
}
293+
if (template.isFeatured()) {
294+
return false;
295+
}
296+
if (TemplateType.SYSTEM.equals(template.getTemplateType())) {
297+
return false;
298+
}
299+
if (zoneId != null && _vmTemplateStoreDao.findByTemplateZone(template.getId(), zoneId, DataStoreRole.Image) == null) {
300+
s_logger.debug(String.format("Template %s is not present on any image store for the zone ID: %d, its download cannot be skipped", template.getUniqueName(), zoneId));
301+
return false;
302+
}
303+
return true;
304+
}
305+
289306
@Override
290307
public void handleTemplateSync(DataStore store) {
291308
if (store == null) {
@@ -513,7 +530,7 @@ public void handleTemplateSync(DataStore store) {
513530
continue;
514531
}
515532
// if this is private template, skip sync to a new image store
516-
if (!tmplt.isPublicTemplate() && !tmplt.isFeatured() && tmplt.getTemplateType() != TemplateType.SYSTEM) {
533+
if (isSkipTemplateStoreDownload(tmplt, zoneId)) {
517534
s_logger.info("Skip sync downloading private template " + tmplt.getUniqueName() + " to a new image store");
518535
continue;
519536
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.image;
20+
21+
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
22+
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.InjectMocks;
27+
import org.mockito.Mock;
28+
import org.mockito.Mockito;
29+
import org.mockito.Spy;
30+
import org.mockito.junit.MockitoJUnitRunner;
31+
32+
import com.cloud.storage.DataStoreRole;
33+
import com.cloud.storage.Storage;
34+
import com.cloud.storage.VMTemplateVO;
35+
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class TemplateServiceImplTest {
38+
39+
@InjectMocks
40+
@Spy
41+
TemplateServiceImpl templateService;
42+
43+
@Mock
44+
TemplateDataStoreDao templateDataStoreDao;
45+
46+
@Test
47+
public void testIsSkipTemplateStoreDownloadPublicTemplate() {
48+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
49+
Mockito.when(templateVO.isPublicTemplate()).thenReturn(true);
50+
Assert.assertFalse(templateService.isSkipTemplateStoreDownload(templateVO, 1L));
51+
}
52+
53+
@Test
54+
public void testIsSkipTemplateStoreDownloadFeaturedTemplate() {
55+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
56+
Mockito.when(templateVO.isFeatured()).thenReturn(true);
57+
Assert.assertFalse(templateService.isSkipTemplateStoreDownload(templateVO, 1L));
58+
}
59+
60+
@Test
61+
public void testIsSkipTemplateStoreDownloadSystemTemplate() {
62+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
63+
Mockito.when(templateVO.getTemplateType()).thenReturn(Storage.TemplateType.SYSTEM);
64+
Assert.assertFalse(templateService.isSkipTemplateStoreDownload(templateVO, 1L));
65+
}
66+
67+
@Test
68+
public void testIsSkipTemplateStoreDownloadPrivateNoRefTemplate() {
69+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
70+
long id = 1L;
71+
Mockito.when(templateVO.getId()).thenReturn(id);
72+
Mockito.when(templateDataStoreDao.findByTemplateZone(id, id, DataStoreRole.Image)).thenReturn(null);
73+
Assert.assertFalse(templateService.isSkipTemplateStoreDownload(templateVO, id));
74+
}
75+
76+
@Test
77+
public void testIsSkipTemplateStoreDownloadPrivateExistingTemplate() {
78+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
79+
long id = 1L;
80+
Mockito.when(templateVO.getId()).thenReturn(id);
81+
Mockito.when(templateDataStoreDao.findByTemplateZone(id, id, DataStoreRole.Image)).thenReturn(Mockito.mock(TemplateDataStoreVO.class));
82+
Assert.assertTrue(templateService.isSkipTemplateStoreDownload(templateVO, id));
83+
}
84+
}

0 commit comments

Comments
 (0)