28
28
import org .elasticsearch .common .collect .Tuple ;
29
29
import org .elasticsearch .common .settings .ClusterSettings ;
30
30
import org .elasticsearch .common .settings .Settings ;
31
+ import org .elasticsearch .index .IndexNotFoundException ;
31
32
import org .elasticsearch .index .mapper .MapperService ;
32
33
import org .elasticsearch .test .ESTestCase ;
33
34
@@ -83,7 +84,9 @@ public void testHandleSpaces() { // see #21449
83
84
public void testAutoCreationDisabled () {
84
85
Settings settings = Settings .builder ().put (AutoCreateIndex .AUTO_CREATE_INDEX_SETTING .getKey (), false ).build ();
85
86
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
86
- assertThat (autoCreateIndex .shouldAutoCreate (randomAsciiOfLengthBetween (1 , 10 ), buildClusterState ()), equalTo (false ));
87
+ IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () ->
88
+ autoCreateIndex .shouldAutoCreate (randomAsciiOfLengthBetween (1 , 10 ), buildClusterState ()));
89
+ assertEquals ("no such index and [action.auto_create_index] is [false]" , e .getMessage ());
87
90
}
88
91
89
92
public void testAutoCreationEnabled () {
@@ -110,7 +113,9 @@ public void testDynamicMappingDisabled() {
110
113
randomAsciiOfLengthBetween (1 , 10 )))
111
114
.put (MapperService .INDEX_MAPPER_DYNAMIC_SETTING .getKey (), false ).build ();
112
115
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
113
- assertThat (autoCreateIndex .shouldAutoCreate (randomAsciiOfLengthBetween (1 , 10 ), buildClusterState ()), equalTo (false ));
116
+ IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () ->
117
+ autoCreateIndex .shouldAutoCreate (randomAsciiOfLengthBetween (1 , 10 ), buildClusterState ()));
118
+ assertEquals ("no such index and [index.mapper.dynamic] is [false]" , e .getMessage ());
114
119
}
115
120
116
121
public void testAutoCreationPatternEnabled () {
@@ -119,36 +124,37 @@ public void testAutoCreationPatternEnabled() {
119
124
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
120
125
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
121
126
assertThat (autoCreateIndex .shouldAutoCreate ("index" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo (true ));
122
- assertThat ( autoCreateIndex . shouldAutoCreate ( "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
127
+ expectNotMatch ( clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
123
128
}
124
129
125
130
public void testAutoCreationPatternDisabled () {
126
131
Settings settings = Settings .builder ().put (AutoCreateIndex .AUTO_CREATE_INDEX_SETTING .getKey (), "-index*" ).build ();
127
132
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
128
133
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
129
- assertThat (autoCreateIndex .shouldAutoCreate ("index" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo (false ));
130
- //default is false when patterns are specified
131
- assertThat (autoCreateIndex .shouldAutoCreate ("does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo (false ));
134
+ expectForbidden (clusterState , autoCreateIndex , "index" + randomAsciiOfLengthBetween (1 , 5 ), "-index*" );
135
+ /* When patterns are specified, even if the are all negative, the default is can't create. So a pure negative pattern is the same
136
+ * as false, really. */
137
+ expectNotMatch (clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
132
138
}
133
139
134
140
public void testAutoCreationMultiplePatternsWithWildcards () {
135
141
Settings settings = Settings .builder ().put (AutoCreateIndex .AUTO_CREATE_INDEX_SETTING .getKey (),
136
142
randomFrom ("+test*,-index*" , "test*,-index*" )).build ();
137
143
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
138
144
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
139
- assertThat ( autoCreateIndex . shouldAutoCreate ( "index" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ) );
145
+ expectForbidden ( clusterState , autoCreateIndex , "index" + randomAsciiOfLengthBetween (1 , 5 ), "-index*" );
140
146
assertThat (autoCreateIndex .shouldAutoCreate ("test" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo (true ));
141
- assertThat ( autoCreateIndex . shouldAutoCreate ( "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
147
+ expectNotMatch ( clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
142
148
}
143
149
144
150
public void testAutoCreationMultiplePatternsNoWildcards () {
145
151
Settings settings = Settings .builder ().put (AutoCreateIndex .AUTO_CREATE_INDEX_SETTING .getKey (), "+test1,-index1" ).build ();
146
152
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
147
153
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
148
154
assertThat (autoCreateIndex .shouldAutoCreate ("test1" , clusterState ), equalTo (true ));
149
- assertThat ( autoCreateIndex . shouldAutoCreate ( "index" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
150
- assertThat ( autoCreateIndex . shouldAutoCreate ( "test" + randomAsciiOfLengthBetween (2 , 5 ), clusterState ), equalTo ( false ));
151
- assertThat ( autoCreateIndex . shouldAutoCreate ( "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
155
+ expectNotMatch ( clusterState , autoCreateIndex , "index" + randomAsciiOfLengthBetween (1 , 5 ));
156
+ expectNotMatch ( clusterState , autoCreateIndex , "test" + randomAsciiOfLengthBetween (2 , 5 ));
157
+ expectNotMatch ( clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
152
158
}
153
159
154
160
public void testAutoCreationMultipleIndexNames () {
@@ -157,7 +163,7 @@ public void testAutoCreationMultipleIndexNames() {
157
163
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
158
164
assertThat (autoCreateIndex .shouldAutoCreate ("test1" , clusterState ), equalTo (true ));
159
165
assertThat (autoCreateIndex .shouldAutoCreate ("test2" , clusterState ), equalTo (true ));
160
- assertThat ( autoCreateIndex . shouldAutoCreate ( "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
166
+ expectNotMatch ( clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
161
167
}
162
168
163
169
public void testAutoCreationConflictingPatternsFirstWins () {
@@ -166,8 +172,8 @@ public void testAutoCreationConflictingPatternsFirstWins() {
166
172
AutoCreateIndex autoCreateIndex = newAutoCreateIndex (settings );
167
173
ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).metaData (MetaData .builder ()).build ();
168
174
assertThat (autoCreateIndex .shouldAutoCreate ("test1" , clusterState ), equalTo (true ));
169
- assertThat ( autoCreateIndex . shouldAutoCreate ( "test2" , clusterState ), equalTo ( false ) );
170
- assertThat ( autoCreateIndex . shouldAutoCreate ( "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ), clusterState ), equalTo ( false ));
175
+ expectForbidden ( clusterState , autoCreateIndex , "test2" , "-test2" );
176
+ expectNotMatch ( clusterState , autoCreateIndex , "does_not_match" + randomAsciiOfLengthBetween (1 , 5 ));
171
177
}
172
178
173
179
public void testUpdate () {
@@ -208,4 +214,18 @@ private AutoCreateIndex newAutoCreateIndex(Settings settings) {
208
214
return new AutoCreateIndex (settings , new ClusterSettings (settings ,
209
215
ClusterSettings .BUILT_IN_CLUSTER_SETTINGS ), new IndexNameExpressionResolver (settings ));
210
216
}
217
+
218
+ private void expectNotMatch (ClusterState clusterState , AutoCreateIndex autoCreateIndex , String index ) {
219
+ IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () ->
220
+ autoCreateIndex .shouldAutoCreate (index , clusterState ));
221
+ assertEquals ("no such index and [action.auto_create_index] ([" + autoCreateIndex .getAutoCreate () + "]) doesn't match" ,
222
+ e .getMessage ());
223
+ }
224
+
225
+ private void expectForbidden (ClusterState clusterState , AutoCreateIndex autoCreateIndex , String index , String forbiddingPattern ) {
226
+ IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () ->
227
+ autoCreateIndex .shouldAutoCreate (index , clusterState ));
228
+ assertEquals ("no such index and [action.auto_create_index] contains [" + forbiddingPattern
229
+ + "] which forbids automatic creation of the index" , e .getMessage ());
230
+ }
211
231
}
0 commit comments