2525import org .elasticsearch .common .settings .MockSecureSettings ;
2626import org .elasticsearch .common .settings .Settings ;
2727import org .elasticsearch .common .settings .SettingsException ;
28+ import org .elasticsearch .common .settings .SettingsModule ;
2829import org .elasticsearch .test .ESTestCase ;
2930
3031import java .io .IOException ;
32+ import java .io .UncheckedIOException ;
3133import java .net .InetAddress ;
3234import java .net .InetSocketAddress ;
3335import java .net .Proxy ;
3436import java .net .URI ;
3537import java .net .URISyntaxException ;
3638import java .net .UnknownHostException ;
3739import java .nio .charset .StandardCharsets ;
40+ import java .util .Collections ;
3841import java .util .Map ;
3942
4043import static org .elasticsearch .repositories .azure .AzureStorageService .blobNameFromUri ;
@@ -60,10 +63,24 @@ public void testReadSecuredSettings() {
6063 assertThat (loadedSettings .get ("azure3" ).getEndpointSuffix (), equalTo ("my_endpoint_suffix" ));
6164 }
6265
66+ private AzureRepositoryPlugin pluginWithSettingsValidation (Settings settings ) {
67+ final AzureRepositoryPlugin plugin = new AzureRepositoryPlugin (settings );
68+ new SettingsModule (settings , plugin .getSettings (), Collections .emptyList (), Collections .emptySet ());
69+ return plugin ;
70+ }
71+
72+ private AzureStorageService storageServiceWithSettingsValidation (Settings settings ) {
73+ try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation (settings )) {
74+ return plugin .azureStoreService ;
75+ } catch (IOException e ) {
76+ throw new UncheckedIOException (e );
77+ }
78+ }
79+
6380 public void testCreateClientWithEndpointSuffix () throws IOException {
6481 final Settings settings = Settings .builder ().setSecureSettings (buildSecureSettings ())
6582 .put ("azure.client.azure1.endpoint_suffix" , "my_endpoint_suffix" ).build ();
66- try (AzureRepositoryPlugin plugin = new AzureRepositoryPlugin (settings )) {
83+ try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation (settings )) {
6784 final AzureStorageService azureStorageService = plugin .azureStoreService ;
6885 final CloudBlobClient client1 = azureStorageService .client ("azure1" ).v1 ();
6986 assertThat (client1 .getEndpoint ().toString (), equalTo ("https://myaccount1.blob.my_endpoint_suffix" ));
@@ -85,7 +102,7 @@ public void testReinitClientSettings() throws IOException {
85102 secureSettings2 .setString ("azure.client.azure3.account" , "myaccount23" );
86103 secureSettings2 .setString ("azure.client.azure3.key" , encodeKey ("mykey23" ));
87104 final Settings settings2 = Settings .builder ().setSecureSettings (secureSettings2 ).build ();
88- try (AzureRepositoryPlugin plugin = new AzureRepositoryPlugin (settings1 )) {
105+ try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation (settings1 )) {
89106 final AzureStorageService azureStorageService = plugin .azureStoreService ;
90107 final CloudBlobClient client11 = azureStorageService .client ("azure1" ).v1 ();
91108 assertThat (client11 .getEndpoint ().toString (), equalTo ("https://myaccount11.blob.core.windows.net" ));
@@ -117,7 +134,7 @@ public void testReinitClientEmptySettings() throws IOException {
117134 secureSettings .setString ("azure.client.azure1.account" , "myaccount1" );
118135 secureSettings .setString ("azure.client.azure1.key" , encodeKey ("mykey11" ));
119136 final Settings settings = Settings .builder ().setSecureSettings (secureSettings ).build ();
120- try (AzureRepositoryPlugin plugin = new AzureRepositoryPlugin (settings )) {
137+ try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation (settings )) {
121138 final AzureStorageService azureStorageService = plugin .azureStoreService ;
122139 final CloudBlobClient client11 = azureStorageService .client ("azure1" ).v1 ();
123140 assertThat (client11 .getEndpoint ().toString (), equalTo ("https://myaccount1.blob.core.windows.net" ));
@@ -141,7 +158,7 @@ public void testReinitClientWrongSettings() throws IOException {
141158 secureSettings2 .setString ("azure.client.azure1.account" , "myaccount1" );
142159 // missing key
143160 final Settings settings2 = Settings .builder ().setSecureSettings (secureSettings2 ).build ();
144- try (AzureRepositoryPlugin plugin = new AzureRepositoryPlugin (settings1 )) {
161+ try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation (settings1 )) {
145162 final AzureStorageService azureStorageService = plugin .azureStoreService ;
146163 final CloudBlobClient client11 = azureStorageService .client ("azure1" ).v1 ();
147164 assertThat (client11 .getEndpoint ().toString (), equalTo ("https://myaccount1.blob.core.windows.net" ));
@@ -154,7 +171,7 @@ public void testReinitClientWrongSettings() throws IOException {
154171 }
155172
156173 public void testGetSelectedClientNonExisting () {
157- final AzureStorageService azureStorageService = new AzureStorageService (buildSettings ());
174+ final AzureStorageService azureStorageService = storageServiceWithSettingsValidation (buildSettings ());
158175 final SettingsException e = expectThrows (SettingsException .class , () -> azureStorageService .client ("azure4" ));
159176 assertThat (e .getMessage (), is ("Unable to find client with name [azure4]" ));
160177 }
@@ -164,21 +181,21 @@ public void testGetSelectedClientDefaultTimeout() {
164181 .setSecureSettings (buildSecureSettings ())
165182 .put ("azure.client.azure3.timeout" , "30s" )
166183 .build ();
167- final AzureStorageService azureStorageService = new AzureStorageService (timeoutSettings );
184+ final AzureStorageService azureStorageService = storageServiceWithSettingsValidation (timeoutSettings );
168185 final CloudBlobClient client1 = azureStorageService .client ("azure1" ).v1 ();
169186 assertThat (client1 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), nullValue ());
170187 final CloudBlobClient client3 = azureStorageService .client ("azure3" ).v1 ();
171188 assertThat (client3 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), is (30 * 1000 ));
172189 }
173190
174191 public void testGetSelectedClientNoTimeout () {
175- final AzureStorageService azureStorageService = new AzureStorageService (buildSettings ());
192+ final AzureStorageService azureStorageService = storageServiceWithSettingsValidation (buildSettings ());
176193 final CloudBlobClient client1 = azureStorageService .client ("azure1" ).v1 ();
177194 assertThat (client1 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), is (nullValue ()));
178195 }
179196
180197 public void testGetSelectedClientBackoffPolicy () {
181- final AzureStorageService azureStorageService = new AzureStorageService (buildSettings ());
198+ final AzureStorageService azureStorageService = storageServiceWithSettingsValidation (buildSettings ());
182199 final CloudBlobClient client1 = azureStorageService .client ("azure1" ).v1 ();
183200 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), is (notNullValue ()));
184201 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), instanceOf (RetryExponentialRetry .class ));
@@ -190,7 +207,7 @@ public void testGetSelectedClientBackoffPolicyNbRetries() {
190207 .put ("azure.client.azure1.max_retries" , 7 )
191208 .build ();
192209
193- final AzureStorageService azureStorageService = new AzureStorageService (timeoutSettings );
210+ final AzureStorageService azureStorageService = storageServiceWithSettingsValidation (timeoutSettings );
194211 final CloudBlobClient client1 = azureStorageService .client ("azure1" ).v1 ();
195212 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), is (notNullValue ()));
196213 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), instanceOf (RetryExponentialRetry .class ));
@@ -200,7 +217,7 @@ public void testNoProxy() {
200217 final Settings settings = Settings .builder ()
201218 .setSecureSettings (buildSecureSettings ())
202219 .build ();
203- final AzureStorageService mock = new AzureStorageService (settings );
220+ final AzureStorageService mock = storageServiceWithSettingsValidation (settings );
204221 assertThat (mock .storageSettings .get ("azure1" ).getProxy (), nullValue ());
205222 assertThat (mock .storageSettings .get ("azure2" ).getProxy (), nullValue ());
206223 assertThat (mock .storageSettings .get ("azure3" ).getProxy (), nullValue ());
@@ -213,7 +230,7 @@ public void testProxyHttp() throws UnknownHostException {
213230 .put ("azure.client.azure1.proxy.port" , 8080 )
214231 .put ("azure.client.azure1.proxy.type" , "http" )
215232 .build ();
216- final AzureStorageService mock = new AzureStorageService (settings );
233+ final AzureStorageService mock = storageServiceWithSettingsValidation (settings );
217234 final Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
218235
219236 assertThat (azure1Proxy , notNullValue ());
@@ -233,7 +250,7 @@ public void testMultipleProxies() throws UnknownHostException {
233250 .put ("azure.client.azure2.proxy.port" , 8081 )
234251 .put ("azure.client.azure2.proxy.type" , "http" )
235252 .build ();
236- final AzureStorageService mock = new AzureStorageService (settings );
253+ final AzureStorageService mock = storageServiceWithSettingsValidation (settings );
237254 final Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
238255 assertThat (azure1Proxy , notNullValue ());
239256 assertThat (azure1Proxy .type (), is (Proxy .Type .HTTP ));
@@ -252,7 +269,7 @@ public void testProxySocks() throws UnknownHostException {
252269 .put ("azure.client.azure1.proxy.port" , 8080 )
253270 .put ("azure.client.azure1.proxy.type" , "socks" )
254271 .build ();
255- final AzureStorageService mock = new AzureStorageService (settings );
272+ final AzureStorageService mock = storageServiceWithSettingsValidation (settings );
256273 final Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
257274 assertThat (azure1Proxy , notNullValue ());
258275 assertThat (azure1Proxy .type (), is (Proxy .Type .SOCKS ));
@@ -267,7 +284,7 @@ public void testProxyNoHost() {
267284 .put ("azure.client.azure1.proxy.port" , 8080 )
268285 .put ("azure.client.azure1.proxy.type" , randomFrom ("socks" , "http" ))
269286 .build ();
270- final SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageService (settings ));
287+ final SettingsException e = expectThrows (SettingsException .class , () -> storageServiceWithSettingsValidation (settings ));
271288 assertEquals ("Azure Proxy type has been set but proxy host or port is not defined." , e .getMessage ());
272289 }
273290
@@ -278,7 +295,7 @@ public void testProxyNoPort() {
278295 .put ("azure.client.azure1.proxy.type" , randomFrom ("socks" , "http" ))
279296 .build ();
280297
281- final SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageService (settings ));
298+ final SettingsException e = expectThrows (SettingsException .class , () -> storageServiceWithSettingsValidation (settings ));
282299 assertEquals ("Azure Proxy type has been set but proxy host or port is not defined." , e .getMessage ());
283300 }
284301
@@ -289,7 +306,7 @@ public void testProxyNoType() {
289306 .put ("azure.client.azure1.proxy.port" , 8080 )
290307 .build ();
291308
292- final SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageService (settings ));
309+ final SettingsException e = expectThrows (SettingsException .class , () -> storageServiceWithSettingsValidation (settings ));
293310 assertEquals ("Azure Proxy port or host have been set but proxy type is not defined." , e .getMessage ());
294311 }
295312
@@ -301,7 +318,7 @@ public void testProxyWrongHost() {
301318 .put ("azure.client.azure1.proxy.port" , 8080 )
302319 .build ();
303320
304- final SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageService (settings ));
321+ final SettingsException e = expectThrows (SettingsException .class , () -> storageServiceWithSettingsValidation (settings ));
305322 assertEquals ("Azure proxy host is unknown." , e .getMessage ());
306323 }
307324
0 commit comments