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 node_create import NodeCreate |
21
|
|
|
from lib.utils import is_ip |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
__all__ = [ |
25
|
|
|
'NodeCreateTestCase' |
26
|
|
|
] |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class NodeCreateTestCase(BaseActionTestCase): |
30
|
|
|
action_cls = NodeCreate |
31
|
|
|
|
32
|
|
|
def test_run_is_ip_v4(self): |
33
|
|
|
self.assertTrue(is_ip("172.16.0.1")) |
34
|
|
|
self.assertTrue(is_ip("1762:0:0:0:0:B03:1:AF18")) |
35
|
|
|
self.assertFalse(is_ip("172.16.0.300")) |
36
|
|
|
self.assertFalse(is_ip("1762:%:0:0:0:B03:1:AF18")) |
37
|
|
|
|
38
|
|
|
def test_run_no_config(self): |
39
|
|
|
self.assertRaises(ValueError, |
40
|
|
|
NodeCreate, |
41
|
|
|
yaml.safe_load( |
42
|
|
|
self.get_fixture_content('blank.yaml'))) |
43
|
|
|
|
44
|
|
|
def test_run_is_instance(self): |
45
|
|
|
action = self.get_action_instance(yaml.safe_load( |
46
|
|
|
self.get_fixture_content('full.yaml'))) |
47
|
|
|
|
48
|
|
|
self.assertIsInstance(action, NodeCreate) |
49
|
|
|
|
50
|
|
|
def test_run_connect_fail(self): |
51
|
|
|
action = self.get_action_instance(yaml.safe_load( |
52
|
|
|
self.get_fixture_content('full.yaml'))) |
53
|
|
|
|
54
|
|
|
action.connect = Mock(side_effect=ValueError( |
55
|
|
|
'Orion host details not in the config.yaml')) |
56
|
|
|
|
57
|
|
|
self.assertRaises(ValueError, |
58
|
|
|
action.run, |
59
|
|
|
"router1", |
60
|
|
|
"192.168.0.1", |
61
|
|
|
"orion", |
62
|
|
|
None, |
63
|
|
|
"snmpv2", |
64
|
|
|
"internal", |
65
|
|
|
None, |
66
|
|
|
"snmp") |
67
|
|
|
|
68
|
|
|
def test_run_node_caption_exists(self): |
69
|
|
|
query_data = [] |
70
|
|
|
query_data.append(yaml.safe_load( |
71
|
|
|
self.get_fixture_content("orion_npm_results.yaml"))) |
72
|
|
|
query_data.append(yaml.safe_load( |
73
|
|
|
self.get_fixture_content("orion_ncm_results.yaml"))) |
74
|
|
|
|
75
|
|
|
action = self.get_action_instance(yaml.safe_load( |
76
|
|
|
self.get_fixture_content('full.yaml'))) |
77
|
|
|
|
78
|
|
|
action.connect = MagicMock(return_value=True) |
79
|
|
|
action.query = MagicMock(side_effect=query_data) |
80
|
|
|
action.invoke = Mock(return_value=None) |
81
|
|
|
action.create = Mock(return_value=None) |
82
|
|
|
|
83
|
|
|
self.assertRaises(ValueError, |
84
|
|
|
action.run, |
85
|
|
|
"router1", |
86
|
|
|
"192.168.0.1", |
87
|
|
|
"orion", |
88
|
|
|
None, |
89
|
|
|
"snmpv2", |
90
|
|
|
"internal", |
91
|
|
|
None, |
92
|
|
|
"snmp") |
93
|
|
|
|
94
|
|
|
def test_run_node_ip_exists(self): |
95
|
|
|
query_data = [] |
96
|
|
|
query_data.append({'results': []}) |
97
|
|
|
query_data.append(yaml.safe_load( |
98
|
|
|
self.get_fixture_content("orion_npm_results.yaml"))) |
99
|
|
|
query_data.append(yaml.safe_load( |
100
|
|
|
self.get_fixture_content("orion_ncm_results.yaml"))) |
101
|
|
|
|
102
|
|
|
action = self.get_action_instance(yaml.safe_load( |
103
|
|
|
self.get_fixture_content('full.yaml'))) |
104
|
|
|
|
105
|
|
|
action.connect = MagicMock(return_value=True) |
106
|
|
|
action.query = MagicMock(side_effect=query_data) |
107
|
|
|
action.invoke = MagicMock(return_value=None) |
108
|
|
|
action.create = MagicMock(return_value=None) |
109
|
|
|
|
110
|
|
|
self.assertRaises(ValueError, |
111
|
|
|
action.run, |
112
|
|
|
"router2", |
113
|
|
|
"192.168.0.1", |
114
|
|
|
"orion", |
115
|
|
|
None, |
116
|
|
|
"snmpv2", |
117
|
|
|
"internal", |
118
|
|
|
None, |
119
|
|
|
"snmp") |
120
|
|
|
|
121
|
|
|
def test_run_poller_is_none(self): |
122
|
|
|
expected = {'node_id': '6', 'platform': 'orion'} |
123
|
|
|
|
124
|
|
|
query_data = {'results': []} |
125
|
|
|
|
126
|
|
|
action = self.get_action_instance(yaml.safe_load( |
127
|
|
|
self.get_fixture_content('full.yaml'))) |
128
|
|
|
|
129
|
|
|
action.connect = MagicMock(return_value=True) |
130
|
|
|
action.query = MagicMock(return_value=query_data) |
131
|
|
|
action.invoke = MagicMock(return_value=None) |
132
|
|
|
action.get_engine_id = MagicMock(return_value=2) |
133
|
|
|
action.create = MagicMock( |
134
|
|
|
return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6") |
135
|
|
|
|
136
|
|
|
result = action.run("router2", |
137
|
|
|
"192.168.0.1", |
138
|
|
|
"orion", |
139
|
|
|
None, |
140
|
|
|
"snmpv2", |
141
|
|
|
"internal", |
142
|
|
|
None, |
143
|
|
|
"snmp") |
144
|
|
|
self.assertEqual(result, expected) |
145
|
|
|
|
146
|
|
|
def test_run_node_additonal_poller(self): |
147
|
|
|
expected = {'node_id': '6', 'platform': 'orion'} |
148
|
|
|
|
149
|
|
|
query_data = [{'results': []}, |
150
|
|
|
{'results': []}, |
151
|
|
|
{'results': [{'EngineID': 2}]}] |
152
|
|
|
|
153
|
|
|
action = self.get_action_instance(yaml.safe_load( |
154
|
|
|
self.get_fixture_content('full.yaml'))) |
155
|
|
|
|
156
|
|
|
action.connect = MagicMock(return_value=True) |
157
|
|
|
action.query = MagicMock(side_effect=query_data) |
158
|
|
|
action.invoke = MagicMock(return_value=None) |
159
|
|
|
action.create = MagicMock( |
160
|
|
|
return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6") |
161
|
|
|
|
162
|
|
|
result = action.run("router2", |
163
|
|
|
"192.168.0.1", |
164
|
|
|
"orion", |
165
|
|
|
"additonal1", |
166
|
|
|
"snmpv2", |
167
|
|
|
"internal", |
168
|
|
|
None, |
169
|
|
|
"snmp") |
170
|
|
|
self.assertEqual(result, expected) |
171
|
|
|
|