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

NodeUnmanageTestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 106
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_run_unmanaged() 0 19 1
A test_run_connect_fail() 0 12 1
A test_run_invoke_returns_text() 0 21 1
A test_run_is_instance() 0 5 1
A test_run_no_config() 0 5 1
A test_run_unmanage_too_long() 0 19 1
A test_run_node_not_found() 0 16 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
import yaml
16
from mock import Mock, MagicMock
17
18
from st2tests.base import BaseActionTestCase
19
20
from node_unmanage import NodeUnmanage
21
22
__all__ = [
23
    'NodeUnmanageTestCase'
24
]
25
26
27
class NodeUnmanageTestCase(BaseActionTestCase):
28
    action_cls = NodeUnmanage
29
30
    def test_run_no_config(self):
31
        self.assertRaises(ValueError,
32
                          NodeUnmanage,
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(yaml.safe_load(
38
            self.get_fixture_content('full.yaml')))
39
40
        self.assertIsInstance(action, NodeUnmanage)
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
                          "orion",
52
                          "router1",
53
                          30)
54
55
    def test_run_node_not_found(self):
56
        query_data = []
57
        query_data.append({'results': []})
58
        query_data.append({'results': []})
59
60
        action = self.get_action_instance(yaml.safe_load(
61
            self.get_fixture_content('full.yaml')))
62
63
        action.connect = MagicMock(return_value=True)
64
        action.query = MagicMock(side_effect=query_data)
65
66
        self.assertRaises(ValueError,
67
                          action.run,
68
                          "orion",
69
                          "router1",
70
                          30)
71
72
    def test_run_unmanaged(self):
73
        query_data = []
74
        query_data.append(yaml.safe_load(
75
            self.get_fixture_content("orion_npm_results.yaml")))
76
        query_data.append(yaml.safe_load(
77
            self.get_fixture_content("orion_ncm_results.yaml")))
78
79
        action = self.get_action_instance(yaml.safe_load(
80
            self.get_fixture_content('full.yaml')))
81
82
        action.connect = MagicMock(return_value=True)
83
        action.query = MagicMock(side_effect=query_data)
84
        action.invoke = Mock(return_value=None)
85
86
        result = action.run("router1",
87
                            "orion",
88
                            30)
89
90
        self.assertTrue(result)
91
92
    def test_run_invoke_returns_text(self):
93
        expected = "fake"
94
95
        query_data = []
96
        query_data.append(yaml.safe_load(
97
            self.get_fixture_content("orion_npm_results.yaml")))
98
        query_data.append(yaml.safe_load(
99
            self.get_fixture_content("orion_ncm_results.yaml")))
100
101
        action = self.get_action_instance(yaml.safe_load(
102
            self.get_fixture_content('full.yaml')))
103
104
        action.connect = MagicMock(return_value=True)
105
        action.query = MagicMock(side_effect=query_data)
106
        action.invoke = MagicMock(return_value="fake")
107
108
        result = action.run("router1",
109
                            "orion",
110
                            30)
111
112
        self.assertEqual(result, expected)
113
114
    def test_run_unmanage_too_long(self):
115
        query_data = []
116
        query_data.append(yaml.safe_load(
117
            self.get_fixture_content("orion_npm_results.yaml")))
118
        query_data.append(yaml.safe_load(
119
            self.get_fixture_content("orion_ncm_results.yaml")))
120
121
        action = self.get_action_instance(yaml.safe_load(
122
            self.get_fixture_content('full.yaml')))
123
124
        action.connect = Mock(return_value=True)
125
        action.query = MagicMock(side_effect=query_data)
126
        action.invoke = Mock(side_effect=True)
127
128
        self.assertRaises(ValueError,
129
                          action.run,
130
                          "orion",
131
                          "router1",
132
                          90)
133