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
|
|
|
|
15
|
|
|
import yaml |
16
|
|
|
from mock import Mock, MagicMock |
17
|
|
|
|
18
|
|
|
from st2tests.base import BaseActionTestCase |
19
|
|
|
|
20
|
|
|
from ncm_config_download import NcmConfigDownload |
21
|
|
|
|
22
|
|
|
__all__ = [ |
23
|
|
|
'NcmConfigDownloadTestCase' |
24
|
|
|
] |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class NcmConfigDownloadTestCase(BaseActionTestCase): |
28
|
|
|
action_cls = NcmConfigDownload |
29
|
|
|
|
30
|
|
|
def test_run_no_config(self): |
31
|
|
|
self.assertRaises(ValueError, |
32
|
|
|
NcmConfigDownload, |
33
|
|
|
yaml.safe_load( |
34
|
|
|
self.get_fixture_content('blank.yaml'))) |
35
|
|
|
|
36
|
|
|
def test_run_basic_config(self): |
37
|
|
|
action = self.get_action_instance(yaml.safe_load( |
38
|
|
|
self.get_fixture_content('full.yaml'))) |
39
|
|
|
|
40
|
|
|
self.assertIsInstance(action, NcmConfigDownload) |
41
|
|
|
|
42
|
|
|
def test_run_connect_fail(self): |
43
|
|
|
action = self.get_action_instance(yaml.safe_load( |
44
|
|
|
self.get_fixture_content('full.yaml'))) |
45
|
|
|
|
46
|
|
|
action.connect = Mock(side_effect=ValueError( |
47
|
|
|
'Orion host details not in the config.yaml')) |
48
|
|
|
|
49
|
|
|
self.assertRaises(ValueError, |
50
|
|
|
action.run, |
51
|
|
|
"router1", |
52
|
|
|
"orion", |
53
|
|
|
["running", "startup"]) |
54
|
|
|
|
55
|
|
|
def test_run_ncm_node_not_found(self): |
56
|
|
|
orion_data = {'results': []} |
57
|
|
|
|
58
|
|
|
action = self.get_action_instance(yaml.safe_load( |
59
|
|
|
self.get_fixture_content('full.yaml'))) |
60
|
|
|
|
61
|
|
|
action.connect = MagicMock(return_value=True) |
62
|
|
|
|
63
|
|
|
action.query = MagicMock(return_value=orion_data) |
64
|
|
|
|
65
|
|
|
self.assertRaises(Exception, |
66
|
|
|
action.run, |
67
|
|
|
"router1", |
68
|
|
|
"orion", |
69
|
|
|
["running", "startup"]) |
70
|
|
|
|
71
|
|
|
def test_run_ncm_download_complete(self): |
72
|
|
|
expected = {'running': {'DeviceOutput': None, |
73
|
|
|
'ErrorMessage': None, |
74
|
|
|
'RequestedReboot': False, |
75
|
|
|
'RequestedScript': None, |
76
|
|
|
'UserName': 'hubot', |
77
|
|
|
'status': 'Complete'}, |
78
|
|
|
'startup': {'DeviceOutput': None, |
79
|
|
|
'ErrorMessage': None, |
80
|
|
|
'RequestedReboot': False, |
81
|
|
|
'RequestedScript': None, |
82
|
|
|
'UserName': 'hubot', |
83
|
|
|
'status': 'Complete'}, |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
query_data = [] |
87
|
|
|
query_data.append(yaml.safe_load( |
88
|
|
|
self.get_fixture_content("orion_npm_results.yaml"))) |
89
|
|
|
query_data.append(yaml.safe_load( |
90
|
|
|
self.get_fixture_content("orion_ncm_results.yaml"))) |
91
|
|
|
query_data.append({'results': [{"Status": 2, |
92
|
|
|
"UserName": "hubot", |
93
|
|
|
"DeviceOutput": None, |
94
|
|
|
"ErrorMessage": None, |
95
|
|
|
"RequestedScript": None, |
96
|
|
|
"RequestedReboot": False}]}) |
97
|
|
|
query_data.append({'results': [{"Status": 2, |
98
|
|
|
"UserName": "hubot", |
99
|
|
|
"DeviceOutput": None, |
100
|
|
|
"ErrorMessage": None, |
101
|
|
|
"RequestedScript": None, |
102
|
|
|
"RequestedReboot": False}]}) |
103
|
|
|
invoke_data = ["1234567890"] |
104
|
|
|
|
105
|
|
|
action = self.get_action_instance(yaml.safe_load( |
106
|
|
|
self.get_fixture_content('full.yaml'))) |
107
|
|
|
|
108
|
|
|
action.connect = MagicMock(return_value=True) |
109
|
|
|
action.query = Mock(side_effect=query_data) |
110
|
|
|
action.invoke = MagicMock(return_value=invoke_data) |
111
|
|
|
|
112
|
|
|
result = action.run("router1", |
113
|
|
|
"orion", |
114
|
|
|
["running", "startup"]) |
115
|
|
|
|
116
|
|
|
self.assertEqual(result, expected) |
117
|
|
|
|