Integration test for metadata service
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/4f021496 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/4f021496 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/4f021496 Branch: refs/heads/stratos-4.1.x Commit: 4f021496f6bf25628f2128edf44da04e75527b89 Parents: 4b8d439 Author: Akila Perera <[email protected]> Authored: Wed Oct 14 16:25:00 2015 +0530 Committer: Akila Perera <[email protected]> Committed: Wed Oct 14 16:43:04 2015 +0530 ---------------------------------------------------------------------- .../modules/integration/test-common/pom.xml | 7 +- .../integration/common/RestConstants.java | 1 + .../integration/common/TopologyHandler.java | 115 ++++---- .../common/rest/IntegrationMockClient.java | 3 +- .../integration/common/rest/RestClient.java | 272 ++++++++++++++----- .../tests/StratosIntegrationTest.java | 26 +- .../SampleApplicationStartupTestCase.java | 66 ++++- .../src/test/resources/common/log4j.properties | 5 +- .../src/test/resources/test-suite-smoke.xml | 2 +- 9 files changed, 345 insertions(+), 152 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-common/pom.xml ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/pom.xml b/products/stratos/modules/integration/test-common/pom.xml index df26f50..6822a32 100755 --- a/products/stratos/modules/integration/test-common/pom.xml +++ b/products/stratos/modules/integration/test-common/pom.xml @@ -114,6 +114,11 @@ </dependency> <dependency> <groupId>org.apache.stratos</groupId> + <artifactId>org.apache.stratos.metadata.client</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.stratos</groupId> <artifactId>org.apache.stratos.mock.iaas.client</artifactId> <version>${project.version}</version> <scope>compile</scope> @@ -123,4 +128,4 @@ <artifactId>org.jacoco.agent</artifactId> </dependency> </dependencies> -</project> \ No newline at end of file +</project> http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java index f3577bc..1778bb9 100644 --- a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java @@ -21,6 +21,7 @@ package org.apache.stratos.integration.common; */ public class RestConstants { public static final String API = "api"; + public static final String METADATA_API = "/metadata/api"; public static final String AUTOSCALING_POLICIES = "/" + API + "/autoscalingPolicies"; public static final String USERS = "/" + API + "/users"; public static final String TENANTS = "/" + API + "/tenants"; http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java index 2144b12..721a5c6 100644 --- a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java @@ -21,7 +21,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.stratos.autoscaler.stub.pojo.ApplicationContext; import org.apache.stratos.common.client.AutoscalerServiceClient; import org.apache.stratos.common.threading.StratosThreadPool; -import org.apache.stratos.integration.common.rest.IntegrationMockClient; import org.apache.stratos.messaging.domain.application.*; import org.apache.stratos.messaging.domain.instance.ClusterInstance; import org.apache.stratos.messaging.domain.instance.GroupInstance; @@ -38,12 +37,10 @@ import org.apache.stratos.messaging.message.receiver.application.ApplicationMana import org.apache.stratos.messaging.message.receiver.application.ApplicationsEventReceiver; import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; +import org.apache.stratos.mock.iaas.client.MockIaasApiClient; import java.rmi.RemoteException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -90,7 +87,6 @@ public class TopologyHandler { return topologyHandler; } - /** * Initialize application event receiver */ @@ -124,8 +120,7 @@ public class TopologyHandler { while (!applicationTopologyInitialized) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } applicationTopologyInitialized = ApplicationManager.getApplications().isInitialized(); if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { @@ -145,8 +140,7 @@ public class TopologyHandler { while (!topologyInitialized) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } topologyInitialized = TopologyManager.getTopology().isInitialized(); if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { @@ -167,8 +161,7 @@ public class TopologyHandler { while (!((application != null) && (application.getStatus() == status))) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } application = ApplicationManager.getApplications().getApplication(applicationName); if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { @@ -188,8 +181,7 @@ public class TopologyHandler { */ public void assertGroupActivation(String applicationName) { Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); Collection<Group> groups = application.getAllGroupsRecursively(); for (Group group : groups) { @@ -204,24 +196,23 @@ public class TopologyHandler { */ public void assertClusterActivation(String applicationName) { Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { String serviceName = clusterDataHolder.getServiceType(); String clusterId = clusterDataHolder.getClusterId(); Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); Cluster cluster = service.getCluster(clusterId); assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", applicationName, serviceName, clusterId), cluster); for (Member member : cluster.getMembers()) { - log.info(String.format("Member [member-id] %s found in cluster instance [cluster-instance] %s of " + - "cluster [cluster-id] %s", member - .getMemberId(), member.getClusterInstanceId(), member.getClusterId())); + log.info(String.format("Member [member-id] %s found in cluster instance [cluster-instance] %s of " + + "cluster [cluster-id] %s", member.getMemberId(), member.getClusterInstanceId(), + member.getClusterId())); } boolean clusterActive = false; int activeInstances; @@ -259,8 +250,8 @@ public class TopologyHandler { if (cartridgeName.equals(serviceName)) { String clusterId = clusterDataHolder.getClusterId(); Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); Cluster cluster = service.getCluster(clusterId); assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", @@ -282,7 +273,7 @@ public class TopologyHandler { * @param memberId * @param mockIaasApiClient */ - public void terminateMemberInMockIaas(String memberId, IntegrationMockClient mockIaasApiClient) { + public void terminateMemberInMockIaas(String memberId, MockIaasApiClient mockIaasApiClient) { boolean memberTerminated = false; memberTerminated = mockIaasApiClient.terminateInstance(memberId); assertTrue(String.format("Member [member-id] %s couldn't be terminated from the mock IaaS", memberId), @@ -309,8 +300,7 @@ public class TopologyHandler { } try { Thread.sleep(1000); - } - catch (InterruptedException e) { + } catch (InterruptedException e) { log.error("Could not sleep", e); } } @@ -322,16 +312,15 @@ public class TopologyHandler { long startTime = System.currentTimeMillis(); Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { String serviceName = clusterDataHolder.getServiceType(); String clusterId = clusterDataHolder.getClusterId(); Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); Cluster cluster = service.getCluster(clusterId); assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", @@ -352,12 +341,12 @@ public class TopologyHandler { while (!clusterActive) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); + assertNotNull( + String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); cluster = service.getCluster(clusterId); activeInstances = 0; @@ -385,7 +374,6 @@ public class TopologyHandler { } - /** * Assert application activation * @@ -397,22 +385,19 @@ public class TopologyHandler { ApplicationContext applicationContext = null; try { applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); - } - catch (RemoteException e) { + } catch (RemoteException e) { log.error("Error while getting the application context for [application] " + applicationName); } - while (((application != null) && application.getInstanceContextCount() > 0) || - (applicationContext == null || applicationContext.getStatus().equals(APPLICATION_STATUS_UNDEPLOYING))) { + while (((application != null) && application.getInstanceContextCount() > 0) || (applicationContext == null + || applicationContext.getStatus().equals(APPLICATION_STATUS_UNDEPLOYING))) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } application = ApplicationManager.getApplications().getApplication(applicationName); try { applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); - } - catch (RemoteException e) { + } catch (RemoteException e) { log.error("Error while getting the application context for [application] " + applicationName); } if ((System.currentTimeMillis() - startTime) > APPLICATION_UNDEPLOYMENT_TIMEOUT) { @@ -421,14 +406,13 @@ public class TopologyHandler { } } - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); - assertNotNull(String.format("Application Context is not found: [application-id] %s", - applicationName), applicationContext); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + assertNotNull(String.format("Application Context is not found: [application-id] %s", applicationName), + applicationContext); //Force undeployment after the graceful deployment - if (application.getInstanceContextCount() > 0 || - applicationContext.getStatus().equals(APPLICATION_STATUS_UNDEPLOYING)) { + if (application.getInstanceContextCount() > 0 || applicationContext.getStatus() + .equals(APPLICATION_STATUS_UNDEPLOYING)) { return false; } assertEquals( @@ -450,8 +434,7 @@ public class TopologyHandler { while (group.getInstanceContextCount() != count) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { log.error("Group instance min count check failed within timeout period"); @@ -462,8 +445,7 @@ public class TopologyHandler { while (!instance.getStatus().equals(GroupStatus.Active)) { try { Thread.sleep(1000); - } - catch (InterruptedException ignore) { + } catch (InterruptedException ignore) { } if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { log.error("Application did not activate within timeout period"); @@ -498,7 +480,6 @@ public class TopologyHandler { } }); - topologyEventReceiver.addEventListener(new ClusterInstanceCreatedEventListener() { @Override protected void onEvent(Event event) { @@ -545,7 +526,6 @@ public class TopologyHandler { } }); - } private void addApplicationEventListeners() { @@ -626,7 +606,6 @@ public class TopologyHandler { return dataHolder.getClusterId(); } - public void removeMembersFromMaps(String applicationId) { for (Map.Entry<String, Long> entry : getActivateddMembers().entrySet()) { if (entry.getKey().contains(applicationId)) { @@ -680,4 +659,28 @@ public class TopologyHandler { public void setActivateddMembers(Map<String, Long> activateddMembers) { this.activateddMembers = activateddMembers; } + + public List<Member> getMembersForApplication(String applicationId) { + Application application = ApplicationManager.getApplications().getApplication(applicationId); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationId), application); + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); + List<Member> memberList = new ArrayList<>(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { + String serviceName = clusterDataHolder.getServiceType(); + String clusterId = clusterDataHolder.getClusterId(); + Service service = TopologyManager.getTopology().getService(serviceName); + assertNotNull( + String.format("Service is not found: [application-id] %s [service] %s", applicationId, serviceName), + service); + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationId, serviceName, clusterId), cluster); + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + for (Member member : cluster.getMembers()) { + memberList.add(member); + } + } + } + return memberList; + } } http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/IntegrationMockClient.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/IntegrationMockClient.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/IntegrationMockClient.java index 73090bd..8ce3311 100644 --- a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/IntegrationMockClient.java +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/IntegrationMockClient.java @@ -66,7 +66,8 @@ public class IntegrationMockClient extends MockIaasApiClient { } else { GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - org.apache.stratos.mock.iaas.domain.ErrorResponse errorResponse = gson.fromJson(response.getContent(), org.apache.stratos.mock.iaas.domain.ErrorResponse.class); + org.apache.stratos.mock.iaas.domain.ErrorResponse errorResponse = gson + .fromJson(response.getContent(), org.apache.stratos.mock.iaas.domain.ErrorResponse.class); if (errorResponse != null) { throw new RuntimeException(errorResponse.getErrorMessage()); } http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/RestClient.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/RestClient.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/RestClient.java index e438a9e..fca36a1 100644 --- a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/RestClient.java +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/rest/RestClient.java @@ -16,49 +16,62 @@ package org.apache.stratos.integration.common.rest; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParser; +import com.google.gson.*; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.client.HttpClient; import org.apache.http.client.methods.*; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContextBuilder; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingClientConnectionManager; +import org.apache.stratos.integration.common.RestConstants; +import org.apache.stratos.metadata.client.beans.PropertyBean; +import org.apache.stratos.metadata.client.exception.RestClientException; +import org.apache.stratos.metadata.client.rest.HTTPConnectionManager; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.lang.reflect.Type; import java.net.URI; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.util.List; /** * Rest client to handle rest requests */ public class RestClient { private static final Log log = LogFactory.getLog(RestClient.class); - private DefaultHttpClient httpClient; + private HttpClient httpClient; private String endPoint; + private String securedEndpoint; private String userName; private String password; - - public RestClient() { - PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); - // Increase max total connection to 200 - cm.setMaxTotal(200); - // Increase default max connection per route to 50 - cm.setDefaultMaxPerRoute(50); - - httpClient = new DefaultHttpClient(cm); - httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient); + private GsonBuilder gsonBuilder = new GsonBuilder(); + private Gson gson = gsonBuilder.create(); + + public RestClient() throws Exception { + SSLContextBuilder builder = new SSLContextBuilder(); + SSLConnectionSocketFactory sslConnectionFactory; + builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); + sslConnectionFactory = new SSLConnectionSocketFactory(builder.build()); + this.httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionFactory) + .setConnectionManager(HTTPConnectionManager.getInstance().getHttpConnectionManager()).build(); } - public RestClient(String endPoint, String userName, String password) { + public RestClient(String endPoint, String securedEndpoint, String userName, String password) throws Exception { this(); this.endPoint = endPoint; + this.securedEndpoint = securedEndpoint; this.userName = userName; this.password = password; } @@ -80,13 +93,12 @@ public class RestClient { postRequest.setEntity(input); String userPass = getUsernamePassword(); - String basicAuth = - "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter + .printBase64Binary(userPass.getBytes("UTF-8")); postRequest.addHeader("Authorization", basicAuth); return httpClient.execute(postRequest, new HttpResponseHandler()); - } - finally { + } finally { releaseConnection(postRequest); } } @@ -105,13 +117,12 @@ public class RestClient { getRequest = new HttpGet(resourcePath); getRequest.addHeader("Content-Type", "application/json"); String userPass = getUsernamePassword(); - String basicAuth = - "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter + .printBase64Binary(userPass.getBytes("UTF-8")); getRequest.addHeader("Authorization", basicAuth); return httpClient.execute(getRequest, new HttpResponseHandler()); - } - finally { + } finally { releaseConnection(getRequest); } } @@ -122,12 +133,11 @@ public class RestClient { httpDelete = new HttpDelete(resourcePath); httpDelete.addHeader("Content-Type", "application/json"); String userPass = getUsernamePassword(); - String basicAuth = - "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter + .printBase64Binary(userPass.getBytes("UTF-8")); httpDelete.addHeader("Authorization", basicAuth); return httpClient.execute(httpDelete, new HttpResponseHandler()); - } - finally { + } finally { releaseConnection(httpDelete); } } @@ -142,12 +152,11 @@ public class RestClient { input.setContentType("application/json"); putRequest.setEntity(input); String userPass = getUsernamePassword(); - String basicAuth = - "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter + .printBase64Binary(userPass.getBytes("UTF-8")); putRequest.addHeader("Authorization", basicAuth); return httpClient.execute(putRequest, new HttpResponseHandler()); - } - finally { + } finally { releaseConnection(putRequest); } } @@ -167,12 +176,7 @@ public class RestClient { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return true; } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - ErrorResponse errorResponse = gson.fromJson(response.getContent(), ErrorResponse.class); - if (errorResponse != null) { - throw new RuntimeException(errorResponse.getErrorMessage()); - } + throw new RuntimeException(response.getContent()); } } throw new Exception("Null response received. Could not add entity [entity name] " + entityName); @@ -186,12 +190,7 @@ public class RestClient { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return true; } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - ErrorResponse errorResponse = gson.fromJson(response.getContent(), ErrorResponse.class); - if (errorResponse != null) { - throw new RuntimeException(errorResponse.getErrorMessage()); - } + throw new RuntimeException(response.getContent()); } } throw new Exception("Null response received. Could not deploy entity [entity name] " + entityName); @@ -205,34 +204,23 @@ public class RestClient { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return true; } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - ErrorResponse errorResponse = gson.fromJson(response.getContent(), ErrorResponse.class); - if (errorResponse != null) { - throw new RuntimeException(errorResponse.getErrorMessage()); - } + throw new RuntimeException(response.getContent()); } } throw new Exception("Null response received. Could not undeploy entity [entity name] " + entityName); } - public Object getEntity(String resourcePath, String identifier, Class responseJsonClass, - String entityName) throws Exception { + public Object getEntity(String resourcePath, String identifier, Class responseJsonClass, String entityName) + throws Exception { URI uri = new URIBuilder(this.endPoint + resourcePath + "/" + identifier).build(); HttpResponse response = doGet(uri); - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); if (response != null) { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return gson.fromJson(response.getContent(), responseJsonClass); } else if (response.getStatusCode() == 404) { return null; } else { - ErrorResponse errorResponse = gson.fromJson(response.getContent(), - ErrorResponse.class); - if (errorResponse != null) { - throw new RuntimeException(errorResponse.getErrorMessage()); - } + throw new RuntimeException(response.getContent()); } } throw new Exception("Null response received. Could not get entity [entity name] " + entityName); @@ -241,19 +229,13 @@ public class RestClient { public Object listEntity(String resourcePath, Type type, String entityName) throws Exception { URI uri = new URIBuilder(this.endPoint + resourcePath).build(); HttpResponse response = doGet(uri); - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); if (response != null) { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return gson.fromJson(response.getContent(), type); } else if (response.getStatusCode() == 404) { return null; } else { - ErrorResponse errorResponse = gson.fromJson(response.getContent(), - ErrorResponse.class); - if (errorResponse != null) { - throw new RuntimeException(errorResponse.getErrorMessage()); - } + throw new RuntimeException(response.getContent()); } } throw new Exception("Null response received. Could not get entity [entity name] " + entityName); @@ -266,10 +248,7 @@ public class RestClient { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return true; } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - ErrorResponse errorResponse = gson.fromJson(response.getContent(), - ErrorResponse.class); + ErrorResponse errorResponse = gson.fromJson(response.getContent(), ErrorResponse.class); log.error("Error response while removing entity [identifier] " + identifier + ", [entity name] " + entityName + ", [error] " + errorResponse.getErrorMessage() + ", [error code] " + errorResponse .getErrorCode()); @@ -288,10 +267,7 @@ public class RestClient { if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { return true; } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - ErrorResponse errorResponse = gson.fromJson(response.getContent(), - ErrorResponse.class); + ErrorResponse errorResponse = gson.fromJson(response.getContent(), ErrorResponse.class); if (errorResponse != null) { throw new RuntimeException(errorResponse.getErrorMessage()); } @@ -300,6 +276,152 @@ public class RestClient { throw new Exception("Null response received. Could not update entity [entity name] " + entityName); } + public boolean addPropertyToApplication(String appId, String propertyKey, String propertyValue, String accessToken) + throws Exception { + URI uri = new URIBuilder( + this.securedEndpoint + RestConstants.METADATA_API + "/applications/" + appId + "/properties").build(); + log.info("Metadata endpoint resource: " + uri.toString()); + PropertyBean property = new PropertyBean(propertyKey, propertyValue); + HttpResponse response; + HttpPost postRequest = null; + String requestBody = gson.toJson(property, PropertyBean.class); + try { + postRequest = new HttpPost(uri); + StringEntity input = new StringEntity(requestBody); + input.setContentType("application/json"); + postRequest.setEntity(input); + String bearerAuth = "Bearer " + accessToken; + postRequest.addHeader("Authorization", bearerAuth); + response = httpClient.execute(postRequest, new HttpResponseHandler()); + } finally { + releaseConnection(postRequest); + } + + if (response != null) { + if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { + return true; + } else { + throw new RuntimeException(response.getContent()); + } + } + throw new Exception("Null response received. Could not add property to application: " + appId); + } + + public boolean addPropertyToCluster(String appId, String clusterId, String propertyKey, String propertyValue, + String accessToken) throws Exception { + URI uri = new URIBuilder( + this.securedEndpoint + RestConstants.METADATA_API + "/applications/" + appId + "/clusters/" + clusterId + + "/properties").build(); + PropertyBean property = new PropertyBean(propertyKey, propertyValue); + HttpResponse response; + HttpPost postRequest = null; + String requestBody = gson.toJson(property); + try { + postRequest = new HttpPost(uri); + StringEntity input = new StringEntity(requestBody); + input.setContentType("application/json"); + postRequest.setEntity(input); + String bearerAuth = "Bearer " + accessToken; + postRequest.addHeader("Authorization", bearerAuth); + response = httpClient.execute(postRequest, new HttpResponseHandler()); + } finally { + releaseConnection(postRequest); + } + + if (response != null) { + if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { + return true; + } else { + throw new RuntimeException(response.getContent()); + } + } + throw new Exception("Null response received. Could not add property to cluster: " + clusterId); + } + + public PropertyBean getClusterProperty(String appId, String clusterId, String propertyName, String accessToken) + throws Exception { + URI uri = new URIBuilder( + this.securedEndpoint + RestConstants.METADATA_API + "/applications/" + appId + "/cluster/" + clusterId + + "/properties/" + propertyName).build(); + HttpResponse response; + HttpGet getRequest = null; + try { + getRequest = new HttpGet(uri); + getRequest.addHeader("Content-Type", "application/json"); + String bearerAuth = "Bearer " + accessToken; + getRequest.addHeader("Authorization", bearerAuth); + response = httpClient.execute(getRequest, new HttpResponseHandler()); + } finally { + releaseConnection(getRequest); + } + Gson gson = new GsonBuilder().registerTypeAdapter(List.class, new JsonSerializer<List<?>>() { + @Override + public JsonElement serialize(List<?> list, Type t, JsonSerializationContext jsc) { + if (list.size() == 1) { + // Don't put single element lists in a json array + return new Gson().toJsonTree(list.get(0)); + } else { + return new Gson().toJsonTree(list); + } + } + }).create(); + return gson.fromJson(response.getContent(), PropertyBean.class); + } + + public PropertyBean getApplicationProperty(String appId, String propertyName, String accessToken) throws Exception { + URI uri = new URIBuilder( + this.securedEndpoint + RestConstants.METADATA_API + "/applications/" + appId + "/properties/" + + propertyName).build(); + HttpResponse response; + HttpGet getRequest = null; + try { + getRequest = new HttpGet(uri); + getRequest.addHeader("Content-Type", "application/json"); + String bearerAuth = "Bearer " + accessToken; + getRequest.addHeader("Authorization", bearerAuth); + response = httpClient.execute(getRequest, new HttpResponseHandler()); + } finally { + releaseConnection(getRequest); + } + Gson gson = new GsonBuilder().registerTypeAdapter(List.class, new JsonSerializer<List<?>>() { + @Override + public JsonElement serialize(List<?> list, Type t, JsonSerializationContext jsc) { + if (list.size() == 1) { + // Don't put single element lists in a json array + return new Gson().toJsonTree(list.get(0)); + } else { + return new Gson().toJsonTree(list); + } + } + }).create(); + return gson.fromJson(response.getContent(), PropertyBean.class); + } + + public boolean deleteApplicationProperties(String appId, String accessToken) throws Exception { + URI uri = new URIBuilder( + this.securedEndpoint + RestConstants.METADATA_API + "/applications/" + appId + "/properties").build(); + HttpResponse response; + HttpDelete httpDelete = null; + try { + httpDelete = new HttpDelete(uri); + httpDelete.addHeader("Content-Type", "application/json"); + String bearerAuth = "Bearer " + accessToken; + httpDelete.addHeader("Authorization", bearerAuth); + response = httpClient.execute(httpDelete, new HttpResponseHandler()); + } finally { + releaseConnection(httpDelete); + } + + if (response != null) { + if ((response.getStatusCode() >= 200) && (response.getStatusCode() < 300)) { + return true; + } else { + throw new RuntimeException(response.getContent()); + } + } + throw new Exception("Null response received. Could not delete properties for application: " + appId); + } + /** * Get the json string from the artifacts directory * @@ -333,4 +455,4 @@ public class RestClient { private String getUsernamePassword() { return this.userName + ":" + this.password; } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/StratosIntegrationTest.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/StratosIntegrationTest.java b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/StratosIntegrationTest.java index 0f90dcb..2836f93 100644 --- a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/StratosIntegrationTest.java +++ b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/StratosIntegrationTest.java @@ -18,9 +18,10 @@ package org.apache.stratos.integration.tests; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.stratos.integration.common.TopologyHandler; import org.apache.stratos.integration.common.extensions.StratosServerExtension; -import org.apache.stratos.integration.common.rest.IntegrationMockClient; import org.apache.stratos.integration.common.rest.RestClient; +import org.apache.stratos.mock.iaas.client.MockIaasApiClient; import org.wso2.carbon.automation.engine.context.AutomationContext; import org.wso2.carbon.automation.engine.context.TestUserMode; @@ -30,26 +31,29 @@ public class StratosIntegrationTest { protected String adminUsername; protected String adminPassword; protected String stratosBackendURL; + protected String stratosSecuredBackendURL; protected RestClient restClient; - protected IntegrationMockClient mockIaasApiClient; + protected MockIaasApiClient mockIaasApiClient; public static final int GLOBAL_TEST_TIMEOUT = 5 * 60 * 1000; // 5 mins public static final int APPLICATION_TEST_TIMEOUT = 20 * 60 * 1000; // 20 mins public StratosIntegrationTest() { try { stratosAutomationCtx = new AutomationContext("STRATOS", "stratos-001", TestUserMode.SUPER_TENANT_ADMIN); - adminUsername = stratosAutomationCtx.getConfigurationValue - ("/automation/userManagement/superTenant/tenant/admin/user/userName"); - adminPassword = stratosAutomationCtx.getConfigurationValue - ("/automation/userManagement/superTenant/tenant/admin/user/password"); + adminUsername = stratosAutomationCtx + .getConfigurationValue("/automation/userManagement/superTenant/tenant/admin/user/userName"); + adminPassword = stratosAutomationCtx + .getConfigurationValue("/automation/userManagement/superTenant/tenant/admin/user/password"); // Do not rely on automation context for context URLs since ports are dynamically picked stratosBackendURL = StratosServerExtension.getStratosTestServerManager().getWebAppURL(); - restClient = new RestClient(stratosBackendURL, adminUsername, adminPassword); - mockIaasApiClient = new IntegrationMockClient(stratosBackendURL + "/mock-iaas/api"); - } - catch (Exception e) { + stratosSecuredBackendURL = StratosServerExtension.getStratosTestServerManager().getWebAppURLHttps(); + restClient = new RestClient(stratosBackendURL, stratosSecuredBackendURL, adminUsername, adminPassword); + mockIaasApiClient = new MockIaasApiClient(stratosBackendURL + "/mock-iaas/api"); + // initialize topology handler before running the tests + TopologyHandler.getInstance(); + } catch (Exception e) { throw new RuntimeException("Could not initialize StratosIntegrationTest", e); } } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SampleApplicationStartupTestCase.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SampleApplicationStartupTestCase.java b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SampleApplicationStartupTestCase.java index bc99bc9..1890287 100644 --- a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SampleApplicationStartupTestCase.java +++ b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SampleApplicationStartupTestCase.java @@ -19,6 +19,9 @@ package org.apache.stratos.integration.tests.application; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.stratos.common.beans.application.ApplicationBean; @@ -28,11 +31,17 @@ import org.apache.stratos.integration.common.TopologyHandler; import org.apache.stratos.integration.tests.StratosIntegrationTest; import org.apache.stratos.messaging.domain.application.ApplicationStatus; import org.apache.stratos.messaging.domain.topology.Member; +import org.apache.stratos.metadata.client.beans.PropertyBean; +import org.apache.stratos.mock.iaas.domain.MockInstanceMetadata; +import org.testng.Assert; import org.testng.annotations.Test; +import java.util.List; import java.util.Map; +import java.util.Properties; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertTrue; @@ -44,6 +53,12 @@ import static org.testng.AssertJUnit.assertTrue; public class SampleApplicationStartupTestCase extends StratosIntegrationTest { private static final Log log = LogFactory.getLog(SampleApplicationStartupTestCase.class); private static final String RESOURCES_PATH = "/sample-application-startup-test"; + private static final String PAYLOAD_PARAMETER_SEPARATOR = ","; + private static final String PAYLOAD_PARAMETER_NAME_VALUE_SEPARATOR = "="; + private static final String PAYLOAD_PARAMETER_TOKEN_KEY = "TOKEN"; + private static final String PAYLOAD_PARAMETER_APPLICATION_ID_KEY = "APPLICATION_ID"; + private GsonBuilder gsonBuilder = new GsonBuilder(); + private Gson gson = gsonBuilder.create(); @Test(timeOut = APPLICATION_TEST_TIMEOUT, description = "Application startup, activation and faulty member " + "detection", @@ -140,6 +155,47 @@ public class SampleApplicationStartupTestCase extends StratosIntegrationTest { log.info("Waiting for cluster status to become ACTIVE..."); topologyHandler.assertClusterActivation(bean.getApplicationId()); + List<Member> memberList = topologyHandler.getMembersForApplication(bean.getApplicationId()); + Assert.assertTrue(memberList.size() > 1, + String.format("Active member list for application %s is empty", bean.getApplicationId())); + MockInstanceMetadata mockInstanceMetadata = mockIaasApiClient.getInstance(memberList.get(0).getMemberId()); + String payloadString = mockInstanceMetadata.getPayload(); + log.info("Mock instance payload properties: " + payloadString); + + Properties payloadProperties = new Properties(); + String[] parameterArray = payloadString.split(PAYLOAD_PARAMETER_SEPARATOR); + for (String parameter : parameterArray) { + if (parameter != null) { + String[] nameValueArray = parameter.split(PAYLOAD_PARAMETER_NAME_VALUE_SEPARATOR, 2); + if ((nameValueArray.length == 2)) { + payloadProperties.put(nameValueArray[0], nameValueArray[1]); + } + } + } + + String key = "mykey"; + String val1 = "myval1"; + String val2 = "myval2"; + String accessToken = payloadProperties.getProperty(PAYLOAD_PARAMETER_TOKEN_KEY); + String appId = payloadProperties.getProperty(PAYLOAD_PARAMETER_APPLICATION_ID_KEY); + assertNotNull(accessToken, "Access token is null in member payload"); + + log.info("Trying to add metadata for application:" + appId + ", with accessToken: " + accessToken); + boolean hasProperty1Added = restClient.addPropertyToApplication(appId, key, val1, accessToken); + Assert.assertTrue(hasProperty1Added, "Could not add metadata property1 to application: " + appId); + + boolean hasProperty2Added = restClient.addPropertyToApplication(appId, key, val2, accessToken); + Assert.assertTrue(hasProperty2Added, "Could not add metadata property2 to application: " + appId); + + PropertyBean propertyBean = restClient.getApplicationProperty(appId, key, accessToken); + log.info("Retrieved metadata property: " + gson.toJson(propertyBean)); + Assert.assertTrue(propertyBean != null && propertyBean.getValues().length > 0, "Empty property list"); + boolean hasPropertiesAdded = ArrayUtils.contains(propertyBean.getValues(), val1) && ArrayUtils + .contains(propertyBean.getValues(), val2); + + Assert.assertTrue(hasPropertiesAdded, "Metadata properties retrieved are not correct"); + log.info("Metadata test completed successfully"); + log.info("Terminating members in [cluster id] c1-sample-application-startup-test in mock IaaS directly to " + "simulate faulty members..."); Map<String, Member> memberMap = TopologyHandler.getInstance() @@ -207,17 +263,15 @@ public class SampleApplicationStartupTestCase extends StratosIntegrationTest { RestConstants.AUTOSCALING_POLICIES_NAME); assertTrue(removedAuto); - log.info( - "Removing the deployment policy [deployment policy id] " - + "deployment-policy-sample-application-startup-test"); + log.info("Removing the deployment policy [deployment policy id] " + + "deployment-policy-sample-application-startup-test"); boolean removedDep = restClient .removeEntity(RestConstants.DEPLOYMENT_POLICIES, "deployment-policy-sample-application-startup-test", RestConstants.DEPLOYMENT_POLICIES_NAME); assertTrue(removedDep); - log.info( - "Removing the network partition [network partition id] " - + "network-partition-sample-application-startup-test"); + log.info("Removing the network partition [network partition id] " + + "network-partition-sample-application-startup-test"); boolean removedNet = restClient .removeEntity(RestConstants.NETWORK_PARTITIONS, "network-partition-sample-application-startup-test", RestConstants.NETWORK_PARTITIONS_NAME); http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-integration/src/test/resources/common/log4j.properties ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-integration/src/test/resources/common/log4j.properties b/products/stratos/modules/integration/test-integration/src/test/resources/common/log4j.properties index fb0da79..6fc6f45 100644 --- a/products/stratos/modules/integration/test-integration/src/test/resources/common/log4j.properties +++ b/products/stratos/modules/integration/test-integration/src/test/resources/common/log4j.properties @@ -59,7 +59,10 @@ log4j.logger.org.apache.stratos.cloud.controller=DEBUG log4j.logger.org.wso2.andes.client=ERROR # Autoscaler rule logs log4j.logger.org.apache.stratos.autoscaler.rule.RuleLog=DEBUG -org.apache.stratos.cloud.controller.messaging.topology.TopologyManager=INFO +log4j.logger.org.apache.stratos.cloud.controller.messaging.topology.TopologyManager=INFO +log4j.logger.org.apache.stratos.mock.iaas.client=DEBUG +log4j.logger.org.apache.stratos.mock.iaas.services=DEBUG +log4j.logger.org.apache.stratos.metadata.service=DEBUG # Apache jclouds #log4j.logger.jclouds.wire=DEBUG http://git-wip-us.apache.org/repos/asf/stratos/blob/4f021496/products/stratos/modules/integration/test-integration/src/test/resources/test-suite-smoke.xml ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-integration/src/test/resources/test-suite-smoke.xml b/products/stratos/modules/integration/test-integration/src/test/resources/test-suite-smoke.xml index bc44369..0f388d7 100644 --- a/products/stratos/modules/integration/test-integration/src/test/resources/test-suite-smoke.xml +++ b/products/stratos/modules/integration/test-integration/src/test/resources/test-suite-smoke.xml @@ -42,4 +42,4 @@ <package name="org.apache.stratos.integration.tests.*"/> </packages> </test> -</suite> \ No newline at end of file +</suite>
