forked from apache/ignite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheMetricsCommandTest.java
288 lines (223 loc) · 10.8 KB
/
CacheMetricsCommandTest.java
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
/*
* 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 org.apache.ignite.util;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.ignite.Ignite;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.management.cache.CacheMetricsCommand;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.visor.cache.metrics.CacheMetricsOperation;
import org.apache.ignite.testframework.GridTestUtils;
import org.junit.Test;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_INVALID_ARGUMENTS;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_UNEXPECTED_ERROR;
import static org.apache.ignite.internal.commandline.CommandList.CACHE;
import static org.apache.ignite.internal.commandline.cache.CacheSubcommands.METRICS;
import static org.apache.ignite.internal.util.lang.GridFunc.asMap;
/**
* Test for {@link CacheMetricsCommand} command.
*/
public class CacheMetricsCommandTest extends GridCommandHandlerAbstractTest {
/** Enable operation. */
private static final String ENABLE = CacheMetricsOperation.ENABLE.toString();
/** Disable operation. */
private static final String DISABLE = CacheMetricsOperation.DISABLE.toString();
/** Status operation. */
private static final String STATUS = CacheMetricsOperation.STATUS.toString();
/** Cache one. */
private static final String CACHE_ONE = "cache-1";
/** Cache two. */
private static final String CACHE_TWO = "cache-2";
/** */
private static final String ALL_CACHES_ARGUMENT = "--all-caches";
/** */
private static final String CACHES_ARGUMENT = "--caches";
/** Incorrect metrics operation message. */
public static final String INCORRECT_METRICS_OPERATION_MESSAGE = "Argument operation required";
/** Expected caches list message. */
public static final String EXPECTED_CACHES_LIST_MESSAGE = "Please specify a value for argument: --caches";
/** Incorrect cache argument message. */
public static final String INCORRECT_CACHE_ARGUMENT_MESSAGE = "One of [--all-caches, --caches] required";
/** {@inheritDoc} */
@Override public void beforeTest() throws Exception {
super.beforeTest();
injectTestSystemOut();
persistenceEnable(false);
autoConfirmation = false;
startGrids(2);
}
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
super.afterTest();
stopAllGrids();
}
/**
* Tests metrics enable / disable operations.
*/
@Test
public void testEnableDisable() {
// Test empty cluster
checkExecutionSuccess(Collections.emptyMap(), ENABLE, ALL_CACHES_ARGUMENT);
createCachesWithMetrics(asMap(CACHE_ONE, false, CACHE_TWO, true));
checkExecutionSuccess(asMap(CACHE_ONE, true), ENABLE, CACHES_ARGUMENT, CACHE_ONE);
checkExecutionSuccess(asMap(CACHE_TWO, false), DISABLE, CACHES_ARGUMENT, CACHE_TWO);
// Cache list with duplicates
String cacheNames = String.join(",", CACHE_TWO, CACHE_ONE, CACHE_ONE, CACHE_TWO);
checkExecutionSuccess(asMap(CACHE_ONE, false, CACHE_TWO, false), DISABLE, CACHES_ARGUMENT, cacheNames);
checkExecutionSuccess(asMap(CACHE_ONE, true, CACHE_TWO, true), ENABLE, ALL_CACHES_ARGUMENT);
}
/**
* Tests metrics status operation.
*/
@Test
public void testStatus() {
// Test empty cluster
checkExecutionSuccess(Collections.emptyMap(), STATUS, ALL_CACHES_ARGUMENT);
createCachesWithMetrics(asMap(CACHE_ONE, false, CACHE_TWO, true));
checkExecutionSuccess(asMap(CACHE_ONE, false), STATUS, CACHES_ARGUMENT, CACHE_ONE);
checkExecutionSuccess(asMap(CACHE_TWO, true), STATUS, CACHES_ARGUMENT, CACHE_TWO);
// Cache list with duplicates
String cacheNames = String.join(",", CACHE_TWO, CACHE_ONE, CACHE_ONE, CACHE_TWO);
checkExecutionSuccess(asMap(CACHE_ONE, false, CACHE_TWO, true), STATUS, CACHES_ARGUMENT, cacheNames);
checkExecutionSuccess(asMap(CACHE_ONE, false, CACHE_TWO, true), STATUS, ALL_CACHES_ARGUMENT);
}
/**
* Tests metrics operations for a non-existing cache.
*/
@Test
public void testNotFoundCache() {
createCachesWithMetrics(asMap(CACHE_ONE, false));
String descriptorsNotFoundMsg = "One or more cache descriptors not found [caches=[" + CACHE_ONE + ", " +
CACHE_TWO + ']';
checkExecutionError(descriptorsNotFoundMsg, ENABLE, CACHES_ARGUMENT, CACHE_ONE + ',' + CACHE_TWO);
// Check that metrics statuses was not changed
checkClusterMetrics(asMap(CACHE_ONE, false));
checkExecutionError("Cache does not exist: " + CACHE_TWO, STATUS, CACHES_ARGUMENT, CACHE_ONE + ',' + CACHE_TWO);
}
/** */
@Test
public void testInvalidArguments() {
String checkArgs = "Check arguments. ";
// Check when no operation passed
checkInvalidArguments(checkArgs + INCORRECT_METRICS_OPERATION_MESSAGE);
// Check when unknown operation passed
checkInvalidArguments(checkArgs + "Can't parse value 'bad-command'", "bad-command");
// Check when no --caches/--all-caches arguments passed
checkInvalidArguments(checkArgs + INCORRECT_CACHE_ARGUMENT_MESSAGE, ENABLE);
checkInvalidArguments(checkArgs + INCORRECT_CACHE_ARGUMENT_MESSAGE, STATUS);
String invalidCacheListFullMsg = checkArgs + EXPECTED_CACHES_LIST_MESSAGE;
// Check when --caches argument passed without list of caches
checkInvalidArguments(invalidCacheListFullMsg, ENABLE, CACHES_ARGUMENT);
checkInvalidArguments(invalidCacheListFullMsg, STATUS, CACHES_ARGUMENT);
String incorrectCacheArgFullMsg = checkArgs + "Unexpected argument: --arg";
// Check when unknown argument is passed after metric operation
checkInvalidArguments(incorrectCacheArgFullMsg, ENABLE, "--arg");
checkInvalidArguments(incorrectCacheArgFullMsg, STATUS, "--arg");
String unexpectedCacheArgMsg = checkArgs + "Only one of [--all-caches, --caches] allowed";
// Check when extra argument passed after correct command
checkInvalidArguments(unexpectedCacheArgMsg, ENABLE, CACHES_ARGUMENT, CACHE_ONE,
ALL_CACHES_ARGUMENT);
checkInvalidArguments(unexpectedCacheArgMsg, STATUS, ALL_CACHES_ARGUMENT, CACHES_ARGUMENT,
CACHE_ONE);
// Check when after --caches argument extra argument is passed instead of list of caches
checkInvalidArguments("Unexpected value: --all-caches", ENABLE, CACHES_ARGUMENT, ALL_CACHES_ARGUMENT);
}
/**
* Check execution of metric operation and parse resulting table.
*
* @param expStatuses Expected table entries in command output.
* @param args Command arguments.
*/
private void checkExecutionSuccess(Map<String, Boolean> expStatuses, String... args) {
exec(EXIT_CODE_OK, args);
checkOutput(expStatuses, testOut.toString());
checkClusterMetrics(expStatuses);
}
/**
* @param expExitCode Expected exit code.
* @param args Command arguments.
*/
private void exec(int expExitCode, String... args) {
String[] fullArgs = F.concat(new String[] {CACHE.text(), METRICS.text()}, args);
int exitCode = execute(fullArgs);
assertEquals("Unexpected exit code", expExitCode, exitCode);
}
/** */
private void checkInvalidArguments(String expOut, String... args) {
checkExecutionAndOutput(EXIT_CODE_INVALID_ARGUMENTS, expOut, args);
}
/** */
private void checkExecutionError(String expOut, String... args) {
checkExecutionAndOutput(EXIT_CODE_UNEXPECTED_ERROR, expOut, args);
}
/**
* Check command execution and results.
*
* @param expExitCode Expected exit code.
* @param expOut Expected command output.
* @param args Command arguments.
*/
private void checkExecutionAndOutput(int expExitCode, String expOut, String... args) {
exec(expExitCode, args);
GridTestUtils.assertContains(log, testOut.toString(), expOut);
}
/**
* @param expStatuses Expected metrics statuses.
* @param testOutStr Test output.
*/
private void checkOutput(Map<String, Boolean> expStatuses, String testOutStr) {
for (Map.Entry<String, Boolean> entry : expStatuses.entrySet()) {
String cacheName = entry.getKey();
String metricsStatus = entry.getValue() ? "enabled" : "disabled";
Matcher cacheStatusMatcher = Pattern.compile(cacheName + "\\s+" + metricsStatus).matcher(testOutStr);
String msg = String.format("Unexpected count of table entries for metrics: [cacheName=%s, metricsStatus=%s]",
cacheName, metricsStatus);
int cnt = 0;
while (cacheStatusMatcher.find())
cnt++;
assertEquals(msg, 1, cnt);
}
}
/**
* @param metricsStatuses Metrics statuses.
*/
private void createCachesWithMetrics(Map<String, Boolean> metricsStatuses) {
for (Map.Entry<String, Boolean> nameAndState : metricsStatuses.entrySet()) {
grid(0).getOrCreateCache(new CacheConfiguration<>()
.setName(nameAndState.getKey())
.setStatisticsEnabled(nameAndState.getValue()));
}
checkClusterMetrics(metricsStatuses);
}
/**
* @param expStatuses Expected cache metrics statuses.
*/
private void checkClusterMetrics(Map<String, Boolean> expStatuses) {
for (Ignite ignite : G.allGrids()) {
for (String cacheName : expStatuses.keySet()) {
Boolean cacheMetricsEnabled = ignite.cache(cacheName).metrics().isStatisticsEnabled();
assertEquals("Unexpected metrics mode for cache: " + cacheName, expStatuses.get(cacheName),
cacheMetricsEnabled);
}
}
}
}