2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.iaas.impl;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 import java.io.IOException;
33 import java.lang.reflect.Field;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Properties;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.openecomp.appc.Constants;
45 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
46 import org.openecomp.appc.adapter.iaas.impl.ProviderAdapterImpl;
47 import org.openecomp.appc.adapter.iaas.impl.ProviderCache;
48 import org.openecomp.appc.adapter.iaas.impl.ServiceCatalog;
49 import org.openecomp.appc.adapter.iaas.impl.TenantCache;
50 import org.openecomp.appc.adapter.iaas.impl.VMURL;
51 import org.openecomp.appc.configuration.ConfigurationFactory;
52 import org.openecomp.appc.exceptions.APPCException;
53 import org.openecomp.appc.exceptions.UnknownProviderException;
54 import com.att.cdp.exceptions.ZoneException;
55 import com.att.cdp.zones.ComputeService;
56 import com.att.cdp.zones.Context;
57 import com.att.cdp.zones.ContextFactory;
58 import com.att.cdp.zones.model.Image;
59 import com.att.cdp.zones.model.Server;
60 import com.att.cdp.zones.model.Server.Status;
61 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
62 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
65 * Test the ProviderAdapter implementation.
67 @Category(org.openecomp.appc.adapter.iaas.impl.TestProviderAdapterImpl.class)
68 public class TestProviderAdapterImpl {
70 @SuppressWarnings("nls")
71 private static final String PROVIDER_NAME = "ILAB";
73 @SuppressWarnings("nls")
74 private static final String PROVIDER_TYPE = "OpenStackProvider";
76 private static String IDENTITY_URL;
78 private static String PRINCIPAL;
80 private static String CREDENTIAL;
82 private static String TENANT_NAME;
84 private static String TENANT_ID;
86 private static String USER_ID;
88 private static String REGION_NAME;
90 private static String SERVER_URL;
92 private static Class<?> providerAdapterImplClass;
93 private static Class<?> configurationFactoryClass;
94 private static Field providerCacheField;
95 private static Field configField;
97 private ProviderAdapterImpl adapter;
100 * Use reflection to locate fields and methods so that they can be manipulated during the test
101 * to change the internal state accordingly.
103 * @throws NoSuchFieldException if the field(s) dont exist
104 * @throws SecurityException if reflective access is not allowed
105 * @throws NoSuchMethodException If the method(s) dont exist
107 @SuppressWarnings("nls")
109 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
110 providerAdapterImplClass = ProviderAdapterImpl.class;
111 configurationFactoryClass = ConfigurationFactory.class;
113 providerCacheField = providerAdapterImplClass.getDeclaredField("providerCache");
114 providerCacheField.setAccessible(true);
116 configField = configurationFactoryClass.getDeclaredField("config");
117 configField.setAccessible(true);
119 Properties props = ConfigurationFactory.getConfiguration().getProperties();
120 IDENTITY_URL = props.getProperty("provider1.identity");
121 PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");
122 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
123 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
124 TENANT_ID = props.getProperty("provider1.tenant1.id", "abcde12345fghijk6789lmnopq123rst");
125 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
126 SERVER_URL = props.getProperty("test.url");
130 * Setup the test environment.
132 * @throws IllegalAccessException if this Field object is enforcing Java language access control
133 * and the underlying field is either inaccessible or final.
134 * @throws IllegalArgumentException if the specified object is not an instance of the class or
135 * interface declaring the underlying field (or a subclass or implementor thereof), or
136 * if an unwrapping conversion fails.
137 * @throws NullPointerException if the specified object is null and the field is an instance
139 * @throws ExceptionInInitializerError if the initialization provoked by this method fails.
142 public void setup() throws IllegalArgumentException, IllegalAccessException {
143 configField.set(null, null);
144 Properties properties = new Properties();
145 adapter = new ProviderAdapterImpl(properties);
149 * This method inspects the provider adapter implementation to make sure that the cache of
150 * providers and tenants, as well as the service catalog, and all pools of contexts have been
153 * @throws IllegalAccessException if this Field object is enforcing Java language access control
154 * and the underlying field is inaccessible.
155 * @throws IllegalArgumentException if the specified object is not an instance of the class or
156 * interface declaring the underlying field (or a subclass or implementor thereof).
158 @SuppressWarnings({"unchecked"})
161 public void validateCacheIsCreatedCorrectly() throws IllegalArgumentException, IllegalAccessException {
162 Map<String, ProviderCache> providerCaches = (Map<String, ProviderCache>) providerCacheField.get(adapter);
164 assertNotNull(providerCaches);
165 assertEquals(1, providerCaches.size());
166 assertTrue(providerCaches.containsKey(PROVIDER_NAME));
168 ProviderCache providerCache = providerCaches.get(PROVIDER_NAME);
169 assertEquals(PROVIDER_NAME, providerCache.getProviderName());
170 assertEquals(PROVIDER_TYPE, providerCache.getProviderType());
172 Map<String, TenantCache> tenantCaches = providerCache.getTenants();
173 assertNotNull(tenantCaches);
174 assertEquals(1, tenantCaches.size());
175 assertTrue(tenantCaches.containsKey(TENANT_NAME));
177 TenantCache tenantCache = tenantCaches.get(TENANT_NAME);
179 assertEquals(TENANT_ID, tenantCache.getTenantId());
180 assertEquals(TENANT_NAME, tenantCache.getTenantName());
181 assertEquals(USER_ID, tenantCache.getUserid());
183 ServiceCatalog catalog = tenantCache.getServiceCatalog();
184 assertNotNull(catalog);
186 System.out.println(catalog.toString());
187 List<String> serviceTypes = catalog.getServiceTypes();
188 assertNotNull(serviceTypes);
189 assertEquals(12, serviceTypes.size());
191 assertEquals(TENANT_NAME, catalog.getProjectName());
192 assertEquals(TENANT_ID, catalog.getProjectId());
194 Set<String> regionNames = catalog.getRegions();
195 assertNotNull(regionNames);
196 assertEquals(1, regionNames.size());
197 assertTrue(regionNames.contains(REGION_NAME));
199 List<?> endpoints = catalog.getEndpoints(ServiceCatalog.IDENTITY_SERVICE);
200 assertNotNull(endpoints);
201 assertEquals(1, endpoints.size());
202 Endpoint endpoint = (Endpoint) endpoints.get(0);
203 assertNotNull(endpoint);
204 assertEquals(REGION_NAME, endpoint.getRegion());
205 assertEquals(IDENTITY_URL, endpoint.getPublicURL());
207 endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);
208 assertNotNull(endpoints);
209 assertEquals(1, endpoints.size());
210 endpoint = (Endpoint) endpoints.get(0);
211 assertNotNull(endpoint);
212 assertEquals(REGION_NAME, endpoint.getRegion());
214 endpoints = catalog.getEndpoints(ServiceCatalog.VOLUME_SERVICE);
215 assertNotNull(endpoints);
216 assertEquals(1, endpoints.size());
217 endpoint = (Endpoint) endpoints.get(0);
218 assertNotNull(endpoint);
219 assertEquals(REGION_NAME, endpoint.getRegion());
221 endpoints = catalog.getEndpoints(ServiceCatalog.IMAGE_SERVICE);
222 assertNotNull(endpoints);
223 assertEquals(1, endpoints.size());
224 endpoint = (Endpoint) endpoints.get(0);
225 assertNotNull(endpoint);
226 assertEquals(REGION_NAME, endpoint.getRegion());
228 endpoints = catalog.getEndpoints(ServiceCatalog.NETWORK_SERVICE);
229 assertNotNull(endpoints);
230 assertEquals(1, endpoints.size());
231 endpoint = (Endpoint) endpoints.get(0);
232 assertNotNull(endpoint);
233 assertEquals(REGION_NAME, endpoint.getRegion());
235 assertTrue(catalog.isServicePublished(ServiceCatalog.IDENTITY_SERVICE));
236 assertTrue(catalog.isServicePublished(ServiceCatalog.COMPUTE_SERVICE));
237 assertTrue(catalog.isServicePublished(ServiceCatalog.VOLUME_SERVICE));
238 assertTrue(catalog.isServicePublished(ServiceCatalog.IMAGE_SERVICE));
239 assertTrue(catalog.isServicePublished(ServiceCatalog.NETWORK_SERVICE));
243 * This test case is used to actually validate that a server has been restarted from an already
246 * @throws ZoneException If the login cannot be performed because the principal and/or
247 * credentials are invalid.
248 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
249 * the expected argument(s) are not defined or are invalid
250 * @throws IllegalStateException If the identity service is not available or cannot be created
251 * @throws IOException if an I/O error occurs
252 * @throws APPCException
256 public void testRestartRunningServer()
257 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
258 Properties properties = new Properties();
259 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
260 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
261 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
262 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
263 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
265 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
266 context.login(PRINCIPAL, CREDENTIAL);
267 VMURL vm = VMURL.parseURL(SERVER_URL);
269 ComputeService computeService = context.getComputeService();
270 Server server = computeService.getServer(vm.getServerId());
271 if (!server.getStatus().equals(Status.RUNNING)) {
273 assertTrue(waitForStateChange(server, Status.RUNNING));
276 Map<String, String> params = new HashMap<>();
277 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
278 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
279 SvcLogicContext svcContext = new SvcLogicContext();
281 server = adapter.restartServer(params, svcContext);
283 assertEquals(Server.Status.RUNNING, server.getStatus());
288 /****************************************/
290 * Tests that the vmStatuschecker method works and returns the correct status of the VM
293 * @throws ZoneException If the login cannot be performed because the principal and/or
294 * credentials are invalid.
295 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
296 * the expected argument(s) are not defined or are invalid
297 * @throws IllegalStateException If the identity service is not available or cannot be created
298 * @throws IOException if an I/O error occurs
299 * @throws UnknownProviderException If the provider cannot be found
303 public void testVmStatuschecker() throws IllegalStateException, IllegalArgumentException, ZoneException,
304 UnknownProviderException, IOException {
305 Properties properties = new Properties();
306 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
307 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
308 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
309 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
310 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
312 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
313 context.login(PRINCIPAL, CREDENTIAL);
314 VMURL vm = VMURL.parseURL(SERVER_URL);
316 ComputeService computeService = context.getComputeService();
317 Server server = computeService.getServer(vm.getServerId());
318 if (!server.getStatus().equals(Status.RUNNING)) {
320 assertTrue(waitForStateChange(server, Status.RUNNING));
322 // or instead of the if-block, can ensureRunning(server) be used?
323 ensureRunning(server);
324 assertEquals(Server.Status.RUNNING, server.getStatus());
328 /****************************************/
332 * Tests that we can restart a server that is already stopped
334 * @throws ZoneException If the login cannot be performed because the principal and/or
335 * credentials are invalid.
336 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
337 * the expected argument(s) are not defined or are invalid.
338 * @throws IllegalStateException If the identity service is not available or cannot be created
339 * @throws IOException if an I/O error occurs
340 * @throws APPCException
344 public void testRestartStoppedServer()
345 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
346 Properties properties = new Properties();
347 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
348 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
349 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
350 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
352 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
353 context.login(PRINCIPAL, CREDENTIAL);
354 VMURL vm = VMURL.parseURL(SERVER_URL);
356 ComputeService computeService = context.getComputeService();
357 Server server = computeService.getServer(vm.getServerId());
358 if (!server.getStatus().equals(Status.READY)) {
360 assertTrue(waitForStateChange(server, Status.READY));
363 Map<String, String> params = new HashMap<>();
364 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
365 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
366 SvcLogicContext svcContext = new SvcLogicContext();
368 server = adapter.restartServer(params, svcContext);
370 assertEquals(Server.Status.RUNNING, server.getStatus());
376 * Tests that we can rebuild a running server (not created from a bootable volume)
378 * @throws ZoneException If the login cannot be performed because the principal and/or
379 * credentials are invalid.
380 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
381 * the expected argument(s) are not defined or are invalid.
382 * @throws IllegalStateException If the identity service is not available or cannot be created
383 * @throws UnknownProviderException If the provider cannot be found
384 * @throws IOException if an I/O error occurs
385 * @throws APPCException If the server cannot be rebuilt for some reason
389 public void testRebuildRunningServer()
390 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
391 Properties properties = new Properties();
392 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
393 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
394 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
395 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
397 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
398 context.login(PRINCIPAL, CREDENTIAL);
399 VMURL vm = VMURL.parseURL(SERVER_URL);
401 ComputeService computeService = context.getComputeService();
402 Server server = computeService.getServer(vm.getServerId());
403 ensureRunning(server);
405 Map<String, String> params = new HashMap<>();
406 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
407 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
408 SvcLogicContext svcContext = new SvcLogicContext();
410 server = adapter.rebuildServer(params, svcContext);
411 assertTrue(waitForStateChange(server, Status.RUNNING));
417 * Tests that we can rebuild a paused server (not created from a bootable volume)
419 * @throws ZoneException If the login cannot be performed because the principal and/or
420 * credentials are invalid.
421 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
422 * the expected argument(s) are not defined or are invalid.
423 * @throws IllegalStateException If the identity service is not available or cannot be created
424 * @throws UnknownProviderException If the provider cannot be found
425 * @throws IOException if an I/O error occurs
426 * @throws APPCException If the server cannot be rebuilt for some reason
430 public void testRebuildPausedServer()
431 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
432 Properties properties = new Properties();
433 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
434 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
435 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
436 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
438 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
439 context.login(PRINCIPAL, CREDENTIAL);
440 VMURL vm = VMURL.parseURL(SERVER_URL);
442 ComputeService computeService = context.getComputeService();
443 Server server = computeService.getServer(vm.getServerId());
444 ensurePaused(server);
446 Map<String, String> params = new HashMap<>();
447 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
448 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
449 SvcLogicContext svcContext = new SvcLogicContext();
451 server = adapter.rebuildServer(params, svcContext);
452 assertTrue(waitForStateChange(server, Status.RUNNING));
457 * Tests that we can rebuild a paused server (not created from a bootable volume)
459 * @throws ZoneException If the login cannot be performed because the principal and/or
460 * credentials are invalid.
461 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
462 * the expected argument(s) are not defined or are invalid.
463 * @throws IllegalStateException If the identity service is not available or cannot be created
464 * @throws UnknownProviderException If the provider cannot be found
465 * @throws IOException if an I/O error occurs
466 * @throws APPCException If the server cannot be rebuilt for some reason
470 public void testRebuildSuspendedServer()
471 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
472 Properties properties = new Properties();
473 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
474 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
475 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
476 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
478 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
479 context.login(PRINCIPAL, CREDENTIAL);
480 VMURL vm = VMURL.parseURL(SERVER_URL);
482 ComputeService computeService = context.getComputeService();
483 Server server = computeService.getServer(vm.getServerId());
484 ensureSuspended(server);
486 Map<String, String> params = new HashMap<>();
487 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
488 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
489 SvcLogicContext svcContext = new SvcLogicContext();
491 server = adapter.rebuildServer(params, svcContext);
492 assertTrue(waitForStateChange(server, Status.RUNNING));
497 * Tests that we can rebuild a paused server (not created from a bootable volume)
499 * @throws ZoneException If the login cannot be performed because the principal and/or
500 * credentials are invalid.
501 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
502 * the expected argument(s) are not defined or are invalid.
503 * @throws IllegalStateException If the identity service is not available or cannot be created
504 * @throws UnknownProviderException If the provider cannot be found
505 * @throws IOException if an I/O error occurs
506 * @throws APPCException If the server cannot be rebuilt for some reason
510 public void testRebuildStoppedServer()
511 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
512 Properties properties = new Properties();
513 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
514 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
515 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
516 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
518 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
519 context.login(PRINCIPAL, CREDENTIAL);
520 VMURL vm = VMURL.parseURL(SERVER_URL);
522 ComputeService computeService = context.getComputeService();
523 Server server = computeService.getServer(vm.getServerId());
524 ensureStopped(server);
526 Map<String, String> params = new HashMap<>();
527 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
528 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
529 SvcLogicContext svcContext = new SvcLogicContext();
531 server = adapter.rebuildServer(params, svcContext);
532 assertTrue(waitForStateChange(server, Status.RUNNING));
537 * Test subsequent action on second vm in different Tenant resulting in {"itemNotFound":
538 * {"message": "Instance could not be found", "code": 404}}
540 * @throws ZoneException If the login cannot be performed because the principal and/or
541 * credentials are invalid.
542 * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
543 * the expected argument(s) are not defined or are invalid
544 * @throws IllegalStateException If the identity service is not available or cannot be created
545 * @throws IOException if an I/O error occurs
546 * @throws APPCException
550 public void testTenantVerification()
551 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
553 Properties properties = new Properties();
554 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
555 properties.setProperty(ContextFactory.PROPERTY_TENANT, "APP-C");
556 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
559 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
561 // try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
562 // context.login("AppC", "AppC");
564 // call lookupServer on vm in defined tenant "APP-C_TLV"
565 VMURL vm = VMURL.parseURL(vmUrl);
567 Map<String, String> params = new HashMap<>();
568 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
569 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
570 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
571 SvcLogicContext svcContext = new SvcLogicContext();
575 System.out.println("\n--------------------Begin lookupServer on tenant 1--------------------");
576 start = System.currentTimeMillis();
577 Server server = adapter.lookupServer(params, svcContext);
578 end = System.currentTimeMillis();
580 System.out.println(String.format("lookupServer on tenant 1 took %ds", (end - start) / 1000));
581 System.out.println("----------------------------------------------------------------------\n");
582 assertNotNull(server);
584 // repeat to show that context is reused for second request
585 System.out.println("\n-----------------Begin repeat lookupServer on tenant 1----------------");
586 start = System.currentTimeMillis();
587 server = adapter.lookupServer(params, svcContext);
588 end = System.currentTimeMillis();
590 System.out.println(String.format("Repeat lookupServer on tenant 1 took %ds", (end - start) / 1000));
591 System.out.println("----------------------------------------------------------------------\n");
592 assertNotNull(server);
594 // call lookupServer on vm in second tenant "Play"
595 // This is where we would fail due to using the previous
597 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
598 vm = VMURL.parseURL(vmUrl);
599 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
601 System.out.println("\n--------------------Begin lookupServer on tenant 2--------------------");
602 start = System.currentTimeMillis();
603 server = adapter.lookupServer(params, svcContext);
604 end = System.currentTimeMillis();
605 System.out.println(String.format("\nlookupServer on tenant 2 took %ds", (end - start) / 1000));
606 System.out.println("----------------------------------------------------------------------\n");
607 assertNotNull(server);
609 // call lookupServer on vm in non-existing tenant
610 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
611 vm = VMURL.parseURL(vmUrl);
612 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
614 System.out.println("\n--------------Begin lookupServer on non-existant tenant--------------");
615 start = System.currentTimeMillis();
616 server = adapter.lookupServer(params, svcContext);
617 end = System.currentTimeMillis();
618 System.out.println(String.format("\nlookupServer on tenant 3 took %ds", (end - start) / 1000));
619 System.out.println("----------------------------------------------------------------------\n");
625 /****************************************/
629 public void testSnapshotServer() throws Exception {
630 Properties properties = new Properties();
631 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
632 // properties.setProperty(ContextFactory.PROPERTY_REGION, "");
633 properties.setProperty(ContextFactory.PROPERTY_TENANT, "Play");
634 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
637 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
639 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
640 context.login("AppC", "AppC");
641 VMURL vm = VMURL.parseURL(vmUrl);
643 Map<String, String> params = new HashMap<>();
644 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
645 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
646 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
647 SvcLogicContext svcContext = new SvcLogicContext();
651 start = System.currentTimeMillis();
652 Image image = adapter.createSnapshot(params, svcContext);
653 end = System.currentTimeMillis();
655 System.out.println(String.format("Image ID: %s", image.getId()));
656 System.out.println(String.format("Snapshot took %ds", (end - start) / 1000));
658 start = System.currentTimeMillis();
659 adapter.rebuildServer(params, svcContext);
660 end = System.currentTimeMillis();
661 System.out.println(String.format("Rebuild took %ds", (end - start) / 1000));
667 * Ensures that the server is in stopped (shutdown) state prior to test
669 * @param server The server to ensure is stopped
670 * @throws ZoneException If the server can't be operated upon for some reason
672 @SuppressWarnings("nls")
673 private static void ensureStopped(Server server) throws ZoneException {
674 switch (server.getStatus()) {
679 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
680 Server.Status.SUSPENDED, Server.Status.ERROR);
681 ensureSuspended(server);
686 waitForStateChange(server, Server.Status.RUNNING);
688 waitForStateChange(server, Server.Status.READY);
693 waitForStateChange(server, Server.Status.RUNNING);
695 waitForStateChange(server, Server.Status.READY);
700 waitForStateChange(server, Server.Status.READY);
706 fail("Server state is not valid for test - " + server.getStatus().name());
711 * Ensures that the server is in suspended state prior to test
713 * @param server The server to ensure is suspended
714 * @throws ZoneException If the server can't be operated upon for some reason
716 @SuppressWarnings("nls")
717 private static void ensureSuspended(Server server) throws ZoneException {
718 switch (server.getStatus()) {
723 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
724 Server.Status.SUSPENDED, Server.Status.ERROR);
725 ensureSuspended(server);
730 waitForStateChange(server, Server.Status.RUNNING);
732 waitForStateChange(server, Server.Status.SUSPENDED);
737 waitForStateChange(server, Server.Status.RUNNING);
739 waitForStateChange(server, Server.Status.SUSPENDED);
744 waitForStateChange(server, Server.Status.SUSPENDED);
750 fail("Server state is not valid for test - " + server.getStatus().name());
755 * This method makes sure that the indicated server is running before performing a test
757 * @param server The server to ensure is running
758 * @throws ZoneException If the server can't be operated upon
760 @SuppressWarnings("nls")
761 private static void ensureRunning(Server server) throws ZoneException {
762 switch (server.getStatus()) {
767 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
768 Server.Status.SUSPENDED, Server.Status.ERROR);
769 ensureRunning(server);
774 waitForStateChange(server, Server.Status.RUNNING);
779 waitForStateChange(server, Server.Status.RUNNING);
784 waitForStateChange(server, Server.Status.RUNNING);
790 fail("Server state is not valid for test - " + server.getStatus().name());
795 * This method will make sure that the server we are testing is paused
797 * @param server The server to make sure is paused for the test
798 * @throws ZoneException If anything fails
800 @SuppressWarnings("nls")
801 private static void ensurePaused(Server server) throws ZoneException {
802 switch (server.getStatus()) {
807 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
808 Server.Status.SUSPENDED, Server.Status.ERROR);
809 ensurePaused(server);
814 waitForStateChange(server, Server.Status.RUNNING);
816 waitForStateChange(server, Server.Status.PAUSED);
821 waitForStateChange(server, Server.Status.PAUSED);
826 waitForStateChange(server, Server.Status.RUNNING);
828 waitForStateChange(server, Server.Status.PAUSED);
834 fail("Server state is not valid for test - " + server.getStatus().name());
839 * Enter a pool-wait loop checking the server state to see if it has entered one of the desired
842 * This method checks the state of the server periodically for one of the desired states. When
843 * the server enters one of the desired states, the method returns a successful indication
844 * (true). If the server never enters one of the desired states within the alloted timeout
845 * period, then the method returns a failed response (false). No exceptions are thrown from this
849 * @param server The server to wait on
850 * @param desiredStates A variable list of desired states, any one of which is allowed.
851 * @return True if the server entered one of the desired states, and false if not and the wait
854 private static boolean waitForStateChange(Server server, Server.Status... desiredStates) {
855 int timeout = ConfigurationFactory.getConfiguration()
856 .getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
857 long limit = System.currentTimeMillis() + (timeout * 1000);
861 while (limit > System.currentTimeMillis()) {
863 for (Server.Status desiredState : desiredStates) {
864 if (server.getStatus().equals(desiredState)) {
870 Thread.sleep(10000L);
871 } catch (InterruptedException e) {
875 } catch (ZoneException e) {
883 * @Test public void testTerminateStack() throws IllegalStateException,
884 * IllegalArgumentException, ZoneException, UnknownProviderException, IOException { Properties
885 * properties = new Properties(); properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL,
886 * IDENTITY_URL); properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
887 * properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
888 * properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
889 * properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true"); try (Context context =
890 * ContextFactory.getContext(PROVIDER_TYPE, properties)) { context.login(PRINCIPAL, CREDENTIAL);
891 * VMURL vm = VMURL.parseURL(SERVER_URL); ComputeService computeService =
892 * context.getComputeService(); Server server = computeService.getServer(vm.getServerId()); if
893 * (!server.getStatus().equals(Status.RUNNING)) { server.start();
894 * assertTrue(waitForStateChange(server, Status.RUNNING)); } Map<String, String> params = new
895 * HashMap<>(); params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
896 * params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME); SvcLogicContext svcContext
897 * = new SvcLogicContext(); Stack stack = adapter.terminateStack(params, svcContext);
898 * assertNotNull(stack); } }