Completed
Pull Request — master (#466)
by
unknown
02:20
created

OrionStatusTestCase.test_run_connect_fail()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 8
rs 9.4285
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 status import OrionStatus
21
22
__all__ = [
23
    'OrionStatusTestCase'
24
]
25
26
MOCK_CONFIG_BLANK = ""
27
28
MOCK_CONFIG_FULL = """
29
orion:
30
  host: orion-npm
31
  user: stanley
32
  password: foobar
33
"""
34
35
36
class OrionStatusTestCase(BaseActionTestCase):
37
    action_cls = OrionStatus
38
39
    def test_run_no_config(self):
40
        config = yaml.safe_load(MOCK_CONFIG_BLANK)
41
42
        self.assertRaises(ValueError, OrionStatus, config)
43
44
    def test_run_basic_config(self):
45
        config = yaml.safe_load(MOCK_CONFIG_FULL)
46
47
        action = self.get_action_instance(config)
48
        self.assertIsInstance(action, OrionStatus)
49
50
    def test_run_connect_fail(self):
51
        config = yaml.safe_load(MOCK_CONFIG_FULL)
52
53
        action = self.get_action_instance(config)
54
        action.connect = Mock(side_effect=ValueError(
55
            'Orion host details not in the config.yaml'))
56
57
        self.assertRaises(ValueError, action.run, "router1", "orion")
58
59
    def test_run_node_not_found(self):
60
        orion_data = {'results': []}
61
62
        config = yaml.safe_load(MOCK_CONFIG_FULL)
63
        action = self.get_action_instance(config)
64
        action.connect = MagicMock(return_value=True)
65
        action.query = MagicMock(return_value=orion_data)
66
67
        self.assertRaises(ValueError, action.run, "router1", "orion")
68
69
    def test_run_node_status_up(self):
70
        expected = {'status': "Up", 'color': "good"}
71
        orion_data = {'results': [{'Status': 1}]}
72
73
        config = yaml.safe_load(MOCK_CONFIG_FULL)
74
        action = self.get_action_instance(config)
75
        action.connect = MagicMock(return_value=True)
76
        action.query = MagicMock(return_value=orion_data)
77
        result = action.run("router1", "orion")
78
        self.assertEqual(result, expected)
79
80
    def test_run_node_status_down(self):
81
        expected = {'status': "Down", 'color': "danger"}
82
        orion_data = {'results': [{'Status': 2}]}
83
84
        config = yaml.safe_load(MOCK_CONFIG_FULL)
85
        action = self.get_action_instance(config)
86
        action.connect = MagicMock(return_value=True)
87
        action.query = MagicMock(return_value=orion_data)
88
        result = action.run("router1", "orion")
89
        self.assertEqual(result, expected)
90
91
    def test_run_node_status_unknown(self):
92
        expected = {'status': "Unknown", 'color': "grey"}
93
        orion_data = {'results': [{'Status': 0}]}
94
95
        config = yaml.safe_load(MOCK_CONFIG_FULL)
96
        action = self.get_action_instance(config)
97
        action.connect = MagicMock(return_value=True)
98
        action.query = MagicMock(return_value=orion_data)
99
        result = action.run("router1", "orion")
100
        self.assertEqual(result, expected)
101
102
    def test_run_node_status_warning(self):
103
        expected = {'status': "Warning", 'color': "warning"}
104
        orion_data = {'results': [{'Status': 3}]}
105
106
        config = yaml.safe_load(MOCK_CONFIG_FULL)
107
        action = self.get_action_instance(config)
108
        action.connect = MagicMock(return_value=True)
109
        action.query = MagicMock(return_value=orion_data)
110
        result = action.run("router1", "orion")
111
        self.assertEqual(result, expected)
112