Skip to content

Commit

Permalink
Merge pull request #794 from YangJiao0817/fix-vc-not-metric
Browse files Browse the repository at this point in the history
Fix vc-worker cannot get metricid and cause error
  • Loading branch information
YangJiao0817 authored May 11, 2021
2 parents 5c76f6e + 27c96b1 commit 85d6e88
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,8 @@ private List<MetricId> getPerformenceMetricsIds(PerformanceManager performanceMa
performanceManager.queryAvailableMetric(hostRef, null, null, new Integer(20));

List<MetricId> metricIdList = new ArrayList<MetricId>();
if (queryAvailableMetric.length > 0) {
for (int i = 0; i < queryAvailableMetric.length; i++) {
MetricId metricId = queryAvailableMetric[i];
if (queryAvailableMetric != null && queryAvailableMetric.length > 0) {
for (MetricId metricId : queryAvailableMetric) {
int counterId = metricId.getCounterId();
String instanceId = metricId.getInstance();
if (counters.containsKey(new Integer(counterId)) && (instanceId == null || instanceId.isEmpty())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,52 @@ public void testFeedClusterMetaData() {
TestCase.assertEquals(needUpdate, true);
}

@Test
public void testFeedHostUsageDataNotAvailableMetricId() {
PerformanceManager performanceManager = Mockito.mock(PerformanceManager.class);
when(vsphereClient.getPerformanceManager()).thenReturn(performanceManager);

CounterInfo[] counterInfos = new CounterInfo[4];
counterInfos[0] = Mockito.mock(CounterInfo.class);
when(counterInfos[0].getKey()).thenReturn(1);
ElementDescription edGroup = Mockito.mock(ElementDescription.class);
when(counterInfos[0].getGroupInfo()).thenReturn(edGroup);
when(edGroup.getKey()).thenReturn(VCConstants.HOST_CPU_GROUP);
ElementDescription edName = Mockito.mock(ElementDescription.class);
when(counterInfos[0].getNameInfo()).thenReturn(edName);
when(edName.getKey()).thenReturn(VCConstants.HOST_METRIC_USAGE);
counterInfos[1] = Mockito.mock(CounterInfo.class);
when(counterInfos[1].getKey()).thenReturn(2);
ElementDescription edGroup1 = Mockito.mock(ElementDescription.class);
when(counterInfos[1].getGroupInfo()).thenReturn(edGroup1);
when(edGroup1.getKey()).thenReturn(VCConstants.HOST_MEMORY_GROUP);
ElementDescription edName1 = Mockito.mock(ElementDescription.class);
when(counterInfos[1].getNameInfo()).thenReturn(edName1);
when(edName1.getKey()).thenReturn(VCConstants.HOST_METRIC_USAGE);
counterInfos[2] = Mockito.mock(CounterInfo.class);
when(counterInfos[2].getKey()).thenReturn(3);
ElementDescription edGroup2 = Mockito.mock(ElementDescription.class);
when(counterInfos[2].getGroupInfo()).thenReturn(edGroup2);
when(edGroup2.getKey()).thenReturn(VCConstants.HOST_POWER_GROUP);
ElementDescription edName2 = Mockito.mock(ElementDescription.class);
when(counterInfos[2].getNameInfo()).thenReturn(edName2);
when(edName2.getKey()).thenReturn(VCConstants.HOST_METRIC_POWER_ENERGY);
counterInfos[3] = Mockito.mock(CounterInfo.class);
when(counterInfos[3].getKey()).thenReturn(4);
ElementDescription edGroup3 = Mockito.mock(ElementDescription.class);
when(counterInfos[3].getGroupInfo()).thenReturn(edGroup3);
when(edGroup3.getKey()).thenReturn(VCConstants.HOST_POWER_GROUP);
ElementDescription edName3 = Mockito.mock(ElementDescription.class);
when(counterInfos[3].getNameInfo()).thenReturn(edName3);
when(edName3.getKey()).thenReturn(VCConstants.HOST_METRIC_POWER_POWER);

when(performanceManager.getPerfCounter()).thenReturn(counterInfos);
HostSystem host = Mockito.mock(HostSystem.class);
ManagedObjectReference managedObjectReference = host._getRef();
when(performanceManager.queryAvailableMetric(managedObjectReference, null, null, new Integer(20))).thenReturn(null);
service.feedHostUsageData(vsphereClient, "avbaebhqw9", managedObjectReference);
}

@Test
public void testQueryHostMetrics() throws Exception {

Expand Down

0 comments on commit 85d6e88

Please sign in to comment.