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

NodePollNowTestCase.test_run_connect_fail()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 11
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 node_pollnow import NodePollNow
21
22
__all__ = [
23
    'NodePollNowTestCase'
24
]
25
26
27
class NodePollNowTestCase(BaseActionTestCase):
28
    action_cls = NodePollNow
29
30
    def test_run_no_config(self):
31
        self.assertRaises(ValueError,
32
                          NodePollNow,
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, NodePollNow)
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
54
    def test_run_node_not_exist(self):
55
        query_data = [{'results': []}]
56
57
        action = self.get_action_instance(yaml.safe_load(
58
            self.get_fixture_content('full.yaml')))
59
60
        action.connect = MagicMock(return_value=True)
61
        action.query = MagicMock(side_effect=query_data)
62
63
        self.assertRaises(ValueError,
64
                          action.run,
65
                          "orion",
66
                          "router1")
67
68
    def test_run_polled(self):
69
        query_data = []
70
        query_data.append(yaml.safe_load(
71
            self.get_fixture_content("orion_npm_results.yaml")))
72
        query_data.append(yaml.safe_load(
73
            self.get_fixture_content("orion_ncm_results.yaml")))
74
75
        action = self.get_action_instance(yaml.safe_load(
76
            self.get_fixture_content('full.yaml')))
77
78
        action.connect = MagicMock(return_value=True)
79
        action.query = MagicMock(side_effect=query_data)
80
        action.invoke = Mock(return_value=None)
81
82
        result = action.run("router1", "orion")
83
84
        self.assertTrue(result)
85
86
    def test_run_polled_text(self):
87
        expected = "fake"
88
89
        query_data = []
90
        query_data.append(yaml.safe_load(
91
            self.get_fixture_content("orion_npm_results.yaml")))
92
        query_data.append(yaml.safe_load(
93
            self.get_fixture_content("orion_ncm_results.yaml")))
94
95
        action = self.get_action_instance(yaml.safe_load(
96
            self.get_fixture_content('full.yaml')))
97
98
        action.connect = MagicMock(return_value=True)
99
        action.query = MagicMock(side_effect=query_data)
100
        action.invoke = Mock(return_value="fake")
101
102
        result = action.run("router1", "orion")
103
104
        self.assertEqual(result, expected)
105