-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
RingTest.java
167 lines (149 loc) · 7.34 KB
/
RingTest.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
/*
* 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.cassandra.tools.nodetool;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.locator.SimpleSnitch;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.tools.ToolRunner;
import org.apache.cassandra.utils.FBUtilities;
import static org.assertj.core.api.Assertions.assertThat;
public class RingTest extends CQLTester
{
private static String token;
@BeforeClass
public static void setup() throws Exception
{
requireNetwork();
startJMXServer();
token = StorageService.instance.getTokens().get(0);
}
/**
* Validate output, making sure the table mappings work with various host-modifying arguments in use.
*/
@Test
public void testRingOutput()
{
final HostStatWithPort host = new HostStatWithPort(null, FBUtilities.getBroadcastAddressAndPort(),
false, null);
validateRingOutput(host.ipOrDns(false), "ring");
Arrays.asList("-pp", "--print-port").forEach(arg -> validateRingOutput(host.ipOrDns(true), "-pp", "ring"));
final HostStatWithPort hostResolved = new HostStatWithPort(null, FBUtilities.getBroadcastAddressAndPort(),
true, null);
Arrays.asList("-r", "--resolve-ip").forEach(arg ->
validateRingOutput(hostResolved.ipOrDns(false), "ring", "-r"));
validateRingOutput(hostResolved.ipOrDns(true), "-pp", "ring", "-r");
}
@SuppressWarnings("DynamicRegexReplaceableByCompiledPattern")
private void validateRingOutput(String hostForm, String... args)
{
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool(args);
tool.assertOnCleanExit();
/*
Datacenter: datacenter1
==========
Address Rack Status State Load Owns Token
127.0.0.1 rack1 Up Normal 45.71 KiB 100.00% 4652409154190094022
*/
String[] lines = tool.getStdout().split("\\R");
assertThat(lines[1].trim()).endsWith(SimpleSnitch.DATA_CENTER_NAME);
assertThat(lines[3]).containsPattern("Address *Rack *Status *State *Load *Owns *Token *");
String hostRing = lines[lines.length-4].trim(); // this command has a couple extra newlines and an empty error message at the end. Not messing with it.
assertThat(hostRing).startsWith(hostForm);
assertThat(hostRing).contains(SimpleSnitch.RACK_NAME);
assertThat(hostRing).contains("Up");
assertThat(hostRing).contains("Normal");
assertThat(hostRing).containsPattern("\\d+\\.?\\d+ KiB");
assertThat(hostRing).containsPattern("\\d+\\.\\d+%");
assertThat(hostRing).endsWith(token);
assertThat(hostRing).doesNotContain("?");
}
@Test
public void testWrongArgFailsAndPrintsHelp()
{
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("--wrongarg", "ring");
tool.assertCleanStdErr();
assertThat(tool.getExitCode()).isEqualTo(1);
assertThat(tool.getStdout()).contains("nodetool help");
}
@Test
@SuppressWarnings("SingleCharacterStringConcatenation")
public void testMaybeChangeDocs()
{
// If you added, modified options or help, please update docs if necessary
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("help", "ring");
tool.assertOnCleanExit();
String help = "NAME\n" + " nodetool ring - Print information about the token ring\n"
+ "\n"
+ "SYNOPSIS\n"
+ " nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]\n"
+ " [(-pp | --print-port)] [(-pw <password> | --password <password>)]\n"
+ " [(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]\n"
+ " [(-u <username> | --username <username>)] ring [(-r | --resolve-ip)]\n"
+ " [--] [<keyspace>]\n"
+ "\n"
+ "OPTIONS\n"
+ " -h <host>, --host <host>\n"
+ " Node hostname or ip address\n"
+ "\n"
+ " -p <port>, --port <port>\n"
+ " Remote jmx agent port number\n"
+ "\n"
+ " -pp, --print-port\n"
+ " Operate in 4.0 mode with hosts disambiguated by port number\n"
+ "\n"
+ " -pw <password>, --password <password>\n"
+ " Remote jmx agent password\n"
+ "\n"
+ " -pwf <passwordFilePath>, --password-file <passwordFilePath>\n"
+ " Path to the JMX password file\n"
+ "\n"
+ " -r, --resolve-ip\n"
+ " Show node domain names instead of IPs\n"
+ "\n"
+ " -u <username>, --username <username>\n"
+ " Remote jmx agent username\n"
+ "\n"
+ " --\n"
+ " This option can be used to separate command-line options from the\n"
+ " list of argument, (useful when arguments might be mistaken for\n"
+ " command-line options\n"
+ "\n"
+ " <keyspace>\n"
+ " Specify a keyspace for accurate ownership information (topology\n"
+ " awareness)\n"
+ "\n"
+ "\n";
assertThat(tool.getStdout()).isEqualTo(help);
}
@Test
public void testRingKeyspace()
{
// Bad KS
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("ring", "mockks");
Assert.assertEquals(1, tool.getExitCode());
assertThat(tool.getStdout()).contains("The keyspace mockks, does not exist");
// Good KS
tool = ToolRunner.invokeNodetool("ring", "system_schema");
tool.assertOnCleanExit();
assertThat(tool.getStdout()).contains("Datacenter: datacenter1");
}
}