diff options
Diffstat (limited to 'azure/aria/aria-extension-cloudify/src/aria/tests/end2end')
4 files changed, 219 insertions, 0 deletions
diff --git a/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/__init__.py b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/__init__.py new file mode 100644 index 0000000..ae1e83e --- /dev/null +++ b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/__init__.py @@ -0,0 +1,14 @@ +# 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. diff --git a/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_hello_world.py b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_hello_world.py new file mode 100644 index 0000000..094ffc3 --- /dev/null +++ b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_hello_world.py @@ -0,0 +1,61 @@ +# 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 requests + +from .testenv import testenv # pylint: disable=unused-import +from .. import helpers + + +def test_hello_world(testenv): + hello_world_template_uri = helpers.get_example_uri('hello-world', 'hello-world.yaml') + service_name = testenv.install_service(hello_world_template_uri) + + try: + _verify_deployed_service_in_storage(service_name, testenv.model_storage) + _verify_webserver_up('http://localhost:9090') + finally: + # Even if some assertions failed, attempt to execute uninstall so the + # webserver process doesn't stay up once the test is finished + testenv.uninstall_service() + + _verify_webserver_down('http://localhost:9090') + testenv.verify_clean_storage() + + +def _verify_webserver_up(http_endpoint): + server_response = requests.get(http_endpoint, timeout=10) + assert server_response.status_code == 200 + + +def _verify_webserver_down(http_endpoint): + try: + requests.get(http_endpoint, timeout=10) + assert False + except requests.exceptions.ConnectionError: + pass + + +def _verify_deployed_service_in_storage(service_name, model_storage): + service_templates = model_storage.service_template.list() + assert len(service_templates) == 1 + assert len(service_templates[0].services) == 1 + service = service_templates[0].services[service_name] + assert service.name == service_name + assert len(service.executions) == 1 + assert len(service.nodes) == 2 + assert service.outputs['port'].value == 9090 + assert all(node.state == node.STARTED for node in service.nodes.itervalues()) + assert len(service.executions[0].logs) > 0 diff --git a/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_nodecellar.py b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_nodecellar.py new file mode 100644 index 0000000..e8cfa84 --- /dev/null +++ b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/test_nodecellar.py @@ -0,0 +1,42 @@ +# 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 .testenv import testenv # pylint: disable=unused-import +from .. import helpers + + +def test_nodecellar(testenv): + nodecellar_template_uri = helpers.get_service_template_uri( + 'tosca-simple-1.0', 'node-cellar', 'node-cellar.yaml') + + service_name = testenv.install_service(nodecellar_template_uri, dry=True) + _verify_deployed_service_in_storage(service_name, testenv.model_storage) + + # testing dry execution of custom workflows + testenv.execute_workflow(service_name, 'maintenance_on', dry=True) + testenv.execute_workflow(service_name, 'maintenance_off', dry=True) + + testenv.uninstall_service(dry=True) + testenv.verify_clean_storage() + + +def _verify_deployed_service_in_storage(service_name, model_storage): + service_templates = model_storage.service_template.list() + assert len(service_templates) == 1 + assert len(service_templates[0].services) == 1 + service = service_templates[0].services[service_name] + assert service.name == service_name + assert len(service.executions) == 0 # dry executions leave no traces + assert len(service.nodes) == 15 diff --git a/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/testenv.py b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/testenv.py new file mode 100644 index 0000000..43ec274 --- /dev/null +++ b/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/testenv.py @@ -0,0 +1,102 @@ +# 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 sys + +import pytest +import sh + + +@pytest.fixture +def testenv(tmpdir, request, monkeypatch): + test_name = request.node.name + workdir = str(tmpdir) + + # Setting the workdir environment variable for the CLI + monkeypatch.setenv('ARIA_WORKDIR', workdir) + return TestEnvironment(workdir, test_name) + + +class TestEnvironment(object): + + def __init__(self, workdir, test_name): + self.workdir = workdir + self.test_name = test_name + + self.cli = self._get_cli() + env = self._get_aria_env() + self.model_storage = env.model_storage + self.resource_storage = env.resource_storage + self.plugin_manager = env.plugin_manager + + def install_service(self, service_template_path, dry=False, service_template_name=None, + service_name=None): + service_template_name = service_template_name or self.test_name + service_name = service_name or self.test_name + + self.cli.service_templates.store(service_template_path, service_template_name) + self.cli.services.create(service_name, service_template_name=service_template_name) + self.execute_workflow(service_name, 'install', dry=dry) + return service_name + + def uninstall_service(self, service_name=None, service_template_name=None, dry=False, + force_service_delete=False): + service_name = service_name or self.test_name + self.execute_workflow(service_name, 'uninstall', dry=dry) + self.cli.services.delete(service_name, force=force_service_delete) + self.cli.service_templates.delete(service_template_name or self.test_name) + + def execute_workflow(self, service_name, workflow_name, dry=False): + self.cli.executions.start(workflow_name, service_name=service_name, dry=dry) + + def verify_clean_storage(self): + assert len(self.model_storage.service_template.list()) == 0 + assert len(self.model_storage.service.list()) == 0 + assert len(self.model_storage.execution.list()) == 0 + assert len(self.model_storage.node_template.list()) == 0 + assert len(self.model_storage.node.list()) == 0 + assert len(self.model_storage.log.list()) == 0 + + def _get_cli(self): + cli = sh.aria.bake('-vvv', _out=sys.stdout, _err=sys.stderr) + + class PatchedCli(object): + """ + The ``sh`` library supports underscore-dash auto-replacement for commands and option + flags yet not for subcommands (e.g. ``aria service-templates``). This class fixes this. + """ + def __getattr__(self, attr): + if '_' in attr: + return cli.bake(attr.replace('_', '-')) + return getattr(cli, attr) + + def __call__(self, *args, **kwargs): + """ + This is to support the ``aria`` command itself (e.g. ``aria --version`` calls). + """ + return cli(*args, **kwargs) + + return PatchedCli() + + def _get_aria_env(self): + """ + A somewhat hacky but most simple way of acquiring environment context such as the model + storage, resource storage, etc. Note that the ``ARIA_WORKDIR`` environment variable must be + exported before the import below is used, as the import itself will initialize the ``.aria`` + directory. + """ + from aria.cli import env as cli_env + reload(cli_env) # reloading the module in-between tests + return cli_env.env |