|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cloudstack.cluster; |
| 21 | + |
| 22 | +import com.cloud.utils.Ternary; |
| 23 | +import junit.framework.TestCase; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Spy; |
| 27 | +import org.mockito.junit.MockitoJUnitRunner; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.mockito.Mockito.doReturn; |
| 32 | + |
| 33 | +@RunWith(MockitoJUnitRunner.class) |
| 34 | +public class ClusterDrsAlgorithmTest extends TestCase { |
| 35 | + |
| 36 | + @Spy |
| 37 | + ClusterDrsAlgorithm drsAlgorithm; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testGetMetricValue() { |
| 41 | + List<Ternary<Boolean, String, Double>> testData = List.of( |
| 42 | + new Ternary<>(true, "free", 0.4), |
| 43 | + new Ternary<>(false, "free", 40.0), |
| 44 | + new Ternary<>(true, "used", 0.3), |
| 45 | + new Ternary<>(false, "used", 30.0) |
| 46 | + ); |
| 47 | + |
| 48 | + long used = 30; |
| 49 | + long free = 40; |
| 50 | + long total = 100; |
| 51 | + |
| 52 | + for (Ternary<Boolean, String, Double> data : testData) { |
| 53 | + boolean useRatio = data.first(); |
| 54 | + String metricType = data.second(); |
| 55 | + double expectedValue = data.third(); |
| 56 | + |
| 57 | + doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L); |
| 58 | + doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L); |
| 59 | + |
| 60 | + assertEquals(expectedValue, drsAlgorithm.getMetricValue(1, used, free, total, null)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testGetMetricValueWithSkipThreshold() { |
| 66 | + List<Ternary<Boolean, String, Double>> testData = List.of( |
| 67 | + new Ternary<>(true, "free", 0.15), |
| 68 | + new Ternary<>(false, "free", 15.0), |
| 69 | + new Ternary<>(true, "used", null), |
| 70 | + new Ternary<>(false, "used", null) |
| 71 | + ); |
| 72 | + |
| 73 | + long used = 80; |
| 74 | + long free = 15; |
| 75 | + long total = 100; |
| 76 | + |
| 77 | + for (Ternary<Boolean, String, Double> data : testData) { |
| 78 | + boolean useRatio = data.first(); |
| 79 | + String metricType = data.second(); |
| 80 | + Double expectedValue = data.third(); |
| 81 | + float skipThreshold = metricType.equals("free") ? 0.1f : 0.7f; |
| 82 | + |
| 83 | + doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L); |
| 84 | + doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L); |
| 85 | + |
| 86 | + assertEquals(expectedValue, drsAlgorithm.getMetricValue(1L, used, free, total, skipThreshold)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments