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 start_discovery import StartDiscovery |
21
|
|
|
|
22
|
|
|
__all__ = [ |
23
|
|
|
'StartDiscoveryTestCase' |
24
|
|
|
] |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class StartDiscoveryTestCase(BaseActionTestCase): |
28
|
|
|
action_cls = StartDiscovery |
29
|
|
|
|
30
|
|
|
def test_run_no_config(self): |
31
|
|
|
self.assertRaises(ValueError, |
32
|
|
|
StartDiscovery, |
33
|
|
|
yaml.safe_load( |
34
|
|
|
self.get_fixture_content('blank.yaml'))) |
35
|
|
|
|
36
|
|
|
def test_run_is_instance(self): |
37
|
|
|
action = self.get_action_instance( |
38
|
|
|
yaml.safe_load(self.get_fixture_content('full.yaml'))) |
39
|
|
|
|
40
|
|
|
self.assertIsInstance(action, StartDiscovery) |
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
|
|
|
name="Unit Test Discovery", |
52
|
|
|
platform="orion", |
53
|
|
|
poller="primary", |
54
|
|
|
snmp_communities=["public"], |
55
|
|
|
nodes=["router1"], |
56
|
|
|
subnets=None, |
57
|
|
|
ip_ranges=None, |
58
|
|
|
no_icmp_only=True, |
59
|
|
|
auto_import=False) |
60
|
|
|
|
61
|
|
|
def test_run_fail_on_no_targets(self): |
62
|
|
|
action = self.get_action_instance(yaml.safe_load( |
63
|
|
|
self.get_fixture_content('full.yaml'))) |
64
|
|
|
|
65
|
|
|
action.connect = MagicMock(return_value=True) |
66
|
|
|
|
67
|
|
|
# nodes = 0, subnets = 0, ip_ranges = 0 |
68
|
|
|
self.assertRaises(ValueError, |
69
|
|
|
action.run, |
70
|
|
|
name="Unit Test Discovery", |
71
|
|
|
platform="orion", |
72
|
|
|
poller="primary", |
73
|
|
|
snmp_communities=["public"], |
74
|
|
|
nodes=None, |
75
|
|
|
subnets=None, |
76
|
|
|
ip_ranges=None, |
77
|
|
|
no_icmp_only=True, |
78
|
|
|
auto_import=False) |
79
|
|
|
|
80
|
|
|
def test_run_fail_on_mutliple_targets(self): |
81
|
|
|
action = self.get_action_instance(yaml.safe_load( |
82
|
|
|
self.get_fixture_content('full.yaml'))) |
83
|
|
|
|
84
|
|
|
action.connect = MagicMock(return_value=True) |
85
|
|
|
|
86
|
|
|
# nodes = 1, subnets = 1, ip_ranges = 1 |
87
|
|
|
nodes = ["192.168.1.1", "192.168.1.10"] |
88
|
|
|
subnets = ["192.168.1.0/24"] |
89
|
|
|
ip_ranges = ["192.168.1.1:192.168.1.10"] |
90
|
|
|
|
91
|
|
|
self.assertRaises(ValueError, |
92
|
|
|
action.run, |
93
|
|
|
name="Unit Test Discovery", |
94
|
|
|
platform="orion", |
95
|
|
|
poller="primary", |
96
|
|
|
snmp_communities=["public"], |
97
|
|
|
nodes=nodes, |
98
|
|
|
subnets=subnets, |
99
|
|
|
ip_ranges=ip_ranges, |
100
|
|
|
no_icmp_only=True, |
101
|
|
|
auto_import=False) |
102
|
|
|
|
103
|
|
|
# nodes = 1, subnets = 0, ip_ranges = 1 |
104
|
|
|
nodes = ["192.168.1.1", "192.168.1.10"] |
105
|
|
|
subnets = None |
106
|
|
|
ip_ranges = ["192.168.1.1:192.168.1.10"] |
107
|
|
|
|
108
|
|
|
self.assertRaises(ValueError, |
109
|
|
|
action.run, |
110
|
|
|
name="Unit Test Discovery", |
111
|
|
|
platform="orion", |
112
|
|
|
poller="primary", |
113
|
|
|
snmp_communities=["public"], |
114
|
|
|
nodes=nodes, |
115
|
|
|
subnets=subnets, |
116
|
|
|
ip_ranges=ip_ranges, |
117
|
|
|
no_icmp_only=True, |
118
|
|
|
auto_import=False) |
119
|
|
|
|
120
|
|
|
# nodes = 1, subnets = 1, ip_ranges = 0 |
121
|
|
|
nodes = ["192.168.1.1", "192.168.1.10"] |
122
|
|
|
subnets = ["192.168.1.0/24"] |
123
|
|
|
ip_ranges = None |
124
|
|
|
|
125
|
|
|
self.assertRaises(ValueError, |
126
|
|
|
action.run, |
127
|
|
|
name="Unit Test Discovery", |
128
|
|
|
platform="orion", |
129
|
|
|
poller="primary", |
130
|
|
|
snmp_communities=["public"], |
131
|
|
|
nodes=nodes, |
132
|
|
|
subnets=subnets, |
133
|
|
|
ip_ranges=ip_ranges, |
134
|
|
|
no_icmp_only=True, |
135
|
|
|
auto_import=False) |
136
|
|
|
|
137
|
|
|
# nodes = 0, subnets = 1, ip_ranges = 1 |
138
|
|
|
nodes = None |
139
|
|
|
subnets = ["192.168.1.0/24"] |
140
|
|
|
ip_ranges = ["192.168.1.1:192.168.1.10"] |
141
|
|
|
|
142
|
|
|
self.assertRaises(ValueError, |
143
|
|
|
action.run, |
144
|
|
|
name="Unit Test Discovery", |
145
|
|
|
platform="orion", |
146
|
|
|
poller="primary", |
147
|
|
|
snmp_communities=["public"], |
148
|
|
|
nodes=nodes, |
149
|
|
|
subnets=subnets, |
150
|
|
|
ip_ranges=ip_ranges, |
151
|
|
|
no_icmp_only=True, |
152
|
|
|
auto_import=False) |
153
|
|
|
|
154
|
|
|
def test_run_poller_lookup_primary(self): |
155
|
|
|
expected = 1 |
156
|
|
|
|
157
|
|
|
action = self.get_action_instance(yaml.safe_load( |
158
|
|
|
self.get_fixture_content('full.yaml'))) |
159
|
|
|
|
160
|
|
|
result = action.get_engine_id("primary") |
161
|
|
|
|
162
|
|
|
self.assertEqual(result, expected) |
163
|
|
|
|
164
|
|
|
def test_run_poller_lookup_fail(self): |
165
|
|
|
query_data = [] |
166
|
|
|
query_data.append({'results': []}) |
167
|
|
|
|
168
|
|
|
action = self.get_action_instance(yaml.safe_load( |
169
|
|
|
self.get_fixture_content('full.yaml'))) |
170
|
|
|
action.connect = MagicMock(return_value=True) |
171
|
|
|
action.query = MagicMock(side_effect=query_data) |
172
|
|
|
|
173
|
|
|
self.assertRaises(ValueError, |
174
|
|
|
action.get_engine_id, |
175
|
|
|
"foobar") |
176
|
|
|
|
177
|
|
|
def test_run_snmp_communities_fail(self): |
178
|
|
|
query_data = [] |
179
|
|
|
query_data.append({'results': []}) |
180
|
|
|
query_data.append({'results': []}) |
181
|
|
|
|
182
|
|
|
action = self.get_action_instance(yaml.safe_load( |
183
|
|
|
self.get_fixture_content('full.yaml'))) |
184
|
|
|
action.connect = MagicMock(return_value=True) |
185
|
|
|
action.query = MagicMock(side_effect=query_data) |
186
|
|
|
|
187
|
|
|
self.assertRaises(ValueError, |
188
|
|
|
action.get_snmp_cred_id, |
189
|
|
|
"foobar") |
190
|
|
|
|
191
|
|
|
self.assertRaises(ValueError, |
192
|
|
|
action.get_snmp_cred_id, |
193
|
|
|
"internal") |
194
|
|
|
|
195
|
|
|
def test_run_discovery_create_completes(self): |
196
|
|
|
expected = 10 |
197
|
|
|
|
198
|
|
|
query_data = [] |
199
|
|
|
query_data.append(yaml.safe_load( |
200
|
|
|
self.get_fixture_content('results_orion_snmp_cred.yaml'))) |
201
|
|
|
query_data.append({'results': [{'ID': 1}]}) |
202
|
|
|
|
203
|
|
|
invoke_data = [] |
204
|
|
|
invoke_data.append("CorePluginConfiguration") |
205
|
|
|
invoke_data.append(10) |
206
|
|
|
|
207
|
|
|
action = self.get_action_instance(yaml.safe_load( |
208
|
|
|
self.get_fixture_content('full.yaml'))) |
209
|
|
|
action.connect = MagicMock(return_value=True) |
210
|
|
|
action.query = MagicMock(side_effect=query_data) |
211
|
|
|
action.invoke = Mock(side_effect=invoke_data) |
212
|
|
|
|
213
|
|
|
result = action.run(name="Unit Test Discovery", |
214
|
|
|
platform="orion", |
215
|
|
|
poller="primary", |
216
|
|
|
snmp_communities=["public"], |
217
|
|
|
nodes=["router1"], |
218
|
|
|
subnets=None, |
219
|
|
|
ip_ranges=None, |
220
|
|
|
no_icmp_only=True, |
221
|
|
|
auto_import=False) |
222
|
|
|
|
223
|
|
|
self.assertEqual(result, expected) |
224
|
|
|
|
225
|
|
|
def test_run_discovery_create_completes_on_poller(self): |
226
|
|
|
expected = 10 |
227
|
|
|
|
228
|
|
|
query_data = [] |
229
|
|
|
query_data.append(yaml.safe_load( |
230
|
|
|
self.get_fixture_content('results_orion_snmp_cred.yaml'))) |
231
|
|
|
query_data.append(yaml.safe_load( |
232
|
|
|
self.get_fixture_content('results_orion_engines.yaml'))) |
233
|
|
|
query_data.append({'results': [{'ID': 1}]}) |
234
|
|
|
|
235
|
|
|
invoke_data = [] |
236
|
|
|
invoke_data.append("CorePluginConfiguration") |
237
|
|
|
invoke_data.append(10) |
238
|
|
|
|
239
|
|
|
action = self.get_action_instance(yaml.safe_load( |
240
|
|
|
self.get_fixture_content('full.yaml'))) |
241
|
|
|
action.connect = MagicMock(return_value=True) |
242
|
|
|
action.query = MagicMock(side_effect=query_data) |
243
|
|
|
action.invoke = Mock(side_effect=invoke_data) |
244
|
|
|
|
245
|
|
|
result = action.run(name="Unit Test Discovery", |
246
|
|
|
platform="orion", |
247
|
|
|
poller="primary", |
248
|
|
|
snmp_communities=["public"], |
249
|
|
|
nodes=["router1"], |
250
|
|
|
subnets=None, |
251
|
|
|
ip_ranges=None, |
252
|
|
|
no_icmp_only=True, |
253
|
|
|
auto_import=False) |
254
|
|
|
|
255
|
|
|
self.assertEqual(result, expected) |
256
|
|
|
|