Skip to content

Commit 95f3376

Browse files
committed
Fix build
1 parent fc78948 commit 95f3376

5 files changed

Lines changed: 30 additions & 14 deletions

File tree

engine/schema/src/main/java/com/cloud/host/HostVO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ public boolean checkHostServiceOfferingTags(ServiceOffering serviceOffering) {
856856
}
857857

858858
if (BooleanUtils.isTrue(this.getIsTagARule())) {
859-
return GenericRuleHelper.interpretTagAsRule(this.getHostTags().get(0), serviceOffering.getHostTag(), HostTagsDao.hostTagRuleExecutionTimeout.value());
859+
return GenericRuleHelper.interpretTagAsRule(this.getHostTags().get(0), serviceOffering.getHostTag(), HostTagsDao.hostTagRuleExecutionTimeout.value(),
860+
HostTagsDao.hostTagRuleExecutionTimeout.key());
860861
}
861862

862863
if (StringUtils.isEmpty(serviceOffering.getHostTag())) {

server/src/test/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocatorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public void retrieveHostsTestHostsToFilterIsNullAndHaTagNotNullShouldReturnOnlyH
245245
Mockito.doReturn(hostsWithHaTag).when(hostDaoMock).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
246246
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsWithUefiEnabled(Mockito.any(Host.Type.class), Mockito.any(VirtualMachineProfile.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList());
247247
Mockito.doNothing().when(firstFitAllocatorSpy).addHostsBasedOnTagRules(Mockito.anyString(), Mockito.anyList());
248+
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
248249
List<HostVO> resultHosts = firstFitAllocatorSpy.retrieveHosts(virtualMachineProfile, type, emptyList, vmTemplateVO, clusterId, podId, dcId, hostTag, templateTag);
249250

250251
Assert.assertEquals(2, resultHosts.size());
@@ -262,6 +263,7 @@ public void retrieveHostsTestHostsToFilterIsNotNullAndHaTagNotNullShouldReturnOn
262263
Mockito.doReturn(hostsWithHaTag).when(hostDaoMock).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
263264
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsWithUefiEnabled(Mockito.any(Host.Type.class), Mockito.any(VirtualMachineProfile.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList());
264265
Mockito.doNothing().when(firstFitAllocatorSpy).addHostsBasedOnTagRules(Mockito.anyString(), Mockito.anyList());
266+
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
265267
List<HostVO> resultHosts = firstFitAllocatorSpy.retrieveHosts(virtualMachineProfile, type, hostsToFilter, vmTemplateVO, clusterId, podId, dcId, hostTag, templateTag);
266268

267269
Assert.assertEquals(2, resultHosts.size());
@@ -278,6 +280,7 @@ public void retrieveHostsTestHostsToFilterIsNullAndNoHaTagAndNoHostTagShouldRetu
278280
Mockito.doReturn(upAndEnabledHostsWithNoHa).when(resourceManagerMock).listAllUpAndEnabledNonHAHosts(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
279281
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsWithUefiEnabled(Mockito.any(Host.Type.class), Mockito.any(VirtualMachineProfile.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList());
280282
Mockito.doNothing().when(firstFitAllocatorSpy).addHostsBasedOnTagRules(Mockito.nullable(String.class), Mockito.anyList());
283+
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
281284
List<HostVO> resultHosts = firstFitAllocatorSpy.retrieveHosts(virtualMachineProfile, type, emptyList, vmTemplateVO, clusterId, podId, dcId, null, null);
282285

283286
Assert.assertEquals(2, resultHosts.size());
@@ -293,6 +296,7 @@ public void retrieveHostsTestHostsToFilterIsNullAndNoHaTagWithHostTagShouldCallR
293296
Mockito.doNothing().when(firstFitAllocatorSpy).retainHostsMatchingServiceOfferingAndTemplateTags(Mockito.anyList(), Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString());
294297
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsWithUefiEnabled(Mockito.any(Host.Type.class), Mockito.any(VirtualMachineProfile.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList());
295298
Mockito.doNothing().when(firstFitAllocatorSpy).addHostsBasedOnTagRules(Mockito.anyString(), Mockito.anyList());
299+
Mockito.doNothing().when(firstFitAllocatorSpy).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
296300
firstFitAllocatorSpy.retrieveHosts(virtualMachineProfile, type, emptyList, vmTemplateVO, clusterId, podId, dcId, hostTag, templateTag);
297301

298302
Mockito.verify(firstFitAllocatorSpy, Mockito.times(1)).retainHostsMatchingServiceOfferingAndTemplateTags(Mockito.anyList(), Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString());

server/src/test/java/com/cloud/agent/manager/allocator/impl/RandomAllocatorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public void retrieveHostsTestProvidedHostsNullAndNoHostTagAndNoTagRuleShouldOnly
222222
Mockito.doReturn(upAndEnabledHosts).when(resourceManagerMock).listAllUpAndEnabledHosts(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
223223
Mockito.doReturn(hostsWithNoRuleTagsAndHostTags).when(hostDao).listAllHostsThatHaveNoRuleTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
224224
Mockito.doReturn(emptyList).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
225+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
225226
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, null, vmTemplateVO, null, clusterId, podId, zoneId);
226227

227228
Assert.assertEquals(1, availableHosts.size());
@@ -237,6 +238,7 @@ public void retrieveHostsTestProvidedHostsNullAndOnlyHostTagsRulesShouldReturnHo
237238
Mockito.doReturn(upAndEnabledHosts).when(resourceManagerMock).listAllUpAndEnabledHosts(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
238239
Mockito.doReturn(hostsWithNoRuleTagsAndHostTags).when(hostDao).listAllHostsThatHaveNoRuleTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
239240
Mockito.doReturn(hostsMatchingRuleTags).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
241+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
240242
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, null, vmTemplateVO, null, clusterId, podId, zoneId);
241243

242244
Assert.assertEquals(2, availableHosts.size());
@@ -252,6 +254,7 @@ public void retrieveHostsTestProvidedHostsNullProvidedHostTagsNotNullAndNoHostWi
252254
Mockito.doReturn(upAndEnabledHosts).when(resourceManagerMock).listAllUpAndEnabledHosts(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
253255
Mockito.doReturn(hostsWithMatchingTags).when(hostDao).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
254256
Mockito.doReturn(emptyList).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
257+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
255258
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, null, vmTemplateVO, hostTag, clusterId, podId, zoneId);
256259

257260
Assert.assertEquals(1, availableHosts.size());
@@ -267,6 +270,7 @@ public void retrieveHostsTestProvidedHostsNullProvidedHostTagsNotNullAndHostWith
267270
Mockito.doReturn(upAndEnabledHosts).when(resourceManagerMock).listAllUpAndEnabledHosts(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
268271
Mockito.doReturn(hostsWithMatchingTags).when(hostDao).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
269272
Mockito.doReturn(hostsMatchingRuleTags).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
273+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
270274
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, null, vmTemplateVO, hostTag, clusterId, podId, zoneId);
271275

272276
Assert.assertEquals(2, availableHosts.size());
@@ -281,6 +285,7 @@ public void retrieveHostsTestProvidedHostsNotNullAndNoHostTagAndNoTagRuleShouldO
281285

282286
Mockito.doReturn(hostsWithNoRuleTagsAndHostTags).when(hostDao).listAllHostsThatHaveNoRuleTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
283287
Mockito.doReturn(emptyList).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
288+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
284289
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, providedHosts, vmTemplateVO, null, clusterId, podId, zoneId);
285290

286291
Assert.assertEquals(1, availableHosts.size());
@@ -295,6 +300,7 @@ public void retrieveHostsTestProvidedHostsNotNullAndOnlyHostTagsRulesShouldRetur
295300

296301
Mockito.doReturn(hostsWithNoRuleTagsAndHostTags).when(hostDao).listAllHostsThatHaveNoRuleTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
297302
Mockito.doReturn(hostsMatchingRuleTags).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
303+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
298304
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, providedHosts, vmTemplateVO, null, clusterId, podId, zoneId);
299305

300306
Assert.assertEquals(2, availableHosts.size());
@@ -309,6 +315,7 @@ public void retrieveHostsTestProvidedHostsNotNullProvidedHostTagsNotNullAndNoHos
309315

310316
Mockito.doReturn(hostsWithMatchingTags).when(hostDao).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
311317
Mockito.doReturn(emptyList).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
318+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
312319
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, providedHosts, vmTemplateVO, hostTag, clusterId, podId, zoneId);
313320

314321
Assert.assertEquals(1, availableHosts.size());
@@ -323,6 +330,7 @@ public void retrieveHostsTestProvidedHostsNullNotProvidedHostTagsNotNullAndHostW
323330

324331
Mockito.doReturn(hostsWithMatchingTags).when(hostDao).listByHostTag(Mockito.any(Host.Type.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
325332
Mockito.doReturn(hostsMatchingRuleTags).when(hostDao).findHostsWithTagRuleThatMatchComputeOfferingTags(Mockito.nullable(String.class));
333+
Mockito.doNothing().when(randomAllocator).filterHostsBasedOnGuestOsRules(Mockito.any(VMTemplateVO.class), Mockito.anyList());
326334
List<HostVO> availableHosts = randomAllocator.retrieveHosts(type, providedHosts, vmTemplateVO, hostTag, clusterId, podId, zoneId);
327335

328336
Assert.assertEquals(2, availableHosts.size());

utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/GenericRuleHelper.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,38 @@
1616
//under the License.
1717
package org.apache.cloudstack.utils.jsinterpreter;
1818

19+
import com.cloud.utils.StringUtils;
1920
import com.cloud.utils.exception.CloudRuntimeException;
20-
import org.apache.commons.lang3.StringEscapeUtils;
2121
import org.apache.logging.log4j.LogManager;
2222
import org.apache.logging.log4j.Logger;
2323

2424
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.List;
2528

2629
public class GenericRuleHelper {
2730

2831
protected static Logger LOGGER = LogManager.getLogger(GenericRuleHelper.class);
2932

30-
private static final String PARSE_TAGS = "tags = tags ? tags.split(',') : [];";
31-
32-
3333
public static boolean interpretTagAsRule(String rule, String tags, long timeout, String configName) {
34-
String script = PARSE_TAGS + rule;
35-
Boolean scriptReturn = interpretRule(tags, timeout, "tags", script, configName);
34+
List<String> tagsPresetVariable = new ArrayList<>();
35+
if (!StringUtils.isEmpty(tags)) {
36+
tagsPresetVariable.addAll(Arrays.asList(tags.split(",")));
37+
}
38+
39+
Boolean scriptReturn = interpretRule("tags", tagsPresetVariable, timeout, rule, configName);
3640

3741
if (scriptReturn != null) {
3842
return scriptReturn;
3943
}
4044

41-
LOGGER.debug("Result of tag rule [{}] was not a boolean, returning false.", script);
45+
LOGGER.debug("Result of tag rule [{}] was not a boolean, returning false.", rule);
4246
return false;
4347
}
4448

4549
public static boolean interpretGuestOsRule(String rule, String vmGuestOs, long timeout, String configName) {
46-
Boolean scriptReturn = interpretRule(vmGuestOs, timeout, "vmGuestOs", rule, configName);
50+
Boolean scriptReturn = interpretRule("vmGuestOs", vmGuestOs, timeout, rule, configName);
4751

4852
if (scriptReturn != null) {
4953
return scriptReturn;
@@ -53,10 +57,9 @@ public static boolean interpretGuestOsRule(String rule, String vmGuestOs, long t
5357
return false;
5458
}
5559

56-
private static Boolean interpretRule(String rule, long timeout, String variable, String script, String configName) {
57-
rule = String.format("'%s'", StringEscapeUtils.escapeEcmaScript(rule));
60+
private static Boolean interpretRule(String variableName, Object variableValue, long timeout, String script, String configName) {
5861
try (JsInterpreter jsInterpreter = new JsInterpreter(timeout, configName)) {
59-
jsInterpreter.injectVariable(variable, rule);
62+
jsInterpreter.injectVariable(variableName, variableValue);
6063
Object scriptReturn = jsInterpreter.executeScript(script);
6164
if (scriptReturn instanceof Boolean) {
6265
return (Boolean) scriptReturn;

utils/src/test/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void setScriptEngineDisablingJavaLanguageTest() {
161161

162162
@Test
163163
public void executeScriptTestValidScriptShouldPassWithMixedVariables() {
164-
try (JsInterpreter jsInterpreter = new JsInterpreter(1000)) {
164+
try (JsInterpreter jsInterpreter = new JsInterpreter(1000, "timeout.configuration")) {
165165
jsInterpreter.injectVariable("x", 10);
166166
jsInterpreter.injectVariable("y", "hello");
167167
jsInterpreter.injectVariable("z", true);
@@ -174,7 +174,7 @@ public void executeScriptTestValidScriptShouldPassWithMixedVariables() {
174174
}
175175

176176
private void runMaliciousScriptFileTest(String script, String filename) {
177-
try (JsInterpreter jsInterpreter = new JsInterpreter(1000)) {
177+
try (JsInterpreter jsInterpreter = new JsInterpreter(1000, "timeout.configuration")) {
178178
jsInterpreter.executeScript(script);
179179
} catch (CloudRuntimeException ex) {
180180
Assert.assertTrue(ex.getMessage().contains("Unable to execute script"));

0 commit comments

Comments
 (0)