Repository: stratos Updated Branches: refs/heads/master 932c3286e -> a760c0694
PCA - Cleanup Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/97706437 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/97706437 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/97706437 Branch: refs/heads/master Commit: 9770643773f6b5f0fdfe9a3f376af450f1a3b26a Parents: 932c328 Author: Chamila de Alwis <[email protected]> Authored: Mon Dec 21 17:15:07 2015 +0530 Committer: Chamila de Alwis <[email protected]> Committed: Mon Dec 21 17:15:07 2015 +0530 ---------------------------------------------------------------------- .../pom.xml | 38 -- .../cartridge.agent/subscriber.py | 1 - .../python/cartridge.agent/tests/__init__.py | 16 - .../tests/conf/complete_topology_event.json | 1 - .../tests/conf/git/auth_repo.json | 10 - .../tests/conf/git/auth_repo2.json | 10 - .../tests/conf/git/simple_repo.json | 10 - .../python/cartridge.agent/tests/test_events.py | 125 ----- .../python/cartridge.agent/tests/test_git.py | 79 --- .../python/cartridge.agent/tests/test_util.py | 127 ----- .../test/PythonCartridgeAgentTest.java | 529 ------------------- 11 files changed, 946 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/pom.xml ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/pom.xml b/components/org.apache.stratos.python.cartridge.agent/pom.xml index 1ea6aa7..95cd071 100644 --- a/components/org.apache.stratos.python.cartridge.agent/pom.xml +++ b/components/org.apache.stratos.python.cartridge.agent/pom.xml @@ -30,44 +30,6 @@ <packaging>pom</packaging> <name>Apache Stratos - Python Cartridge Agent</name> - <profiles> - <profile> - <id>default</id> - <activation> - <activeByDefault>true</activeByDefault> - </activation> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <configuration> - <excludes> - <exclude>**/*/PythonCartridgeAgentTest.java</exclude> - </excludes> - </configuration> - </plugin> - </plugins> - </build> - </profile> - <profile> - <id>live</id> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <configuration> - <includes> - <include>**/*/PythonCartridgeAgentTest.java</include> - </includes> - </configuration> - </plugin> - </plugins> - </build> - </profile> - </profiles> - <dependencies> <dependency> <groupId>org.wso2.carbon</groupId> http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/subscriber.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/subscriber.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/subscriber.py index 165cd5b..87dceeb 100644 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/subscriber.py +++ b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/subscriber.py @@ -209,4 +209,3 @@ class EventExecutor(threading.Thread): def terminate(self): self.terminate() - http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/__init__.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/__init__.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/__init__.py deleted file mode 100644 index 13a8339..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/__init__.py +++ /dev/null @@ -1,16 +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. http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/complete_topology_event.json ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/complete_topology_event.json b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/complete_topology_event.json deleted file mode 100644 index 14d6d8b..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/complete_topology_event.json +++ /dev/null @@ -1 +0,0 @@ -{"topology":{"serviceMap":{"tomcat":{"serviceName":"tomcat","serviceType":"SingleTenant","clusterIdClusterMap":{"php1.tomcat.domain":{"serviceName":"tomcat","clusterId":"php1.tomcat.domain","autoscalePolicyName":"autoscale_policy_1","hostNames":["php1.isuruh.lk"],"tenantRange":"*","isLbCluster":false,"isKubernetesCluster":false,"memberMap":{"php1.tomcat.domain6d4d09ee-2ec8-4c00-962d-3449305a4dfa":{"serviceName":"tomcat","clusterId":"php1.tomcat.domain","memberId":"php1.tomcat.domain6d4d09ee-2ec8-4c00-962d-3449305a4dfa","clusterInstanceId":"single-cartridge-app-1","networkPartitionId":"openstack_R1","partitionId":"P1","initTime":1422699519228,"portMap":{"8280":{"protocol":"http","value":22,"proxy":8280}},"memberPublicIPs":["192.168.17.200"],"defaultPublicIP":"192.168.17.200","memberPrivateIPs":["10.0.0.59"],"defaultPrivateIP":"10.0.0.59","properties":{"PRIMARY":"false","MIN_COUNT":"1"},"memberStateManager":{"stateStack":["Created","Initialized"],"identifier":"php1.tomcat.domain6d4d09 ee-2ec8-4c00-962d-3449305a4dfa"}}},"appId":"single-cartridge-app","properties":{},"instanceIdToInstanceContextMap":{"single-cartridge-app-1":{"instanceId":"single-cartridge-app-1","instanceProperties":{},"lifeCycleStateManager":{"stateStack":["Created"],"identifier":"php1.tomcat.domain_single-cartridge-app-1"},"networkPartitionId":"openstack_R1"}}}},"portMap":{"8280":{"protocol":"http","value":22,"proxy":8280}},"properties":{}}},"initialized":false}} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo.json ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo.json b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo.json deleted file mode 100644 index b6143a0..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "repoURL": "https://[email protected]/testapache2211/testrepo.git", - "repoUsername" : "testapache2211", - "repoPassword" : "iXSLhimFulqe", - "repoPath" : "/tmp/apachestratos/testauth_repo", - "tenantId" : "0002", - "multitenant" : "False", - "commitEnabled" : "False", - "fallbackHash" : "a71503a2fed5e2c00b050ac69c912a33483949a8" -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo2.json ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo2.json b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo2.json deleted file mode 100644 index fa91094..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/auth_repo2.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "repoURL": "https://bitbucket.org/testapache2211/testrepo.git", - "repoUsername" : "testapache2211", - "repoPassword" : "iXSLhimFulqe", - "repoPath" : "/tmp/apachestratos/testauth_repo2", - "tenantId" : "0003", - "multitenant" : "False", - "commitEnabled" : "False", - "fallbackHash" : "a71503a2fed5e2c00b050ac69c912a33483949a8" -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/simple_repo.json ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/simple_repo.json b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/simple_repo.json deleted file mode 100644 index c88b1aa..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/conf/git/simple_repo.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "repoURL": "https://bitbucket.org/testapache2211/opentestrepo1.git", - "repoUsername" : "testapache2211", - "repoPassword" : "iXSLhimFulqe", - "repoPath" : "/tmp/apachestratos/opentest_repo", - "tenantId" : "0001", - "multitenant" : "False", - "commitEnabled" : "False", - "fallbackHash" : "a1ae3d7d00bf4d5a63b21bcdc82d075e1def4121" -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_events.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_events.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_events.py deleted file mode 100644 index ae4021c..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_events.py +++ /dev/null @@ -1,125 +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. - -from .. cartridgeagent.modules.event.topology.events import CompleteTopologyEvent -import pytest - - -def test_complete_topology_event(): - with open("conf/complete_topology_event.json", "r") as f: - event_json = f.read() - - event_object = CompleteTopologyEvent.create_from_json(event_json) - topology = event_object.get_topology() - - assert not topology.initialized and \ - len(topology.service_map.keys()) == 1 and \ - topology.service_map.keys()[0] == "tomcat", \ - "Topology object not serialized properly" - - service = topology.get_service("tomcat") - - assert service.service_name == "tomcat", \ - "Service object not serialized properly. [serviceName] %s" % service.service_name - assert service.service_type == "SingleTenant", \ - "Service object not serialized properly. [srviceType] %s" % service.service_type - assert len(service.cluster_id_cluster_map.keys()) == 1, \ - "Service object not serialized properly. Count[clusterIdClusterMap] %s" % \ - len(service.cluster_id_cluster_map.keys()) - assert service.cluster_id_cluster_map.keys()[0] == "php1.tomcat.domain", \ - "Service object not serialized properly. [clusterIdClusterMap] %s" % service.cluster_id_cluster_map.keys()[0] - assert len(service.port_map.keys()) == 1, \ - "Service object not serialized properly. Count[portMap] %s" % len(service.port_map.keys()) - assert service.port_map.keys()[0] == "8280", \ - "Service object not serialized properly. [portMap] %s" % service.port_map.keys()[0] - assert len(service.properties.keys()) == 0, \ - "Service object not serialized properly. Count[properties] %s" % len(service.properties.keys()) - - cluster = service.get_cluster("php1.tomcat.domain") - - assert cluster.service_name == "tomcat", \ - "Cluster object not serialized properly. [serviceName] %s" % cluster.service_name - assert cluster.cluster_id == "php1.tomcat.domain", \ - "Cluster object not serialized properly. [clusterId] %s" % cluster.cluster_id - assert cluster.deployment_policy_name is None, \ - "Cluster object not serialized properly. [deploymentPolicyName] %s" % cluster.deployment_policy_name - assert cluster.autoscale_policy_name == "autoscale_policy_1", \ - "Cluster object not serialized properly. [autoscalePolicyName] %s" % cluster.autoscale_policy_name - assert len(cluster.hostnames) == 1, \ - "Cluster object not serialized properly. Count[hostNames] %s" % len(cluster.hostnames) - assert cluster.tenant_range == "*", \ - "Cluster object not serialized properly. [tenantRange] %s" % cluster.tenant_range - assert not cluster.is_lb_cluster, \ - "Cluster object not serialized properly. [isLbCluster] %s" % cluster.is_lb_cluster - assert not cluster.is_kubernetes_cluster, \ - "Cluster object not serialized properly. [isKubernetesCluster] %s" % cluster.is_kubernetes_cluster - assert cluster.load_balancer_algorithm_name is None, \ - "Cluster object not serialized properly. [loadBalancerAlogrithmName] %s" % cluster.load_balancer_algorithm_name - assert cluster.app_id == "single-cartridge-app", \ - "Cluster object not serialized properly. [appId] %s" % cluster.app_id - assert len(cluster.properties.keys()) == 0, \ - "Cluster object not serialized properly. Count[properties] %s" % len(cluster.properties.keys()) - assert len(cluster.member_map.keys()) == 1, \ - "Cluster object not serialized properly. Count[memberMap] %s" % len(cluster.member_map.keys()) - assert cluster.member_exists("php1.tomcat.domain6d4d09ee-2ec8-4c00-962d-3449305a4dfa"), \ - "Cluster object not serialized properly. Targeted member not found." - - # assert cluster.tenant_id_in_range("222"), "Checking tenant id in range" - - try: - cluster.validate_tenant_range("*") - except RuntimeError: - pytest.fail("Validating tenant range logic failed. [input] *") - - try: - cluster.validate_tenant_range("22-45") - except RuntimeError: - pytest.fail("Validating tenant range logic failed. [input] 22-45") - - with pytest.raises(RuntimeError): - cluster.validate_tenant_range("rf-56") - - member = cluster.get_member("php1.tomcat.domain6d4d09ee-2ec8-4c00-962d-3449305a4dfa") - - assert member.service_name == "tomcat",\ - "Member object not serialized properly. [serviceName] %s " % member.service_name - assert member.cluster_id == "php1.tomcat.domain",\ - "Member object not serialized properly. [clusterId] %s " % member.cluster_id - assert member.member_id == "php1.tomcat.domain6d4d09ee-2ec8-4c00-962d-3449305a4dfa",\ - "Member object not serialized properly. [memberId] %s " % member.member_id - assert member.cluster_instance_id == "single-cartridge-app-1",\ - "Member object not serialized properly. [clusterInstanceId] %s " % member.cluster_instance_id - assert member.network_partition_id == "openstack_R1",\ - "Member object not serialized properly. [networkPartitionId] %s " % member.network_partition_id - assert member.partition_id == "P1",\ - "Member object not serialized properly. [partitionId] %s " % member.partition_id - assert member.init_time == 1422699519228,\ - "Member object not serialized properly. [initTime] %s " % member.init_time - assert member.member_public_ips[0] == "192.168.17.200",\ - "Member object not serialized properly. [memberPublicIps] %s " % member.member_public_ips[0] - assert member.member_default_public_ip == member.member_public_ips[0],\ - "Member object not serialized properly. [defaultPublicIp] %s " % member.member_default_public_ip - assert member.member_private_ips[0] == "10.0.0.59",\ - "Member object not serialized properly. [memberPrivateIps] %s " % member.member_private_ips[0] - assert member.member_default_private_ip == member.member_private_ips[0],\ - "Member object not serialized properly. [defaultPrivateIp] %s " % member.member_default_private_ip - assert member.properties["MIN_COUNT"] == "1",\ - "Member object not serialized properly. [properties] %s " % member.properties["MIN_COUNT"] - assert member.status == "Initialized",\ - "Member object not serialized properly. [status] %s " % member.status - assert member.lb_cluster_id is None,\ - "Member object not serialized properly. [lbClusterId] %s " % member.lb_cluster_id \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_git.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_git.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_git.py deleted file mode 100644 index c1074aa..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_git.py +++ /dev/null @@ -1,79 +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. - -import pytest -import json -import shutil -import os -from .. cartridgeagent.modules.artifactmgt.repository import Repository -from .. cartridgeagent.modules.artifactmgt.git.agentgithandler import * - - [email protected]("input, expected", [ - ("simple_repo.json", True), - ("auth_repo.json", True), - ("auth_repo2.json", True), -]) -def test_clone(input, expected): - with open("conf/git/" + input, "r") as f: - repo_string = f.read() - - repo_info = json.loads(repo_string, object_hook=repo_object_decoder) - sub_run, repo_context = AgentGitHandler.run_checkout_job(repo_info) - - assert sub_run, "Not detected as subscription run" - - result, msg = verify_git_repo(repo_info) - assert result == expected, msg - - -def setup_module(module): - # clear the temp folder path to clone new folders - try: - shutil.rmtree("/tmp/apachestratos") - except: - pass - - try: - os.makedirs("/tmp/apachestratos") - except: - pass - - -def verify_git_repo(repo_info): - """ - Assert the status of the git repository - :param repo_info: - :return: - """ - if not os.path.isdir(repo_info.repo_path): - return False, "Local repository directory not created." - - output, errors = AgentGitHandler.execute_git_command(["status"], repo_info.repo_path) - if not (len(errors) == 0 and "nothing to commit, working directory clean" in output): - return False, "Git clone failed. " - - return True, None - - -def repo_object_decoder(obj): - """ Repository object decoder from JSON - :param obj: json object - :return: - """ - return Repository(str(obj["repoURL"]), str(obj["repoUsername"]), str(obj["repoPassword"]), str(obj["repoPath"]), - str(obj["tenantId"]), str(obj["multitenant"]), str(obj["commitEnabled"])) http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_util.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_util.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_util.py deleted file mode 100644 index ff6a6fe..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/tests/test_util.py +++ /dev/null @@ -1,127 +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. - -from ..cartridgeagent.modules.util.asyncscheduledtask import * -from ..cartridgeagent.modules.util import cartridgeagentutils -import time -import socket -from threading import Thread - -# ASYNC_WRITE_FILE = "asynctest.txt" - - -# def test_async_task(): -# with open(ASYNC_WRITE_FILE, "r") as f: -# init_context = f.read() -# -# test_task = TestTask() -# astask = ScheduledExecutor(1, test_task) -# start_time = time.time() * 1000 -# astask.start() -# contents_changed = False -# timeout = 10 #seconds -# -# # wait till file content is written -# while not contents_changed and (time.time() * 1000 - start_time) < (10 * 1000): -# time.sleep(2) -# with open(ASYNC_WRITE_FILE, "r") as f: -# now_content = f.read() -# -# if init_context != now_content: -# contents_changed = True -# -# astask.terminate() -# f = open(ASYNC_WRITE_FILE, "r") -# end_time = float(f.read()) -# assert (end_time - start_time) >= 1 * 1000, "Task was executed before specified delay" -# -# -# class TestTask(AbstractAsyncScheduledTask): -# -# def execute_task(self): -# with open(ASYNC_WRITE_FILE, "w") as f: -# f.seek(0) -# f.truncate() -# f.write("%1.4f" % (time.time()*1000)) - - -def test_decrypt_password_success(): - plain_password = "plaintext" - secret_key = "tvnw63ufg9gh5111" - encrypted_password= "jP1lZ5xMlpLzu8MbY2Porg==" - - decrypted_password = cartridgeagentutils.decrypt_password(encrypted_password, secret_key) - #print decrypted_password - - assert decrypted_password == plain_password, "Password decryption failed" - - -def test_decrypt_password_failure(): - plain_password = "plaintext" - secret_key = "notsecretkeyhere" - encrypted_password= "jP1lZ5xMlpLzu8MbY2Porg==" - assert cartridgeagentutils.decrypt_password(encrypted_password, secret_key) != plain_password, "Password decrypted for wrong key" -# -# -# def test_create_dir_normal(): -# assert True -# -# -# def test_create_dir_system_path(): -# assert True -# -# -# def test_create_dir_existing_dir(): -# assert True - - -def test_wait_for_ports_activity_normal(): - portnumber = 12345 - listener = PortListener(portnumber) - listener.start() - - assert cartridgeagentutils.check_ports_active(socket.gethostbyname(socket.gethostname()), [str(portnumber)]) - - -def test_wait_for_ports_activity_non_existent(): - assert cartridgeagentutils.check_ports_active(socket.gethostbyname(socket.gethostname()), [str(34565)]) == False - - -class PortListener(Thread): - - def __init__(self, portnumber): - Thread.__init__(self) - self.portnumber = portnumber - self.terminated = False - - def run(self): - s = socket.socket() - host = socket.gethostname() - - s.bind((host, self.portnumber)) - s.listen(5) - - # while not self.terminated: - c, addr = s.accept() # Establish connection with client. - # print 'Got connection from', addr - c.send('Thank you for connecting') - c.close() - - s.close() - - def terminate(self): - self.terminated = True \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/97706437/components/org.apache.stratos.python.cartridge.agent/src/test/java/org/apache/stratos/python.cartridge.agent/test/PythonCartridgeAgentTest.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/test/java/org/apache/stratos/python.cartridge.agent/test/PythonCartridgeAgentTest.java b/components/org.apache.stratos.python.cartridge.agent/src/test/java/org/apache/stratos/python.cartridge.agent/test/PythonCartridgeAgentTest.java deleted file mode 100644 index 620e11b..0000000 --- a/components/org.apache.stratos.python.cartridge.agent/src/test/java/org/apache/stratos/python.cartridge.agent/test/PythonCartridgeAgentTest.java +++ /dev/null @@ -1,529 +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. - */ - -package org.apache.stratos.python.cartridge.agent.test; - -import org.apache.activemq.broker.BrokerService; -import org.apache.commons.exec.*; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.output.ByteArrayOutputStream; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.common.domain.LoadBalancingIPType; -import org.apache.stratos.common.threading.StratosThreadPool; -import org.apache.stratos.messaging.broker.publish.EventPublisher; -import org.apache.stratos.messaging.broker.publish.EventPublisherPool; -import org.apache.stratos.messaging.domain.topology.*; -import org.apache.stratos.messaging.event.Event; -import org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent; -import org.apache.stratos.messaging.event.topology.CompleteTopologyEvent; -import org.apache.stratos.messaging.event.topology.MemberInitializedEvent; -import org.apache.stratos.messaging.listener.instance.status.InstanceActivatedEventListener; -import org.apache.stratos.messaging.listener.instance.status.InstanceStartedEventListener; -import org.apache.stratos.messaging.message.receiver.instance.status.InstanceStatusEventReceiver; -import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; -import org.apache.stratos.messaging.util.MessagingUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.io.File; -import java.io.IOException; -import java.net.ServerSocket; -import java.util.*; -import java.util.concurrent.ExecutorService; - -import static junit.framework.Assert.assertTrue; - -@RunWith(Parameterized.class) -public class PythonCartridgeAgentTest { - - private static final Log log = LogFactory.getLog(PythonCartridgeAgentTest.class); - - private static final String NEW_LINE = System.getProperty("line.separator"); - // private static final long TIMEOUT = 1440000; - private static final long TIMEOUT = 120000; - private static final String CLUSTER_ID = "php.php.domain"; - private static final String DEPLOYMENT_POLICY_NAME = "deployment-policy-1"; - private static final String AUTOSCALING_POLICY_NAME = "autoscaling-policy-1"; - private static final String APP_ID = "application-1"; - private static final String MEMBER_ID = "php.member-1"; - private static final String CLUSTER_INSTANCE_ID = "cluster-1-instance-1"; - private static final String NETWORK_PARTITION_ID = "network-partition-1"; - private static final String PARTITION_ID = "partition-1"; - private static final String TENANT_ID = "-1234"; - private static final String SERVICE_NAME = "php"; - public static final String SOURCE_PATH = "/tmp/stratos-pca-test-app-path/"; - - private static List<ServerSocket> serverSocketList; - private static Map<String, Executor> executorList; - private final ArtifactUpdatedEvent artifactUpdatedEvent; - private final Boolean expectedResult; - private boolean instanceStarted; - private boolean instanceActivated; - private ByteArrayOutputStreamLocal outputStream; - private boolean eventReceiverInitiated = false; - private TopologyEventReceiver topologyEventReceiver; - private InstanceStatusEventReceiver instanceStatusEventReceiver; - private int cepPort = 7712; - private BrokerService broker = new BrokerService(); - private static final String ACTIVEMQ_AMQP_BIND_ADDRESS = "tcp://localhost:61617"; - private static final String ACTIVEMQ_MQTT_BIND_ADDRESS = "mqtt://localhost:1884"; - private static final UUID PYTHON_AGENT_DIR_NAME = UUID.randomUUID(); - - public PythonCartridgeAgentTest(ArtifactUpdatedEvent artifactUpdatedEvent, Boolean expectedResult) { - this.artifactUpdatedEvent = artifactUpdatedEvent; - this.expectedResult = expectedResult; - } - - /** - * Setup method for test class - */ - @BeforeClass - public static void oneTimeSetUp() { - // Set jndi.properties.dir system property for initializing event publishers and receivers - System.setProperty("jndi.properties.dir", getResourcesFolderPath()); - } - - /** - * Setup method for test method testPythonCartridgeAgent - */ - @Before - public void setup() { - serverSocketList = new ArrayList<ServerSocket>(); - executorList = new HashMap<String, Executor>(); - try { - broker.addConnector(ACTIVEMQ_AMQP_BIND_ADDRESS); - broker.addConnector(ACTIVEMQ_MQTT_BIND_ADDRESS); - broker.setBrokerName("testBroker"); - broker.setDataDirectory(PythonCartridgeAgentTest.class.getResource("/").getPath() + - File.separator + ".." + File.separator + PYTHON_AGENT_DIR_NAME + File.separator + "activemq-data"); - broker.start(); - log.info("Broker service started!"); - } - catch (Exception e) { - log.error("Error while setting up broker service", e); - } - if (!this.eventReceiverInitiated) { - ExecutorService executorService = StratosThreadPool.getExecutorService("TEST_THREAD_POOL", 15); - topologyEventReceiver = new TopologyEventReceiver(); - topologyEventReceiver.setExecutorService(executorService); - topologyEventReceiver.execute(); - - instanceStatusEventReceiver = new InstanceStatusEventReceiver(); - instanceStatusEventReceiver.setExecutorService(executorService); - instanceStatusEventReceiver.execute(); - - this.instanceStarted = false; - instanceStatusEventReceiver.addEventListener(new InstanceStartedEventListener() { - @Override - protected void onEvent(Event event) { - log.info("Instance started event received"); - instanceStarted = true; - } - }); - - this.instanceActivated = false; - instanceStatusEventReceiver.addEventListener(new InstanceActivatedEventListener() { - @Override - protected void onEvent(Event event) { - log.info("Instance activated event received"); - instanceActivated = true; - } - }); - - this.eventReceiverInitiated = true; - } - // Simulate CEP server socket - startServerSocket(cepPort); - String agentPath = setupPythonAgent(); - log.info("Python agent working directory name: " + PYTHON_AGENT_DIR_NAME); - log.info("Starting python cartridge agent..."); - this.outputStream = executeCommand( - "python " + agentPath + "/agent.py > " + getResourcesFolderPath() + File.separator + ".." + - File.separator + PYTHON_AGENT_DIR_NAME + File.separator + "cartridge-agent.log"); - } - - /** - * TearDown method for test method testPythonCartridgeAgent - */ - @After - public void tearDown() { - for (Map.Entry<String, Executor> entry : executorList.entrySet()) { - try { - String commandText = entry.getKey(); - Executor executor = entry.getValue(); - ExecuteWatchdog watchdog = executor.getWatchdog(); - if (watchdog != null) { - log.info("Terminating process: " + commandText); - watchdog.destroyProcess(); - } - } - catch (Exception ignore) { - } - } - for (ServerSocket serverSocket : serverSocketList) { - try { - log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress()); - serverSocket.close(); - } - catch (IOException ignore) { - } - } - - try { - log.info("Deleting source checkout folder..."); - FileUtils.deleteDirectory(new File(SOURCE_PATH)); - } - catch (Exception ignore) { - - } - - this.instanceStatusEventReceiver.terminate(); - this.topologyEventReceiver.terminate(); - - this.instanceActivated = false; - this.instanceStarted = false; - try { - broker.stop(); - } - catch (Exception e) { - log.error("Error while stopping the broker service", e); - } - } - - - /** - * This method returns a collection of {@link org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent} - * objects as parameters to the test - * - * @return - */ - @Parameterized.Parameters - public static Collection getArtifactUpdatedEventsAsParams() { - ArtifactUpdatedEvent publicRepoEvent = createTestArtifactUpdatedEvent(); - - ArtifactUpdatedEvent privateRepoEvent = createTestArtifactUpdatedEvent(); - privateRepoEvent.setRepoURL("https://bitbucket.org/testapache2211/testrepo.git"); - privateRepoEvent.setRepoUserName("testapache2211"); - privateRepoEvent.setRepoPassword("RExPDGa4GkPJj4kJDzSROQ=="); - - ArtifactUpdatedEvent privateRepoEvent2 = createTestArtifactUpdatedEvent(); - privateRepoEvent2.setRepoURL("https://[email protected]/testapache2211/testrepo.git"); - privateRepoEvent2.setRepoUserName("testapache2211"); - privateRepoEvent2.setRepoPassword("iF7qT+BKKPE3PGV1TeDsJA=="); - - return Arrays.asList(new Object[][]{ - {publicRepoEvent, true}, - {privateRepoEvent, true}, - {privateRepoEvent2, true} - }); - -// return Arrays.asList(new Object[][]{ -// {publicRepoEvent, true} -// }); - - } - - /** - * Creates an {@link org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent} object with a public - * repository URL - * - * @return - */ - private static ArtifactUpdatedEvent createTestArtifactUpdatedEvent() { - ArtifactUpdatedEvent publicRepoEvent = new ArtifactUpdatedEvent(); - publicRepoEvent.setClusterId(CLUSTER_ID); - publicRepoEvent.setTenantId(TENANT_ID); - publicRepoEvent.setRepoURL("https://bitbucket.org/testapache2211/opentestrepo1.git"); - return publicRepoEvent; - } - - @Test(timeout = TIMEOUT) - public void testPythonCartridgeAgent() { - Thread communicatorThread = new Thread(new Runnable() { - @Override - public void run() { - List<String> outputLines = new ArrayList<String>(); - while (!outputStream.isClosed()) { - List<String> newLines = getNewLines(outputLines, outputStream.toString()); - if (newLines.size() > 0) { - for (String line : newLines) { - if (line.contains("Subscribed to 'topology/#'")) { - sleep(1000); - // Send complete topology event - log.info("Publishing complete topology event..."); - Topology topology = createTestTopology(); - CompleteTopologyEvent completeTopologyEvent = new CompleteTopologyEvent(topology); - publishEvent(completeTopologyEvent); - log.info("Complete topology event published"); - - sleep(3000); - // Publish member initialized event - log.info("Publishing member initialized event..."); - MemberInitializedEvent memberInitializedEvent = new MemberInitializedEvent( - SERVICE_NAME, CLUSTER_ID, CLUSTER_INSTANCE_ID, MEMBER_ID, NETWORK_PARTITION_ID, - PARTITION_ID - ); - publishEvent(memberInitializedEvent); - log.info("Member initialized event published"); - - // Simulate server socket - startServerSocket(8080); - } - if (line.contains("Artifact repository found")) { - // Send artifact updated event - publishEvent(artifactUpdatedEvent); - } - - if (line.contains("Exception in thread") || line.contains("ERROR")) { - //throw new RuntimeException(line); - } - log.info(line); - } - } - sleep(100); - } - } - }); - - communicatorThread.start(); - - while (!instanceActivated) { - // wait until the instance activated event is received. - sleep(2000); - } - - assertTrue("Instance started event was not received", instanceStarted); - assertTrue("Instance activated event was not received", instanceActivated == this.expectedResult); - } - - /** - * Publish messaging event - * - * @param event - */ - private void publishEvent(Event event) { - String topicName = MessagingUtil.getMessageTopicName(event); - EventPublisher eventPublisher = EventPublisherPool.getPublisher(topicName); - eventPublisher.publish(event); - } - - /** - * Start server socket - * - * @param port - */ - private void startServerSocket(final int port) { - Thread socketThread = new Thread(new Runnable() { - @Override - public void run() { - try { - ServerSocket serverSocket = new ServerSocket(port); - serverSocketList.add(serverSocket); - log.info("Server socket started on port: " + port); - serverSocket.accept(); - } - catch (IOException e) { - String message = "Could not start server socket: [port] " + port; - log.error(message, e); - throw new RuntimeException(message, e); - } - } - }); - socketThread.start(); - } - - /** - * Create test topology - * - * @return - */ - private Topology createTestTopology() { - Topology topology = new Topology(); - Service service = new Service(SERVICE_NAME, ServiceType.SingleTenant); - topology.addService(service); - - Cluster cluster = new Cluster(service.getServiceName(), CLUSTER_ID, DEPLOYMENT_POLICY_NAME, - AUTOSCALING_POLICY_NAME, APP_ID); - service.addCluster(cluster); - - Member member = new Member(service.getServiceName(), cluster.getClusterId(), MEMBER_ID, - CLUSTER_INSTANCE_ID, NETWORK_PARTITION_ID, PARTITION_ID, LoadBalancingIPType.Private, - System.currentTimeMillis()); - - member.setDefaultPrivateIP("10.0.0.1"); - member.setDefaultPublicIP("20.0.0.1"); - Properties properties = new Properties(); - properties.setProperty("prop1", "value1"); - member.setProperties(properties); - member.setStatus(MemberStatus.Created); - cluster.addMember(member); - - return topology; - } - - /** - * Return new lines found in the output - * - * @param currentOutputLines current output lines - * @param output output - * @return - */ - private List<String> getNewLines(List<String> currentOutputLines, String output) { - List<String> newLines = new ArrayList<String>(); - - if (StringUtils.isNotBlank(output)) { - String[] lines = output.split(NEW_LINE); - if (lines != null) { - for (String line : lines) { - if (!currentOutputLines.contains(line)) { - currentOutputLines.add(line); - newLines.add(line); - } - } - } - } - return newLines; - } - - /** - * Sleep current thread - * - * @param time - */ - private void sleep(long time) { - try { - Thread.sleep(time); - } - catch (InterruptedException ignore) { - } - } - - /** - * Copy python agent distribution to a new folder, extract it and copy sample configuration files - * - * @return - */ - private String setupPythonAgent() { - try { - log.info("Setting up python cartridge agent..."); - String srcAgentPath = getResourcesFolderPath() + "/../../src/main/python/cartridge.agent/cartridge.agent"; - String destAgentPath = - getResourcesFolderPath() + File.separator + ".." + File.separator + PYTHON_AGENT_DIR_NAME + - "/cartridge.agent"; - FileUtils.copyDirectory(new File(srcAgentPath), new File(destAgentPath)); - - String srcAgentConfPath = getResourcesFolderPath() + "/agent.conf"; - String destAgentConfPath = destAgentPath + "/agent.conf"; - FileUtils.copyFile(new File(srcAgentConfPath), new File(destAgentConfPath)); - - String srcLoggingIniPath = getResourcesFolderPath() + "/logging.ini"; - String destLoggingIniPath = destAgentPath + "/logging.ini"; - FileUtils.copyFile(new File(srcLoggingIniPath), new File(destLoggingIniPath)); - - String srcPayloadPath = getResourcesFolderPath() + "/payload"; - String destPayloadPath = destAgentPath + "/payload"; - FileUtils.copyDirectory(new File(srcPayloadPath), new File(destPayloadPath)); - - log.info("Changing extension scripts permissions"); - File extensionsPath = new File(destAgentPath + "/extensions/bash"); - File[] extensions = extensionsPath.listFiles(); - for (File extension : extensions) { - extension.setExecutable(true); - } - - log.info("Python cartridge agent setup completed"); - - return destAgentPath; - } - catch (Exception e) { - String message = "Could not copy cartridge agent distribution"; - log.error(message, e); - throw new RuntimeException(message, e); - } - } - - /** - * Execute shell command - * - * @param commandText - */ - private ByteArrayOutputStreamLocal executeCommand(final String commandText) { - final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); - try { - CommandLine commandline = CommandLine.parse(commandText); - DefaultExecutor exec = new DefaultExecutor(); - PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); - exec.setWorkingDirectory(new File( - getResourcesFolderPath() + File.separator + ".." + File.separator + PYTHON_AGENT_DIR_NAME)); - exec.setStreamHandler(streamHandler); - ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); - exec.setWatchdog(watchdog); - exec.execute(commandline, new ExecuteResultHandler() { - @Override - public void onProcessComplete(int i) { - log.info(commandText + " process completed"); - } - - @Override - public void onProcessFailed(ExecuteException e) { - log.error(commandText + " process failed", e); - } - }); - executorList.put(commandText, exec); - return outputStream; - } - catch (Exception e) { - log.error(outputStream.toString(), e); - throw new RuntimeException(e); - } - } - - /** - * Get resources folder path - * - * @return - */ - private static String getResourcesFolderPath() { - String path = PythonCartridgeAgentTest.class.getResource(File.separator).getPath(); - return StringUtils.removeEnd(path, File.separator); - } - - /** - * Implements ByteArrayOutputStream.isClosed() method - */ - private class ByteArrayOutputStreamLocal extends ByteArrayOutputStream { - private boolean closed; - - @Override - public void close() throws IOException { - super.close(); - closed = true; - } - - public boolean isClosed() { - return closed; - } - } -}
