Skip to content

Commit ba0503d

Browse files
Nitin Kumar Maharananitin-maharana
authored andcommitted
CLOUDSTACK-8847: ListServiceOfferings is returning incompatible tagged offerings when called with VM id
Fixed the subset and superset issue. Added unit test for the same.
1 parent 3ee53d3 commit ba0503d

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,12 +2908,12 @@ public void checkIfCanUpgrade(final VirtualMachine vmInstance, final ServiceOffe
29082908
newServiceOffering.getCpu() + " cpu(s) at " + newServiceOffering.getSpeed() + " Mhz, and " + newServiceOffering.getRamSize() + " MB of memory");
29092909
}
29102910

2911-
// Check that the service offering being upgraded to has storage tags subset of the current service offering storage tags, since volume is not migrated.
2911+
// Check that the service offering being upgraded to has all the tags of the current service offering.
29122912
final List<String> currentTags = StringUtils.csvTagsToList(currentServiceOffering.getTags());
29132913
final List<String> newTags = StringUtils.csvTagsToList(newServiceOffering.getTags());
2914-
if (!currentTags.containsAll(newTags)) {
2915-
throw new InvalidParameterValueException("Unable to upgrade virtual machine; the new service offering " + " should have tags as subset of " +
2916-
"current service offering tags. Current service offering tags: " + currentTags + "; " + "new service " + "offering tags: " + newTags);
2914+
if (!newTags.containsAll(currentTags)) {
2915+
throw new InvalidParameterValueException("Unable to upgrade virtual machine; the current service offering " + " should have tags as subset of " +
2916+
"the new service offering tags. Current service offering tags: " + currentTags + "; " + "new service " + "offering tags: " + newTags);
29172917
}
29182918
}
29192919

engine/orchestration/test/com/cloud/vm/VirtualMachineManagerImplTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import java.util.Iterator;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.ArrayList;
3233

34+
import com.cloud.service.dao.ServiceOfferingDao;
3335
import junit.framework.Assert;
3436

3537
import org.junit.Before;
@@ -135,6 +137,8 @@ public class VirtualMachineManagerImplTest {
135137
@Mock
136138
VMInstanceDao _vmInstanceDao;
137139
@Mock
140+
ServiceOfferingDao _offeringDao;
141+
@Mock
138142
VMTemplateDao _templateDao;
139143
@Mock
140144
VolumeDao _volsDao;
@@ -149,6 +153,8 @@ public class VirtualMachineManagerImplTest {
149153
@Mock
150154
VMInstanceVO _vmInstance;
151155
@Mock
156+
ServiceOfferingVO _serviceOfferingMock;
157+
@Mock
152158
HostVO _host;
153159
@Mock
154160
VMTemplateVO _templateMock;
@@ -227,6 +233,8 @@ public void setup() {
227233
_vmMgr._uservmDetailsDao = _vmDetailsDao;
228234
_vmMgr._entityMgr = _entityMgr;
229235
_vmMgr._configDepot = _configDepot;
236+
_vmMgr._offeringDao = _offeringDao;
237+
_vmMgr.hostAllocators = new ArrayList<>();
230238

231239
when(_vmMock.getId()).thenReturn(314l);
232240
when(_vmInstance.getId()).thenReturn(1L);
@@ -505,4 +513,24 @@ public void testSendStopWithNullAnswer() throws Exception {
505513

506514
Assert.assertFalse(actual);
507515
}
516+
517+
@Test
518+
public void testCheckIfCanUpgrade() throws Exception {
519+
when(_vmInstance.getState()).thenReturn(State.Stopped);
520+
when(_serviceOfferingMock.isDynamic()).thenReturn(true);
521+
when(_vmInstance.getServiceOfferingId()).thenReturn(1l);
522+
when(_serviceOfferingMock.getId()).thenReturn(2l);
523+
524+
ServiceOfferingVO mockCurrentServiceOffering = mock(ServiceOfferingVO.class);
525+
526+
when(_offeringDao.findByIdIncludingRemoved(anyLong(), anyLong())).thenReturn(mockCurrentServiceOffering);
527+
when(mockCurrentServiceOffering.getUseLocalStorage()).thenReturn(true);
528+
when(_serviceOfferingMock.getUseLocalStorage()).thenReturn(true);
529+
when(mockCurrentServiceOffering.getSystemUse()).thenReturn(true);
530+
when(_serviceOfferingMock.getSystemUse()).thenReturn(true);
531+
when(mockCurrentServiceOffering.getTags()).thenReturn("x,y");
532+
when(_serviceOfferingMock.getTags()).thenReturn("z,x,y");
533+
534+
_vmMgr.checkIfCanUpgrade(_vmInstance, _serviceOfferingMock);
535+
}
508536
}

server/src/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,11 +2620,11 @@ private List<ServiceOfferingJoinVO> filterOfferingsOnCurrentTags(List<ServiceOff
26202620
if(currentVmOffering == null) return offerings;
26212621
List<String> currentTagsList = StringUtils.csvTagsToList(currentVmOffering.getTags());
26222622

2623-
// New offerings should be a subset of existing storage tags. Discard offerings who are not.
2623+
// New service offering should have all the tags of the current service offering.
26242624
List<ServiceOfferingJoinVO> filteredOfferings = new ArrayList<>();
26252625
for (ServiceOfferingJoinVO offering : offerings){
2626-
List<String> tags = StringUtils.csvTagsToList(offering.getTags());
2627-
if(currentTagsList.containsAll(tags)){
2626+
List<String> newTagsList = StringUtils.csvTagsToList(offering.getTags());
2627+
if(newTagsList.containsAll(currentTagsList)){
26282628
filteredOfferings.add(offering);
26292629
}
26302630
}

0 commit comments

Comments
 (0)