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

NcmExecuteScriptTestCase.test_run_is_instance()   A

Complexity

Conditions 1

Size

Total Lines 5

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 5
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 ncm_execute_script import NcmExecuteScript
21
22
__all__ = [
23
    'NcmExecuteScriptTestCase'
24
]
25
26
27
class NcmExecuteScriptTestCase(BaseActionTestCase):
28
    action_cls = NcmExecuteScript
29
30
    def test_run_no_config(self):
31
        self.assertRaises(ValueError,
32
                          NcmExecuteScript,
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, NcmExecuteScript)
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
                          "show failover")
54
55
    def test_run_node_not_found(self):
56
        action = self.get_action_instance(yaml.safe_load(
57
            self.get_fixture_content('full.yaml')))
58
59
        action.connect = MagicMock(return_value=True)
60
        action.query = MagicMock(return_value={'results': []})
61
62
        self.assertRaises(ValueError,
63
                          action.run,
64
                          "orion",
65
                          "router1",
66
                          "show failover")
67
68
    def test_run_exec_script(self):
69
        expected = {'job_id': "fake-job-id",
70
                    'transfer': {'DeviceOutput': 'show failover',
71
                                 'ErrorMessage': None,
72
                                 'RequestedReboot': False,
73
                                 'RequestedScript': 'show failover',
74
                                 'UserName': 'hubot',
75
                                 'status': 'Complete'}}
76
        query_data = []
77
        query_data.append(yaml.safe_load(
78
            self.get_fixture_content("orion_npm_results.yaml")))
79
        query_data.append(yaml.safe_load(
80
            self.get_fixture_content("orion_ncm_results.yaml")))
81
        query_data.append({'results': [{"Status": 2,
82
                                        "UserName": "hubot",
83
                                        "DeviceOutput": "show failover",
84
                                        "ErrorMessage": None,
85
                                        "RequestedScript": "show failover",
86
                                        "RequestedReboot": False}]})
87
        invoke_data = ["fake-job-id"]
88
89
        action = self.get_action_instance(yaml.safe_load(
90
            self.get_fixture_content('full.yaml')))
91
92
        action.connect = MagicMock(return_value=True)
93
        action.query = MagicMock(side_effect=query_data)
94
        action.invoke = MagicMock(return_value=invoke_data)
95
96
        result = action.run("orion",
97
                            "router1",
98
                            "show failover")
99
100
        self.assertEqual(result, expected)
101