summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/tests/end2end/testenv.py
blob: 43ec274a85fcff94b9522e065850d6b2f56d722a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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