| 1 |
# Copyright 2014 - Mirantis, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import time |
| 16 |
|
| 17 |
from mistral_lib import actions |
| 18 |
from mistral_lib.tests import base |
| 19 |
from mistral_lib.utils import inspect_utils as i_u |
| 20 |
|
| 21 |
|
| 22 |
class DummyHTTPAction(actions.Action): |
| 23 |
def __init__(self, |
| 24 |
url, |
| 25 |
method="GET", |
| 26 |
params=None, |
| 27 |
body=None, |
| 28 |
headers=None, |
| 29 |
cookies=None, |
| 30 |
auth=None, |
| 31 |
timeout=None, |
| 32 |
allow_redirects=None, |
| 33 |
proxies=None, |
| 34 |
verify=None): |
| 35 |
super(DummyHTTPAction, self).__init__() |
| 36 |
|
| 37 |
def run(self, context): |
| 38 |
return context |
| 39 |
|
| 40 |
|
| 41 |
class DummyRunTask(object): |
| 42 |
def __init__(self, wf_ex, wf_spec, task_spec, ctx, triggered_by=None, |
| 43 |
handles_error=False): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class ClassWithProperties(object): |
| 48 |
|
| 49 |
a = 1 |
| 50 |
|
| 51 |
@property |
| 52 |
def prop(self): |
| 53 |
pass |
| 54 |
|
| 55 |
|
| 56 |
class InspectUtilsTest(base.TestCase): |
| 57 |
def test_get_parameters_str(self): |
| 58 |
action_class = DummyHTTPAction |
| 59 |
parameters_str = i_u.get_arg_list_as_str(action_class.__init__) |
| 60 |
|
| 61 |
http_action_params = ( |
| 62 |
'url, method="GET", params=null, body=null, ' |
| 63 |
'headers=null, cookies=null, auth=null, ' |
| 64 |
'timeout=null, allow_redirects=null, ' |
| 65 |
'proxies=null, verify=null' |
| 66 |
) |
| 67 |
|
| 68 |
self.assertEqual(http_action_params, parameters_str) |
| 69 |
|
| 70 |
def test_get_parameters_str_all_mandatory(self): |
| 71 |
clazz = DummyRunTask |
| 72 |
parameters_str = i_u.get_arg_list_as_str(clazz.__init__) |
| 73 |
|
| 74 |
self.assertEqual( |
| 75 |
'wf_ex, wf_spec, task_spec, ctx, triggered_by=null,' |
| 76 |
' handles_error=false', |
| 77 |
parameters_str |
| 78 |
) |
| 79 |
|
| 80 |
def test_get_parameters_str_with_function_parameter(self): |
| 81 |
|
| 82 |
def test_func(foo, bar=None, test_func=time.sleep): |
| 83 |
pass |
| 84 |
|
| 85 |
parameters_str = i_u.get_arg_list_as_str(test_func) |
| 86 |
|
| 87 |
self.assertEqual("foo, bar=null", parameters_str) |
| 88 |
|
| 89 |
def test_get_public_fields(self): |
| 90 |
|
| 91 |
attrs = i_u.get_public_fields(ClassWithProperties) |
| 92 |
|
| 93 |
self.assertEqual(attrs, {'a': 1}) |