sangyun-han | 805219a | 2016-03-26 12:58:27 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.graphitemetrics; |
| 17 | |
| 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Gauge; |
| 20 | import com.codahale.metrics.Histogram; |
| 21 | import com.codahale.metrics.Meter; |
| 22 | import com.codahale.metrics.Metric; |
| 23 | import com.codahale.metrics.MetricFilter; |
| 24 | import com.codahale.metrics.MetricRegistry; |
| 25 | import com.codahale.metrics.Timer; |
| 26 | import com.google.common.collect.ImmutableSet; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.metrics.MetricsComponent; |
| 30 | import org.onlab.metrics.MetricsFeature; |
| 31 | import org.onlab.metrics.MetricsService; |
| 32 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 33 | import org.onosproject.core.CoreServiceAdapter; |
| 34 | |
| 35 | import java.util.Map; |
| 36 | |
| 37 | import static org.junit.Assert.assertFalse; |
| 38 | import static org.junit.Assert.assertTrue; |
| 39 | import static org.junit.Assert.assertNotNull; |
| 40 | |
| 41 | /** |
| 42 | * Unit test for metrics reporter of graphite. |
| 43 | */ |
| 44 | public class GraphiteMetricsReporterTest { |
| 45 | |
| 46 | private GraphiteMetricsReporter gmr; |
| 47 | |
| 48 | private static final String METRIC_NAME1 = "consistentMap.onos-app-ids.putIfAbsent"; |
| 49 | private static final String METRIC_NAME2 = "consistentMap.onos-hosts.entrySet"; |
| 50 | private static final String METRIC_NAME3 = "clusterCommunication.endpoint.*"; |
| 51 | private static final String METRIC_NAME4 = "atomicCounter.onos-app-id-counter.*"; |
| 52 | |
| 53 | private static final String PREFIXES1 = "consistentMap"; |
| 54 | private static final String PREFIXES2 = "topology"; |
| 55 | private static final String PREFIXES3 = "consistentMap.onos-app-ids"; |
| 56 | private static final String PREFIXES4 = "consistentMap, clusterCommunication, atomicCounter"; |
| 57 | |
| 58 | /** |
| 59 | * Sets up graphite metrics reporter instance. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | gmr = new GraphiteMetricsReporter(); |
| 64 | gmr.coreService = new CoreServiceAdapter(); |
| 65 | gmr.cfgService = new ComponentConfigAdapter(); |
| 66 | gmr.metricsService = new TestMetricsService(); |
| 67 | gmr.activate(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tears down graphite metrics reporter instance. |
| 72 | */ |
| 73 | public void tearDown() { |
| 74 | gmr.deactivate(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests whether the containsName method can always return the correct result |
| 79 | * with the given metric name and a set of prefixes. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testContainsName() { |
| 83 | assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES1)); |
| 84 | assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES3)); |
| 85 | assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES4)); |
| 86 | assertTrue(gmr.containsName(METRIC_NAME2, PREFIXES4)); |
| 87 | assertTrue(gmr.containsName(METRIC_NAME3, PREFIXES4)); |
| 88 | assertTrue(gmr.containsName(METRIC_NAME4, PREFIXES4)); |
| 89 | assertFalse(gmr.containsName(METRIC_NAME1, PREFIXES2)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests whether the filter method can always return the correct result. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testFilter() { |
| 97 | MetricRegistry filtered; |
| 98 | MetricRegistry full = new MetricRegistry(); |
| 99 | full.meter(METRIC_NAME1); |
| 100 | full.meter(METRIC_NAME2); |
| 101 | full.meter(METRIC_NAME3); |
| 102 | full.meter(METRIC_NAME4); |
| 103 | |
| 104 | gmr.monitorAll = true; |
| 105 | filtered = gmr.filter(full); |
| 106 | |
| 107 | assertTrue(filtered.getNames() |
| 108 | .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2, |
| 109 | METRIC_NAME3, METRIC_NAME4))); |
| 110 | |
| 111 | gmr.monitorAll = false; |
| 112 | gmr.metricNames = PREFIXES1; |
| 113 | filtered = gmr.filter(full); |
| 114 | |
| 115 | assertTrue(filtered.getNames() |
| 116 | .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2))); |
| 117 | assertFalse(filtered.getNames() |
| 118 | .containsAll(ImmutableSet.of(METRIC_NAME3, METRIC_NAME4))); |
| 119 | |
| 120 | gmr.metricNames = PREFIXES2; |
| 121 | filtered = gmr.filter(full); |
| 122 | |
| 123 | assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1))); |
| 124 | |
| 125 | gmr.metricNames = PREFIXES3; |
| 126 | filtered = gmr.filter(full); |
| 127 | |
| 128 | assertTrue(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1))); |
| 129 | assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME2))); |
| 130 | |
| 131 | gmr.metricNames = PREFIXES4; |
| 132 | filtered = gmr.filter(full); |
| 133 | |
| 134 | assertTrue(filtered.getNames() |
| 135 | .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2, |
| 136 | METRIC_NAME3, METRIC_NAME4))); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests whether the methods of MetricsService receives null value parameter. |
| 141 | */ |
| 142 | private class TestMetricsService implements MetricsService { |
| 143 | |
| 144 | @Override |
| 145 | public MetricsComponent registerComponent(String name) { |
| 146 | assertNotNull("Component name is null.", name); |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public MetricRegistry getMetricRegistry() { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Counter createCounter(MetricsComponent component, |
| 157 | MetricsFeature feature, String metricName) { |
| 158 | assertNotNull("MetricsComponent is null.", component); |
| 159 | assertNotNull("MetricsFeature is null.", feature); |
| 160 | assertNotNull("Metric name is null.", metricName); |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Histogram createHistogram(MetricsComponent component, |
| 166 | MetricsFeature feature, String metricName) { |
| 167 | assertNotNull("MetricsComponent is null.", component); |
| 168 | assertNotNull("MetricsFeature is null.", feature); |
| 169 | assertNotNull("Metric name is null.", metricName); |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Timer createTimer(MetricsComponent component, |
| 175 | MetricsFeature feature, String metricName) { |
| 176 | assertNotNull("MetricsComponent is null.", component); |
| 177 | assertNotNull("MetricsFeature is null.", feature); |
| 178 | assertNotNull("Metric name is null.", metricName); |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public Meter createMeter(MetricsComponent component, |
| 184 | MetricsFeature feature, String metricName) { |
| 185 | assertNotNull("MetricsComponent is null.", component); |
| 186 | assertNotNull("MetricsFeature is null.", feature); |
| 187 | assertNotNull("Metric name is null.", metricName); |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public <T extends Metric> T registerMetric(MetricsComponent component, |
| 193 | MetricsFeature feature, String metricName, T metric) { |
| 194 | assertNotNull("MetricsComponent is null.", component); |
| 195 | assertNotNull("MetricsFeature is null.", feature); |
| 196 | assertNotNull("Metric name is null.", metricName); |
| 197 | assertNotNull("Metric is null.", metric); |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public boolean removeMetric(MetricsComponent component, |
| 203 | MetricsFeature feature, String metricName) { |
| 204 | assertNotNull("MetricsComponent is null.", component); |
| 205 | assertNotNull("MetricsFeature is null.", feature); |
| 206 | assertNotNull("Metric name is null.", metricName); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Map<String, Timer> getTimers(MetricFilter filter) { |
| 212 | assertNotNull("MetricFilter is null.", filter); |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Map<String, Gauge> getGauges(MetricFilter filter) { |
| 218 | assertNotNull("MetricFilter is null.", filter); |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Map<String, Counter> getCounters(MetricFilter filter) { |
| 224 | assertNotNull("MetricFilter is null.", filter); |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Map<String, Meter> getMeters(MetricFilter filter) { |
| 230 | assertNotNull("MetricFilter is null.", filter); |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Map<String, Histogram> getHistograms(MetricFilter filter) { |
| 236 | assertNotNull("MetricFilter is null.", filter); |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Map<String, Metric> getMetrics() { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void removeMatching(MetricFilter filter) { |
| 247 | assertNotNull("MetricFilter is null.", filter); |
| 248 | } |
| 249 | } |
| 250 | } |