1 | # -*- coding: utf-8 -*- |
||
2 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
3 | # contributor license agreements. See the NOTICE file distributed with |
||
4 | # this work for additional information regarding copyright ownership. |
||
5 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
6 | # (the "License"); you may not use this file except in compliance with |
||
7 | # the License. You may obtain a copy of the License at |
||
8 | # |
||
9 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
10 | # |
||
11 | # Unless required by applicable law or agreed to in writing, software |
||
12 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
14 | # See the License for the specific language governing permissions and |
||
15 | # limitations under the License. |
||
16 | |||
17 | from __future__ import absolute_import |
||
18 | import unittest2 |
||
19 | |||
20 | import mock |
||
21 | |||
22 | from action_chain_runner import action_chain_runner as acr |
||
23 | from st2common.exceptions.action import ParameterRenderingFailedException |
||
24 | from st2common.models.system.actionchain import Node |
||
25 | |||
26 | |||
27 | class ActionChainRunnerResolveParamsTests(unittest2.TestCase): |
||
28 | |||
29 | def test_render_params_action_context(self): |
||
30 | runner = acr.get_runner() |
||
31 | chain_context = { |
||
32 | 'parent': { |
||
33 | 'execution_id': 'some_awesome_exec_id', |
||
34 | 'user': 'dad' |
||
35 | }, |
||
36 | 'user': 'son', |
||
37 | 'k1': 'v1' |
||
38 | } |
||
39 | task_params = { |
||
40 | 'exec_id': {'default': '{{action_context.parent.execution_id}}'}, |
||
41 | 'k2': {}, |
||
42 | 'foo': {'default': 1} |
||
43 | } |
||
44 | action_node = Node(name='test_action_context_params', ref='core.local', params=task_params) |
||
45 | rendered_params = runner._resolve_params(action_node, {}, {}, {}, chain_context) |
||
0 ignored issues
–
show
|
|||
46 | self.assertEqual(rendered_params['exec_id']['default'], 'some_awesome_exec_id') |
||
47 | |||
48 | def test_render_params_action_context_non_existent_member(self): |
||
49 | runner = acr.get_runner() |
||
50 | chain_context = { |
||
51 | 'parent': { |
||
52 | 'execution_id': 'some_awesome_exec_id', |
||
53 | 'user': 'dad' |
||
54 | }, |
||
55 | 'user': 'son', |
||
56 | 'k1': 'v1' |
||
57 | } |
||
58 | task_params = { |
||
59 | 'exec_id': {'default': '{{action_context.parent.yo_gimme_tha_key}}'}, |
||
60 | 'k2': {}, |
||
61 | 'foo': {'default': 1} |
||
62 | } |
||
63 | action_node = Node(name='test_action_context_params', ref='core.local', params=task_params) |
||
64 | try: |
||
65 | runner._resolve_params(action_node, {}, {}, {}, chain_context) |
||
0 ignored issues
–
show
It seems like
_resolve_params was declared protected and should not be accessed from this context.
Prefixing a member variable class MyParent:
def __init__(self):
self._x = 1;
self.y = 2;
class MyChild(MyParent):
def some_method(self):
return self._x # Ok, since accessed from a child class
class AnotherClass:
def some_method(self, instance_of_my_child):
return instance_of_my_child._x # Would be flagged as AnotherClass is not
# a child class of MyParent
Loading history...
|
|||
66 | self.fail('Should have thrown an instance of %s' % ParameterRenderingFailedException) |
||
67 | except ParameterRenderingFailedException: |
||
68 | pass |
||
69 | |||
70 | def test_render_params_with_config(self): |
||
71 | with mock.patch('st2common.util.config_loader.ContentPackConfigLoader') as config_loader: |
||
72 | config_loader().get_config.return_value = { |
||
73 | 'amazing_config_value_fo_lyfe': 'no' |
||
74 | } |
||
75 | |||
76 | runner = acr.get_runner() |
||
77 | chain_context = { |
||
78 | 'parent': { |
||
79 | 'execution_id': 'some_awesome_exec_id', |
||
80 | 'user': 'dad', |
||
81 | 'pack': 'mom' |
||
82 | }, |
||
83 | 'user': 'son', |
||
84 | } |
||
85 | task_params = { |
||
86 | 'config_val': '{{config_context.amazing_config_value_fo_lyfe}}' |
||
87 | } |
||
88 | action_node = Node( |
||
89 | name='test_action_context_params', |
||
90 | ref='core.local', |
||
91 | params=task_params |
||
92 | ) |
||
93 | rendered_params = runner._resolve_params(action_node, {}, {}, {}, chain_context) |
||
94 | self.assertEqual(rendered_params['config_val'], 'no') |
||
95 | |||
96 | def test_init_params_vars_with_unicode_value(self): |
||
97 | chain_spec = { |
||
98 | 'vars': { |
||
99 | 'unicode_var': u'٩(̾●̮̮̃̾•̃̾)۶ ٩(̾●̮̮̃̾•̃̾)۶ ćšž', |
||
100 | 'unicode_var_param': u'{{ param }}' |
||
101 | }, |
||
102 | 'chain': [ |
||
103 | { |
||
104 | 'name': 'c1', |
||
105 | 'ref': 'core.local', |
||
106 | 'parameters': { |
||
107 | 'cmd': 'echo {{ unicode_var }}' |
||
108 | } |
||
109 | } |
||
110 | ] |
||
111 | } |
||
112 | |||
113 | chain_holder = acr.ChainHolder(chainspec=chain_spec, chainname='foo') |
||
114 | chain_holder.init_vars(action_parameters={'param': u'٩(̾●̮̮̃̾•̃̾)۶'}) |
||
115 | |||
116 | expected = { |
||
117 | 'unicode_var': u'٩(̾●̮̮̃̾•̃̾)۶ ٩(̾●̮̮̃̾•̃̾)۶ ćšž', |
||
118 | 'unicode_var_param': u'٩(̾●̮̮̃̾•̃̾)۶' |
||
119 | } |
||
120 | self.assertEqual(chain_holder.vars, expected) |
||
121 |
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: