Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

tests/unit/test_winrm_ps_script_runner.py (1 issue)

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
# limitations under the License.
15
16
from __future__ import absolute_import
17
18
import mock
19
import os.path
20
from st2common.runners.base import ActionRunner
21
from st2tests.base import RunnerTestCase
22
from winrm_runner import winrm_ps_script_runner
23
from winrm_runner.winrm_base import WinRmBaseRunner
24
25
FIXTURES_PATH = os.path.join(os.path.dirname(__file__), 'fixtures')
26
27
POWERSHELL_SCRIPT_PATH = os.path.join(FIXTURES_PATH, "TestScript.ps1")
28
29
30
class WinRmPsScriptRunnerTestCase(RunnerTestCase):
31
32
    def setUp(self):
33
        super(WinRmPsScriptRunnerTestCase, self).setUpClass()
34
        self._runner = winrm_ps_script_runner.get_runner()
35
36
    def test_init(self):
37
        runner = winrm_ps_script_runner.WinRmPsScriptRunner('abcdef')
38
        self.assertIsInstance(runner, WinRmBaseRunner)
39
        self.assertIsInstance(runner, ActionRunner)
40
        self.assertEquals(runner.runner_id, 'abcdef')
41
42
    @mock.patch('winrm_runner.winrm_ps_script_runner.WinRmPsScriptRunner._get_script_args')
43
    @mock.patch('winrm_runner.winrm_ps_script_runner.WinRmPsScriptRunner.run_ps')
44
    def test_run(self, mock_run_ps, mock_get_script_args):
45
        mock_run_ps.return_value = 'expected'
46
        pos_args = [1, 'abc']
47
        named_args = {"d": {"test": ["\r", True, 3]}}
48
        mock_get_script_args.return_value = (pos_args, named_args)
49
50
        self._runner.entry_point = POWERSHELL_SCRIPT_PATH
51
        self._runner.runner_parameters = {}
52
        self._runner._kwarg_op = '-'
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _kwarg_op was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
53
54
        result = self._runner.run({})
55
56
        self.assertEquals(result, 'expected')
57
        mock_run_ps.assert_called_with('''& {[CmdletBinding()]
58
Param(
59
  [bool]$p_bool,
60
  [int]$p_integer,
61
  [double]$p_number,
62
  [string]$p_str,
63
  [array]$p_array,
64
  [hashtable]$p_obj,
65
  [Parameter(Position=0)]
66
  [string]$p_pos0,
67
  [Parameter(Position=1)]
68
  [string]$p_pos1
69
)
70
71
72
Write-Output "p_bool = $p_bool"
73
Write-Output "p_integer = $p_integer"
74
Write-Output "p_number = $p_number"
75
Write-Output "p_str = $p_str"
76
Write-Output "p_array = $($p_array | ConvertTo-Json -Compress)"
77
Write-Output "p_obj = $($p_obj | ConvertTo-Json -Compress)"
78
Write-Output "p_pos0 = $p_pos0"
79
Write-Output "p_pos1 = $p_pos1"
80
} -d @{"test" = @("`r", $true, 3)} 1 "abc"''')
81