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;
33 import java.io.IOException;
34 import java.lang.reflect.Field;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Properties;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.openecomp.appc.Constants;
47 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
48 import org.openecomp.appc.adapter.iaas.impl.ProviderAdapterImpl;
49 import org.openecomp.appc.adapter.iaas.impl.ProviderCache;
50 import org.openecomp.appc.adapter.iaas.impl.ServiceCatalog;
51 import org.openecomp.appc.adapter.iaas.impl.TenantCache;
52 import org.openecomp.appc.adapter.iaas.impl.VMURL;
53 import org.openecomp.appc.configuration.ConfigurationFactory;
54 import org.openecomp.appc.exceptions.APPCException;
55 import org.openecomp.appc.exceptions.UnknownProviderException;
56 import com.att.cdp.exceptions.ZoneException;
57 import com.att.cdp.zones.ComputeService;
58 import com.att.cdp.zones.Context;
59 import com.att.cdp.zones.ContextFactory;
60 import com.att.cdp.zones.model.Image;
61 import com.att.cdp.zones.model.Server;
62 import com.att.cdp.zones.model.Server.Status;
63 import org.openecomp.sdnc.sli.SvcLogicContext;
65 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
68 * Test the ProviderAdapter implementation.
70 @Category(org.openecomp.appc.adapter.iaas.impl.TestProviderAdapterImpl.class)
71 public class TestProviderAdapterImpl {
73 @SuppressWarnings("nls")
74 private static final String PROVIDER_NAME = "ILAB";
76 @SuppressWarnings("nls")
77 private static final String PROVIDER_TYPE = "OpenStackProvider";
79 private static String IDENTITY_URL;
81 private static String PRINCIPAL;
83 private static String CREDENTIAL;
85 private static String TENANT_NAME;
87 private static String TENANT_ID;
89 private static String USER_ID;
91 private static String REGION_NAME;
93 private static String SERVER_URL;
95 private static Class<?> providerAdapterImplClass;
96 private static Class<?> configurationFactoryClass;
97 private static Field providerCacheField;
98 private static Field configField;
100 private ProviderAdapterImpl adapter;
103 * Use reflection to locate fields and methods so that they can be manipulated during the test to change the
104 * internal state accordingly.
106 * @throws NoSuchFieldException
107 * if the field(s) dont exist
108 * @throws SecurityException
109 * if reflective access is not allowed
110 * @throws NoSuchMethodException
111 * If the method(s) dont exist
113 @SuppressWarnings("nls")
115 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
116 providerAdapterImplClass = ProviderAdapterImpl.class;
117 configurationFactoryClass = ConfigurationFactory.class;
119 providerCacheField = providerAdapterImplClass.getDeclaredField("providerCache");
120 providerCacheField.setAccessible(true);
122 configField = configurationFactoryClass.getDeclaredField("config");
123 configField.setAccessible(true);
125 Properties props = ConfigurationFactory.getConfiguration().getProperties();
126 IDENTITY_URL = props.getProperty("provider1.identity");
127 PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");
128 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
129 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
130 TENANT_ID = props.getProperty("provider1.tenant1.id", "abcde12345fghijk6789lmnopq123rst");
131 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
132 SERVER_URL = props.getProperty("test.url");
136 * Setup the test environment.
138 * @throws IllegalAccessException
139 * if this Field object is enforcing Java language access control and the underlying field is either
140 * inaccessible or final.
141 * @throws IllegalArgumentException
142 * if the specified object is not an instance of the class or interface declaring the underlying field
143 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
144 * @throws NullPointerException
145 * if the specified object is null and the field is an instance field.
146 * @throws ExceptionInInitializerError
147 * if the initialization provoked by this method fails.
150 public void setup() throws IllegalArgumentException, IllegalAccessException {
151 configField.set(null, null);
152 Properties properties = new Properties();
153 adapter = new ProviderAdapterImpl(properties);
157 * This method inspects the provider adapter implementation to make sure that the cache of providers and tenants, as
158 * well as the service catalog, and all pools of contexts have been set up correctly.
160 * @throws IllegalAccessException
161 * if this Field object is enforcing Java language access control and the underlying field is
163 * @throws IllegalArgumentException
164 * if the specified object is not an instance of the class or interface declaring the underlying field
165 * (or a subclass or implementor thereof).
172 public void validateCacheIsCreatedCorrectly() throws IllegalArgumentException, IllegalAccessException {
173 Map<String, ProviderCache> providerCaches = (Map<String, ProviderCache>) providerCacheField.get(adapter);
175 assertNotNull(providerCaches);
176 assertEquals(1, providerCaches.size());
177 assertTrue(providerCaches.containsKey(PROVIDER_NAME));
179 ProviderCache providerCache = providerCaches.get(PROVIDER_NAME);
180 assertEquals(PROVIDER_NAME, providerCache.getProviderName());
181 assertEquals(PROVIDER_TYPE, providerCache.getProviderType());
183 Map<String, TenantCache> tenantCaches = providerCache.getTenants();
184 assertNotNull(tenantCaches);
185 assertEquals(1, tenantCaches.size());
186 assertTrue(tenantCaches.containsKey(TENANT_NAME));
188 TenantCache tenantCache = tenantCaches.get(TENANT_NAME);
190 assertEquals(TENANT_ID, tenantCache.getTenantId());
191 assertEquals(TENANT_NAME, tenantCache.getTenantName());
192 assertEquals(USER_ID, tenantCache.getUserid());
194 ServiceCatalog catalog = tenantCache.getServiceCatalog();
195 assertNotNull(catalog);
197 System.out.println(catalog.toString());
198 List<String> serviceTypes = catalog.getServiceTypes();
199 assertNotNull(serviceTypes);
200 assertEquals(12, serviceTypes.size());
202 assertEquals(TENANT_NAME, catalog.getTenantName());
203 assertEquals(TENANT_ID, catalog.getTenantId());
205 Set<String> regionNames = catalog.getRegions();
206 assertNotNull(regionNames);
207 assertEquals(1, regionNames.size());
208 assertTrue(regionNames.contains(REGION_NAME));
210 List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.IDENTITY_SERVICE);
211 assertNotNull(endpoints);
212 assertEquals(1, endpoints.size());
213 Endpoint endpoint = endpoints.get(0);
214 assertNotNull(endpoint);
215 assertEquals(REGION_NAME, endpoint.getRegion());
216 assertEquals(IDENTITY_URL, endpoint.getPublicURL());
218 endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);
219 assertNotNull(endpoints);
220 assertEquals(1, endpoints.size());
221 endpoint = endpoints.get(0);
222 assertNotNull(endpoint);
223 assertEquals(REGION_NAME, endpoint.getRegion());
225 endpoints = catalog.getEndpoints(ServiceCatalog.VOLUME_SERVICE);
226 assertNotNull(endpoints);
227 assertEquals(1, endpoints.size());
228 endpoint = endpoints.get(0);
229 assertNotNull(endpoint);
230 assertEquals(REGION_NAME, endpoint.getRegion());
232 endpoints = catalog.getEndpoints(ServiceCatalog.IMAGE_SERVICE);
233 assertNotNull(endpoints);
234 assertEquals(1, endpoints.size());
235 endpoint = endpoints.get(0);
236 assertNotNull(endpoint);
237 assertEquals(REGION_NAME, endpoint.getRegion());
239 endpoints = catalog.getEndpoints(ServiceCatalog.NETWORK_SERVICE);
240 assertNotNull(endpoints);
241 assertEquals(1, endpoints.size());
242 endpoint = endpoints.get(0);
243 assertNotNull(endpoint);
244 assertEquals(REGION_NAME, endpoint.getRegion());
246 assertTrue(catalog.isServicePublished(ServiceCatalog.IDENTITY_SERVICE));
247 assertTrue(catalog.isServicePublished(ServiceCatalog.COMPUTE_SERVICE));
248 assertTrue(catalog.isServicePublished(ServiceCatalog.VOLUME_SERVICE));
249 assertTrue(catalog.isServicePublished(ServiceCatalog.IMAGE_SERVICE));
250 assertTrue(catalog.isServicePublished(ServiceCatalog.NETWORK_SERVICE));
254 * This test case is used to actually validate that a server has been restarted from an already running state
256 * @throws ZoneException
257 * If the login cannot be performed because the principal and/or credentials are invalid.
258 * @throws IllegalArgumentException
259 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
261 * @throws IllegalStateException
262 * If the identity service is not available or cannot be created
263 * @throws IOException
264 * if an I/O error occurs
265 * @throws APPCException
269 public void testRestartRunningServer()
270 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
271 Properties properties = new Properties();
272 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
273 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
274 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
275 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
276 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
278 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
279 context.login(PRINCIPAL, CREDENTIAL);
280 VMURL vm = VMURL.parseURL(SERVER_URL);
282 ComputeService computeService = context.getComputeService();
283 Server server = computeService.getServer(vm.getServerId());
284 if (!server.getStatus().equals(Status.RUNNING)) {
286 assertTrue(waitForStateChange(server, Status.RUNNING));
289 Map<String, String> params = new HashMap<>();
290 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
291 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
292 SvcLogicContext svcContext = new SvcLogicContext();
294 server = adapter.restartServer(params, svcContext);
296 assertEquals(Server.Status.RUNNING, server.getStatus());
301 /****************************************/
303 * Tests that the vmStatuschecker method works and returns the correct status of the VM requested
305 * @throws ZoneException
306 * If the login cannot be performed because the principal and/or credentials are invalid.
307 * @throws IllegalArgumentException
308 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
310 * @throws IllegalStateException
311 * If the identity service is not available or cannot be created
312 * @throws IOException
313 * if an I/O error occurs
314 * @throws UnknownProviderException
315 * If the provider cannot be found
319 public void testVmStatuschecker() throws IllegalStateException, IllegalArgumentException, ZoneException,
320 UnknownProviderException, IOException {
321 Properties properties = new Properties();
322 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
323 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
324 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
325 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
326 properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true");
328 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
329 context.login(PRINCIPAL, CREDENTIAL);
330 VMURL vm = VMURL.parseURL(SERVER_URL);
332 ComputeService computeService = context.getComputeService();
333 Server server = computeService.getServer(vm.getServerId());
334 if (!server.getStatus().equals(Status.RUNNING)) {
336 assertTrue(waitForStateChange(server, Status.RUNNING));}
337 //or instead of the if-block, can ensureRunning(server) be used?
338 ensureRunning(server);
339 assertEquals(Server.Status.RUNNING, server.getStatus());
342 /****************************************/
346 * Tests that we can restart a server that is already stopped
348 * @throws ZoneException
349 * If the login cannot be performed because the principal and/or credentials are invalid.
350 * @throws IllegalArgumentException
351 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
353 * @throws IllegalStateException
354 * If the identity service is not available or cannot be created
355 * @throws IOException
356 * if an I/O error occurs
357 * @throws APPCException
361 public void testRestartStoppedServer()
362 throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
363 Properties properties = new Properties();
364 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
365 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
366 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
367 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
369 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
370 context.login(PRINCIPAL, CREDENTIAL);
371 VMURL vm = VMURL.parseURL(SERVER_URL);
373 ComputeService computeService = context.getComputeService();
374 Server server = computeService.getServer(vm.getServerId());
375 if (!server.getStatus().equals(Status.READY)) {
377 assertTrue(waitForStateChange(server, Status.READY));
380 Map<String, String> params = new HashMap<>();
381 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
382 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
383 SvcLogicContext svcContext = new SvcLogicContext();
385 server = adapter.restartServer(params, svcContext);
387 assertEquals(Server.Status.RUNNING, server.getStatus());
393 * Tests that we can rebuild a running server (not created from a bootable volume)
395 * @throws ZoneException
396 * If the login cannot be performed because the principal and/or credentials are invalid.
397 * @throws IllegalArgumentException
398 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
400 * @throws IllegalStateException
401 * If the identity service is not available or cannot be created
402 * @throws UnknownProviderException
403 * If the provider cannot be found
404 * @throws IOException
405 * if an I/O error occurs
406 * @throws APPCException
407 * If the server cannot be rebuilt for some reason
411 public void testRebuildRunningServer()
412 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
413 Properties properties = new Properties();
414 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
415 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
416 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
417 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
419 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
420 context.login(PRINCIPAL, CREDENTIAL);
421 VMURL vm = VMURL.parseURL(SERVER_URL);
423 ComputeService computeService = context.getComputeService();
424 Server server = computeService.getServer(vm.getServerId());
425 ensureRunning(server);
427 Map<String, String> params = new HashMap<>();
428 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
429 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
430 SvcLogicContext svcContext = new SvcLogicContext();
432 server = adapter.rebuildServer(params, svcContext);
433 assertTrue(waitForStateChange(server, Status.RUNNING));
439 * Tests that we can rebuild a paused server (not created from a bootable volume)
441 * @throws ZoneException
442 * If the login cannot be performed because the principal and/or credentials are invalid.
443 * @throws IllegalArgumentException
444 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
446 * @throws IllegalStateException
447 * If the identity service is not available or cannot be created
448 * @throws UnknownProviderException
449 * If the provider cannot be found
450 * @throws IOException
451 * if an I/O error occurs
452 * @throws APPCException
453 * If the server cannot be rebuilt for some reason
457 public void testRebuildPausedServer()
458 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
459 Properties properties = new Properties();
460 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
461 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
462 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
463 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
465 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
466 context.login(PRINCIPAL, CREDENTIAL);
467 VMURL vm = VMURL.parseURL(SERVER_URL);
469 ComputeService computeService = context.getComputeService();
470 Server server = computeService.getServer(vm.getServerId());
471 ensurePaused(server);
473 Map<String, String> params = new HashMap<>();
474 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
475 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
476 SvcLogicContext svcContext = new SvcLogicContext();
478 server = adapter.rebuildServer(params, svcContext);
479 assertTrue(waitForStateChange(server, Status.RUNNING));
484 * Tests that we can rebuild a paused server (not created from a bootable volume)
486 * @throws ZoneException
487 * If the login cannot be performed because the principal and/or credentials are invalid.
488 * @throws IllegalArgumentException
489 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
491 * @throws IllegalStateException
492 * If the identity service is not available or cannot be created
493 * @throws UnknownProviderException
494 * If the provider cannot be found
495 * @throws IOException
496 * if an I/O error occurs
497 * @throws APPCException
498 * If the server cannot be rebuilt for some reason
502 public void testRebuildSuspendedServer()
503 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
504 Properties properties = new Properties();
505 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
506 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
507 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
508 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
510 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
511 context.login(PRINCIPAL, CREDENTIAL);
512 VMURL vm = VMURL.parseURL(SERVER_URL);
514 ComputeService computeService = context.getComputeService();
515 Server server = computeService.getServer(vm.getServerId());
516 ensureSuspended(server);
518 Map<String, String> params = new HashMap<>();
519 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
520 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
521 SvcLogicContext svcContext = new SvcLogicContext();
523 server = adapter.rebuildServer(params, svcContext);
524 assertTrue(waitForStateChange(server, Status.RUNNING));
529 * Tests that we can rebuild a paused server (not created from a bootable volume)
531 * @throws ZoneException
532 * If the login cannot be performed because the principal and/or credentials are invalid.
533 * @throws IllegalArgumentException
534 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
536 * @throws IllegalStateException
537 * If the identity service is not available or cannot be created
538 * @throws UnknownProviderException
539 * If the provider cannot be found
540 * @throws IOException
541 * if an I/O error occurs
542 * @throws APPCException
543 * If the server cannot be rebuilt for some reason
547 public void testRebuildStoppedServer()
548 throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
549 Properties properties = new Properties();
550 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
551 properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
552 properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
553 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
555 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
556 context.login(PRINCIPAL, CREDENTIAL);
557 VMURL vm = VMURL.parseURL(SERVER_URL);
559 ComputeService computeService = context.getComputeService();
560 Server server = computeService.getServer(vm.getServerId());
561 ensureStopped(server);
563 Map<String, String> params = new HashMap<>();
564 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
565 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
566 SvcLogicContext svcContext = new SvcLogicContext();
568 server = adapter.rebuildServer(params, svcContext);
569 assertTrue(waitForStateChange(server, Status.RUNNING));
574 * Test subsequent action on second vm in different Tenant resulting in {"itemNotFound": {"message": "Instance could not be found", "code": 404}}
576 * @throws ZoneException
577 * If the login cannot be performed because the principal and/or credentials are invalid.
578 * @throws IllegalArgumentException
579 * If the principal and/or credential are null or empty, or if the expected argument(s) are not defined
581 * @throws IllegalStateException
582 * If the identity service is not available or cannot be created
583 * @throws IOException
584 * if an I/O error occurs
585 * @throws APPCException
589 public void testTenantVerification() throws IllegalStateException, IllegalArgumentException, ZoneException,
590 IOException, APPCException {
592 Properties properties = new Properties();
593 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
594 properties.setProperty(ContextFactory.PROPERTY_TENANT, "APP-C");
595 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
598 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
600 //try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
601 // context.login("AppC", "AppC");
603 // call lookupServer on vm in defined tenant "APP-C_TLV"
604 VMURL vm = VMURL.parseURL(vmUrl);
606 Map<String, String> params = new HashMap<>();
607 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
608 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
609 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
610 SvcLogicContext svcContext = new SvcLogicContext();
614 System.out.println("\n--------------------Begin lookupServer on tenant 1--------------------");
615 start = System.currentTimeMillis();
616 Server server = adapter.lookupServer(params, svcContext);
617 end = System.currentTimeMillis();
619 System.out.println(String.format("lookupServer on tenant 1 took %ds", (end - start) / 1000));
620 System.out.println("----------------------------------------------------------------------\n");
621 assertNotNull(server);
623 //repeat to show that context is reused for second request
624 System.out.println("\n-----------------Begin repeat lookupServer on tenant 1----------------");
625 start = System.currentTimeMillis();
626 server = adapter.lookupServer(params, svcContext);
627 end = System.currentTimeMillis();
629 System.out.println(String.format("Repeat lookupServer on tenant 1 took %ds", (end - start) / 1000));
630 System.out.println("----------------------------------------------------------------------\n");
631 assertNotNull(server);
633 // call lookupServer on vm in second tenant "Play"
634 // This is where we would fail due to using the previous
636 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
637 vm = VMURL.parseURL(vmUrl);
638 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
640 System.out.println("\n--------------------Begin lookupServer on tenant 2--------------------");
641 start = System.currentTimeMillis();
642 server = adapter.lookupServer(params, svcContext);
643 end = System.currentTimeMillis();
644 System.out.println(String.format("\nlookupServer on tenant 2 took %ds", (end - start) / 1000));
645 System.out.println("----------------------------------------------------------------------\n");
646 assertNotNull(server);
648 // call lookupServer on vm in non-existing tenant
649 vmUrl = "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
650 vm = VMURL.parseURL(vmUrl);
651 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
653 System.out.println("\n--------------Begin lookupServer on non-existant tenant--------------");
654 start = System.currentTimeMillis();
655 server = adapter.lookupServer(params, svcContext);
656 end = System.currentTimeMillis();
657 System.out.println(String.format("\nlookupServer on tenant 3 took %ds", (end - start) / 1000));
658 System.out.println("----------------------------------------------------------------------\n");
663 /****************************************/
667 public void testSnapshotServer() throws Exception {
668 Properties properties = new Properties();
669 properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, "http://example.com:5000");
670 // properties.setProperty(ContextFactory.PROPERTY_REGION, "");
671 properties.setProperty(ContextFactory.PROPERTY_TENANT, "Play");
672 properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
675 "http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
677 try (Context context = ContextFactory.getContext(PROVIDER_TYPE, properties)) {
678 context.login("AppC", "AppC");
679 VMURL vm = VMURL.parseURL(vmUrl);
681 Map<String, String> params = new HashMap<>();
682 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, vmUrl);
683 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL, "http://example.com:5000/v2.0");
684 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "http://example.com:5000/v2.0");
685 SvcLogicContext svcContext = new SvcLogicContext();
689 start = System.currentTimeMillis();
690 Image image = adapter.createSnapshot(params, svcContext);
691 end = System.currentTimeMillis();
693 System.out.println(String.format("Image ID: %s", image.getId()));
694 System.out.println(String.format("Snapshot took %ds", (end - start) / 1000));
696 start = System.currentTimeMillis();
697 adapter.rebuildServer(params, svcContext);
698 end = System.currentTimeMillis();
699 System.out.println(String.format("Rebuild took %ds", (end - start) / 1000));
705 * Ensures that the server is in stopped (shutdown) state prior to test
708 * The server to ensure is stopped
709 * @throws ZoneException
710 * If the server can't be operated upon for some reason
712 @SuppressWarnings("nls")
713 private static void ensureStopped(Server server) throws ZoneException {
714 switch (server.getStatus()) {
719 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
720 Server.Status.SUSPENDED, Server.Status.ERROR);
721 ensureSuspended(server);
726 waitForStateChange(server, Server.Status.RUNNING);
728 waitForStateChange(server, Server.Status.READY);
733 waitForStateChange(server, Server.Status.RUNNING);
735 waitForStateChange(server, Server.Status.READY);
740 waitForStateChange(server, Server.Status.READY);
746 fail("Server state is not valid for test - " + server.getStatus().name());
751 * Ensures that the server is in suspended state prior to test
754 * The server to ensure is suspended
755 * @throws ZoneException
756 * If the server can't be operated upon for some reason
758 @SuppressWarnings("nls")
759 private static void ensureSuspended(Server server) throws ZoneException {
760 switch (server.getStatus()) {
765 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
766 Server.Status.SUSPENDED, Server.Status.ERROR);
767 ensureSuspended(server);
772 waitForStateChange(server, Server.Status.RUNNING);
774 waitForStateChange(server, Server.Status.SUSPENDED);
779 waitForStateChange(server, Server.Status.RUNNING);
781 waitForStateChange(server, Server.Status.SUSPENDED);
786 waitForStateChange(server, Server.Status.SUSPENDED);
792 fail("Server state is not valid for test - " + server.getStatus().name());
797 * This method makes sure that the indicated server is running before performing a test
800 * The server to ensure is running
801 * @throws ZoneException
802 * If the server can't be operated upon
804 @SuppressWarnings("nls")
805 private static void ensureRunning(Server server) throws ZoneException {
806 switch (server.getStatus()) {
811 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
812 Server.Status.SUSPENDED, Server.Status.ERROR);
813 ensureRunning(server);
818 waitForStateChange(server, Server.Status.RUNNING);
823 waitForStateChange(server, Server.Status.RUNNING);
828 waitForStateChange(server, Server.Status.RUNNING);
834 fail("Server state is not valid for test - " + server.getStatus().name());
839 * This method will make sure that the server we are testing is paused
842 * The server to make sure is paused for the test
843 * @throws ZoneException
846 @SuppressWarnings("nls")
847 private static void ensurePaused(Server server) throws ZoneException {
848 switch (server.getStatus()) {
853 waitForStateChange(server, Server.Status.READY, Server.Status.RUNNING, Server.Status.PAUSED,
854 Server.Status.SUSPENDED, Server.Status.ERROR);
855 ensurePaused(server);
860 waitForStateChange(server, Server.Status.RUNNING);
862 waitForStateChange(server, Server.Status.PAUSED);
867 waitForStateChange(server, Server.Status.PAUSED);
872 waitForStateChange(server, Server.Status.RUNNING);
874 waitForStateChange(server, Server.Status.PAUSED);
880 fail("Server state is not valid for test - " + server.getStatus().name());
885 * Enter a pool-wait loop checking the server state to see if it has entered one of the desired states or not.
887 * This method checks the state of the server periodically for one of the desired states. When the server enters one
888 * of the desired states, the method returns a successful indication (true). If the server never enters one of the
889 * desired states within the alloted timeout period, then the method returns a failed response (false). No
890 * exceptions are thrown from this method.
894 * The server to wait on
895 * @param desiredStates
896 * A variable list of desired states, any one of which is allowed.
897 * @return True if the server entered one of the desired states, and false if not and the wait loop timed out.
899 private static boolean waitForStateChange(Server server, Server.Status... desiredStates) {
901 ConfigurationFactory.getConfiguration().getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
902 long limit = System.currentTimeMillis() + (timeout * 1000);
906 while (limit > System.currentTimeMillis()) {
908 for (Server.Status desiredState : desiredStates) {
909 if (server.getStatus().equals(desiredState)) {
915 Thread.sleep(10000L);
916 } catch (InterruptedException e) {
920 } catch (ZoneException e) {
928 * @Test public void testTerminateStack() throws IllegalStateException, IllegalArgumentException, ZoneException,
929 * UnknownProviderException, IOException { Properties properties = new Properties();
930 * properties.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, IDENTITY_URL);
931 * properties.setProperty(ContextFactory.PROPERTY_REGION, REGION_NAME);
932 * properties.setProperty(ContextFactory.PROPERTY_TENANT, TENANT_NAME);
933 * properties.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, "*");
934 * properties.setProperty(ContextFactory.PROPERTY_DISABLE_PROXY, "true"); try (Context context =
935 * ContextFactory.getContext(PROVIDER_TYPE, properties)) { context.login(PRINCIPAL, CREDENTIAL); VMURL vm =
936 * VMURL.parseURL(SERVER_URL); ComputeService computeService = context.getComputeService(); Server server =
937 * computeService.getServer(vm.getServerId()); if (!server.getStatus().equals(Status.RUNNING)) { server.start();
938 * assertTrue(waitForStateChange(server, Status.RUNNING)); } Map<String, String> params = new HashMap<>();
939 * params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL); params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME,
940 * PROVIDER_NAME); SvcLogicContext svcContext = new SvcLogicContext(); Stack stack = adapter.terminateStack(params,
941 * svcContext); assertNotNull(stack); } }