10
10
11
11
import org .apache .lucene .tests .util .LuceneTestCase ;
12
12
import org .elasticsearch .Version ;
13
- import org .elasticsearch .cli .ExitCodes ;
14
- import org .elasticsearch .cli .MockTerminal ;
15
- import org .elasticsearch .cli .UserException ;
13
+ import org .elasticsearch .cli .Command ;
14
+ import org .elasticsearch .cli .CommandTestCase ;
16
15
import org .elasticsearch .common .settings .Settings ;
17
16
import org .elasticsearch .env .Environment ;
18
17
import org .elasticsearch .env .TestEnvironment ;
19
18
import org .elasticsearch .plugins .PluginInfo ;
20
19
import org .elasticsearch .plugins .PluginTestUtil ;
21
- import org .elasticsearch .test .ESTestCase ;
22
20
import org .junit .Before ;
23
21
24
22
import java .io .IOException ;
30
28
import java .util .stream .Collectors ;
31
29
32
30
@ LuceneTestCase .SuppressFileSystems ("*" )
33
- public class ListPluginsCommandTests extends ESTestCase {
31
+ public class ListPluginsCommandTests extends CommandTestCase {
34
32
35
33
private Path home ;
36
34
private Environment env ;
37
35
38
36
@ Before
39
- public void setUp () throws Exception {
40
- super .setUp ();
37
+ public void initEnv () throws Exception {
41
38
home = createTempDir ();
42
39
Files .createDirectories (home .resolve ("plugins" ));
43
40
Settings settings = Settings .builder ().put ("path.home" , home ).build ();
44
41
env = TestEnvironment .newEnvironment (settings );
45
42
}
46
43
47
- static MockTerminal listPlugins (Path home ) throws Exception {
48
- return listPlugins (home , new String [0 ]);
49
- }
50
-
51
- static MockTerminal listPlugins (Path home , String [] args ) throws Exception {
52
- String [] argsAndHome = new String [args .length + 1 ];
53
- System .arraycopy (args , 0 , argsAndHome , 0 , args .length );
54
- argsAndHome [args .length ] = "-Epath.home=" + home ;
55
- MockTerminal terminal = new MockTerminal ();
56
- int status = new ListPluginsCommand () {
57
- @ Override
58
- protected Environment createEnv (Map <String , String > settings ) throws UserException {
59
- Settings .Builder builder = Settings .builder ().put ("path.home" , home );
60
- settings .forEach ((k , v ) -> builder .put (k , v ));
61
- final Settings realSettings = builder .build ();
62
- return new Environment (realSettings , home .resolve ("config" ));
63
- }
64
-
65
- @ Override
66
- protected boolean addShutdownHook () {
67
- return false ;
68
- }
69
- }.main (argsAndHome , terminal );
70
- assertEquals (ExitCodes .OK , status );
71
- return terminal ;
72
- }
73
-
74
44
private static String buildMultiline (String ... args ) {
75
45
return Arrays .stream (args ).collect (Collectors .joining ("\n " , "" , "\n " ));
76
46
}
@@ -108,32 +78,31 @@ private static void buildFakePlugin(
108
78
109
79
public void testPluginsDirMissing () throws Exception {
110
80
Files .delete (env .pluginsFile ());
111
- IOException e = expectThrows (IOException .class , () -> listPlugins ( home ));
81
+ IOException e = expectThrows (IOException .class , () -> execute ( ));
112
82
assertEquals ("Plugins directory missing: " + env .pluginsFile (), e .getMessage ());
113
83
}
114
84
115
85
public void testNoPlugins () throws Exception {
116
- MockTerminal terminal = listPlugins ( home );
86
+ execute ( );
117
87
assertTrue (terminal .getOutput (), terminal .getOutput ().isEmpty ());
118
88
}
119
89
120
90
public void testOnePlugin () throws Exception {
121
91
buildFakePlugin (env , "fake desc" , "fake" , "org.fake" );
122
- MockTerminal terminal = listPlugins ( home );
92
+ execute ( );
123
93
assertEquals (buildMultiline ("fake" ), terminal .getOutput ());
124
94
}
125
95
126
96
public void testTwoPlugins () throws Exception {
127
97
buildFakePlugin (env , "fake desc" , "fake1" , "org.fake" );
128
98
buildFakePlugin (env , "fake desc 2" , "fake2" , "org.fake" );
129
- MockTerminal terminal = listPlugins ( home );
99
+ execute ( );
130
100
assertEquals (buildMultiline ("fake1" , "fake2" ), terminal .getOutput ());
131
101
}
132
102
133
103
public void testPluginWithVerbose () throws Exception {
134
104
buildFakePlugin (env , "fake desc" , "fake_plugin" , "org.fake" );
135
- String [] params = { "-v" };
136
- MockTerminal terminal = listPlugins (home , params );
105
+ execute ("-v" );
137
106
assertEquals (
138
107
buildMultiline (
139
108
"Plugins directory: " + env .pluginsFile (),
@@ -156,8 +125,7 @@ public void testPluginWithVerbose() throws Exception {
156
125
157
126
public void testPluginWithNativeController () throws Exception {
158
127
buildFakePlugin (env , "fake desc 1" , "fake_plugin1" , "org.fake" , true );
159
- String [] params = { "-v" };
160
- MockTerminal terminal = listPlugins (home , params );
128
+ execute ("-v" );
161
129
assertEquals (
162
130
buildMultiline (
163
131
"Plugins directory: " + env .pluginsFile (),
@@ -181,8 +149,7 @@ public void testPluginWithNativeController() throws Exception {
181
149
public void testPluginWithVerboseMultiplePlugins () throws Exception {
182
150
buildFakePlugin (env , "fake desc 1" , "fake_plugin1" , "org.fake" );
183
151
buildFakePlugin (env , "fake desc 2" , "fake_plugin2" , "org.fake2" );
184
- String [] params = { "-v" };
185
- MockTerminal terminal = listPlugins (home , params );
152
+ execute ("-v" );
186
153
assertEquals (
187
154
buildMultiline (
188
155
"Plugins directory: " + env .pluginsFile (),
@@ -218,22 +185,21 @@ public void testPluginWithVerboseMultiplePlugins() throws Exception {
218
185
public void testPluginWithoutVerboseMultiplePlugins () throws Exception {
219
186
buildFakePlugin (env , "fake desc 1" , "fake_plugin1" , "org.fake" );
220
187
buildFakePlugin (env , "fake desc 2" , "fake_plugin2" , "org.fake2" );
221
- MockTerminal terminal = listPlugins (home , new String [0 ]);
222
- String output = terminal .getOutput ();
223
- assertEquals (buildMultiline ("fake_plugin1" , "fake_plugin2" ), output );
188
+ execute ();
189
+ assertEquals (buildMultiline ("fake_plugin1" , "fake_plugin2" ), terminal .getOutput ());
224
190
}
225
191
226
192
public void testPluginWithoutDescriptorFile () throws Exception {
227
193
final Path pluginDir = env .pluginsFile ().resolve ("fake1" );
228
194
Files .createDirectories (pluginDir );
229
- NoSuchFileException e = expectThrows (NoSuchFileException .class , () -> listPlugins ( home ));
195
+ NoSuchFileException e = expectThrows (NoSuchFileException .class , () -> execute ( ));
230
196
assertEquals (pluginDir .resolve (PluginInfo .ES_PLUGIN_PROPERTIES ).toString (), e .getFile ());
231
197
}
232
198
233
199
public void testPluginWithWrongDescriptorFile () throws Exception {
234
200
final Path pluginDir = env .pluginsFile ().resolve ("fake1" );
235
201
PluginTestUtil .writePluginProperties (pluginDir , "description" , "fake desc" );
236
- IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> listPlugins ( home ));
202
+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> execute ( ));
237
203
final Path descriptorPath = pluginDir .resolve (PluginInfo .ES_PLUGIN_PROPERTIES );
238
204
assertEquals ("property [name] is missing in [" + descriptorPath .toString () + "]" , e .getMessage ());
239
205
}
@@ -256,19 +222,34 @@ public void testExistingIncompatiblePlugin() throws Exception {
256
222
);
257
223
buildFakePlugin (env , "fake desc 2" , "fake_plugin2" , "org.fake2" );
258
224
259
- MockTerminal terminal = listPlugins ( home );
225
+ execute ( );
260
226
String message = "plugin [fake_plugin1] was built for Elasticsearch version 1.0.0 but version " + Version .CURRENT + " is required" ;
261
227
assertEquals ("""
262
228
fake_plugin1
263
229
fake_plugin2
264
230
""" , terminal .getOutput ());
265
231
assertEquals ("WARNING: " + message + "\n " , terminal .getErrorOutput ());
266
232
267
- String [] params = { "-s" } ;
268
- terminal = listPlugins ( home , params );
233
+ terminal . reset () ;
234
+ execute ( "-s" );
269
235
assertEquals ("""
270
236
fake_plugin1
271
237
fake_plugin2
272
238
""" , terminal .getOutput ());
273
239
}
240
+
241
+ @ Override
242
+ protected Command newCommand () {
243
+ return new ListPluginsCommand () {
244
+ @ Override
245
+ protected Environment createEnv (Map <String , String > settings ) {
246
+ return env ;
247
+ }
248
+
249
+ @ Override
250
+ protected boolean addShutdownHook () {
251
+ return false ;
252
+ }
253
+ };
254
+ }
274
255
}
0 commit comments