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
|
|
|
from mock import MagicMock |
16
|
|
|
|
17
|
|
|
from orion_base_action_test_case import OrionBaseActionTestCase |
18
|
|
|
|
19
|
|
|
from node_create import NodeCreate |
20
|
|
|
from lib.utils import is_ip |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
__all__ = [ |
24
|
|
|
'NodeCreateTestCase' |
25
|
|
|
] |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class NodeCreateTestCase(OrionBaseActionTestCase): |
29
|
|
|
__test__ = True |
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
|
|
|
self.assertFalse(is_ip("server.example.com")) |
38
|
|
|
self.assertFalse(is_ip("router1")) |
39
|
|
|
self.assertFalse(is_ip("router:8080")) |
40
|
|
|
|
41
|
|
|
def test_run_connect_fail(self): |
42
|
|
|
action = self.setup_connect_fail() |
43
|
|
|
self.assertRaises(ValueError, |
44
|
|
|
action.run, |
45
|
|
|
"router1", |
46
|
|
|
"192.168.0.1", |
47
|
|
|
None, |
48
|
|
|
"snmpv2", |
49
|
|
|
"internal", |
50
|
|
|
"snmp") |
51
|
|
|
|
52
|
|
|
def test_run_node_caption_exists(self): |
53
|
|
|
action = self.setup_node_exists() |
54
|
|
|
self.assertRaises(ValueError, |
55
|
|
|
action.run, |
56
|
|
|
"router1", |
57
|
|
|
"192.168.0.1", |
58
|
|
|
None, |
59
|
|
|
"snmpv2", |
60
|
|
|
"internal", |
61
|
|
|
"snmp") |
62
|
|
|
|
63
|
|
|
def test_run_node_ip_exists(self): |
64
|
|
|
query_data = [] |
65
|
|
|
query_data.append(self.query_no_results) |
66
|
|
|
query_data.append(self.query_npm_node) |
67
|
|
|
query_data.append(self.query_ncm_node) |
68
|
|
|
|
69
|
|
|
action = self.get_action_instance(config=self.full_config) |
70
|
|
|
|
71
|
|
|
action.connect = MagicMock(return_value="orion") |
72
|
|
|
action.query = MagicMock(side_effect=query_data) |
73
|
|
|
action.invoke = MagicMock(return_value=None) |
74
|
|
|
action.create = MagicMock(return_value=None) |
75
|
|
|
|
76
|
|
|
self.assertRaises(ValueError, |
77
|
|
|
action.run, |
78
|
|
|
"router2", |
79
|
|
|
"192.168.0.1", |
80
|
|
|
None, |
81
|
|
|
"snmpv2", |
82
|
|
|
"internal", |
83
|
|
|
"snmp") |
84
|
|
|
|
85
|
|
|
def test_run_get_snmp_community(self): |
86
|
|
|
action = self.get_action_instance(config=self.full_config) |
87
|
|
|
|
88
|
|
|
self.assertEqual(action.get_snmp_community(None), "publ1c") |
89
|
|
|
self.assertEqual(action.get_snmp_community("customer"), "foobar") |
90
|
|
|
self.assertEqual(action.get_snmp_community("internal"), "barbaz") |
91
|
|
|
self.assertEqual(action.get_snmp_community("bazfoo"), "bazfoo") |
92
|
|
|
|
93
|
|
|
def test_run_poller_is_none(self): |
94
|
|
|
expected = {'node_id': '6', |
95
|
|
|
'label': 'orion'} |
96
|
|
|
|
97
|
|
|
query_data = self.query_no_results |
98
|
|
|
|
99
|
|
|
action = self.get_action_instance(config=self.full_config) |
100
|
|
|
|
101
|
|
|
action.connect = MagicMock(return_value="orion") |
102
|
|
|
action.query = MagicMock(return_value=query_data) |
103
|
|
|
action.invoke = MagicMock(return_value=None) |
104
|
|
|
action.get_engine_id = MagicMock(return_value=2) |
105
|
|
|
action.create = MagicMock( |
106
|
|
|
return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6") |
107
|
|
|
|
108
|
|
|
result = action.run("router2", |
109
|
|
|
"192.168.0.1", |
110
|
|
|
None, |
111
|
|
|
"snmpv2", |
112
|
|
|
"internal", |
113
|
|
|
"snmp") |
114
|
|
|
self.assertEqual(result, expected) |
115
|
|
|
|
116
|
|
|
def test_run_node_additonal_poller(self): |
117
|
|
|
expected = {'node_id': '6', |
118
|
|
|
'label': 'orion'} |
119
|
|
|
|
120
|
|
|
query_data = [self.query_no_results, |
121
|
|
|
self.query_no_results, |
122
|
|
|
{'results': [{'EngineID': 2}]}] |
123
|
|
|
|
124
|
|
|
action = self.get_action_instance(config=self.full_config) |
125
|
|
|
|
126
|
|
|
action.connect = MagicMock(return_value="orion") |
127
|
|
|
action.query = MagicMock(side_effect=query_data) |
128
|
|
|
action.invoke = MagicMock(return_value=None) |
129
|
|
|
action.create = MagicMock( |
130
|
|
|
return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6") |
131
|
|
|
|
132
|
|
|
result = action.run("router2", |
133
|
|
|
"192.168.0.1", |
134
|
|
|
"additonal1", |
135
|
|
|
"snmpv2", |
136
|
|
|
"internal", |
137
|
|
|
"snmp") |
138
|
|
|
self.assertEqual(result, expected) |
139
|
|
|
|