2323import com .microsoft .azure .storage .RetryExponentialRetry ;
2424import com .microsoft .azure .storage .blob .CloudBlobClient ;
2525import com .microsoft .azure .storage .core .Base64 ;
26-
2726import org .elasticsearch .common .settings .MockSecureSettings ;
2827import org .elasticsearch .common .settings .Settings ;
2928import org .elasticsearch .common .settings .SettingsException ;
3635import java .net .URISyntaxException ;
3736import java .net .UnknownHostException ;
3837import java .nio .charset .StandardCharsets ;
38+ import java .util .Collections ;
3939import java .util .Map ;
4040
4141import static org .elasticsearch .repositories .azure .AzureStorageServiceImpl .blobNameFromUri ;
4949
5050public class AzureStorageServiceTests extends ESTestCase {
5151
52- private MockSecureSettings buildSecureSettings () {
53- MockSecureSettings secureSettings = new MockSecureSettings ();
54- secureSettings .setString ("azure.client.azure1.account" , "myaccount1" );
55- secureSettings .setString ("azure.client.azure1.key" , "mykey1" );
56- secureSettings .setString ("azure.client.azure2.account" , "myaccount2" );
57- secureSettings .setString ("azure.client.azure2.key" , "mykey2" );
58- secureSettings .setString ("azure.client.azure3.account" , "myaccount3" );
59- secureSettings .setString ("azure.client.azure3.key" , "mykey3" );
60- return secureSettings ;
61- }
62- private Settings buildSettings () {
63- Settings settings = Settings .builder ()
64- .setSecureSettings (buildSecureSettings ())
65- .build ();
66- return settings ;
67- }
68-
6952 public void testReadSecuredSettings () {
7053 MockSecureSettings secureSettings = new MockSecureSettings ();
7154 secureSettings .setString ("azure.client.azure1.account" , "myaccount1" );
72- secureSettings .setString ("azure.client.azure1.key" , "mykey1" );
55+ secureSettings .setString ("azure.client.azure1.key" , encodeKey ( "mykey1" ) );
7356 secureSettings .setString ("azure.client.azure2.account" , "myaccount2" );
74- secureSettings .setString ("azure.client.azure2.key" , "mykey2" );
57+ secureSettings .setString ("azure.client.azure2.key" , encodeKey ( "mykey2" ) );
7558 secureSettings .setString ("azure.client.azure3.account" , "myaccount3" );
76- secureSettings .setString ("azure.client.azure3.key" , "mykey3" );
59+ secureSettings .setString ("azure.client.azure3.key" , encodeKey ( "mykey3" ) );
7760 Settings settings = Settings .builder ().setSecureSettings (secureSettings )
7861 .put ("azure.client.azure3.endpoint_suffix" , "my_endpoint_suffix" ).build ();
7962
@@ -88,9 +71,9 @@ public void testReadSecuredSettings() {
8871 public void testCreateClientWithEndpointSuffix () {
8972 MockSecureSettings secureSettings = new MockSecureSettings ();
9073 secureSettings .setString ("azure.client.azure1.account" , "myaccount1" );
91- secureSettings .setString ("azure.client.azure1.key" , Base64 . encode ("mykey1" . getBytes ( StandardCharsets . UTF_8 ) ));
74+ secureSettings .setString ("azure.client.azure1.key" , encodeKey ("mykey1" ));
9275 secureSettings .setString ("azure.client.azure2.account" , "myaccount2" );
93- secureSettings .setString ("azure.client.azure2.key" , Base64 . encode ("mykey2" . getBytes ( StandardCharsets . UTF_8 ) ));
76+ secureSettings .setString ("azure.client.azure2.key" , encodeKey ("mykey2" ));
9477 Settings settings = Settings .builder ().setSecureSettings (secureSettings )
9578 .put ("azure.client.azure1.endpoint_suffix" , "my_endpoint_suffix" ).build ();
9679 AzureStorageServiceImpl azureStorageService = new AzureStorageServiceImpl (settings , AzureStorageSettings .load (settings ));
@@ -103,41 +86,41 @@ public void testCreateClientWithEndpointSuffix() {
10386
10487 public void testGetSelectedClientWithNoPrimaryAndSecondary () {
10588 try {
106- new AzureStorageServiceMockForSettings (Settings .EMPTY );
89+ new AzureStorageServiceImpl (Settings .EMPTY , Collections . emptyMap () );
10790 fail ("we should have raised an IllegalArgumentException" );
10891 } catch (IllegalArgumentException e ) {
10992 assertThat (e .getMessage (), is ("If you want to use an azure repository, you need to define a client configuration." ));
11093 }
11194 }
11295
11396 public void testGetSelectedClientNonExisting () {
114- AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMockForSettings (buildSettings ());
97+ AzureStorageServiceImpl azureStorageService = createAzureService (buildSettings ());
11598 IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> {
11699 azureStorageService .getSelectedClient ("azure4" , LocationMode .PRIMARY_ONLY );
117100 });
118- assertThat (e .getMessage (), is ("Can not find named azure client [azure4]. Check your settings. " ));
101+ assertThat (e .getMessage (), is ("Unable to find client with name [azure4]" ));
119102 }
120103
121104 public void testGetSelectedClientDefaultTimeout () {
122105 Settings timeoutSettings = Settings .builder ()
123106 .setSecureSettings (buildSecureSettings ())
124107 .put ("azure.client.azure3.timeout" , "30s" )
125108 .build ();
126- AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMockForSettings (timeoutSettings );
109+ AzureStorageServiceImpl azureStorageService = createAzureService (timeoutSettings );
127110 CloudBlobClient client1 = azureStorageService .getSelectedClient ("azure1" , LocationMode .PRIMARY_ONLY );
128111 assertThat (client1 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), nullValue ());
129112 CloudBlobClient client3 = azureStorageService .getSelectedClient ("azure3" , LocationMode .PRIMARY_ONLY );
130113 assertThat (client3 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), is (30 * 1000 ));
131114 }
132115
133116 public void testGetSelectedClientNoTimeout () {
134- AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMockForSettings (buildSettings ());
117+ AzureStorageServiceImpl azureStorageService = createAzureService (buildSettings ());
135118 CloudBlobClient client1 = azureStorageService .getSelectedClient ("azure1" , LocationMode .PRIMARY_ONLY );
136119 assertThat (client1 .getDefaultRequestOptions ().getTimeoutIntervalInMs (), is (nullValue ()));
137120 }
138121
139122 public void testGetSelectedClientBackoffPolicy () {
140- AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMockForSettings (buildSettings ());
123+ AzureStorageServiceImpl azureStorageService = createAzureService (buildSettings ());
141124 CloudBlobClient client1 = azureStorageService .getSelectedClient ("azure1" , LocationMode .PRIMARY_ONLY );
142125 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), is (notNullValue ()));
143126 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), instanceOf (RetryExponentialRetry .class ));
@@ -149,7 +132,7 @@ public void testGetSelectedClientBackoffPolicyNbRetries() {
149132 .put ("azure.client.azure1.max_retries" , 7 )
150133 .build ();
151134
152- AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMockForSettings (timeoutSettings );
135+ AzureStorageServiceImpl azureStorageService = createAzureService (timeoutSettings );
153136 CloudBlobClient client1 = azureStorageService .getSelectedClient ("azure1" , LocationMode .PRIMARY_ONLY );
154137 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), is (notNullValue ()));
155138 assertThat (client1 .getDefaultRequestOptions ().getRetryPolicyFactory (), instanceOf (RetryExponentialRetry .class ));
@@ -159,7 +142,7 @@ public void testNoProxy() {
159142 Settings settings = Settings .builder ()
160143 .setSecureSettings (buildSecureSettings ())
161144 .build ();
162- AzureStorageServiceMockForSettings mock = new AzureStorageServiceMockForSettings (settings );
145+ AzureStorageServiceImpl mock = createAzureService (settings );
163146 assertThat (mock .storageSettings .get ("azure1" ).getProxy (), nullValue ());
164147 assertThat (mock .storageSettings .get ("azure2" ).getProxy (), nullValue ());
165148 assertThat (mock .storageSettings .get ("azure3" ).getProxy (), nullValue ());
@@ -172,7 +155,7 @@ public void testProxyHttp() throws UnknownHostException {
172155 .put ("azure.client.azure1.proxy.port" , 8080 )
173156 .put ("azure.client.azure1.proxy.type" , "http" )
174157 .build ();
175- AzureStorageServiceMockForSettings mock = new AzureStorageServiceMockForSettings (settings );
158+ AzureStorageServiceImpl mock = createAzureService (settings );
176159 Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
177160
178161 assertThat (azure1Proxy , notNullValue ());
@@ -192,7 +175,7 @@ public void testMultipleProxies() throws UnknownHostException {
192175 .put ("azure.client.azure2.proxy.port" , 8081 )
193176 .put ("azure.client.azure2.proxy.type" , "http" )
194177 .build ();
195- AzureStorageServiceMockForSettings mock = new AzureStorageServiceMockForSettings (settings );
178+ AzureStorageServiceImpl mock = createAzureService (settings );
196179 Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
197180 assertThat (azure1Proxy , notNullValue ());
198181 assertThat (azure1Proxy .type (), is (Proxy .Type .HTTP ));
@@ -211,7 +194,7 @@ public void testProxySocks() throws UnknownHostException {
211194 .put ("azure.client.azure1.proxy.port" , 8080 )
212195 .put ("azure.client.azure1.proxy.type" , "socks" )
213196 .build ();
214- AzureStorageServiceMockForSettings mock = new AzureStorageServiceMockForSettings (settings );
197+ AzureStorageServiceImpl mock = createAzureService (settings );
215198 Proxy azure1Proxy = mock .storageSettings .get ("azure1" ).getProxy ();
216199 assertThat (azure1Proxy , notNullValue ());
217200 assertThat (azure1Proxy .type (), is (Proxy .Type .SOCKS ));
@@ -227,7 +210,7 @@ public void testProxyNoHost() {
227210 .put ("azure.client.azure1.proxy.type" , randomFrom ("socks" , "http" ))
228211 .build ();
229212
230- SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageServiceMockForSettings (settings ));
213+ SettingsException e = expectThrows (SettingsException .class , () -> createAzureService (settings ));
231214 assertEquals ("Azure Proxy type has been set but proxy host or port is not defined." , e .getMessage ());
232215 }
233216
@@ -238,7 +221,7 @@ public void testProxyNoPort() {
238221 .put ("azure.client.azure1.proxy.type" , randomFrom ("socks" , "http" ))
239222 .build ();
240223
241- SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageServiceMockForSettings (settings ));
224+ SettingsException e = expectThrows (SettingsException .class , () -> createAzureService (settings ));
242225 assertEquals ("Azure Proxy type has been set but proxy host or port is not defined." , e .getMessage ());
243226 }
244227
@@ -249,7 +232,7 @@ public void testProxyNoType() {
249232 .put ("azure.client.azure1.proxy.port" , 8080 )
250233 .build ();
251234
252- SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageServiceMockForSettings (settings ));
235+ SettingsException e = expectThrows (SettingsException .class , () -> createAzureService (settings ));
253236 assertEquals ("Azure Proxy port or host have been set but proxy type is not defined." , e .getMessage ());
254237 }
255238
@@ -261,26 +244,10 @@ public void testProxyWrongHost() {
261244 .put ("azure.client.azure1.proxy.port" , 8080 )
262245 .build ();
263246
264- SettingsException e = expectThrows (SettingsException .class , () -> new AzureStorageServiceMockForSettings (settings ));
247+ SettingsException e = expectThrows (SettingsException .class , () -> createAzureService (settings ));
265248 assertEquals ("Azure proxy host is unknown." , e .getMessage ());
266249 }
267250
268- /**
269- * This internal class just overload createClient method which is called by AzureStorageServiceImpl.doStart()
270- */
271- class AzureStorageServiceMockForSettings extends AzureStorageServiceImpl {
272- AzureStorageServiceMockForSettings (Settings settings ) {
273- super (settings , AzureStorageSettings .load (settings ));
274- }
275-
276- // We fake the client here
277- @ Override
278- void createClient (AzureStorageSettings azureStorageSettings ) {
279- this .clients .put (azureStorageSettings .getAccount (),
280- new CloudBlobClient (URI .create ("https://" + azureStorageSettings .getAccount ())));
281- }
282- }
283-
284251 public void testBlobNameFromUri () throws URISyntaxException {
285252 String name = blobNameFromUri (new URI ("https://myservice.azure.net/container/path/to/myfile" ));
286253 assertThat (name , is ("path/to/myfile" ));
@@ -291,4 +258,27 @@ public void testBlobNameFromUri() throws URISyntaxException {
291258 name = blobNameFromUri (new URI ("https://127.0.0.1/container/path/to/myfile" ));
292259 assertThat (name , is ("path/to/myfile" ));
293260 }
261+
262+ private static MockSecureSettings buildSecureSettings () {
263+ MockSecureSettings secureSettings = new MockSecureSettings ();
264+ secureSettings .setString ("azure.client.azure1.account" , "myaccount1" );
265+ secureSettings .setString ("azure.client.azure1.key" , encodeKey ("mykey1" ));
266+ secureSettings .setString ("azure.client.azure2.account" , "myaccount2" );
267+ secureSettings .setString ("azure.client.azure2.key" , encodeKey ("mykey2" ));
268+ secureSettings .setString ("azure.client.azure3.account" , "myaccount3" );
269+ secureSettings .setString ("azure.client.azure3.key" , encodeKey ("mykey3" ));
270+ return secureSettings ;
271+ }
272+
273+ private static Settings buildSettings () {
274+ return Settings .builder ().setSecureSettings (buildSecureSettings ()).build ();
275+ }
276+
277+ private static AzureStorageServiceImpl createAzureService (final Settings settings ) {
278+ return new AzureStorageServiceImpl (settings , AzureStorageSettings .load (settings ));
279+ }
280+
281+ private static String encodeKey (final String value ) {
282+ return Base64 .encode (value .getBytes (StandardCharsets .UTF_8 ));
283+ }
294284}
0 commit comments