2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.iaas.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Properties;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43 import org.openecomp.appc.Constants;
44 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
45 import org.openecomp.appc.adapter.iaas.impl.ProviderAdapterImpl;
46 import org.openecomp.appc.adapter.iaas.impl.ProviderCache;
47 import org.openecomp.appc.adapter.iaas.impl.ServiceCatalog;
48 import org.openecomp.appc.adapter.iaas.impl.TenantCache;
49 import org.openecomp.appc.adapter.iaas.impl.VMURL;
50 import org.openecomp.appc.configuration.ConfigurationFactory;
51 import org.openecomp.appc.exceptions.APPCException;
52 import org.openecomp.appc.exceptions.UnknownProviderException;
53 import com.att.cdp.exceptions.ZoneException;
54 import com.att.cdp.zones.ComputeService;
55 import com.att.cdp.zones.Context;
56 import com.att.cdp.zones.ContextFactory;
57 import com.att.cdp.zones.model.Image;
58 import com.att.cdp.zones.model.Server;
59 import com.att.cdp.zones.model.Server.Status;
60 import org.openecomp.sdnc.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 to change the
101 * internal state accordingly.
103 * @throws NoSuchFieldException
104 * if the field(s) dont exist
105 * @throws SecurityException
106 * if reflective access is not allowed
107 * @throws NoSuchMethodException
108 * If the method(s) dont exist
110 @SuppressWarnings("nls")
112 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
113 providerAdapterImplClass = ProviderAdapterImpl.class;
114 configurationFactoryClass = ConfigurationFactory.class;
116 providerCacheField = providerAdapterImplClass.getDeclaredField("providerCache");
117 providerCacheField.setAccessible(true);
119 configField = configurationFactoryClass.getDeclaredField("config");
120 configField.setAccessible(true);
122 Properties props = ConfigurationFactory.getConfiguration().getProperties();
123 IDENTITY_URL = props.getProperty("provider1.identity");
124 PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");
125 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
126 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
127 TENANT_ID = props.getProperty("provider1.tenant1.id", "abcde12345fghijk6789lmnopq123rst");
128 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
129 SERVER_URL = props.getProperty("test.url");
133 * Setup the test environment.
135 * @throws IllegalAccessException
136 * if this Field object is enforcing Java language access control and the underlying field is either
137 * inaccessible or final.
138 * @throws IllegalArgumentException
139 * if the specified object is not an instance of the class or interface declaring the underlying field
140 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
141 * @throws NullPointerException
142 * if the specified object is null and the field is an instance field.
143 * @throws ExceptionInInitializerError
144 * if the initialization provoked by this method fails.
147 public void setup() throws IllegalArgumentException, IllegalAccessException {
148 configField.set(null, null);
149 Properties properties = new Properties();
150 adapter = new ProviderAdapterImpl(properties);
154 * This method inspects the provider adapter implementation to make sure that the cache of providers and tenants, as
155 * well as the service catalog, and all pools of contexts have been set up correctly.
157 * @throws IllegalAccessException
158 * if this Field object is enforcing Java language access control and the underlying field is
160 * @throws IllegalArgumentException
161 * if the specified object is not an instance of the class or interface declaring the underlying field
162 * (or a subclass or implementor thereof).
169 public void validateCacheIsCreatedCorrectly() throws IllegalArgumentException, IllegalAccessException {
170 Map<String, ProviderCache> providerCaches = (Map<String, ProviderCache>) providerCacheField.get(adapter);
172 assertNotNull(providerCaches);
173 assertEquals(1, providerCaches.size());
174 assertTrue(providerCaches.containsKey(PROVIDER_NAME));
176 ProviderCache providerCache = providerCaches.get(PROVIDER_NAME);
177 assertEquals(PROVIDER_NAME, providerCache.getProviderName());
178 assertEquals(PROVIDER_TYPE, providerCache.getProviderType());
180 Map<String, TenantCache> tenantCaches = providerCache.getTenants();
181 assertNotNull(tenantCaches);
182 assertEquals(1, tenantCaches.size());
183 assertTrue(tenantCaches.containsKey(TENANT_NAME));
185 TenantCache tenantCache = tenantCaches.get(TENANT_NAME);
187 assertEquals(TENANT_ID, tenantCache.getTenantId());
188 assertEquals(TENANT_NAME, tenantCache.getTenantName());
189 assertEquals(USER_ID, tenantCache.getUserid());
191 ServiceCatalog catalog = tenantCache.getServiceCatalog();
192 assertNotNull(catalog);
194 System.out.println(catalog.toString());
195 List<String> serviceTypes = catalog.getServiceTypes();
196 assertNotNull(serviceTypes);
197 assertEquals(12, serviceTypes.size());
199 assertEquals(TENANT_NAME, catalog.getTenantName());
200 assertEquals(TENANT_ID, catalog.getTenantId());
202 Set<String> regionNames = catalog.getRegions();
203 assertNotNull(regionNames);
204 assertEquals(1, regionNames.size());
205 assertTrue(regionNames.contains(REGION_NAME));
207 List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.IDENTITY_SERVICE);
208 assertNotNull(endpoints);
209 assertEquals(1, endpoints.size());
210 Endpoint endpoint = endpoints.get(0);
211 assertNotNull(endpoint);
212 assertEquals(REGION_NAME, endpoint.getRegion());
213 assertEquals(IDENTITY_URL, endpoint.getPublicURL());
215 endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);
216 assertNotNull(endpoints);
217 assertEquals(1, endpoints.size());
218 endpoint = endpoints.get(0);
219 assertNotNull(endpoint);
220 assertEquals(REGION_NAME, endpoint.getRegion());
222 endpoints = catalog.getEndpoints(ServiceCatalog.VOLUME_SERVICE);
223 assertNotNull(endpoints);
224 assertEquals(1, endpoints.size());
225 endpoint = endpoints.get(0);
226 assertNotNull(endpoint);
227 assertEquals(REGION_NAME, endpoint.getRegion());
229 endpoints = catalog.getEndpoints(ServiceCatalog.IMAGE_SERVICE);
230 assertNotNull(endpoints);
231 assertEquals(1, endpoints.size());
232 endpoint = endpoints.get(0);
233 assertNotNull(endpoint);
234 assertEquals(REGION_NAME, endpoint.getRegion());
236 endpoints = catalog.getEndpoints(ServiceCatalog.NETWORK_SERVICE);
237 assertNotNull(endpoints);
238 assertEquals(1, endpoints.size());
239 endpoint = endpoints.get(0);
240 assertNotNull(endpoint);
241 assertEquals(REGION_NAME, endpoint.getRegion());
243 assertTrue(catalog.isServicePublished(ServiceCatalog.IDENTITY_SERVICE));
244 assertTrue(catalog.isServicePublished(ServiceCatalog.COMPUTE_SERVICE));
245 assertTrue(catalog.isServicePublished(ServiceCatalog.VOLUME_SERVICE));
246 assertTrue(catalog.isServicePublished(ServiceCatalog.IMAGE_SERVICE));
247 assertTrue(catalog.isServicePublished(ServiceCatalog.NETWORK_SERVICE));
251 * This test case is used to actually validate that a server has been restarted from an already running state
253 * @throws ZoneException
254 * If the login cannot be performed because the principal and/or credentials are invalid.
255 * @throws IllegalArgumentException
256 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
258 * @throws IllegalStateException
259 * If the identity service is not available or cannot be created
260 * @throws IOException
261 * if an I/O error occurs
262 * @throws APPCException
266 public void testRestartRunningServer()
267 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
268 Properties properties = new Properties();
269 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
270 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
271 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
272 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
273 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
275 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
276 context.login(PRINCIPAL, CREDENTIAL);
277 VMURL vm = VMURL.parseURL(SERVER_URL);
279 ComputeService computeService = context.getComputeService();
280 Server server = computeService.getServer(vm.getServerId());
281 if (!server.getStatus().equals(Status.RUNNING)) {
283 assertTrue(waitForStateChange(server, Status.RUNNING));
286 Map<String, String> params = new HashMap<>();
287 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
288 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
289 SvcLogicContext svcContext = new SvcLogicContext();
291 server = adapter.restartServer(params, svcContext);
293 assertEquals(Server.Status.RUNNING, server.getStatus());
298 /****************************************/
300 * Tests that the vmStatuschecker method works and returns the correct status of the VM requested
302 * @throws ZoneException
303 * If the login cannot be performed because the principal and/or credentials are invalid.
304 * @throws IllegalArgumentException
305 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
307 * @throws IllegalStateException
308 * If the identity service is not available or cannot be created
309 * @throws IOException
310 * if an I/O error occurs
311 * @throws UnknownProviderException
312 * If the provider cannot be found
316 public void testVmStatuschecker() throws IllegalStateException, IllegalArgumentException, ZoneException,
317 UnknownProviderException, IOException {
318 Properties properties = new Properties();
319 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
320 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
321 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
322 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
323 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
325 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
326 context.login(PRINCIPAL, CREDENTIAL);
327 VMURL vm = VMURL.parseURL(SERVER_URL);
329 ComputeService computeService = context.getComputeService();
330 Server server = computeService.getServer(vm.getServerId());
331 if (!server.getStatus().equals(Status.RUNNING)) {
333 assertTrue(waitForStateChange(server, Status.RUNNING));}
334 //or instead of the if-block, can ensureRunning(server) be used?
335 ensureRunning(server);
336 assertEquals(Server.Status.RUNNING, server.getStatus());
339 /****************************************/
343 * Tests that we can restart a server that is already stopped
345 * @throws ZoneException
346 * If the login cannot be performed because the principal and/or credentials are invalid.
347 * @throws IllegalArgumentException
348 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
350 * @throws IllegalStateException
351 * If the identity service is not available or cannot be created
352 * @throws IOException
353 * if an I/O error occurs
354 * @throws APPCException
358 public void testRestartStoppedServer()
359 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
360 Properties properties = new Properties();
361 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
362 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
363 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
364 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
366 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
367 context.login(PRINCIPAL, CREDENTIAL);
368 VMURL vm = VMURL.parseURL(SERVER_URL);
370 ComputeService computeService = context.getComputeService();
371 Server server = computeService.getServer(vm.getServerId());
372 if (!server.getStatus().equals(Status.READY)) {
374 assertTrue(waitForStateChange(server, Status.READY));
377 Map<String, String> params = new HashMap<>();
378 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
379 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
380 SvcLogicContext svcContext = new SvcLogicContext();
382 server = adapter.restartServer(params, svcContext);
384 assertEquals(Server.Status.RUNNING, server.getStatus());
390 * Tests that we can rebuild a running server (not created from a bootable volume)
392 * @throws ZoneException
393 * If the login cannot be performed because the principal and/or credentials are invalid.
394 * @throws IllegalArgumentException
395 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
397 * @throws IllegalStateException
398 * If the identity service is not available or cannot be created
399 * @throws UnknownProviderException
400 * If the provider cannot be found
401 * @throws IOException
402 * if an I/O error occurs
403 * @throws APPCException
404 * If the server cannot be rebuilt for some reason
408 public void testRebuildRunningServer()
409 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
410 Properties properties = new Properties();
411 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
412 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
413 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
414 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
416 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
417 context.login(PRINCIPAL, CREDENTIAL);
418 VMURL vm = VMURL.parseURL(SERVER_URL);
420 ComputeService computeService = context.getComputeService();
421 Server server = computeService.getServer(vm.getServerId());
422 ensureRunning(server);
424 Map<String, String> params = new HashMap<>();
425 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
426 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
427 SvcLogicContext svcContext = new SvcLogicContext();
429 server = adapter.rebuildServer(params, svcContext);
430 assertTrue(waitForStateChange(server, Status.RUNNING));
436 * Tests that we can rebuild a paused server (not created from a bootable volume)
438 * @throws ZoneException
439 * If the login cannot be performed because the principal and/or credentials are invalid.
440 * @throws IllegalArgumentException
441 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
443 * @throws IllegalStateException
444 * If the identity service is not available or cannot be created
445 * @throws UnknownProviderException
446 * If the provider cannot be found
447 * @throws IOException
448 * if an I/O error occurs
449 * @throws APPCException
450 * If the server cannot be rebuilt for some reason
454 public void testRebuildPausedServer()
455 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
456 Properties properties = new Properties();
457 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
458 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
459 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
460 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
462 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
463 context.login(PRINCIPAL, CREDENTIAL);
464 VMURL vm = VMURL.parseURL(SERVER_URL);
466 ComputeService computeService = context.getComputeService();
467 Server server = computeService.getServer(vm.getServerId());
468 ensurePaused(server);
470 Map<String, String> params = new HashMap<>();
471 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
472 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
473 SvcLogicContext svcContext = new SvcLogicContext();
475 server = adapter.rebuildServer(params, svcContext);
476 assertTrue(waitForStateChange(server, Status.RUNNING));
481 * Tests that we can rebuild a paused server (not created from a bootable volume)
483 * @throws ZoneException
484 * If the login cannot be performed because the principal and/or credentials are invalid.
485 * @throws IllegalArgumentException
486 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
488 * @throws IllegalStateException
489 * If the identity service is not available or cannot be created
490 * @throws UnknownProviderException
491 * If the provider cannot be found
492 * @throws IOException
493 * if an I/O error occurs
494 * @throws APPCException
495 * If the server cannot be rebuilt for some reason
499 public void testRebuildSuspendedServer()
500 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
501 Properties properties = new Properties();
502 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
503 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
504 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
505 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
507 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
508 context.login(PRINCIPAL, CREDENTIAL);
509 VMURL vm = VMURL.parseURL(SERVER_URL);
511 ComputeService computeService = context.getComputeService();
512 Server server = computeService.getServer(vm.getServerId());
513 ensureSuspended(server);
515 Map<String, String> params = new HashMap<>();
516 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
517 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
518 SvcLogicContext svcContext = new SvcLogicContext();
520 server = adapter.rebuildServer(params, svcContext);
521 assertTrue(waitForStateChange(server, Status.RUNNING));
526 * Tests that we can rebuild a paused server (not created from a bootable volume)
528 * @throws ZoneException
529 * If the login cannot be performed because the principal and/or credentials are invalid.
530 * @throws IllegalArgumentException
531 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
533 * @throws IllegalStateException
534 * If the identity service is not available or cannot be created
535 * @throws UnknownProviderException
536 * If the provider cannot be found
537 * @throws IOException
538 * if an I/O error occurs
539 * @throws APPCException
540 * If the server cannot be rebuilt for some reason
544 public void testRebuildStoppedServer()
545 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
546 Properties properties = new Properties();
547 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
548 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
549 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
550 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
552 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
553 context.login(PRINCIPAL, CREDENTIAL);
554 VMURL vm = VMURL.parseURL(SERVER_URL);
556 ComputeService computeService = context.getComputeService();
557 Server server = computeService.getServer(vm.getServerId());
558 ensureStopped(server);
560 Map<String, String> params = new HashMap<>();
561 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
562 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
563 SvcLogicContext svcContext = new SvcLogicContext();
565 server = adapter.rebuildServer(params, svcContext);
566 assertTrue(waitForStateChange(server, Status.RUNNING));
571 * Test subsequent action on second vm in different Tenant resulting in {"itemNotFound": {"message": "Instance could not be found", "code": 404}}
573 * @throws ZoneException
574 * If the login cannot be performed because the principal and/or credentials are invalid.
575 * @throws IllegalArgumentException
576 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
578 * @throws IllegalStateException
579 * If the identity service is not available or cannot be created
580 * @throws IOException
581 * if an I/O error occurs
582 * @throws APPCException
586 public void testTenantVerification() throws IllegalStateException, IllegalArgumentException, ZoneException,
587 IOException, APPCException {
589 Properties properties = new Properties();
590 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
591 properties.setProperty(ContextFactory.PROPERTY_TENANT, "APP-C");
592 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
595 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
597 //try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
598 // context.login("AppC", "AppC");
600 // call lookupServer on vm in defined tenant "APP-C_TLV"
601 VMURL vm = VMURL.parseURL(vmUrl);
603 Map<String, String> params = new HashMap<>();
604 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
605 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
606 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
607 SvcLogicContext svcContext = new SvcLogicContext();
611 System.out.println("\n--------------------Begin lookupServer on tenant 1--------------------");
612 start = System.currentTimeMillis();
613 Server server = adapter.lookupServer(params, svcContext);
614 end = System.currentTimeMillis();
616 System.out.println(String.format("lookupServer on tenant 1 took %ds", (end - start) / 1000));
617 System.out.println("----------------------------------------------------------------------\n");
618 assertNotNull(server);
620 //repeat to show that context is reused for second request
621 System.out.println("\n-----------------Begin repeat lookupServer on tenant 1----------------");
622 start = System.currentTimeMillis();
623 server = adapter.lookupServer(params, svcContext);
624 end = System.currentTimeMillis();
626 System.out.println(String.format("Repeat lookupServer on tenant 1 took %ds", (end - start) / 1000));
627 System.out.println("----------------------------------------------------------------------\n");
628 assertNotNull(server);
630 // call lookupServer on vm in second tenant "Play"
631 // This is where we would fail due to using the previous
633 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
634 vm = VMURL.parseURL(vmUrl);
635 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
637 System.out.println("\n--------------------Begin lookupServer on tenant 2--------------------");
638 start = System.currentTimeMillis();
639 server = adapter.lookupServer(params, svcContext);
640 end = System.currentTimeMillis();
641 System.out.println(String.format("\nlookupServer on tenant 2 took %ds", (end - start) / 1000));
642 System.out.println("----------------------------------------------------------------------\n");
643 assertNotNull(server);
645 // call lookupServer on vm in non-existing tenant
646 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
647 vm = VMURL.parseURL(vmUrl);
648 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
650 System.out.println("\n--------------Begin lookupServer on non-existant tenant--------------");
651 start = System.currentTimeMillis();
652 server = adapter.lookupServer(params, svcContext);
653 end = System.currentTimeMillis();
654 System.out.println(String.format("\nlookupServer on tenant 3 took %ds", (end - start) / 1000));
655 System.out.println("----------------------------------------------------------------------\n");
660 /****************************************/
664 public void testSnapshotServer() throws Exception {
665 Properties properties = new Properties();
666 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
667 // properties.setProperty(ContextFactory.PROPERTY_REGION, "");
668 properties.setProperty(ContextFactory.PROPERTY_TENANT, "Play");
669 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
672 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
674 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
675 context.login("AppC", "AppC");
676 VMURL vm = VMURL.parseURL(vmUrl);
678 Map<String, String> params = new HashMap<>();
679 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
680 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
681 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
682 SvcLogicContext svcContext = new SvcLogicContext();
686 start = System.currentTimeMillis();
687 Image image = adapter.createSnapshot(params, svcContext);
688 end = System.currentTimeMillis();
690 System.out.println(String.format("Image ID: %s", image.getId()));
691 System.out.println(String.format("Snapshot took %ds", (end - start) / 1000));
693 start = System.currentTimeMillis();
694 adapter.rebuildServer(params, svcContext);
695 end = System.currentTimeMillis();
696 System.out.println(String.format("Rebuild took %ds", (end - start) / 1000));
702 * Ensures that the server is in stopped (shutdown) state prior to test
705 * The server to ensure is stopped
706 * @throws ZoneException
707 * If the server can't be operated upon for some reason
709 @SuppressWarnings("nls")
710 private static void ensureStopped(Server server) throws ZoneException {
711 switch (server.getStatus()) {
716 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
717 Server.Status.SUSPENDED, Server.Status.ERROR);
718 ensureSuspended(server);
723 waitForStateChange(server, Server.Status.RUNNING);
725 waitForStateChange(server, Server.Status.READY);
730 waitForStateChange(server, Server.Status.RUNNING);
732 waitForStateChange(server, Server.Status.READY);
737 waitForStateChange(server, Server.Status.READY);
743 fail("Server state is not valid for test - " + server.getStatus().name());
748 * Ensures that the server is in suspended state prior to test
751 * The server to ensure is suspended
752 * @throws ZoneException
753 * If the server can't be operated upon for some reason
755 @SuppressWarnings("nls")
756 private static void ensureSuspended(Server server) throws ZoneException {
757 switch (server.getStatus()) {
762 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
763 Server.Status.SUSPENDED, Server.Status.ERROR);
764 ensureSuspended(server);
769 waitForStateChange(server, Server.Status.RUNNING);
771 waitForStateChange(server, Server.Status.SUSPENDED);
776 waitForStateChange(server, Server.Status.RUNNING);
778 waitForStateChange(server, Server.Status.SUSPENDED);
783 waitForStateChange(server, Server.Status.SUSPENDED);
789 fail("Server state is not valid for test - " + server.getStatus().name());
794 * This method makes sure that the indicated server is running before performing a test
797 * The server to ensure is running
798 * @throws ZoneException
799 * If the server can't be operated upon
801 @SuppressWarnings("nls")
802 private static void ensureRunning(Server server) throws ZoneException {
803 switch (server.getStatus()) {
808 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
809 Server.Status.SUSPENDED, Server.Status.ERROR);
810 ensureRunning(server);
815 waitForStateChange(server, Server.Status.RUNNING);
820 waitForStateChange(server, Server.Status.RUNNING);
825 waitForStateChange(server, Server.Status.RUNNING);
831 fail("Server state is not valid for test - " + server.getStatus().name());
836 * This method will make sure that the server we are testing is paused
839 * The server to make sure is paused for the test
840 * @throws ZoneException
843 @SuppressWarnings("nls")
844 private static void ensurePaused(Server server) throws ZoneException {
845 switch (server.getStatus()) {
850 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
851 Server.Status.SUSPENDED, Server.Status.ERROR);
852 ensurePaused(server);
857 waitForStateChange(server, Server.Status.RUNNING);
859 waitForStateChange(server, Server.Status.PAUSED);
864 waitForStateChange(server, Server.Status.PAUSED);
869 waitForStateChange(server, Server.Status.RUNNING);
871 waitForStateChange(server, Server.Status.PAUSED);
877 fail("Server state is not valid for test - " + server.getStatus().name());
882 * Enter a pool-wait loop checking the server state to see if it has entered one of the desired states or not.
884 * This method checks the state of the server periodically for one of the desired states. When the server enters one
885 * of the desired states, the method returns a successful indication (true). If the server never enters one of the
886 * desired states within the alloted timeout period, then the method returns a failed response (false). No
887 * exceptions are thrown from this method.
891 * The server to wait on
892 * @param desiredStates
893 * A variable list of desired states, any one of which is allowed.
894 * @return True if the server entered one of the desired states, and false if not and the wait loop timed out.
896 private static boolean waitForStateChange(Server server, Server.Status... desiredStates) {
898 ConfigurationFactory.getConfiguration().getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
899 long limit = System.currentTimeMillis() + (timeout * 1000);
903 while (limit > System.currentTimeMillis()) {
905 for (Server.Status desiredState : desiredStates) {
906 if (server.getStatus().equals(desiredState)) {
912 Thread.sleep(10000L);
913 } catch (InterruptedException e) {
917 } catch (ZoneException e) {
925 * @Test public void testTerminateStack() throws IllegalStateException, IllegalArgumentException, ZoneException,
926 * UnknownProviderException, IOException { Properties properties = new Properties();
927 * properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
928 * properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
929 * properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
930 * properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
931 * properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true"); try (Context context =
932 * ContextFactory.getContext(PROVIDER_TYPE, properties)) { context.login(PRINCIPAL, CREDENTIAL); VMURL vm =
933 * VMURL.parseURL(SERVER_URL); ComputeService computeService = context.getComputeService(); Server server =
934 * computeService.getServer(vm.getServerId()); if (!server.getStatus().equals(Status.RUNNING)) { server.start();
935 * assertTrue(waitForStateChange(server, Status.RUNNING)); } Map<String, String> params = new HashMap<>();
936 * params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL); params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME,
937 * PROVIDER_NAME); SvcLogicContext svcContext = new SvcLogicContext(); Stack stack = adapter.terminateStack(params,
938 * svcContext); assertNotNull(stack); } }