|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') 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 os |
|
17
|
|
|
|
|
18
|
|
|
from st2common.content.loader import ContentPackLoader |
|
19
|
|
|
from st2common.content.loader import MetaLoader |
|
20
|
|
|
from st2common.constants.pack import MANIFEST_FILE_NAME |
|
21
|
|
|
from st2common.util.pack import get_pack_ref_from_metadata |
|
22
|
|
|
from st2common.exceptions.content import ParseException |
|
23
|
|
|
from st2common.bootstrap.aliasesregistrar import AliasesRegistrar |
|
24
|
|
|
from st2common.models.utils.action_alias_utils import extract_parameters_for_action_alias_db |
|
25
|
|
|
from st2common.models.utils.action_alias_utils import extract_parameters |
|
26
|
|
|
from st2tests.pack_resource import BasePackResourceTestCase |
|
27
|
|
|
|
|
28
|
|
|
__all__ = [ |
|
29
|
|
|
'BaseActionAliasTestCase' |
|
30
|
|
|
] |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class BaseActionAliasTestCase(BasePackResourceTestCase): |
|
34
|
|
|
""" |
|
35
|
|
|
Base class for testing action aliases. |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
action_alias_name = None |
|
39
|
|
|
action_alias_db = None |
|
40
|
|
|
|
|
41
|
|
|
def setUp(self): |
|
42
|
|
|
super(BaseActionAliasTestCase, self).setUp() |
|
43
|
|
|
|
|
44
|
|
|
if not self.action_alias_name: |
|
45
|
|
|
raise ValueError('"action_alias_name" class attribute needs to be provided') |
|
46
|
|
|
|
|
47
|
|
|
self.action_alias_db = self._get_action_alias_db_by_name(name=self.action_alias_name) |
|
48
|
|
|
|
|
49
|
|
|
def assertCommandMatchesExactlyOneFormatString(self, format_strings, command): |
|
50
|
|
|
""" |
|
51
|
|
|
Assert that the provided command matches exactly one format string from the provided list. |
|
52
|
|
|
""" |
|
53
|
|
|
matched_format_strings = [] |
|
54
|
|
|
|
|
55
|
|
|
for format_string in format_strings: |
|
56
|
|
|
try: |
|
57
|
|
|
extract_parameters(format_str=format_string, |
|
58
|
|
|
param_stream=command) |
|
59
|
|
|
except ParseException: |
|
60
|
|
|
continue |
|
61
|
|
|
|
|
62
|
|
|
matched_format_strings.append(format_string) |
|
63
|
|
|
|
|
64
|
|
|
if len(matched_format_strings) == 0: |
|
65
|
|
|
msg = ('Command "%s" didn\'t match any of the provided format strings' % (command)) |
|
66
|
|
|
raise AssertionError(msg) |
|
67
|
|
|
elif len(matched_format_strings) > 1: |
|
68
|
|
|
msg = ('Command "%s" matched multiple format strings: %s' % |
|
69
|
|
|
(command, ', '.join(matched_format_strings))) |
|
70
|
|
|
raise AssertionError(msg) |
|
71
|
|
|
|
|
72
|
|
|
def assertExtractedParametersMatch(self, format_string, command, parameters): |
|
73
|
|
|
""" |
|
74
|
|
|
Assert that the provided command matches the format string. |
|
75
|
|
|
|
|
76
|
|
|
In addition to that, also assert that the parameters which have been extracted from the |
|
77
|
|
|
user input (command) also match the provided parameters. |
|
78
|
|
|
""" |
|
79
|
|
|
extracted_params = extract_parameters_for_action_alias_db( |
|
80
|
|
|
action_alias_db=self.action_alias_db, |
|
81
|
|
|
format_str=format_string, |
|
82
|
|
|
param_stream=command) |
|
83
|
|
|
|
|
84
|
|
|
if extracted_params != parameters: |
|
85
|
|
|
msg = ('Extracted parameters from command string "%s" against format string "%s"' |
|
86
|
|
|
' didn\'t match the provided parameters: ' % (command, format_string)) |
|
87
|
|
|
|
|
88
|
|
|
# Note: We intercept the exception so we can can include diff for the dictionaries |
|
89
|
|
|
try: |
|
90
|
|
|
self.assertEqual(extracted_params, parameters) |
|
91
|
|
|
except AssertionError as e: |
|
92
|
|
|
msg += str(e) |
|
93
|
|
|
|
|
94
|
|
|
raise AssertionError(msg) |
|
95
|
|
|
|
|
96
|
|
|
def _get_action_alias_db_by_name(self, name): |
|
97
|
|
|
""" |
|
98
|
|
|
Retrieve ActionAlias DB object for the provided alias name. |
|
99
|
|
|
""" |
|
100
|
|
|
base_pack_path = self._get_base_pack_path() |
|
101
|
|
|
pack_yaml_path = os.path.join(base_pack_path, MANIFEST_FILE_NAME) |
|
102
|
|
|
|
|
103
|
|
|
if os.path.isfile(pack_yaml_path): |
|
104
|
|
|
# 1. 1st try to infer pack name from pack metadata file |
|
105
|
|
|
meta_loader = MetaLoader() |
|
106
|
|
|
pack_metadata = meta_loader.load(pack_yaml_path) |
|
107
|
|
|
pack = get_pack_ref_from_metadata(metadata=pack_metadata) |
|
108
|
|
|
else: |
|
109
|
|
|
# 2. If pack.yaml is not available, fail back to directory name |
|
110
|
|
|
# Note: For this to work, directory name needs to match pack name |
|
111
|
|
|
_, pack = os.path.split(base_pack_path) |
|
112
|
|
|
|
|
113
|
|
|
pack_loader = ContentPackLoader() |
|
114
|
|
|
registrar = AliasesRegistrar(use_pack_cache=False) |
|
115
|
|
|
|
|
116
|
|
|
aliases_path = pack_loader.get_content_from_pack(pack_dir=base_pack_path, |
|
117
|
|
|
content_type='aliases') |
|
118
|
|
|
aliases = registrar._get_aliases_from_pack(aliases_dir=aliases_path) |
|
|
|
|
|
|
119
|
|
|
for alias_path in aliases: |
|
120
|
|
|
action_alias_db = registrar._get_action_alias_db(pack=pack, |
|
|
|
|
|
|
121
|
|
|
action_alias=alias_path) |
|
122
|
|
|
|
|
123
|
|
|
if action_alias_db.name == name: |
|
124
|
|
|
return action_alias_db |
|
125
|
|
|
|
|
126
|
|
|
raise ValueError('Alias with name "%s" not found' % (name)) |
|
127
|
|
|
|
Prefixing a member variable
_is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: