Completed
Pull Request — master (#487)
by
unknown
03:03
created

NodeCreateTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 111
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_run_node_caption_exists() 0 12 1
B test_run_node_additonal_poller() 0 24 1
A test_run_poller_is_none() 0 23 1
A test_run_node_ip_exists() 0 23 1
A test_run_is_ip_v4() 0 8 1
A test_run_connect_fail() 0 12 1
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
                          "orion",
48
                          None,
49
                          "snmpv2",
50
                          "internal",
51
                          None,
52
                          "snmp")
53
54
    def test_run_node_caption_exists(self):
55
        action = self.setup_node_exists()
56
        self.assertRaises(ValueError,
57
                          action.run,
58
                          "router1",
59
                          "192.168.0.1",
60
                          "orion",
61
                          None,
62
                          "snmpv2",
63
                          "internal",
64
                          None,
65
                          "snmp")
66
67
    def test_run_node_ip_exists(self):
68
        query_data = []
69
        query_data.append(self.query_no_results)
70
        query_data.append(self.query_npm_node)
71
        query_data.append(self.query_ncm_node)
72
73
        action = self.get_action_instance(config=self.full_config)
74
75
        action.connect = MagicMock(return_value=True)
76
        action.query = MagicMock(side_effect=query_data)
77
        action.invoke = MagicMock(return_value=None)
78
        action.create = MagicMock(return_value=None)
79
80
        self.assertRaises(ValueError,
81
                          action.run,
82
                          "router2",
83
                          "192.168.0.1",
84
                          "orion",
85
                          None,
86
                          "snmpv2",
87
                          "internal",
88
                          None,
89
                          "snmp")
90
91
    def test_run_poller_is_none(self):
92
        expected = {'node_id': '6', 'platform': 'orion'}
93
94
        query_data = self.query_no_results
95
96
        action = self.get_action_instance(config=self.full_config)
97
98
        action.connect = MagicMock(return_value=True)
99
        action.query = MagicMock(return_value=query_data)
100
        action.invoke = MagicMock(return_value=None)
101
        action.get_engine_id = MagicMock(return_value=2)
102
        action.create = MagicMock(
103
            return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6")
104
105
        result = action.run("router2",
106
                            "192.168.0.1",
107
                            "orion",
108
                            None,
109
                            "snmpv2",
110
                            "internal",
111
                            None,
112
                            "snmp")
113
        self.assertEqual(result, expected)
114
115
    def test_run_node_additonal_poller(self):
116
        expected = {'node_id': '6', 'platform': 'orion'}
117
118
        query_data = [self.query_no_results,
119
                      self.query_no_results,
120
                      {'results': [{'EngineID': 2}]}]
121
122
        action = self.get_action_instance(config=self.full_config)
123
124
        action.connect = MagicMock(return_value=True)
125
        action.query = MagicMock(side_effect=query_data)
126
        action.invoke = MagicMock(return_value=None)
127
        action.create = MagicMock(
128
            return_value="swis://orionr/Orion/Orion.Nodes/NodeID=6")
129
130
        result = action.run("router2",
131
                            "192.168.0.1",
132
                            "orion",
133
                            "additonal1",
134
                            "snmpv2",
135
                            "internal",
136
                            None,
137
                            "snmp")
138
        self.assertEqual(result, expected)
139