vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / end2end / testenv.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import sys
17
18 import pytest
19 import sh
20
21
22 @pytest.fixture
23 def testenv(tmpdir, request, monkeypatch):
24     test_name = request.node.name
25     workdir = str(tmpdir)
26
27     # Setting the workdir environment variable for the CLI
28     monkeypatch.setenv('ARIA_WORKDIR', workdir)
29     return TestEnvironment(workdir, test_name)
30
31
32 class TestEnvironment(object):
33
34     def __init__(self, workdir, test_name):
35         self.workdir = workdir
36         self.test_name = test_name
37
38         self.cli = self._get_cli()
39         env = self._get_aria_env()
40         self.model_storage = env.model_storage
41         self.resource_storage = env.resource_storage
42         self.plugin_manager = env.plugin_manager
43
44     def install_service(self, service_template_path, dry=False, service_template_name=None,
45                         service_name=None):
46         service_template_name = service_template_name or self.test_name
47         service_name = service_name or self.test_name
48
49         self.cli.service_templates.store(service_template_path, service_template_name)
50         self.cli.services.create(service_name, service_template_name=service_template_name)
51         self.execute_workflow(service_name, 'install', dry=dry)
52         return service_name
53
54     def uninstall_service(self, service_name=None, service_template_name=None, dry=False,
55                           force_service_delete=False):
56         service_name = service_name or self.test_name
57         self.execute_workflow(service_name, 'uninstall', dry=dry)
58         self.cli.services.delete(service_name, force=force_service_delete)
59         self.cli.service_templates.delete(service_template_name or self.test_name)
60
61     def execute_workflow(self, service_name, workflow_name, dry=False):
62         self.cli.executions.start(workflow_name, service_name=service_name, dry=dry)
63
64     def verify_clean_storage(self):
65         assert len(self.model_storage.service_template.list()) == 0
66         assert len(self.model_storage.service.list()) == 0
67         assert len(self.model_storage.execution.list()) == 0
68         assert len(self.model_storage.node_template.list()) == 0
69         assert len(self.model_storage.node.list()) == 0
70         assert len(self.model_storage.log.list()) == 0
71
72     def _get_cli(self):
73         cli = sh.aria.bake('-vvv', _out=sys.stdout, _err=sys.stderr)
74
75         class PatchedCli(object):
76             """
77             The ``sh`` library supports underscore-dash auto-replacement for commands and option
78             flags yet not for subcommands (e.g. ``aria service-templates``). This class fixes this.
79             """
80             def __getattr__(self, attr):
81                 if '_' in attr:
82                     return cli.bake(attr.replace('_', '-'))
83                 return getattr(cli, attr)
84
85             def __call__(self, *args, **kwargs):
86                 """
87                 This is to support the ``aria`` command itself (e.g. ``aria --version`` calls).
88                 """
89                 return cli(*args, **kwargs)
90
91         return PatchedCli()
92
93     def _get_aria_env(self):
94         """
95         A somewhat hacky but most simple way of acquiring environment context such as the model
96         storage, resource storage, etc. Note that the ``ARIA_WORKDIR`` environment variable must be
97         exported before the import below is used, as the import itself will initialize the ``.aria``
98         directory.
99         """
100         from aria.cli import env as cli_env
101         reload(cli_env)  # reloading the module in-between tests
102         return cli_env.env