-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathStatsCollectorTest.java
More file actions
815 lines (687 loc) · 36.3 KB
/
StatsCollectorTest.java
File metadata and controls
815 lines (687 loc) · 36.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.server;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import com.cloud.utils.DateUtil;
import com.google.gson.JsonSyntaxException;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.commons.collections.CollectionUtils;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.BatchPoints.Builder;
import org.influxdb.dto.Point;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
import org.springframework.test.util.ReflectionTestUtils;
import com.cloud.agent.api.GetStorageStatsAnswer;
import com.cloud.agent.api.GetStorageStatsCommand;
import com.cloud.agent.api.VmDiskStatsEntry;
import com.cloud.agent.api.VmStatsEntry;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.VlanDao;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.server.StatsCollector.ExternalStatsProtocol;
import com.cloud.storage.StorageStats;
import com.cloud.storage.VolumeStatsVO;
import com.cloud.storage.dao.VolumeStatsDao;
import com.cloud.user.VmDiskStatisticsVO;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VmStats;
import com.cloud.vm.VmStatsVO;
import com.cloud.vm.dao.VmStatsDaoImpl;
import com.google.gson.Gson;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
@RunWith(DataProviderRunner.class)
public class StatsCollectorTest {
@InjectMocks
private StatsCollector statsCollector = Mockito.spy(new StatsCollector());
private static final int GRAPHITE_DEFAULT_PORT = 2003;
private static final int INFLUXDB_DEFAULT_PORT = 8086;
private static final String HOST_ADDRESS = "192.168.16.10";
private static final String URL = String.format("http://%s:%s/", HOST_ADDRESS, INFLUXDB_DEFAULT_PORT);
private static final String DEFAULT_DATABASE_NAME = "cloudstack";
@Mock
VmStatsDaoImpl vmStatsDaoMock;
@Mock
VmStatsEntry statsForCurrentIterationMock;
@Captor
ArgumentCaptor<VmStatsVO> vmStatsVOCaptor = ArgumentCaptor.forClass(VmStatsVO.class);
@Captor
ArgumentCaptor<Boolean> booleanCaptor = ArgumentCaptor.forClass(Boolean.class);
@Mock
VmStatsVO vmStatsVoMock1 = Mockito.mock(VmStatsVO.class);
@Mock
VmStatsVO vmStatsVoMock2 = Mockito.mock(VmStatsVO.class);
@Mock
VmStatsEntry vmStatsEntryMock = Mockito.mock(VmStatsEntry.class);
@Mock
VolumeStatsDao volumeStatsDao = Mockito.mock(VolumeStatsDao.class);
@Mock
private StoragePoolVO mockPool;
@Mock
private RoutedIpv4Manager routedIpv4Manager;
@Mock
private NetworkDao networkDao;
@Mock
private VlanDao vlanDao;
private static Gson gson = new Gson();
private Gson msStatsGson;
private MockedStatic<InfluxDBFactory> influxDBFactoryMocked;
private AutoCloseable closeable;
@Before
public void setUp() throws Exception {
closeable = MockitoAnnotations.openMocks(this);
statsCollector.vmStatsDao = vmStatsDaoMock;
statsCollector.volumeStatsDao = volumeStatsDao;
Field msStatsGsonField = StatsCollector.class.getDeclaredField("msStatsGson");
msStatsGsonField.setAccessible(true);
msStatsGson = (Gson) msStatsGsonField.get(null);
}
@After
public void tearDown() throws Exception {
if (influxDBFactoryMocked != null) {
influxDBFactoryMocked.close();
}
closeable.close();
}
@Test
public void createInfluxDbConnectionTest() {
configureAndTestCreateInfluxDbConnection(true);
}
@Test(expected = CloudRuntimeException.class)
public void createInfluxDbConnectionTestExpectException() {
configureAndTestCreateInfluxDbConnection(false);
}
private void configureAndTestCreateInfluxDbConnection(boolean databaseExists) {
statsCollector.externalStatsHost = HOST_ADDRESS;
statsCollector.externalStatsPort = INFLUXDB_DEFAULT_PORT;
InfluxDB influxDbConnection = Mockito.mock(InfluxDB.class);
when(influxDbConnection.databaseExists(DEFAULT_DATABASE_NAME)).thenReturn(databaseExists);
influxDBFactoryMocked = Mockito.mockStatic(InfluxDBFactory.class);
influxDBFactoryMocked.when(() -> InfluxDBFactory.connect(URL)).thenReturn(influxDbConnection);
InfluxDB returnedConnection = statsCollector.createInfluxDbConnection();
Assert.assertEquals(influxDbConnection, returnedConnection);
}
@Test
public void writeBatchesTest() {
InfluxDB influxDbConnection = Mockito.mock(InfluxDB.class);
Mockito.doNothing().when(influxDbConnection).write(Mockito.any(Point.class));
Builder builder = Mockito.mock(Builder.class);
BatchPoints batchPoints = Mockito.mock(BatchPoints.class);
try (MockedStatic<BatchPoints> ignored = Mockito.mockStatic(BatchPoints.class)) {
Mockito.when(BatchPoints.database(DEFAULT_DATABASE_NAME)).thenReturn(builder);
when(builder.build()).thenReturn(batchPoints);
Map<String, String> tagsToAdd = new HashMap<>();
tagsToAdd.put("hostId", "1");
Map<String, Object> fieldsToAdd = new HashMap<>();
fieldsToAdd.put("total_memory_kbs", 10000000);
Point point = Point.measurement("measure").tag(tagsToAdd).time(System.currentTimeMillis(), TimeUnit.MILLISECONDS).fields(fieldsToAdd).build();
List<Point> points = new ArrayList<>();
points.add(point);
when(batchPoints.point(point)).thenReturn(batchPoints);
statsCollector.writeBatches(influxDbConnection, DEFAULT_DATABASE_NAME, points);
Mockito.verify(influxDbConnection).write(batchPoints);
}
}
@Test
public void configureExternalStatsPortTestGraphitePort() throws URISyntaxException {
URI uri = new URI(HOST_ADDRESS);
statsCollector.externalStatsType = ExternalStatsProtocol.GRAPHITE;
int port = statsCollector.retrieveExternalStatsPortFromUri(uri);
Assert.assertEquals(GRAPHITE_DEFAULT_PORT, port);
}
@Test
public void configureExternalStatsPortTestInfluxdbPort() throws URISyntaxException {
URI uri = new URI(HOST_ADDRESS);
statsCollector.externalStatsType = ExternalStatsProtocol.INFLUXDB;
int port = statsCollector.retrieveExternalStatsPortFromUri(uri);
Assert.assertEquals(INFLUXDB_DEFAULT_PORT, port);
}
@Test(expected = URISyntaxException.class)
public void configureExternalStatsPortTestExpectException() throws URISyntaxException {
statsCollector.externalStatsType = ExternalStatsProtocol.NONE;
URI uri = new URI(HOST_ADDRESS);
statsCollector.retrieveExternalStatsPortFromUri(uri);
}
@Test
public void configureExternalStatsPortTestInfluxDbCustomizedPort() throws URISyntaxException {
statsCollector.externalStatsType = ExternalStatsProtocol.INFLUXDB;
URI uri = new URI("test://" + HOST_ADDRESS + ":1234");
int port = statsCollector.retrieveExternalStatsPortFromUri(uri);
Assert.assertEquals(1234, port);
}
@Test
public void configureDatabaseNameTestDefaultDbName() throws URISyntaxException {
URI uri = new URI(URL);
String dbName = statsCollector.configureDatabaseName(uri);
Assert.assertEquals(DEFAULT_DATABASE_NAME, dbName);
}
@Test
public void configureDatabaseNameTestCustomDbName() throws URISyntaxException {
String configuredDbName = "dbName";
URI uri = new URI(URL + configuredDbName);
String dbName = statsCollector.configureDatabaseName(uri);
Assert.assertEquals(configuredDbName, dbName);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestNull() {
VmDiskStatisticsVO currentVmDiskStatisticsVO = new VmDiskStatisticsVO(1l, 1l, 1l, 1l);
boolean result = statsCollector.isCurrentVmDiskStatsDifferentFromPrevious(null, currentVmDiskStatisticsVO);
Assert.assertTrue(result);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestDifferentIoWrite() {
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(123l, 123l, 123l, 12l, true);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestDifferentIoRead() {
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(123l, 123l, 12l, 123l, true);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestDifferentBytesRead() {
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(12l, 123l, 123l, 123l, true);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestDifferentBytesWrite() {
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(123l, 12l, 123l, 123l, true);
}
@Test
public void isCurrentVmDiskStatsDifferentFromPreviousTestAllEqual() {
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(123l, 123l, 123l, 123l, false);
}
private void configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(long bytesRead, long bytesWrite, long ioRead,
long ioWrite, boolean expectedResult) {
VmDiskStatisticsVO previousVmDiskStatisticsVO = new VmDiskStatisticsVO(1l, 1l, 1l, 1l);
previousVmDiskStatisticsVO.setCurrentBytesRead(123l);
previousVmDiskStatisticsVO.setCurrentBytesWrite(123l);
previousVmDiskStatisticsVO.setCurrentIORead(123l);
previousVmDiskStatisticsVO.setCurrentIOWrite(123l);
VmDiskStatisticsVO currentVmDiskStatisticsVO = new VmDiskStatisticsVO(1l, 1l, 1l, 1l);
currentVmDiskStatisticsVO.setCurrentBytesRead(bytesRead);
currentVmDiskStatisticsVO.setCurrentBytesWrite(bytesWrite);
currentVmDiskStatisticsVO.setCurrentIORead(ioRead);
currentVmDiskStatisticsVO.setCurrentIOWrite(ioWrite);
boolean result = statsCollector.isCurrentVmDiskStatsDifferentFromPrevious(previousVmDiskStatisticsVO, currentVmDiskStatisticsVO);
Assert.assertEquals(expectedResult, result);
}
@Test
@DataProvider({
"0,0,0,0,true", "1,0,0,0,false", "0,1,0,0,false", "0,0,1,0,false",
"0,0,0,1,false", "1,0,0,1,false", "1,0,1,0,false", "1,1,0,0,false",
"0,1,1,0,false", "0,1,0,1,false", "0,0,1,1,false", "0,1,1,1,false",
"1,1,0,1,false", "1,0,1,1,false", "1,1,1,0,false", "1,1,1,1,false",
})
public void configureAndTestCheckIfDiskStatsAreZero(long bytesRead, long bytesWrite, long ioRead, long ioWrite,
boolean expected) {
VmDiskStatsEntry vmDiskStatsEntry = new VmDiskStatsEntry();
vmDiskStatsEntry.setBytesRead(bytesRead);
vmDiskStatsEntry.setBytesWrite(bytesWrite);
vmDiskStatsEntry.setIORead(ioRead);
vmDiskStatsEntry.setIOWrite(ioWrite);
boolean result = statsCollector.areAllDiskStatsZero(vmDiskStatsEntry);
Assert.assertEquals(expected, result);
}
private void setVmStatsIncrementMetrics(String value) {
StatsCollector.vmStatsIncrementMetrics = new ConfigKey<Boolean>("Advanced", Boolean.class, "vm.stats.increment.metrics", value,
"When set to 'true', VM metrics(NetworkReadKBs, NetworkWriteKBs, DiskWriteKBs, DiskReadKBs, DiskReadIOs and DiskWriteIOs) that are collected from the hypervisor are summed before being returned. "
+ "On the other hand, when set to 'false', the VM metrics API will just display the latest metrics collected.", true);
}
private void setVmStatsMaxRetentionTimeValue(String value) {
StatsCollector.vmStatsMaxRetentionTime = new ConfigKey<Integer>("Advanced", Integer.class, "vm.stats.max.retention.time", value,
"The maximum time (in minutes) for keeping VM stats records in the database. The VM stats cleanup process will be disabled if this is set to 0 or less than 0.", true);
}
@Test
public void cleanUpVirtualMachineStatsTestIsDisabled() {
setVmStatsMaxRetentionTimeValue("0");
statsCollector.cleanUpVirtualMachineStats();
Mockito.verify(vmStatsDaoMock, Mockito.never()).removeAllByTimestampLessThan(Mockito.any(), Mockito.anyLong());
}
@Test
public void cleanUpVirtualMachineStatsTestIsEnabled() {
setVmStatsMaxRetentionTimeValue("1");
statsCollector.cleanUpVirtualMachineStats();
Mockito.verify(vmStatsDaoMock).removeAllByTimestampLessThan(Mockito.any(), Mockito.anyLong());
}
@Test
public void persistVirtualMachineStatsTestPersistsSuccessfully() {
statsCollector.msId = 1L;
Date timestamp = new Date();
VmStatsEntry statsForCurrentIteration = new VmStatsEntry(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "vm");
Mockito.doReturn(new VmStatsVO()).when(vmStatsDaoMock).persist(Mockito.any());
String expectedVmStatsStr = "{\"vmId\":2,\"cpuUtilization\":6.0,\"networkReadKBs\":7.0,\"networkWriteKBs\":8.0,\"diskReadIOs\":12.0,\"diskWriteIOs\":13.0,\"diskReadKBs\":10.0"
+ ",\"diskWriteKBs\":11.0,\"memoryKBs\":3.0,\"intFreeMemoryKBs\":4.0,\"targetMemoryKBs\":5.0,\"numCPUs\":9,\"entityType\":\"vm\"}";
statsCollector.persistVirtualMachineStats(statsForCurrentIteration, timestamp);
Mockito.verify(vmStatsDaoMock).persist(vmStatsVOCaptor.capture());
VmStatsVO actual = vmStatsVOCaptor.getAllValues().get(0);
Assert.assertEquals(Long.valueOf(2L), actual.getVmId());
Assert.assertEquals(Long.valueOf(1L), actual.getMgmtServerId());
Assert.assertEquals(convertJsonToOrderedMap(expectedVmStatsStr), convertJsonToOrderedMap(actual.getVmStatsData()));
Assert.assertEquals(timestamp, actual.getTimestamp());
}
@Test
public void getVmStatsTestWithAccumulateNotNull() {
Mockito.doReturn(Arrays.asList(vmStatsVoMock1)).when(vmStatsDaoMock).findByVmIdOrderByTimestampDesc(Mockito.anyLong());
Mockito.doReturn(vmStatsEntryMock).when(statsCollector).getLatestOrAccumulatedVmMetricsStats(Mockito.anyList(), Mockito.anyBoolean());
VmStats result = statsCollector.getVmStats(1L, false);
Mockito.verify(statsCollector).getLatestOrAccumulatedVmMetricsStats(Mockito.anyList(), booleanCaptor.capture());
boolean actualArg = booleanCaptor.getValue().booleanValue();
Assert.assertEquals(false, actualArg);
Assert.assertEquals(vmStatsEntryMock, result);
}
@Test
public void getVmStatsTestWithNullAccumulate() {
setVmStatsIncrementMetrics("true");
Mockito.doReturn(Arrays.asList(vmStatsVoMock1)).when(vmStatsDaoMock).findByVmIdOrderByTimestampDesc(Mockito.anyLong());
Mockito.doReturn(vmStatsEntryMock).when(statsCollector).getLatestOrAccumulatedVmMetricsStats(Mockito.anyList(), Mockito.anyBoolean());
VmStats result = statsCollector.getVmStats(1L, null);
Mockito.verify(statsCollector).getLatestOrAccumulatedVmMetricsStats(Mockito.anyList(), booleanCaptor.capture());
boolean actualArg = booleanCaptor.getValue().booleanValue();
Assert.assertEquals(true, actualArg);
Assert.assertEquals(vmStatsEntryMock, result);
}
@Test
public void getLatestOrAccumulatedVmMetricsStatsTestAccumulate() {
Mockito.doReturn(null).when(statsCollector).accumulateVmMetricsStats(Mockito.anyList());
statsCollector.getLatestOrAccumulatedVmMetricsStats(Arrays.asList(vmStatsVoMock1), true);
Mockito.verify(statsCollector).accumulateVmMetricsStats(Mockito.anyList());
}
@Test
public void getLatestOrAccumulatedVmMetricsStatsTestLatest() {
statsCollector.getLatestOrAccumulatedVmMetricsStats(Arrays.asList(vmStatsVoMock1), false);
Mockito.verify(statsCollector, Mockito.never()).accumulateVmMetricsStats(Mockito.anyList());
}
@Test
public void accumulateVmMetricsStatsTest() {
String fakeStatsData1 = "{\"vmId\":1,\"cpuUtilization\":1.0,\"networkReadKBs\":1.0,"
+ "\"networkWriteKBs\":1.1,\"diskReadIOs\":3.0,\"diskWriteIOs\":3.1,\"diskReadKBs\":2.0,"
+ "\"diskWriteKBs\":2.1,\"memoryKBs\":1.0,\"intFreeMemoryKBs\":1.0,"
+ "\"targetMemoryKBs\":1.0,\"numCPUs\":1,\"entityType\":\"vm\"}";
String fakeStatsData2 = "{\"vmId\":1,\"cpuUtilization\":10.0,\"networkReadKBs\":1.0,"
+ "\"networkWriteKBs\":1.1,\"diskReadIOs\":3.0,\"diskWriteIOs\":3.1,\"diskReadKBs\":2.0,"
+ "\"diskWriteKBs\":2.1,\"memoryKBs\":1.0,\"intFreeMemoryKBs\":1.0,"
+ "\"targetMemoryKBs\":1.0,\"numCPUs\":1,\"entityType\":\"vm\"}";
Mockito.when(vmStatsVoMock1.getVmStatsData()).thenReturn(fakeStatsData1);
Mockito.when(vmStatsVoMock2.getVmStatsData()).thenReturn(fakeStatsData2);
VmStatsEntry result = statsCollector.accumulateVmMetricsStats(new ArrayList<VmStatsVO>(
Arrays.asList(vmStatsVoMock1, vmStatsVoMock2)));
Assert.assertEquals("vm", result.getEntityType());
Assert.assertEquals(1, result.getVmId());
Assert.assertEquals(1.0, result.getCPUUtilization(), 0);
Assert.assertEquals(1, result.getNumCPUs());
Assert.assertEquals(1.0, result.getMemoryKBs(), 0);
Assert.assertEquals(1.0, result.getIntFreeMemoryKBs(), 0);
Assert.assertEquals(1.0, result.getTargetMemoryKBs(), 0);
Assert.assertEquals(2.0, result.getNetworkReadKBs(), 0);
Assert.assertEquals(2.2, result.getNetworkWriteKBs(), 0);
Assert.assertEquals(4.0, result.getDiskReadKBs(), 0);
Assert.assertEquals(4.2, result.getDiskWriteKBs(), 0);
Assert.assertEquals(6.0, result.getDiskReadIOs(), 0);
Assert.assertEquals(6.2, result.getDiskWriteIOs(), 0);
}
@Test
public void testIsDbIpv6Local() {
Properties p = new Properties();
p.put("db.cloud.host", "::1");
when(statsCollector.getDbProperties()).thenReturn(p);
Assert.assertTrue(statsCollector.isDbLocal());
}
@Test
public void testIsDbIpv4Local() {
Properties p = new Properties();
p.put("db.cloud.host", "127.0.0.1");
when(statsCollector.getDbProperties()).thenReturn(p);
Assert.assertTrue(statsCollector.isDbLocal());
}
@Test
public void testIsDbSymbolicLocal() {
Properties p = new Properties();
p.put("db.cloud.host", "localhost");
when(statsCollector.getDbProperties()).thenReturn(p);
Assert.assertTrue(statsCollector.isDbLocal());
}
@Test
public void testIsDbOnSameIp() {
Properties p = new Properties();
p.put("db.cloud.host", "10.10.10.10");
p.put("cluster.node.IP", "10.10.10.10");
when(statsCollector.getDbProperties()).thenReturn(p);
Assert.assertTrue(statsCollector.isDbLocal());
}
@Test
public void testIsDbNotLocal() {
Properties p = new Properties();
p.put("db.cloud.host", "10.10.10.11");
p.put("cluster.node.IP", "10.10.10.10");
when(statsCollector.getDbProperties()).thenReturn(p);
Assert.assertFalse(statsCollector.isDbLocal());
}
private void performPersistVolumeStatsTest(Hypervisor.HypervisorType hypervisorType) {
Date timestamp = new Date();
String vmName = "vm";
String path = "path";
long ioReadDiff = 100;
long ioWriteDiff = 200;
long readDiff = 1024;
long writeDiff = 0;
Long volumeId = 1L;
VmDiskStatsEntry statsForCurrentIteration = null;
if (Hypervisor.HypervisorType.KVM.equals(hypervisorType)) {
statsForCurrentIteration = new VmDiskStatsEntry(vmName, path,
2000 + ioWriteDiff,
1000 + ioReadDiff,
20480 + writeDiff,
10240 + readDiff);
statsForCurrentIteration.setDeltaIoRead(ioReadDiff);
statsForCurrentIteration.setDeltaIoWrite(ioWriteDiff);
statsForCurrentIteration.setDeltaBytesRead(readDiff);
statsForCurrentIteration.setDeltaBytesWrite(writeDiff);
} else {
statsForCurrentIteration = new VmDiskStatsEntry(vmName, path,
ioWriteDiff,
ioReadDiff,
writeDiff,
readDiff);
}
List<VolumeStatsVO> persistedStats = new ArrayList<>();
Mockito.when(volumeStatsDao.persist(Mockito.any(VolumeStatsVO.class))).thenAnswer((Answer<VolumeStatsVO>) invocation -> {
VolumeStatsVO statsVO = (VolumeStatsVO) invocation.getArguments()[0];
persistedStats.add(statsVO);
return statsVO;
});
statsCollector.persistVolumeStats(volumeId, statsForCurrentIteration, hypervisorType, timestamp);
Assert.assertTrue(CollectionUtils.isNotEmpty(persistedStats));
Assert.assertNotNull(persistedStats.get(0));
VolumeStatsVO stat = persistedStats.get(0);
Assert.assertEquals(volumeId, stat.getVolumeId());
VmDiskStatsEntry entry = gson.fromJson(stat.getVolumeStatsData(), VmDiskStatsEntry.class);
Assert.assertEquals(vmName, entry.getVmName());
Assert.assertEquals(path, entry.getPath());
Assert.assertEquals(ioReadDiff, entry.getIORead());
Assert.assertEquals(ioWriteDiff, entry.getIOWrite());
Assert.assertEquals(readDiff, entry.getBytesRead());
Assert.assertEquals(writeDiff, entry.getBytesWrite());
}
@Test
public void testPersistVolumeStatsKVM() {
performPersistVolumeStatsTest(Hypervisor.HypervisorType.KVM);
}
@Test
public void testPersistVolumeStatsVmware() {
performPersistVolumeStatsTest(Hypervisor.HypervisorType.VMware);
}
private Map<String, String> convertJsonToOrderedMap(String json) {
Map<String, String> jsonMap = new TreeMap<String, String>();
String[] keyValuePairs = json.replace("{", "").replace("}", "").split(",");
for (String pair : keyValuePairs) {
String[] keyValue = pair.split(":");
jsonMap.put(keyValue[0], keyValue[1]);
}
return jsonMap;
}
private void setCollectorIopsStats(long poolId, Long capacityIops, Long usedIops) {
ConcurrentHashMap<Long, StorageStats> storagePoolStats = new ConcurrentHashMap<>();
storagePoolStats.put(poolId, new GetStorageStatsAnswer(Mockito.mock(GetStorageStatsCommand.class),
10L, 2L, capacityIops, usedIops));
ReflectionTestUtils.setField(statsCollector, "_storagePoolStats", storagePoolStats);
}
@Test
public void testPoolNeedsIopsStatsUpdating_NoChanges() {
long poolId = 1L;
long capacityIops = 100L;
long usedIops = 50L;
when(mockPool.getId()).thenReturn(poolId);
when(mockPool.getCapacityIops()).thenReturn(capacityIops);
when(mockPool.getUsedIops()).thenReturn(usedIops);
setCollectorIopsStats(poolId, capacityIops, usedIops);
boolean result = statsCollector.isPoolNeedsIopsStatsUpdate(mockPool, capacityIops, usedIops);
Assert.assertFalse(result);
Mockito.verify(mockPool, Mockito.never()).setCapacityIops(Mockito.anyLong());
Mockito.verify(mockPool, Mockito.never()).setUsedIops(Mockito.anyLong());
}
@Test
public void testPoolNeedsIopsStatsUpdating_CapacityIopsNeedsUpdating() {
long poolId = 1L;
when(mockPool.getId()).thenReturn(poolId);
when(mockPool.getCapacityIops()).thenReturn(100L);
when(mockPool.getUsedIops()).thenReturn(50L);
setCollectorIopsStats(poolId, 90L, 50L);
boolean result = statsCollector.isPoolNeedsIopsStatsUpdate(mockPool, 120L, 50L);
Assert.assertTrue(result);
Mockito.verify(mockPool).setCapacityIops(120L);
Mockito.verify(mockPool, Mockito.never()).setUsedIops(Mockito.anyLong());
}
@Test
public void testPoolNeedsIopsStatsUpdating_UsedIopsNeedsUpdating() {
long poolId = 1L;
when(mockPool.getId()).thenReturn(poolId);
when(mockPool.getCapacityIops()).thenReturn(100L);
when(mockPool.getUsedIops()).thenReturn(50L);
setCollectorIopsStats(poolId, 100L, 45L);
boolean result = statsCollector.isPoolNeedsIopsStatsUpdate(mockPool, 100L, 60L);
Assert.assertTrue(result);
Mockito.verify(mockPool).setUsedIops(60L);
Mockito.verify(mockPool, Mockito.never()).setCapacityIops(Mockito.anyLong());
}
@Test
public void testPoolNeedsIopsStatsUpdating_BothNeedUpdating() {
long poolId = 1L;
when(mockPool.getId()).thenReturn(poolId);
when(mockPool.getCapacityIops()).thenReturn(100L);
when(mockPool.getUsedIops()).thenReturn(50L);
setCollectorIopsStats(poolId, 90L, 45L);
boolean result = statsCollector.isPoolNeedsIopsStatsUpdate(mockPool, 120L, 60L);
Assert.assertTrue(result);
Mockito.verify(mockPool).setCapacityIops(120L);
Mockito.verify(mockPool).setUsedIops(60L);
}
@Test
public void testPoolNeedsIopsStatsUpdating_NullIops() {
long poolId = 1L;
when(mockPool.getId()).thenReturn(poolId);
boolean result = statsCollector.isPoolNeedsIopsStatsUpdate(mockPool, null, null);
Assert.assertFalse(result);
Mockito.verify(mockPool, Mockito.never()).setCapacityIops(Mockito.anyLong());
Mockito.verify(mockPool, Mockito.never()).setUsedIops(Mockito.anyLong());
}
@Test
public void testGsonDateFormatSerialization() {
Date now = new Date();
TestClass testObj = new TestClass("TestString", 999, now);
String json = msStatsGson.toJson(testObj);
Assert.assertTrue(json.contains("TestString"));
Assert.assertTrue(json.contains("999"));
String expectedDate = new SimpleDateFormat(DateUtil.ZONED_DATETIME_FORMAT).format(now);
Assert.assertTrue(json.contains(expectedDate));
}
@Test
public void testGsonDateFormatDeserializationWithSameDateFormat() throws Exception {
String json = "{\"str\":\"TestString\",\"num\":999,\"date\":\"2025-08-22T15:39:43+0000\"}";
TestClass testObj = msStatsGson.fromJson(json, TestClass.class);
Assert.assertEquals("TestString", testObj.getStr());
Assert.assertEquals(999, testObj.getNum());
Date expectedDate = new SimpleDateFormat(DateUtil.ZONED_DATETIME_FORMAT).parse("2025-08-22T15:39:43+0000");
Assert.assertEquals(expectedDate, testObj.getDate());
}
@Test (expected = JsonSyntaxException.class)
public void testGsonDateFormatDeserializationWithDifferentDateFormat() throws Exception {
String json = "{\"str\":\"TestString\",\"num\":999,\"date\":\"22/08/2025T15:39:43+0000\"}";
msStatsGson.fromJson(json, TestClass.class);
/* Deserialization throws the below exception:
com.google.gson.JsonSyntaxException: 22/08/2025T15:39:43+0000
at com.google.gson.DefaultTypeAdapters$DefaultDateTypeAdapter.deserializeToDate(DefaultTypeAdapters.java:376)
at com.google.gson.DefaultTypeAdapters$DefaultDateTypeAdapter.deserialize(DefaultTypeAdapters.java:351)
at com.google.gson.DefaultTypeAdapters$DefaultDateTypeAdapter.deserialize(DefaultTypeAdapters.java:307)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)
at com.google.gson.Gson.fromJson(Gson.java:551)
at com.google.gson.Gson.fromJson(Gson.java:498)
at com.google.gson.Gson.fromJson(Gson.java:467)
at com.google.gson.Gson.fromJson(Gson.java:417)
at com.google.gson.Gson.fromJson(Gson.java:389)
at com.cloud.serializer.GsonHelperTest.testGsonDateFormatDeserializationWithDifferentDateFormat(GsonHelperTest.java:113)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:231)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: java.text.ParseException: Unparseable date: "22/08/2025T15:39:43+0000"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
at com.google.gson.DefaultTypeAdapters$DefaultDateTypeAdapter.deserializeToDate(DefaultTypeAdapters.java:374)
... 42 more
*/
}
// -----------------------------------------------------------------------
// Tests for isNetworkEligibleForNetworkStats
// -----------------------------------------------------------------------
private VlanVO buildVlan(VlanType type) {
VlanVO vlan = Mockito.mock(VlanVO.class);
Mockito.when(vlan.getVlanType()).thenReturn(type);
return vlan;
}
@Test
public void isNetworkEligibleForNetworkStats_RoutedNetwork_ReturnsTrue() {
Long networkId = 1L;
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
Mockito.when(networkDao.findById(networkId)).thenReturn(networkVO);
Mockito.when(vlanDao.listVlansByNetworkId(networkId)).thenReturn(Collections.emptyList());
Mockito.when(routedIpv4Manager.isRoutedNetwork(networkVO)).thenReturn(true);
Assert.assertTrue(statsCollector.isNetworkEligibleForNetworkStats(networkId));
}
@Test
public void isNetworkEligibleForNetworkStats_DirectAttachedNetwork_ReturnsTrue() {
Long networkId = 1L;
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
Mockito.when(networkDao.findById(networkId)).thenReturn(networkVO);
Mockito.when(routedIpv4Manager.isRoutedNetwork(networkVO)).thenReturn(false);
List<VlanVO> vlans = Collections.singletonList(buildVlan(VlanType.DirectAttached));
Mockito.when(vlanDao.listVlansByNetworkId(networkId)).thenReturn(vlans);
Assert.assertTrue(statsCollector.isNetworkEligibleForNetworkStats(networkId));
}
@Test
public void isNetworkEligibleForNetworkStats_NeitherRoutedNorDirectAttached_ReturnsFalse() {
Long networkId = 1L;
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
Mockito.when(networkDao.findById(networkId)).thenReturn(networkVO);
Mockito.when(routedIpv4Manager.isRoutedNetwork(networkVO)).thenReturn(false);
List<VlanVO> vlans = Collections.singletonList(buildVlan(VlanType.VirtualNetwork));
Mockito.when(vlanDao.listVlansByNetworkId(networkId)).thenReturn(vlans);
Assert.assertFalse(statsCollector.isNetworkEligibleForNetworkStats(networkId));
}
@Test
public void isNetworkEligibleForNetworkStats_NullNetworkAndEmptyVlans_ReturnsFalse() {
Long networkId = 1L;
Mockito.when(networkDao.findById(networkId)).thenReturn(null);
Mockito.when(vlanDao.listVlansByNetworkId(networkId)).thenReturn(Collections.emptyList());
Assert.assertFalse(statsCollector.isNetworkEligibleForNetworkStats(networkId));
}
@Test
public void isNetworkEligibleForNetworkStats_NullNetworkButDirectAttached_ReturnsTrue() {
Long networkId = 1L;
Mockito.when(networkDao.findById(networkId)).thenReturn(null);
List<VlanVO> vlans = Collections.singletonList(buildVlan(VlanType.DirectAttached));
Mockito.when(vlanDao.listVlansByNetworkId(networkId)).thenReturn(vlans);
Assert.assertTrue(statsCollector.isNetworkEligibleForNetworkStats(networkId));
Mockito.verify(routedIpv4Manager, Mockito.never()).isRoutedNetwork(Mockito.any());
}
@Test
public void isNetworkEligibleForNetworkStats_NullNetworkId_ReturnsFalse() {
Assert.assertFalse(statsCollector.isNetworkEligibleForNetworkStats(null));
Mockito.verify(networkDao, Mockito.never()).findById(Mockito.anyLong());
Mockito.verify(vlanDao, Mockito.never()).listVlansByNetworkId(Mockito.anyLong());
}
private static class TestClass {
private String str;
private int num;
private Date date;
public TestClass(String str, int num, Date date) {
this.str = str;
this.num = num;
this.date = date;
}
public String getStr() {
return str;
}
public int getNum() {
return num;
}
public Date getDate() {
return date;
}
}
}