Skip to content

Commit e0d869d

Browse files
Update the displayText of XenServer ISO when it already exist in the DB
Besides updating the ISO display text, I also created unit test cases for 'createXenServerToolsIsoEntryInDatabase' and 'getActualIsoTemplate' methods.
1 parent 5a5b135 commit e0d869d

File tree

4 files changed

+187
-22
lines changed

4 files changed

+187
-22
lines changed

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/discoverer/XcpServerDiscoverer.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
121121
@Inject
122122
private HostPodDao _podDao;
123123

124+
private static String XENSERVER_ISO_NAME = "xs-tools.iso";
125+
private static String XENSERVER_ISO_DISPLAY_TEXT = "XenServer Tools Installer ISO (xen-pv-drv-iso)";
126+
124127
protected XcpServerDiscoverer() {
125128
}
126129

@@ -486,7 +489,7 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
486489

487490
_agentMgr.registerForHostEvents(this, true, false, true);
488491

489-
createXsToolsISO();
492+
createXenServerToolsIsoEntryInDatabase();
490493
_resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
491494
return true;
492495
}
@@ -528,20 +531,24 @@ public boolean processCommands(long agentId, long seq, Command[] commands) {
528531
return false;
529532
}
530533

531-
private void createXsToolsISO() {
532-
String isoName = "xs-tools.iso";
533-
VMTemplateVO tmplt = _tmpltDao.findByTemplateName(isoName);
534-
Long id;
534+
/**
535+
* Create the XenServer tools ISO entry in the database.
536+
* If there is already an entry with 'isoName' equals to {@value #XENSERVER_ISO_NAME} , we update its 'displayText' to {@value #XENSERVER_ISO_DISPLAY_TEXT}.
537+
* Otherwise, we create a new entry.
538+
*/
539+
protected void createXenServerToolsIsoEntryInDatabase() {
540+
VMTemplateVO tmplt = _tmpltDao.findByTemplateName(XENSERVER_ISO_NAME);
535541
if (tmplt == null) {
536-
id = _tmpltDao.getNextInSequence(Long.class, "id");
542+
long id = _tmpltDao.getNextInSequence(Long.class, "id");
537543
VMTemplateVO template =
538-
VMTemplateVO.createPreHostIso(id, isoName, isoName, ImageFormat.ISO, true, true, TemplateType.PERHOST, null, null, true, 64, Account.ACCOUNT_ID_SYSTEM,
539-
null, "XenServer Tools Installer ISO (xen-pv-drv-iso)", false, 1, false, HypervisorType.XenServer);
544+
VMTemplateVO.createPreHostIso(id, XENSERVER_ISO_NAME, XENSERVER_ISO_NAME, ImageFormat.ISO, true, true, TemplateType.PERHOST, null, null, true, 64, Account.ACCOUNT_ID_SYSTEM,
545+
null, XENSERVER_ISO_DISPLAY_TEXT, false, 1, false, HypervisorType.XenServer);
540546
_tmpltDao.persist(template);
541547
} else {
542-
id = tmplt.getId();
548+
long id = tmplt.getId();
543549
tmplt.setTemplateType(TemplateType.PERHOST);
544550
tmplt.setUrl(null);
551+
tmplt.setDisplayText(XENSERVER_ISO_DISPLAY_TEXT);
545552
_tmpltDao.update(id, tmplt);
546553
}
547554
}

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ public String toString() {
204204
static final Random Rand = new Random(System.currentTimeMillis());
205205
private static final Logger s_logger = Logger.getLogger(CitrixResourceBase.class);
206206
protected static final HashMap<VmPowerState, PowerState> s_powerStatesTable;
207+
208+
private static final String XENSERVER_70PLUS_GUEST_TOOLS_NAME = "guest-tools.iso";
209+
private static final String XENSERVER_BEFORE_70_GUEST_TOOLS_NAME = "xs-tools.iso";
207210

208211
static {
209212
s_powerStatesTable = new HashMap<VmPowerState, PowerState>();
@@ -2593,7 +2596,7 @@ public VDI getIsoVDIByURL(final Connection conn, final String vmName, final Stri
25932596
String mountpoint = null;
25942597
if (isoURL.startsWith("xs-tools")) {
25952598
try {
2596-
final String actualIsoURL = actualIsoTemplate(conn);
2599+
final String actualIsoURL = getActualIsoTemplate(conn);
25972600
final Set<VDI> vdis = VDI.getByNameLabel(conn, actualIsoURL);
25982601
if (vdis.isEmpty()) {
25992602
throw new CloudRuntimeException("Could not find ISO with URL: " + actualIsoURL);
@@ -2632,20 +2635,27 @@ public VDI getIsoVDIByURL(final Connection conn, final String vmName, final Stri
26322635
}
26332636
}
26342637

2635-
private String actualIsoTemplate(final Connection conn) throws BadServerResponse, XenAPIException, XmlRpcException {
2636-
final Host host = Host.getByUuid(conn, _host.getUuid());
2637-
final Host.Record record = host.getRecord(conn);
2638-
final String xenBrand = record.softwareVersion.get("product_brand");
2639-
final String xenVersion = record.softwareVersion.get("product_version");
2640-
final String[] items = xenVersion.split("\\.");
2638+
/**
2639+
* Retrieve the actual ISO 'name-label' to be used.
2640+
* We based our decision on XenServer version.
2641+
* <ul>
2642+
* <li> for XenServer 7.0+, we use {@value #XENSERVER_70PLUS_GUEST_TOOLS_NAME};
2643+
* <li> for versions before 7.0, we use {@value #XENSERVER_BEFORE_70_GUEST_TOOLS_NAME}.
2644+
* </ul>
2645+
*
2646+
* For XCP we always use {@value #XENSERVER_BEFORE_70_GUEST_TOOLS_NAME}.
2647+
*/
2648+
protected String getActualIsoTemplate(Connection conn) throws XenAPIException, XmlRpcException {
2649+
Host host = Host.getByUuid(conn, _host.getUuid());
2650+
Host.Record record = host.getRecord(conn);
2651+
String xenBrand = record.softwareVersion.get("product_brand");
2652+
String xenVersion = record.softwareVersion.get("product_version");
2653+
String[] items = xenVersion.split("\\.");
26412654

2642-
// guest-tools.iso for XenServer version 7.0+
26432655
if (xenBrand.equals("XenServer") && Integer.parseInt(items[0]) >= 7) {
2644-
return "guest-tools.iso";
2656+
return XENSERVER_70PLUS_GUEST_TOOLS_NAME;
26452657
}
2646-
2647-
// xs-tools.iso for older XenServer versions
2648-
return "xs-tools.iso";
2658+
return XENSERVER_BEFORE_70_GUEST_TOOLS_NAME;
26492659
}
26502660

26512661
public String getLabel() {
@@ -3900,7 +3910,7 @@ protected VDI mount(final Connection conn, final String vmName, final DiskTO vol
39003910
final String templateName = iso.getName();
39013911
if (templateName.startsWith("xs-tools")) {
39023912
try {
3903-
final String actualTemplateName = actualIsoTemplate(conn);
3913+
final String actualTemplateName = getActualIsoTemplate(conn);
39043914
final Set<VDI> vdis = VDI.getByNameLabel(conn, actualTemplateName);
39053915
if (vdis.isEmpty()) {
39063916
throw new CloudRuntimeException("Could not find ISO with URL: " + actualTemplateName);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
18+
package com.cloud.hypervisor.xenserver.discoverer;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.InOrder;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.Mockito;
26+
import org.mockito.Spy;
27+
import org.mockito.runners.MockitoJUnitRunner;
28+
29+
import com.cloud.storage.VMTemplateVO;
30+
import com.cloud.storage.Storage.TemplateType;
31+
import com.cloud.storage.dao.VMTemplateDao;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class XcpServerDiscovererTest {
35+
36+
@Spy
37+
@InjectMocks
38+
private XcpServerDiscoverer xcpServerDiscoverer;
39+
40+
@Mock
41+
private VMTemplateDao vmTemplateDao;
42+
43+
@Test
44+
public void createXenServerToolsIsoEntryInDatabaseTestNoEntryFound() {
45+
Mockito.when(vmTemplateDao.findByTemplateName("xs-tools.iso")).thenReturn(null);
46+
Mockito.when(vmTemplateDao.getNextInSequence(Long.class, "id")).thenReturn(1L);
47+
48+
xcpServerDiscoverer.createXenServerToolsIsoEntryInDatabase();
49+
50+
InOrder inOrder = Mockito.inOrder(vmTemplateDao);
51+
inOrder.verify(vmTemplateDao).findByTemplateName("xs-tools.iso");
52+
inOrder.verify(vmTemplateDao).getNextInSequence(Long.class, "id");
53+
inOrder.verify(vmTemplateDao).persist(Mockito.any(VMTemplateVO.class));
54+
}
55+
56+
@Test
57+
public void createXenServerToolsIsoEntryInDatabaseTestEntryAlreadyExist() {
58+
VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class);
59+
Mockito.when(vmTemplateDao.findByTemplateName("xs-tools.iso")).thenReturn(vmTemplateVOMock);
60+
Mockito.when(vmTemplateVOMock.getId()).thenReturn(1L);
61+
62+
xcpServerDiscoverer.createXenServerToolsIsoEntryInDatabase();
63+
64+
InOrder inOrder = Mockito.inOrder(vmTemplateDao, vmTemplateVOMock);
65+
inOrder.verify(vmTemplateDao).findByTemplateName("xs-tools.iso");
66+
inOrder.verify(vmTemplateDao, Mockito.times(0)).getNextInSequence(Long.class, "id");
67+
inOrder.verify(vmTemplateVOMock).setTemplateType(TemplateType.PERHOST);
68+
inOrder.verify(vmTemplateVOMock).setUrl(null);
69+
inOrder.verify(vmTemplateVOMock).setDisplayText("XenServer Tools Installer ISO (xen-pv-drv-iso)");
70+
inOrder.verify(vmTemplateDao).update(1L, vmTemplateVOMock);
71+
}
72+
}

plugins/hypervisors/xenserver/src/test/java/com/cloud/hypervisor/xenserver/resource/CitrixResourceBaseTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,60 @@
1616
package com.cloud.hypervisor.xenserver.resource;
1717

1818
import java.io.File;
19+
import java.util.HashMap;
1920
import java.util.List;
2021

22+
import org.apache.xmlrpc.XmlRpcException;
2123
import org.junit.Assert;
24+
import org.junit.Before;
2225
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.Mock;
2328
import org.mockito.Mockito;
29+
import org.mockito.Spy;
2430
import org.powermock.api.mockito.PowerMockito;
31+
import org.powermock.core.classloader.annotations.PrepareForTest;
32+
import org.powermock.modules.junit4.PowerMockRunner;
2533

2634
import com.cloud.utils.script.Script;
35+
import com.xensource.xenapi.Connection;
36+
import com.xensource.xenapi.Host;
37+
import com.xensource.xenapi.Host.Record;
38+
import com.xensource.xenapi.Types.XenAPIException;
2739

40+
@RunWith(PowerMockRunner.class)
41+
@PrepareForTest(Host.class)
2842
public class CitrixResourceBaseTest {
2943

44+
@Spy
3045
protected CitrixResourceBase citrixResourceBase = new CitrixResourceBase() {
3146
@Override
3247
protected String getPatchFilePath() {
3348
return null;
3449
}
3550
};
3651

52+
@Mock
53+
private Connection connectionMock;
54+
@Mock
55+
private Host hostMock;
56+
@Mock
57+
private Record hostRecordMock;
58+
59+
private String hostUuidMock = "hostUuidMock";
60+
61+
@Before
62+
public void beforeTest() throws XenAPIException, XmlRpcException {
63+
citrixResourceBase._host.setUuid(hostUuidMock);
64+
65+
PowerMockito.mockStatic(Host.class);
66+
PowerMockito.when(Host.getByUuid(connectionMock, hostUuidMock)).thenReturn(hostMock);
67+
68+
hostRecordMock.softwareVersion = new HashMap<>();
69+
Mockito.when(hostMock.getRecord(connectionMock)).thenReturn(hostRecordMock);
70+
71+
}
72+
3773
public void testGetPathFilesExeption() {
3874
String patch = citrixResourceBase.getPatchFilePath();
3975

@@ -92,4 +128,44 @@ public void testGetGuestOsTypeOther() {
92128
String guestOsType = citrixResourceBase.getGuestOsType(platformEmulator);
93129
Assert.assertEquals(platformEmulator, guestOsType);
94130
}
131+
132+
@Test
133+
public void actualIsoTemplateTestXcpHots() throws XenAPIException, XmlRpcException {
134+
hostRecordMock.softwareVersion.put("product_brand", "XCP");
135+
hostRecordMock.softwareVersion.put("product_version", "1.0");
136+
137+
String returnedIsoTemplateName = citrixResourceBase.getActualIsoTemplate(connectionMock);
138+
139+
Assert.assertEquals("xs-tools.iso", returnedIsoTemplateName);
140+
}
141+
142+
@Test
143+
public void actualIsoTemplateTestXenServerBefore70() throws XenAPIException, XmlRpcException {
144+
hostRecordMock.softwareVersion.put("product_brand", "XenServer");
145+
hostRecordMock.softwareVersion.put("product_version", "6.0");
146+
147+
String returnedIsoTemplateName = citrixResourceBase.getActualIsoTemplate(connectionMock);
148+
149+
Assert.assertEquals("xs-tools.iso", returnedIsoTemplateName);
150+
}
151+
152+
@Test
153+
public void actualIsoTemplateTestXenServer70() throws XenAPIException, XmlRpcException {
154+
hostRecordMock.softwareVersion.put("product_brand", "XenServer");
155+
hostRecordMock.softwareVersion.put("product_version", "7.0");
156+
157+
String returnedIsoTemplateName = citrixResourceBase.getActualIsoTemplate(connectionMock);
158+
159+
Assert.assertEquals("guest-tools.iso", returnedIsoTemplateName);
160+
}
161+
162+
@Test
163+
public void actualIsoTemplateTestXenServer71() throws XenAPIException, XmlRpcException {
164+
hostRecordMock.softwareVersion.put("product_brand", "XenServer");
165+
hostRecordMock.softwareVersion.put("product_version", "7.1");
166+
167+
String returnedIsoTemplateName = citrixResourceBase.getActualIsoTemplate(connectionMock);
168+
169+
Assert.assertEquals("guest-tools.iso", returnedIsoTemplateName);
170+
}
95171
}

0 commit comments

Comments
 (0)