2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Copyright (C) 2017 Amdocs
\r
8 * =============================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
21 * ============LICENSE_END=========================================================
\r
24 package org.onap.appc.adapter.iaas.impl;
\r
26 import static org.junit.Assert.assertEquals;
\r
27 import static org.junit.Assert.assertFalse;
\r
28 import static org.junit.Assert.assertNotNull;
\r
29 import static org.junit.Assert.assertNull;
\r
30 import static org.junit.Assert.assertTrue;
\r
31 import static org.mockito.Mockito.when;
\r
32 import java.util.ArrayList;
\r
33 import java.util.Arrays;
\r
34 import java.util.HashSet;
\r
35 import java.util.List;
\r
36 import java.util.Map;
\r
37 import java.util.Properties;
\r
38 import java.util.Set;
\r
39 import org.junit.Before;
\r
40 import org.junit.BeforeClass;
\r
41 import org.junit.Ignore;
\r
42 import org.junit.Test;
\r
43 import org.junit.runner.RunWith;
\r
44 import org.mockito.Mock;
\r
45 import org.mockito.Mockito;
\r
46 import org.mockito.runners.MockitoJUnitRunner;
\r
47 import org.onap.appc.configuration.ConfigurationFactory;
\r
48 import org.powermock.reflect.Whitebox;
\r
49 import com.att.cdp.exceptions.ZoneException;
\r
50 import com.att.cdp.zones.ContextFactory;
\r
51 import com.google.common.collect.ImmutableMap;
\r
52 import com.woorea.openstack.keystone.model.Access;
\r
53 import com.woorea.openstack.keystone.model.Access.Service;
\r
54 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
\r
55 import com.woorea.openstack.base.client.OpenStackClientConnector;
\r
56 import com.woorea.openstack.base.client.OpenStackConnectException;
\r
57 import com.woorea.openstack.base.client.OpenStackResponseException;
\r
58 import com.woorea.openstack.keystone.Keystone;
\r
59 import com.woorea.openstack.keystone.api.TokensResource;
\r
60 import com.woorea.openstack.keystone.model.Tenant;
\r
61 import com.woorea.openstack.keystone.model.Token;
\r
64 * This class tests the service catalog against a known provider.
\r
66 @RunWith(MockitoJUnitRunner.class)
\r
67 public class TestServiceCatalogV2 {
\r
70 private static int EXPECTED_REGIONS = 1;
\r
71 private static int EXPECTED_ENDPOINTS = 1;
\r
73 private static String PRINCIPAL;
\r
74 private static String CREDENTIAL;
\r
75 private static String TENANT_NAME;
\r
76 private static String TENANT_ID;
\r
77 private static String IDENTITY_URL;
\r
78 private static String REGION_NAME;
\r
79 private static String PUBLIC_URL;
\r
81 private static String IP;
\r
82 private static String PORT;
\r
83 private static String TENANTID;
\r
84 private static String VMID;
\r
85 private static String URL;
\r
87 private ServiceCatalogV2 catalog;
\r
89 private Properties properties;
\r
92 private Tenant tenant;
\r
94 private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
\r
96 private Map<String, Service> serviceTypes;
\r
98 private Map<String, List<Service.Endpoint>> serviceEndpoints;
\r
101 public static void before() {
\r
102 final Properties props = ConfigurationFactory.getConfiguration().getProperties();
\r
103 IDENTITY_URL = props.getProperty("provider1.identity", "appc");
\r
104 PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");
\r
105 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
\r
106 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
\r
107 TENANT_ID = props.getProperty("provider1.tenant1.id",
\r
108 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));
\r
109 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
\r
111 IP = props.getProperty("test.ip");
\r
112 PORT = props.getProperty("test.port");
\r
113 TENANTID = props.getProperty("test.tenantid");
\r
114 VMID = props.getProperty("test.vmid");
\r
116 EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));
\r
117 EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));
\r
120 "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
\r
124 * Setup the test environment by loading a new service catalog for each test Use reflection to
\r
125 * locate fields and methods so that they can be manipulated during the test to change the
\r
126 * internal state accordingly.
\r
130 public void setup() {
\r
131 URL = String.format("http://%s:%s/v2/%s/servers/%s", IP, PORT, TENANTID, VMID);
\r
132 properties = new Properties();
\r
133 properties.setProperty(ContextFactory.PROPERTY_PROXY_HOST, "PROXY_HOST");
\r
134 properties.setProperty(ContextFactory.PROPERTY_PROXY_PORT, "PROXY_PORT");
\r
135 catalog = new ServiceCatalogV2(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, properties);
\r
136 final Service service = new Service();
\r
137 serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)
\r
138 .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)
\r
139 .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();
\r
140 Map<String, Object> endpointPrivateFields =
\r
141 ImmutableMap.<String, Object>builder().put("publicURL", PUBLIC_URL).put("region", REGION_NAME).build();
\r
142 Service.Endpoint endpoint = new Service.Endpoint();
\r
143 CommonUtility.injectMockObjects(endpointPrivateFields, endpoint);
\r
144 final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);
\r
145 serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()
\r
146 .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();
\r
147 Map<String, Object> privateFields =
\r
148 ImmutableMap.<String, Object>builder().put("regions", regions).put("tenant", tenant)
\r
149 .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();
\r
150 CommonUtility.injectMockObjects(privateFields, catalog);
\r
151 CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);
\r
156 * Ensure that we get the Tenant Name & Tenant Id property are returned correctly
\r
159 public void testKnownTenant() {
\r
160 when(tenant.getName()).thenReturn(TENANT_NAME);
\r
161 when(tenant.getId()).thenReturn(TENANT_ID);
\r
162 assertEquals(TENANT_NAME, catalog.getProjectName());
\r
163 assertEquals(TENANT_ID, catalog.getProjectId());
\r
167 * Ensure that we set up the Region property correctly
\r
170 public void testKnownRegions() {
\r
171 assertEquals(EXPECTED_REGIONS, catalog.getRegions().size());
\r
172 assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);
\r
176 * Ensure that that we can check for published services correctly
\r
179 public void testServiceTypesPublished() {
\r
180 assertTrue(catalog.isServicePublished("compute"));
\r
181 assertFalse(catalog.isServicePublished("bogus"));
\r
185 * Ensure that we can get the list of published services
\r
188 public void testPublishedServicesList() {
\r
189 final List<String> services = catalog.getServiceTypes();
\r
190 assertTrue(services.contains(ServiceCatalog.COMPUTE_SERVICE));
\r
191 assertTrue(services.contains(ServiceCatalog.IDENTITY_SERVICE));
\r
192 assertTrue(services.contains(ServiceCatalog.IMAGE_SERVICE));
\r
193 assertTrue(services.contains(ServiceCatalog.NETWORK_SERVICE));
\r
194 assertTrue(services.contains(ServiceCatalog.VOLUME_SERVICE));
\r
198 * Ensure that we can get the endpoint(s) for a service
\r
201 public void testEndpointList() {
\r
202 List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);
\r
203 assertNotNull(endpoints);
\r
204 assertFalse(endpoints.isEmpty());
\r
205 assertEquals(EXPECTED_ENDPOINTS, endpoints.size());
\r
209 * Ensure that we override the toString method
\r
212 public void testToString() {
\r
213 when(tenant.getId()).thenReturn(TENANT_ID);
\r
214 when(tenant.getDescription()).thenReturn("Tenant one");
\r
215 final String testString = catalog.toString();
\r
216 assertNotNull(testString);
\r
220 * Ensure that we can get the VM Region
\r
223 public void testGetVMRegion() {
\r
224 VMURL url = VMURL.parseURL(URL);
\r
225 String region = catalog.getVMRegion(url);
\r
226 assertEquals(REGION_NAME, region);
\r
230 * Ensure that we can get the null region when no URL is passed
\r
233 public void testGetVMRegionWithoutURL() {
\r
234 String region = catalog.getVMRegion(null);
\r
235 assertNull(region);
\r
240 public void liveConnectionTest() {
\r
241 // this test should only be used by developers when testing against a live Openstack
\r
242 // instance, otherwise it should be ignored
\r
243 properties = new Properties();
\r
244 String identity = "http://192.168.0.1:5000/v2.0";
\r
245 String tenantName = "Tenant";
\r
246 String user = "user";
\r
247 String pass = "pass";
\r
249 ServiceCatalogV2 catalog = new ServiceCatalogV2(identity, tenantName, user, pass, properties);
\r
253 } catch (ZoneException e) {
\r
254 // TODO Auto-generated catch block
\r
255 e.printStackTrace();
\r
258 String out = catalog.toString();
\r
259 System.out.println(out);
\r
260 assertNotNull(catalog);
\r
264 public void testInit() throws ZoneException, ClassNotFoundException, InstantiationException, IllegalAccessException, OpenStackConnectException, OpenStackResponseException {
\r
265 ServiceCatalogV2 catalogSpy = Mockito.spy(catalog);
\r
266 Class<?> connectorClass = Class.forName(ServiceCatalogV2.CLIENT_CONNECTOR_CLASS);
\r
267 OpenStackClientConnector connector = (OpenStackClientConnector) connectorClass.newInstance();
\r
268 Keystone keystone = Mockito.spy(new Keystone(IDENTITY_URL, connector));
\r
269 TokensResource tokens = Mockito.mock(TokensResource.class);
\r
270 TokensResource.Authenticate authenticate = Mockito.mock(TokensResource.Authenticate.class);
\r
271 Mockito.when(keystone.tokens()).thenReturn(tokens);
\r
272 Mockito.when(tokens.authenticate(Mockito.any())).thenReturn(authenticate);
\r
273 Access access = Mockito.mock(Access.class);
\r
275 Token token = new Token();
\r
276 Mockito.when(access.getToken()).thenReturn(token);
\r
277 Mockito.when(authenticate.execute()).thenReturn(access);
\r
278 Mockito.when(authenticate.withTenantName(Mockito.anyString())).thenReturn(authenticate);
\r
279 Mockito.when(catalogSpy.getKeystone(Mockito.anyString(), Mockito.any())).thenReturn(keystone);
\r
280 Access.Service service = new Access.Service();
\r
281 Endpoint endpoint = new Endpoint();
\r
282 List<Endpoint> endpointList = new ArrayList<>();
\r
283 endpointList.add(endpoint);
\r
284 Whitebox.setInternalState(service, "endpoints", endpointList);
\r
285 List<Service> serviceList = new ArrayList<>();
\r
286 serviceList.add(service);
\r
287 Mockito.when(access.getServiceCatalog()).thenReturn(serviceList);
\r
289 Mockito.verify(access).getServiceCatalog();
\r