http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/deployment-policies/deployment-policy-single-cluster-scaling-test.json ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/deployment-policies/deployment-policy-single-cluster-scaling-test.json b/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/deployment-policies/deployment-policy-single-cluster-scaling-test.json deleted file mode 100644 index 4818751..0000000 --- a/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/deployment-policies/deployment-policy-single-cluster-scaling-test.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "id": "deployment-policy-single-cluster-scaling-test", - "networkPartitions": [ - { - "id": "network-partition-single-cluster-scaling-test", - "partitionAlgo": "round-robin", - "partitions": [ - { - "id": "network-partition-11-partition-1", - "partitionMax": 3 - }, - { - "id": "network-partition-11-partition-2", - "partitionMax": 3 - } - ] - } - ] -} - - -
http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/network-partitions/mock/network-partition-single-cluster-scaling-test.json ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/network-partitions/mock/network-partition-single-cluster-scaling-test.json b/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/network-partitions/mock/network-partition-single-cluster-scaling-test.json deleted file mode 100644 index 13f1723..0000000 --- a/products/stratos/modules/integration/src/test/resources/single-cluster-scaling-test/network-partitions/mock/network-partition-single-cluster-scaling-test.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "id": "network-partition-single-cluster-scaling-test", - "provider": "mock", - "partitions": [ - { - "id": "network-partition-11-partition-1", - "property": [ - { - "name": "region", - "value": "default" - } - ] - }, - { - "id": "network-partition-11-partition-2", - "property": [ - { - "name": "region", - "value": "default" - } - ] - } - ] -} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/stratos-testing.xml ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/stratos-testing.xml b/products/stratos/modules/integration/src/test/resources/stratos-testing.xml deleted file mode 100644 index e1c3d95..0000000 --- a/products/stratos/modules/integration/src/test/resources/stratos-testing.xml +++ /dev/null @@ -1,94 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one - ~ or more contributor license agreements. See the NOTICE file - ~ distributed with this work for additional information - ~ regarding copyright ownership. The ASF licenses this file - ~ to you under the Apache License, Version 2.0 (the - ~ "License"); you may not use this file except in compliance - ~ with the License. You may obtain a copy of the License at - ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 - ~ - ~ Unless required by applicable law or agreed to in writing, - ~ software distributed under the License is distributed on an - ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - ~ KIND, either express or implied. See the License for the - ~ specific language governing permissions and limitations - ~ under the License. - --> - -<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > - -<suite name="StratosIntegrationSuite"> - <test name="UserTest"> - <classes> - <class name="org.apache.stratos.integration.tests.users.UserTest"/> - </classes> - </test> - <test name="CartridgeTest"> - <classes> - <class name="org.apache.stratos.integration.tests.group.CartridgeTest"/> - </classes> - </test> - <test name="CartridgeGroupTest"> - <classes> - <class name="org.apache.stratos.integration.tests.group.CartridgeGroupTest"/> - </classes> - </test> - <test name="NetworkPartitionTest"> - <classes> - <class name="org.apache.stratos.integration.tests.policies.NetworkPartitionTest"/> - </classes> - </test> - <test name="ApplicationPolicyTest"> - <classes> - <class name="org.apache.stratos.integration.tests.policies.ApplicationPolicyTest"/> - </classes> - </test> - <test name="DeploymentPolicyTest"> - <classes> - <class name="org.apache.stratos.integration.tests.policies.DeploymentPolicyTest"/> - </classes> - </test> - <test name="AutoscalingPolicyTest"> - <classes> - <class name="org.apache.stratos.integration.tests.policies.AutoscalingPolicyTest"/> - </classes> - </test> - <test name="SampleApplicationsTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.SampleApplicationsTest"/> - </classes> - </test> - <test name="ApplicationUpdateTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.ApplicationUpdateTest"/> - </classes> - </test> - <test name="SingleClusterScalingTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.SingleClusterScalingTest"/> - </classes> - </test> - <test name="ApplicationBurstingTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.ApplicationBurstingTest"/> - </classes> - </test> - <test name="PartitionRoundRobinClusterTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.PartitionRoundRobinClusterTest"/> - </classes> - </test> - <test name="GroupStartupOrderTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.GroupStartupOrderTest"/> - </classes> - </test> - <!--test name="GroupTerminationBehaviorTest"> - <classes> - <class name="org.apache.stratos.integration.tests.application.GroupTerminationBehaviorTest"/> - </classes> - </test--> -</suite> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/test-conf/integration-test.properties ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/test-conf/integration-test.properties b/products/stratos/modules/integration/src/test/resources/test-conf/integration-test.properties deleted file mode 100644 index a26383c..0000000 --- a/products/stratos/modules/integration/src/test/resources/test-conf/integration-test.properties +++ /dev/null @@ -1,26 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Stratos distribution properties added via filters during the build -distribution.path=/../../../distribution/target/${stratos.distribution.name}-${project.version}.zip -distribution.version=${project.version} -distribution.name=${stratos.distribution.name} -carbon.port.offset=0 -activemq.bind.address=tcp://localhost:61617 -stratos.endpoint=http://localhost:9763 -stratos.admin.username=admin -stratos.admin.password=admin \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/user-test/tenant-1.json ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/user-test/tenant-1.json b/products/stratos/modules/integration/src/test/resources/user-test/tenant-1.json deleted file mode 100644 index 0f599e4..0000000 --- a/products/stratos/modules/integration/src/test/resources/user-test/tenant-1.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "admin": "admin", - "firstName": "Frank", - "lastName": "Myers", - "adminPassword": "admin123", - "tenantDomain": "frank.com", - "email": "[email protected]", - "active": "true" -} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/user-test/user-1-v1.json ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/user-test/user-1-v1.json b/products/stratos/modules/integration/src/test/resources/user-test/user-1-v1.json deleted file mode 100644 index 1d2bbfd..0000000 --- a/products/stratos/modules/integration/src/test/resources/user-test/user-1-v1.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "userName": "user-1", - "credential": "kim123456", - "role": "admin", - "firstName": "Frankn", - "lastName": "Myersn", - "email": "[email protected]" -} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/src/test/resources/user-test/user-1.json ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/src/test/resources/user-test/user-1.json b/products/stratos/modules/integration/src/test/resources/user-test/user-1.json deleted file mode 100644 index 6f7da8a..0000000 --- a/products/stratos/modules/integration/src/test/resources/user-test/user-1.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "userName": "user-1", - "credential": "kim12345", - "role": "admin", - "firstName": "Frank", - "lastName": "Myers", - "email": "[email protected]" -} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/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 new file mode 100755 index 0000000..9abbd26 --- /dev/null +++ b/products/stratos/modules/integration/test-common/pom.xml @@ -0,0 +1,126 @@ +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.stratos</groupId> + <artifactId>stratos-integration</artifactId> + <version>4.1.3-SNAPSHOT</version> + </parent> + + <artifactId>org.apache.stratos.integration.common</artifactId> + <packaging>jar</packaging> + <name>Apache Stratos - Integration Common Module</name> + <description>Apache Stratos Integration Common Module</description> + + <dependencies> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-exec</artifactId> + </dependency> + <dependency> + <groupId>org.apache.stratos</groupId> + <artifactId>org.apache.stratos.common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.wso2.carbon.automation</groupId> + <artifactId>org.wso2.carbon.automation.test.utils</artifactId> + </dependency> + <dependency> + <groupId>org.wso2.carbon.automationutils</groupId> + <artifactId>org.wso2.carbon.integration.common.admin.client</artifactId> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.wso2.carbon.automation</groupId> + <artifactId>org.wso2.carbon.automation.extensions</artifactId> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>org.apache.tomcat.embed</groupId> + <artifactId>tomcat-embed-core</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.tomcat.embed</groupId> + <artifactId>tomcat-embed-logging-juli</artifactId> + </exclusion> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.wso2.carbon.automationutils</groupId> + <artifactId>org.wso2.carbon.integration.common.utils</artifactId> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>log4j-over-slf4j</artifactId> + </exclusion> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>jul-to-slf4j</artifactId> + </exclusion> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jdk14</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.wso2.carbon.automation</groupId> + <artifactId>org.wso2.carbon.automation.engine</artifactId> + </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-all</artifactId> + <version>5.10.0</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents.wso2</groupId> + <artifactId>httpcore</artifactId> + <version>${httpcore.version}</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents.wso2</groupId> + <artifactId>httpclient</artifactId> + <version>${httpclient.version}</version> + </dependency> + <dependency> + <groupId>org.apache.stratos</groupId> + <artifactId>org.apache.stratos.messaging</artifactId> + <version>${project.version}</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.apache.stratos</groupId> + <artifactId>org.apache.stratos.mock.iaas.client</artifactId> + <version>${project.version}</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.jacoco</groupId> + <artifactId>org.jacoco.agent</artifactId> + </dependency> + </dependencies> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/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 new file mode 100644 index 0000000..f3577bc --- /dev/null +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/RestConstants.java @@ -0,0 +1,53 @@ +/* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.stratos.integration.common; + +/** + * Constant class to keep track of rest endpoint context + */ +public class RestConstants { + public static final String API = "api"; + public static final String AUTOSCALING_POLICIES = "/" + API + "/autoscalingPolicies"; + public static final String USERS = "/" + API + "/users"; + public static final String TENANTS = "/" + API + "/tenants"; + public static final String DEPLOYMENT_POLICIES = "/" + API + "/deploymentPolicies"; + public static final String NETWORK_PARTITIONS = "/" + API + "/networkPartitions"; + public static final String CARTRIDGES = "/" + API + "/cartridges"; + public static final String CARTRIDGE_GROUPS = "/" + API + "/cartridgeGroups"; + public static final String APPLICATION_POLICIES = "/" + API + "/applicationPolicies"; + public static final String APPLICATIONS = "/" + API + "/applications"; + public static final String APPLICATIONS_RUNTIME = "/runtime"; + public static final String APPLICATIONS_DEPLOY = "/deploy"; + public static final String APPLICATIONS_UNDEPLOY = "/undeploy"; + + public static final String AUTOSCALING_POLICIES_PATH = "/autoscaling-policies/"; + public static final String AUTOSCALING_POLICIES_NAME = "autoscalingPolicy"; + public static final String CARTRIDGE_GROUPS_PATH = "/cartridges-groups/"; + public static final String CARTRIDGE_GROUPS_NAME = "cartridgeGroup"; + public static final String CARTRIDGES_PATH = "/cartridges/mock/"; + public static final String CARTRIDGES_NAME = "cartridge"; + public static final String NETWORK_PARTITIONS_PATH = "/network-partitions/mock/"; + public static final String NETWORK_PARTITIONS_NAME = "networkPartition"; + public static final String USERS_NAME = "users"; + public static final String DEPLOYMENT_POLICIES_PATH = "/deployment-policies/"; + public static final String DEPLOYMENT_POLICIES_NAME = "deploymentPolicy"; + public static final String APPLICATIONS_PATH = "/applications/"; + public static final String APPLICATIONS_NAME = "application"; + public static final String APPLICATION_POLICIES_PATH = "/application-policies/"; + public static final String APPLICATION_POLICIES_NAME = "applicationPolicy"; + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/StratosTestServerManager.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/StratosTestServerManager.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/StratosTestServerManager.java new file mode 100644 index 0000000..f3e0475 --- /dev/null +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/StratosTestServerManager.java @@ -0,0 +1,516 @@ +/* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.stratos.integration.common; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.automation.engine.context.AutomationContext; +import org.wso2.carbon.automation.engine.context.beans.User; +import org.wso2.carbon.automation.engine.exceptions.AutomationFrameworkException; +import org.wso2.carbon.automation.engine.frameworkutils.CodeCoverageUtils; +import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil; +import org.wso2.carbon.automation.engine.frameworkutils.ReportGenerator; +import org.wso2.carbon.automation.engine.frameworkutils.TestFrameworkUtils; +import org.wso2.carbon.automation.extensions.servers.carbonserver.CarbonServerManager; +import org.wso2.carbon.automation.extensions.servers.carbonserver.TestServerManager; +import org.wso2.carbon.automation.extensions.servers.utils.ArchiveExtractor; +import org.wso2.carbon.automation.extensions.servers.utils.ClientConnectionUtil; +import org.wso2.carbon.automation.extensions.servers.utils.FileManipulator; +import org.wso2.carbon.automation.extensions.servers.utils.ServerLogReader; + +import javax.xml.xpath.XPathExpressionException; +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +public class StratosTestServerManager extends TestServerManager { + private static final Log log = LogFactory.getLog(StratosTestServerManager.class); + public static final String PATH_SEP = File.separator; + public static final String MOCK_IAAS_XML_FILENAME = "mock-iaas.xml"; + public static final String SCALING_DROOL_FILENAME = "scaling.drl"; + public static final String JNDI_PROPERTIES_FILENAME = "jndi.properties"; + public static final String JMS_OUTPUT_ADAPTER_FILENAME = "JMSOutputAdaptor.xml"; + public static final String CLOUD_CONTROLLER_FILENAME = "cloud-controller.xml"; + public static final String AUTOSCALER_FILENAME = "autoscaler.xml"; + public static final String CARTRIDGE_CONFIG_PROPERTIES_FILENAME = "cartridge-config.properties"; + public static final String IDENTITY_FILENAME = "identity.xml"; + private static final String LOG4J_PROPERTIES_FILENAME = "log4j.properties"; + private static final String THRIFT_CLIENT_CONFIG_FILENAME = "thrift-client-config.xml"; + + public StratosTestServerManager(AutomationContext context) { + super(context); + this.carbonServer = new StratosServerManager(context); + } + + public StratosTestServerManager(AutomationContext context, String carbonZip, Map<String, String> commandMap) { + super(context, carbonZip, commandMap); + this.carbonServer = new StratosServerManager(context); + this.carbonZip = carbonZip; + if (commandMap.get("-DportOffset") != null) { + this.portOffset = Integer.parseInt((String) commandMap.get("-DportOffset")); + this.commandMap = commandMap; + } else { + throw new IllegalArgumentException("portOffset value must be set in command list"); + } + } + + public StratosTestServerManager(AutomationContext context, int portOffset) { + super(context, portOffset); + this.carbonServer = new StratosServerManager(context); + this.portOffset = portOffset; + this.commandMap.put("-DportOffset", String.valueOf(portOffset)); + } + + public StratosTestServerManager(AutomationContext context, String carbonZip) { + super(context, carbonZip); + this.carbonServer = new StratosServerManager(context); + this.carbonZip = carbonZip; + } + + // TODO: get rid of this method once startup script issue is fixed in automation engine + public String startServer() throws AutomationFrameworkException, IOException, XPathExpressionException { + if (this.carbonHome == null) { + if (this.carbonZip == null) { + this.carbonZip = System.getProperty("carbon.zip"); + } + + if (this.carbonZip == null) { + throw new IllegalArgumentException("carbon zip file cannot find in the given location"); + } + + this.carbonHome = this.carbonServer.setUpCarbonHome(this.carbonZip); + this.configureServer(); + } + + log.info("Carbon Home - " + this.carbonHome); + if (this.commandMap.get("-DportOffset") != null) { + this.portOffset = Integer.parseInt((String) this.commandMap.get("-DportOffset")); + } else { + this.portOffset = 0; + } + + this.carbonServer.startServerUsingCarbonHome(this.carbonHome, this.commandMap); + return this.carbonHome; + } + + public void configureServer() throws AutomationFrameworkException { + try { + log.info("Configuring server using CARBON_HOME: " + carbonHome); + copyArtifacts(carbonHome); + } + catch (IOException e) { + log.error("Could not configure Stratos server", e); + } + } + + public void stopServer() throws AutomationFrameworkException { + super.stopServer(); + } + + protected void copyArtifacts(String carbonHome) throws IOException { + String commonResourcesPath = Util.getCommonResourcesFolderPath(); + copyConfigFile(carbonHome, commonResourcesPath, MOCK_IAAS_XML_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, JNDI_PROPERTIES_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, LOG4J_PROPERTIES_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, CLOUD_CONTROLLER_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, AUTOSCALER_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, CARTRIDGE_CONFIG_PROPERTIES_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, IDENTITY_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, THRIFT_CLIENT_CONFIG_FILENAME, Util.CARBON_CONF_PATH); + copyConfigFile(carbonHome, commonResourcesPath, SCALING_DROOL_FILENAME, + Util.CARBON_CONF_PATH + PATH_SEP + "drools"); + copyConfigFile(carbonHome, commonResourcesPath, JMS_OUTPUT_ADAPTER_FILENAME, + "repository" + PATH_SEP + "deployment" + PATH_SEP + "server" + PATH_SEP + "outputeventadaptors"); + + } + + private void copyConfigFile(String carbonHome, String filePath, String fileName, String destinationFolder) + throws IOException { + assertNotNull(carbonHome, "CARBON_HOME is null"); + String fileAbsPath = filePath + PATH_SEP + fileName; + log.info("Copying file: " + fileAbsPath); + File srcFile = new File(fileAbsPath); + assertTrue(srcFile.exists(), "File does not exist [file] " + srcFile.getAbsolutePath()); + File destFile = new File(carbonHome + PATH_SEP + destinationFolder + PATH_SEP + fileName); + FileUtils.copyFile(srcFile, destFile); + log.info("Copying file [source] " + srcFile.getAbsolutePath() + " to [dest] " + destFile.getAbsolutePath()); + } +} + +// TODO: get rid of this class once startup script issue is fixed in automation engine +class StratosServerManager extends CarbonServerManager { + private final static Log log = LogFactory.getLog(StratosServerManager.class); + private Process process; + private String carbonHome; + private AutomationContext automationContext; + private ServerLogReader inputStreamHandler; + private ServerLogReader errorStreamHandler; + private boolean isCoverageEnable = false; + private String coverageDumpFilePath; + private int portOffset = 0; + private static final String SERVER_SHUTDOWN_MESSAGE = "Halting JVM"; + private static final String SERVER_STARTUP_MESSAGE = "Mgt Console URL"; + private static final long DEFAULT_START_STOP_WAIT_MS = 300000L; + private static final String CMD_ARG = "cmdArg"; + private static int defaultHttpPort = Integer.parseInt("9763"); + private static int defaultHttpsPort = Integer.parseInt("9443"); + + public StratosServerManager(AutomationContext context) { + super(context); + this.automationContext = context; + } + + public synchronized void startServerUsingCarbonHome(String carbonHome, Map<String, String> commandMap) + throws AutomationFrameworkException { + if (this.process == null) { + this.portOffset = this.checkPortAvailability(commandMap); + Process tempProcess = null; + + try { + if (!commandMap.isEmpty() && this.getPortOffsetFromCommandMap(commandMap) == 0) { + System.setProperty("carbon.home", carbonHome); + } + + File e = new File(carbonHome); + log.info("Starting carbon server............. "); + String scriptName = TestFrameworkUtils.getStartupScriptFileName(carbonHome); + String[] parameters = this.expandServerStartupCommandList(commandMap); + String[] cmdArray; + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + e = new File(carbonHome + File.separator + "bin"); + cmdArray = new String[]{"cmd.exe", "/c", scriptName + ".bat"}; + cmdArray = this.mergePropertiesToCommandArray(parameters, cmdArray); + tempProcess = Runtime.getRuntime().exec(cmdArray, (String[]) null, e); + } else { + cmdArray = new String[]{"sh", "bin/" + scriptName + ".sh"}; + cmdArray = this.mergePropertiesToCommandArray(parameters, cmdArray); + tempProcess = Runtime.getRuntime().exec(cmdArray, (String[]) null, e); + } + + this.errorStreamHandler = new ServerLogReader("errorStream", tempProcess.getErrorStream()); + this.inputStreamHandler = new ServerLogReader("inputStream", tempProcess.getInputStream()); + this.inputStreamHandler.start(); + this.errorStreamHandler.start(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { + StratosServerManager.this.serverShutdown(StratosServerManager.this.portOffset); + } + catch (Exception var2) { + log.error("Error while server shutdown ..", var2); + } + + } + }); + ClientConnectionUtil.waitForPort(defaultHttpPort + this.portOffset, 300000L, false, + (String) this.automationContext.getInstance().getHosts().get("default")); + long time = System.currentTimeMillis() + 60000L; + + while (true) { + if (this.inputStreamHandler.getOutput().contains("Mgt Console URL") || + System.currentTimeMillis() >= time) { + int httpsPort = defaultHttpsPort + this.portOffset; + String backendURL = this.automationContext.getContextUrls().getSecureServiceUrl() + .replaceAll("(:\\d+)", ":" + httpsPort); + User superUser = this.automationContext.getSuperTenant().getTenantAdmin(); + ClientConnectionUtil.waitForLogin(backendURL, superUser); + log.info("Server started successfully."); + break; + } + } + } + catch (XPathExpressionException | IOException var13) { + throw new IllegalStateException("Unable to start server", var13); + } + + this.process = tempProcess; + } + } + + private int checkPortAvailability(Map<String, String> commandMap) throws AutomationFrameworkException { + int portOffset = this.getPortOffsetFromCommandMap(commandMap); + if (ClientConnectionUtil.isPortOpen(defaultHttpPort + portOffset)) { + throw new AutomationFrameworkException( + "Unable to start carbon server on port " + (defaultHttpPort + portOffset) + + " : Port already in use"); + } else if (ClientConnectionUtil.isPortOpen(defaultHttpsPort + portOffset)) { + throw new AutomationFrameworkException( + "Unable to start carbon server on port " + (defaultHttpsPort + portOffset) + + " : Port already in use"); + } else { + return portOffset; + } + } + + private String[] mergePropertiesToCommandArray(String[] parameters, String[] cmdArray) { + if (parameters != null) { + cmdArray = this.mergerArrays(cmdArray, parameters); + } + + return cmdArray; + } + + public synchronized String setUpCarbonHome(String carbonServerZipFile) + throws IOException, AutomationFrameworkException { + if (this.process != null) { + return this.carbonHome; + } else { + int indexOfZip = carbonServerZipFile.lastIndexOf(".zip"); + if (indexOfZip == -1) { + throw new IllegalArgumentException(carbonServerZipFile + " is not a zip file"); + } else { + String fileSeparator = File.separator.equals("\\") ? "\\" : "/"; + if (fileSeparator.equals("\\")) { + carbonServerZipFile = carbonServerZipFile.replace("/", "\\"); + } + + String extractedCarbonDir = carbonServerZipFile + .substring(carbonServerZipFile.lastIndexOf(fileSeparator) + 1, indexOfZip); + FileManipulator.deleteDir(extractedCarbonDir); + String extractDir = "carbontmp" + System.currentTimeMillis(); + String baseDir = System.getProperty("basedir", ".") + File.separator + "target"; + log.info("Extracting carbon zip file.. "); + (new ArchiveExtractor()).extractFile(carbonServerZipFile, baseDir + File.separator + extractDir); + this.carbonHome = + (new File(baseDir)).getAbsolutePath() + File.separator + extractDir + File.separator + + extractedCarbonDir; + + try { + this.isCoverageEnable = + Boolean.parseBoolean(this.automationContext.getConfigurationValue("//coverage")); + } + catch (XPathExpressionException var8) { + throw new AutomationFrameworkException("Coverage configuration not found in automation.xml", + var8); + } + // Fix startup script issue by copying stratos.sh as stratos-server.sh + // TODO: remove this class after automation engine provides a way to pass startup script name + // currently startup script should be either wso2server.sh or contain the string 'server' + FileUtils.copyFile(new File(carbonHome + File.separator + "bin" + File.separator + "stratos.sh"), + new File(carbonHome + File.separator + "bin" + File.separator + "stratos-server.sh")); + + if (this.isCoverageEnable) { + this.instrumentForCoverage(); + } + + return this.carbonHome; + } + } + } + + public synchronized void serverShutdown(int portOffset) throws AutomationFrameworkException { + if (this.process != null) { + log.info("Shutting down server.."); + if (ClientConnectionUtil.isPortOpen(Integer.parseInt("9443") + portOffset)) { + int e = defaultHttpsPort + portOffset; + String url = null; + + try { + url = this.automationContext.getContextUrls().getBackEndUrl(); + } + catch (XPathExpressionException var10) { + throw new AutomationFrameworkException("Get context failed", var10); + } + + String backendURL = url.replaceAll("(:\\d+)", ":" + e); + + try { + ClientConnectionUtil.sendForcefulShutDownRequest(backendURL, + this.automationContext.getSuperTenant().getContextUser().getUserName(), + this.automationContext.getSuperTenant().getContextUser().getPassword()); + } + catch (AutomationFrameworkException var8) { + throw new AutomationFrameworkException("Get context failed", var8); + } + catch (XPathExpressionException var9) { + throw new AutomationFrameworkException("Get context failed", var9); + } + + long time = System.currentTimeMillis() + 300000L; + + while (!this.inputStreamHandler.getOutput().contains("Halting JVM") && + System.currentTimeMillis() < time) { + ; + } + + log.info("Server stopped successfully..."); + } + + this.inputStreamHandler.stop(); + this.errorStreamHandler.stop(); + this.process.destroy(); + this.process = null; + if (this.isCoverageEnable) { + try { + log.info("Generating Jacoco code coverage..."); + this.generateCoverageReport(new File( + this.carbonHome + File.separator + "repository" + File.separator + "components" + + File.separator + "plugins" + File.separator)); + } + catch (IOException var7) { + log.error("Failed to generate code coverage ", var7); + throw new AutomationFrameworkException("Failed to generate code coverage ", var7); + } + } + + if (portOffset == 0) { + System.clearProperty("carbon.home"); + } + } + + } + + private void generateCoverageReport(File classesDir) throws IOException, AutomationFrameworkException { + CodeCoverageUtils + .executeMerge(FrameworkPathUtil.getJacocoCoverageHome(), + FrameworkPathUtil.getCoverageMergeFilePath()); + ReportGenerator reportGenerator = + new ReportGenerator(new File(FrameworkPathUtil.getCoverageMergeFilePath()), classesDir, + new File(CodeCoverageUtils.getJacocoReportDirectory()), (File) null); + reportGenerator.create(); + log.info("Jacoco coverage dump file path : " + FrameworkPathUtil.getCoverageDumpFilePath()); + log.info("Jacoco class file path : " + classesDir); + log.info("Jacoco coverage HTML report path : " + CodeCoverageUtils.getJacocoReportDirectory() + + File.separator + "index.html"); + } + + public synchronized void restartGracefully() throws AutomationFrameworkException { + try { + int time = defaultHttpsPort + this.portOffset; + String backendURL = + this.automationContext.getContextUrls().getSecureServiceUrl().replaceAll("(:\\d+)", ":" + time); + User e = this.automationContext.getSuperTenant().getTenantAdmin(); + ClientConnectionUtil.sendGraceFullRestartRequest(backendURL, e.getUserName(), e.getPassword()); + } + catch (XPathExpressionException var5) { + throw new AutomationFrameworkException("restart failed", var5); + } + + long time1 = System.currentTimeMillis() + 300000L; + + while (!this.inputStreamHandler.getOutput().contains("Halting JVM") && System.currentTimeMillis() < time1) { + ; + } + + time1 = System.currentTimeMillis(); + + while (System.currentTimeMillis() < time1 + 5000L) { + ; + } + + try { + ClientConnectionUtil.waitForPort( + Integer.parseInt((String) this.automationContext.getInstance().getPorts().get("https")), + (String) this.automationContext.getInstance().getHosts().get("default")); + ClientConnectionUtil.waitForLogin(this.automationContext); + } + catch (XPathExpressionException var4) { + throw new AutomationFrameworkException("Connection attempt to carbon server failed", var4); + } + } + + private String[] expandServerStartupCommandList(Map<String, String> commandMap) { + if (commandMap != null && commandMap.size() != 0) { + String[] cmdParaArray = null; + String cmdArg = null; + if (commandMap.containsKey("cmdArg")) { + cmdArg = (String) commandMap.get("cmdArg"); + cmdParaArray = cmdArg.trim().split("\\s+"); + commandMap.remove("cmdArg"); + } + + String[] parameterArray = new String[commandMap.size()]; + int arrayIndex = 0; + Set entries = commandMap.entrySet(); + + String parameter; + for (Iterator i$ = entries.iterator(); i$.hasNext(); parameterArray[arrayIndex++] = parameter) { + Map.Entry entry = (Map.Entry) i$.next(); + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + if (value != null && !value.isEmpty()) { + parameter = key + "=" + value; + } else { + parameter = key; + } + } + + if (cmdArg != null) { + commandMap.put("cmdArg", cmdArg); + } + + if (cmdParaArray != null && cmdParaArray.length != 0) { + return (String[]) ArrayUtils.addAll(parameterArray, cmdParaArray); + } else { + return parameterArray; + } + } else { + return null; + } + } + + private int getPortOffsetFromCommandMap(Map<String, String> commandMap) { + return commandMap.containsKey("-DportOffset") ? Integer.parseInt((String) commandMap.get("-DportOffset")) : + 0; + } + + private String[] mergerArrays(String[] array1, String[] array2) { + return (String[]) ArrayUtils.addAll(array1, array2); + } + + private void insertJacocoAgentToShellScript(String scriptName) throws IOException { + String jacocoAgentFile = CodeCoverageUtils.getJacocoAgentJarLocation(); + this.coverageDumpFilePath = FrameworkPathUtil.getCoverageDumpFilePath(); + CodeCoverageUtils.insertStringToFile( + new File(this.carbonHome + File.separator + "bin" + File.separator + scriptName + ".sh"), + new File(this.carbonHome + File.separator + "tmp" + File.separator + scriptName + ".sh"), + "-Dwso2.server.standalone=true", + "-javaagent:" + jacocoAgentFile + "=destfile=" + this.coverageDumpFilePath + "" + + ",append=true,includes=" + CodeCoverageUtils.getInclusionJarsPattern(":") + " \\"); + } + + private void insertJacocoAgentToBatScript(String scriptName) throws IOException { + String jacocoAgentFile = CodeCoverageUtils.getJacocoAgentJarLocation(); + this.coverageDumpFilePath = FrameworkPathUtil.getCoverageDumpFilePath(); + CodeCoverageUtils.insertJacocoAgentToStartupBat( + new File(this.carbonHome + File.separator + "bin" + File.separator + scriptName + ".bat"), + new File(this.carbonHome + File.separator + "tmp" + File.separator + scriptName + ".bat"), + "-Dcatalina.base", "-javaagent:" + jacocoAgentFile + "=destfile=" + this.coverageDumpFilePath + "" + + ",append=true,includes=" + CodeCoverageUtils.getInclusionJarsPattern(":")); + } + + private void instrumentForCoverage() throws IOException, AutomationFrameworkException { + String scriptName = TestFrameworkUtils.getStartupScriptFileName(this.carbonHome); + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + this.insertJacocoAgentToBatScript(scriptName); + if (log.isDebugEnabled()) { + log.debug("Included files " + CodeCoverageUtils.getInclusionJarsPattern(":")); + log.debug("Excluded files " + CodeCoverageUtils.getExclusionJarsPattern(":")); + } + } else { + this.insertJacocoAgentToShellScript(scriptName); + } + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/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 new file mode 100644 index 0000000..9c7579f --- /dev/null +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java @@ -0,0 +1,677 @@ +/* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.stratos.integration.common; + +import org.apache.commons.logging.Log; +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; +import org.apache.stratos.messaging.domain.topology.Cluster; +import org.apache.stratos.messaging.domain.topology.Member; +import org.apache.stratos.messaging.domain.topology.MemberStatus; +import org.apache.stratos.messaging.domain.topology.Service; +import org.apache.stratos.messaging.event.Event; +import org.apache.stratos.messaging.event.application.*; +import org.apache.stratos.messaging.event.topology.*; +import org.apache.stratos.messaging.listener.application.*; +import org.apache.stratos.messaging.listener.topology.*; +import org.apache.stratos.messaging.message.receiver.application.ApplicationManager; +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 java.rmi.RemoteException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +import static org.testng.AssertJUnit.*; + +/** + * To start the Topology receivers + */ +public class TopologyHandler { + private static final Log log = LogFactory.getLog(TopologyHandler.class); + + public static final int APPLICATION_ACTIVATION_TIMEOUT = 360000; + public static final int APPLICATION_UNDEPLOYMENT_TIMEOUT = 120000; + public static final int MEMBER_TERMINATION_TIMEOUT = 120000; + public static final int APPLICATION_TOPOLOGY_TIMEOUT = 90000; + public static final String APPLICATION_STATUS_CREATED = "Created"; + public static final String APPLICATION_STATUS_UNDEPLOYING = "Undeploying"; + private ApplicationsEventReceiver applicationsEventReceiver; + private TopologyEventReceiver topologyEventReceiver; + public static TopologyHandler topologyHandler; + private Map<String, Long> terminatedMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> terminatingMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> createdMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> inActiveMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> activateddMembers = new ConcurrentHashMap<String, Long>(); + + private TopologyHandler() { + initializeApplicationEventReceiver(); + initializeTopologyEventReceiver(); + assertApplicationTopologyInitialized(); + assertTopologyInitialized(); + addTopologyEventListeners(); + addApplicationEventListeners(); + } + + public static TopologyHandler getInstance() { + if (topologyHandler == null) { + synchronized (TopologyHandler.class) { + if (topologyHandler == null) { + topologyHandler = new TopologyHandler(); + } + } + } + return topologyHandler; + } + + + /** + * Initialize application event receiver + */ + private void initializeApplicationEventReceiver() { + if (applicationsEventReceiver == null) { + applicationsEventReceiver = new ApplicationsEventReceiver(); + ExecutorService executorService = StratosThreadPool.getExecutorService("STRATOS_TEST_SERVER", 1); + applicationsEventReceiver.setExecutorService(executorService); + applicationsEventReceiver.execute(); + } + } + + /** + * Initialize Topology event receiver + */ + private void initializeTopologyEventReceiver() { + if (topologyEventReceiver == null) { + topologyEventReceiver = new TopologyEventReceiver(); + ExecutorService executorService = StratosThreadPool.getExecutorService("STRATOS_TEST_SERVER1", 1); + topologyEventReceiver.setExecutorService(executorService); + topologyEventReceiver.execute(); + } + } + + /** + * Assert application Topology initialization + */ + private void assertApplicationTopologyInitialized() { + long startTime = System.currentTimeMillis(); + boolean applicationTopologyInitialized = ApplicationManager.getApplications().isInitialized(); + while (!applicationTopologyInitialized) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + applicationTopologyInitialized = ApplicationManager.getApplications().isInitialized(); + if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { + break; + } + } + assertEquals(String.format("Application Topology didn't get initialized "), applicationTopologyInitialized, + true); + } + + /** + * Assert Topology initialization + */ + private void assertTopologyInitialized() { + long startTime = System.currentTimeMillis(); + boolean topologyInitialized = TopologyManager.getTopology().isInitialized(); + while (!topologyInitialized) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + topologyInitialized = TopologyManager.getTopology().isInitialized(); + if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { + break; + } + } + assertEquals(String.format("Topology didn't get initialized "), topologyInitialized, true); + } + + /** + * Assert application activation + * + * @param applicationName + */ + public void assertApplicationStatus(String applicationName, ApplicationStatus status) { + long startTime = System.currentTimeMillis(); + Application application = ApplicationManager.getApplications().getApplication(applicationName); + while (!((application != null) && (application.getStatus() == status))) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + application = ApplicationManager.getApplications().getApplication(applicationName); + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + break; + } + } + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + assertEquals(String.format("Application status did not change to %s: [application-id] %s", status.toString(), + applicationName), status, application.getStatus()); + } + + /** + * Assert application activation + * + * @param applicationName + */ + public void assertGroupActivation(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNotNull(String.format("Application is not found: [application-id] %s", + applicationName), application); + + Collection<Group> groups = application.getAllGroupsRecursively(); + for (Group group : groups) { + assertEquals(group.getInstanceContextCount() >= group.getGroupMinInstances(), true); + } + } + + /** + * Assert application activation + * + * @param applicationName + */ + public void assertClusterActivation(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + 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); + + 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())); + } + boolean clusterActive = false; + int activeInstances; + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + log.info("Checking for active members in cluster instance: " + instance.getInstanceId()); + activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = (activeInstances >= clusterDataHolder.getMinInstances()); + assertTrue(String.format("Cluster status did not change to active: [cluster-id] %s", clusterId), + clusterActive); + } + } + } + + /** + * Get all the members that belongs to the cluster identified by cartridge name and application name in the + * topology + * + * @param cartridgeName + * @param applicationName + */ + public Map<String, Member> getMembersForCluster(String cartridgeName, String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); + Map<String, Member> memberMap = new HashMap<String, Member>(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { + String serviceName = clusterDataHolder.getServiceType(); + 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); + + 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 (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + for (Member member : cluster.getMembers()) { + memberMap.put(member.getMemberId(), member); + } + } + } + } + return memberMap; + } + + /** + * Terminate a member in mock iaas directly without involving Stratos REST API + * This is similar to manually terminating an instance in an IaaS. This could be used to simulate member failures + * + * @param memberId + * @param mockIaasApiClient + */ + public void terminateMemberInMockIaas(String memberId, IntegrationMockClient mockIaasApiClient) { + boolean memberTerminated = false; + memberTerminated = mockIaasApiClient.terminateInstance(memberId); + assertTrue(String.format("Member [member-id] %s couldn't be terminated from the mock IaaS", memberId), + memberTerminated); + } + + public void assertMemberTermination(String memberId) { + long startTime = System.currentTimeMillis(); + assertNotNull(String.format("Member id is not found: [member-id] %s", memberId)); + boolean hasMemberRemoved = false; + while (!hasMemberRemoved) { + // Wait until the member gets removed by MemberTerminatedEvent topology receiver + if (getTerminatingMembers().get(memberId) == null && + getInActiveMembers().get(memberId) == null && + getActivateddMembers().get(memberId) == null && + getCreatedMembers().get(memberId) == null) { + getTerminatedMembers().remove(memberId); + hasMemberRemoved = true; + } else { + if (getTerminatedMembers().get(memberId) - startTime > MEMBER_TERMINATION_TIMEOUT) { + break; + } + } + try { + Thread.sleep(1000); + } + catch (InterruptedException e) { + log.error("Could not sleep", e); + } + } + assertTrue(String.format("Member [member-id] %s did not get removed from the topology", memberId), + hasMemberRemoved); + } + + public void assertClusterMinMemberCount(String applicationName, int minMembers) { + long startTime = System.currentTimeMillis(); + + Application application = ApplicationManager.getApplications().getApplication(applicationName); + 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); + + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationName, serviceName, clusterId), cluster); + boolean clusterActive = false; + + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + int activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = activeInstances >= minMembers; + + while (!clusterActive) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + service = TopologyManager.getTopology().getService(serviceName); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", + applicationName, serviceName), service); + + cluster = service.getCluster(clusterId); + activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = activeInstances >= minMembers; + assertNotNull( + String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationName, serviceName, clusterId), cluster); + + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + break; + } + } + } + assertEquals(String.format("Cluster status did not change to active: [cluster-id] %s", clusterId), + clusterActive, true); + } + + } + + + /** + * Assert application activation + * + * @param applicationName + */ + public boolean assertApplicationUndeploy(String applicationName) { + long startTime = System.currentTimeMillis(); + Application application = ApplicationManager.getApplications().getApplication(applicationName); + ApplicationContext applicationContext = null; + try { + applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); + } + 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))) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + application = ApplicationManager.getApplications().getApplication(applicationName); + try { + applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); + } + catch (RemoteException e) { + log.error("Error while getting the application context for [application] " + applicationName); + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_UNDEPLOYMENT_TIMEOUT) { + break; + } + } + + 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)) { + return false; + } + assertEquals( + String.format("Application status did not change to Created: [application-id] %s", applicationName), + APPLICATION_STATUS_CREATED, applicationContext.getStatus()); + return true; + } + + /** + * Assert application activation + * + * @param applicationName + */ + public void assertGroupInstanceCount(String applicationName, String groupAlias, int count) { + long startTime = System.currentTimeMillis(); + Application application = ApplicationManager.getApplications().getApplication(applicationName); + if (application != null) { + Group group = application.getGroupRecursively(groupAlias); + while (group.getInstanceContextCount() != count) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + break; + } + } + for (GroupInstance instance : group.getInstanceIdToInstanceContextMap().values()) { + while (!instance.getStatus().equals(GroupStatus.Active)) { + try { + Thread.sleep(1000); + } + catch (InterruptedException ignore) { + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + break; + } + } + } + assertEquals( + String.format("Application status did not change to active: [application-id] %s", applicationName), + group.getInstanceContextCount(), count); + } + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + + } + + public void assertApplicationNotExists(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNull(String.format("Application is found in the topology : [application-id] %s", applicationName), + application); + } + + private void addTopologyEventListeners() { + topologyEventReceiver.addEventListener(new MemberTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + MemberTerminatedEvent memberTerminatedEvent = (MemberTerminatedEvent) event; + getTerminatedMembers().put(memberTerminatedEvent.getMemberId(), System.currentTimeMillis()); + getActivateddMembers().remove(((MemberTerminatedEvent) event).getMemberId()); + getCreatedMembers().remove(((MemberTerminatedEvent) event).getMemberId()); + getInActiveMembers().remove(((MemberTerminatedEvent) event).getMemberId()); + getTerminatingMembers().remove(((MemberTerminatedEvent) event).getMemberId()); + } + }); + + + topologyEventReceiver.addEventListener(new ClusterInstanceCreatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceCreatedEvent event1 = (ClusterInstanceCreatedEvent) event; + String clusterId = event1.getClusterId(); + getCreatedMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceActivatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceActivatedEvent event1 = (ClusterInstanceActivatedEvent) event; + String clusterId = event1.getClusterId(); + getActivateddMembers().put(clusterId, System.currentTimeMillis()); + + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceInactivateEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceInactivateEvent event1 = (ClusterInstanceInactivateEvent) event; + String clusterId = event1.getClusterId(); + getInActiveMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceTerminatedEvent event1 = (ClusterInstanceTerminatedEvent) event; + String clusterId = event1.getClusterId(); + getTerminatedMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceTerminatingEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceTerminatingEvent event1 = (ClusterInstanceTerminatingEvent) event; + String clusterId = event1.getClusterId(); + getTerminatingMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + + } + + private void addApplicationEventListeners() { + + applicationsEventReceiver.addEventListener(new GroupInstanceCreatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceCreatedEvent event1 = (GroupInstanceCreatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getGroupInstance().getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getCreatedMembers().put(id, System.currentTimeMillis()); + + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceActivatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceActivatedEvent event1 = (GroupInstanceActivatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getActivateddMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceInactivateEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceInactivatedEvent event1 = (GroupInstanceInactivatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getInActiveMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceTerminatedEvent event1 = (GroupInstanceTerminatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getTerminatedMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceTerminatingEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceTerminatingEvent event1 = (GroupInstanceTerminatingEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getTerminatingMembers().put(id, System.currentTimeMillis()); + } + }); + } + + public String generateId(String appId, String groupId, String instanceId) { + return appId + "-" + groupId + "-" + instanceId; + } + + public String getClusterIdFromAlias(String applicationId, String alias) { + Application application = ApplicationManager.getApplications().getApplication(applicationId); + assertNotNull(application); + + ClusterDataHolder dataHolder = application.getClusterDataHolderRecursivelyByAlias(alias); + assertNotNull(dataHolder); + + return dataHolder.getClusterId(); + } + + + public void removeMembersFromMaps(String applicationId) { + for (Map.Entry<String, Long> entry : getActivateddMembers().entrySet()) { + if (entry.getKey().contains(applicationId)) { + getActivateddMembers().remove(entry.getKey()); + } + } + + for (Map.Entry<String, Long> entry : getTerminatedMembers().entrySet()) { + if (entry.getKey().contains(applicationId)) { + getTerminatedMembers().remove(entry.getKey()); + } + } + } + + public Map<String, Long> getTerminatedMembers() { + return terminatedMembers; + } + + public void setTerminatedMembers(Map<String, Long> terminatedMembers) { + this.terminatedMembers = terminatedMembers; + } + + public Map<String, Long> getTerminatingMembers() { + return terminatingMembers; + } + + public void setTerminatingMembers(Map<String, Long> terminatingMembers) { + this.terminatingMembers = terminatingMembers; + } + + public Map<String, Long> getCreatedMembers() { + return createdMembers; + } + + public void setCreatedMembers(Map<String, Long> createdMembers) { + this.createdMembers = createdMembers; + } + + public Map<String, Long> getInActiveMembers() { + return inActiveMembers; + } + + public void setInActiveMembers(Map<String, Long> inActiveMembers) { + this.inActiveMembers = inActiveMembers; + } + + public Map<String, Long> getActivateddMembers() { + return activateddMembers; + } + + public void setActivateddMembers(Map<String, Long> activateddMembers) { + this.activateddMembers = activateddMembers; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/Util.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/Util.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/Util.java new file mode 100644 index 0000000..547ee16 --- /dev/null +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/Util.java @@ -0,0 +1,38 @@ +/* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.stratos.integration.common; + +import org.apache.commons.lang.StringUtils; + +import java.io.File; + +public class Util { + public static final String CARBON_ZIP_KEY = "carbon.zip"; + public static final String ACTIVEMQ_BIND_ADDRESS = "activemq.bind.address"; + public static final String CARBON_CONF_PATH = "repository" + File.separator + "conf"; + public static final String BASE_PATH = Util.class.getResource(File.separator).getPath(); + + /** + * Get resources folder path + * + * @return + */ + public static String getCommonResourcesFolderPath() { + String path = Util.class.getResource(File.separator + "common").getPath(); + return StringUtils.removeEnd(path, File.separator); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/395be450/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/extensions/StratosServerExtension.java ---------------------------------------------------------------------- diff --git a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/extensions/StratosServerExtension.java b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/extensions/StratosServerExtension.java new file mode 100644 index 0000000..3d260e5 --- /dev/null +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/extensions/StratosServerExtension.java @@ -0,0 +1,145 @@ +/* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.stratos.integration.common.extensions; + +import org.apache.activemq.broker.BrokerService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.stratos.common.test.TestLogAppender; +import org.apache.stratos.integration.common.StratosTestServerManager; +import org.apache.stratos.integration.common.Util; +import org.wso2.carbon.automation.engine.context.AutomationContext; +import org.wso2.carbon.automation.engine.context.TestUserMode; +import org.wso2.carbon.automation.engine.exceptions.AutomationFrameworkException; +import org.wso2.carbon.automation.engine.extensions.ExecutionListenerExtension; +import org.wso2.carbon.automation.extensions.ExtensionConstants; + +import java.io.File; + +import static org.testng.Assert.assertNotNull; + +public class StratosServerExtension extends ExecutionListenerExtension { + private static final Log log = LogFactory.getLog(StratosServerExtension.class); + public static final String PATH_SEP = File.separator; + private TestLogAppender testLogAppender; + private StratosTestServerManager carbonTestServerManager; + private BrokerService broker; + + @Override + public void initiate() throws AutomationFrameworkException { + broker = new BrokerService(); + testLogAppender = new TestLogAppender(); + } + + @Override + public void onExecutionStart() throws AutomationFrameworkException { + Logger.getRootLogger().addAppender(testLogAppender); + Logger.getRootLogger().setLevel(Level.INFO); + try { + String activemqBindAddress = getParameters().get(Util.ACTIVEMQ_BIND_ADDRESS); + long time1 = System.currentTimeMillis(); + log.info("Starting ActiveMQ..."); + broker.setDataDirectory(StratosServerExtension.class.getResource(File.separator).getPath() + + File.separator + ".." + File.separator + "activemq-data"); + broker.setBrokerName("testBroker"); + broker.addConnector(activemqBindAddress); + broker.start(); + long time2 = System.currentTimeMillis(); + log.info(String.format("ActiveMQ started in %d sec", (time2 - time1) / 1000)); + } + catch (Exception e) { + throw new RuntimeException("Could not start ActiveMQ", e); + } + + try { + log.info("Setting up Stratos server..."); + AutomationContext stratosAutomationCtx = + new AutomationContext("STRATOS", "stratos-001", TestUserMode.SUPER_TENANT_ADMIN); + String stratosBackendURL = stratosAutomationCtx.getContextUrls().getWebAppURL(); + //if port offset is not set, setting it to 0 + if (getParameters().get(ExtensionConstants.SERVER_STARTUP_PORT_OFFSET_COMMAND) == null) { + getParameters().put(ExtensionConstants.SERVER_STARTUP_PORT_OFFSET_COMMAND, "0"); + } + carbonTestServerManager = + new StratosTestServerManager(stratosAutomationCtx, System.getProperty(Util.CARBON_ZIP_KEY), + getParameters()); + + log.info("Stratos server port offset: " + carbonTestServerManager.getPortOffset()); + log.info("Stratos backend URL: " + stratosBackendURL); + + long time3 = System.currentTimeMillis(); + String carbonHome = carbonTestServerManager.startServer(); + assertNotNull(carbonHome, "CARBON_HOME is null"); + + while (!serverStarted()) { + log.info("Waiting for topology to be initialized..."); + Thread.sleep(5000); + } + + while (!mockServiceStarted()) { + log.info("Waiting for mock service to be initialized..."); + Thread.sleep(1000); + } + + long time4 = System.currentTimeMillis(); + log.info(String.format("Stratos server started in %d sec", (time4 - time3) / 1000)); + + } + catch (Exception e) { + throw new RuntimeException("Could not start Stratos server", e); + } + } + + @Override + public void onExecutionFinish() throws AutomationFrameworkException { + try { + carbonTestServerManager.stopServer(); + log.info("Stopped Stratos server"); + } + catch (Exception e) { + log.error("Could not stop Stratos server", e); + } + + try { + broker.stop(); + log.info("Stopped ActiveMQ server"); + } + catch (Exception e) { + log.error("Could not stop ActiveMQ server", e); + } + } + + private boolean serverStarted() { + for (String message : testLogAppender.getMessages()) { + if (message.contains("Topology initialized")) { + return true; + } + } + return false; + } + + private boolean mockServiceStarted() { + for (String message : testLogAppender.getMessages()) { + if (message.contains("Mock IaaS service component activated")) { + return true; + } + } + return false; + } +} \ No newline at end of file
