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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 def testenv(tmpdir, request, monkeypatch):
24 test_name = request.node.name
27 # Setting the workdir environment variable for the CLI
28 monkeypatch.setenv('ARIA_WORKDIR', workdir)
29 return TestEnvironment(workdir, test_name)
32 class TestEnvironment(object):
34 def __init__(self, workdir, test_name):
35 self.workdir = workdir
36 self.test_name = test_name
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
44 def install_service(self, service_template_path, dry=False, service_template_name=None,
46 service_template_name = service_template_name or self.test_name
47 service_name = service_name or self.test_name
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)
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)
61 def execute_workflow(self, service_name, workflow_name, dry=False):
62 self.cli.executions.start(workflow_name, service_name=service_name, dry=dry)
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
73 cli = sh.aria.bake('-vvv', _out=sys.stdout, _err=sys.stderr)
75 class PatchedCli(object):
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.
80 def __getattr__(self, attr):
82 return cli.bake(attr.replace('_', '-'))
83 return getattr(cli, attr)
85 def __call__(self, *args, **kwargs):
87 This is to support the ``aria`` command itself (e.g. ``aria --version`` calls).
89 return cli(*args, **kwargs)
93 def _get_aria_env(self):
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``
100 from aria.cli import env as cli_env
101 reload(cli_env) # reloading the module in-between tests