Skip to content

Commit 06cc462

Browse files
committed
Added test for specified_target parameter
1 parent 306d57f commit 06cc462

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

actions/acoslib/action.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BaseAction(Action):
99
DEFAULT_AXAPI_VERSION = acos.AXAPI_30
1010

1111
# These are the parameters for acos pack, not used by the ACOS Client
12-
PARAMS_FOR_PACK = ['appliance', 'action', 'object_path', 'one_target', 'specified_target']
12+
PARAMS_FOR_PACK = ['appliance', 'action', 'object_path', 'one_target']
1313

1414
def __init__(self, config):
1515
super(BaseAction, self).__init__(config)
@@ -18,12 +18,12 @@ def __init__(self, config):
1818

1919
self.config = config
2020

21-
def login(self, appliance, specified_target=None):
21+
def login(self, target, specified_target=None):
2222
try:
2323
if specified_target:
2424
config = specified_target
2525
else:
26-
config = next(x for x in self.config['appliance'] if x['target'] == appliance)
26+
config = next(x for x in self.config['appliance'] if x['target'] == target)
2727
return acos.Client(config['target'],
2828
config['api_version'],
2929
config['userid'],
@@ -34,7 +34,7 @@ def login(self, appliance, specified_target=None):
3434
self.logger.error(e)
3535
except StopIteration:
3636
self.logger.error("Specified appliance(%s) doesn't exist in the configuration file " %
37-
appliance)
37+
target)
3838

3939
def get_object(self, base_obj, object_path):
4040
obj = base_obj

actions/ax_action_runner.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55

66
class AXActionRunner(BaseAction):
7-
def do_run(self, action, object_path, appliance, specified_target, **kwargs):
8-
target = appliance
9-
if specified_target:
10-
target = specified_target.get("target")
11-
client = self.login(appliance, specified_target)
7+
def do_run(self, action, object_path, target, **kwargs):
8+
if kwargs.get('specified_target'):
9+
target = kwargs.get('specified_target').get("target")
10+
client = self.login(target, kwargs.get('specified_target'))
1211
if client:
1312
try:
1413
# transforms user parameters as needed
@@ -28,12 +27,10 @@ def do_run(self, action, object_path, appliance, specified_target, **kwargs):
2827
return (False, '[%s] Failed to initilaize Client object of acos_client' % target)
2928

3029
def run(self, action, object_path, one_target, **kwargs):
31-
if one_target or kwargs.get('appliance') or kwargs.get('specified_target'):
32-
return self.do_run(
33-
action,
34-
object_path,
35-
**kwargs
36-
)
30+
if kwargs.get('specified_target'):
31+
return self.do_run(action, object_path, None, **kwargs)
32+
if one_target or kwargs.get('appliance'):
33+
return self.do_run(action, object_path, kwargs.get('appliance'), **kwargs)
3734
else:
3835
results = []
3936
for config in self.config['appliance']:

tests/test_specified_target.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import mock
2+
3+
from acos_base_action_test_case import ACOSBaseActionTestCase
4+
from ax_action_runner import AXActionRunner
5+
6+
7+
class SpecifiedTargetTestCase(ACOSBaseActionTestCase):
8+
__test__ = True
9+
action_cls = AXActionRunner
10+
11+
def setUp(self):
12+
super(SpecifiedTargetTestCase, self).setUp()
13+
14+
self.action = self.get_action_instance(self._full_config)
15+
self.action.logger.addHandler(self._log_handler)
16+
17+
@mock.patch('acos_client.Client')
18+
def test_specified_target_without_one_target(self, mock_client):
19+
# set mock of action
20+
mock_action = mock.Mock()
21+
mock_action.slb.server.get.return_value = 'test-result'
22+
23+
mock_client.return_value = mock_action
24+
25+
# execute action
26+
params = {
27+
'object_path': 'slb.server',
28+
'action': 'get',
29+
'name': 'hoge',
30+
'one_target': False,
31+
'specified_target': {
32+
'target': 'appliance_acos_v2.1',
33+
'userid': 'admin',
34+
'passwd': 'hoge',
35+
'api_version': 'v2.1',
36+
}
37+
}
38+
result = self.action.run(**params)
39+
40+
self.assertTrue(result[0])
41+
self.assertEqual(result[1], 'test-result')
42+
self.assertEqual(len(self._log_handler.messages['error']), 0)
43+
44+
@mock.patch('acos_client.Client')
45+
def test_specified_target_with_one_target(self, mock_client):
46+
# set mock of action
47+
mock_action = mock.Mock()
48+
mock_action.slb.server.get.return_value = 'test-result'
49+
50+
mock_client.return_value = mock_action
51+
52+
params = {
53+
'object_path': 'slb.server',
54+
'action': 'get',
55+
'name': 'hoge',
56+
'one_target': True,
57+
'appliance': 'fuga',
58+
'specified_target': {
59+
'target': 'appliance_acos_v2.1',
60+
'userid': 'admin',
61+
'passwd': 'hoge',
62+
'api_version': 'v2.1',
63+
}
64+
}
65+
result = self.action.run(**params)
66+
67+
self.assertTrue(result[0])
68+
self.assertEqual(result[1], 'test-result')
69+
self.assertEqual(len(self._log_handler.messages['error']), 0)

0 commit comments

Comments
 (0)