File: which_test.py

package info (click to toggle)
salt 2014.1.13%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,572 kB
  • ctags: 9,221
  • sloc: python: 155,066; sh: 4,692; xml: 462; makefile: 197
file content (122 lines) | stat: -rw-r--r-- 5,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-

# Import python libs
import os

# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch
ensure_in_syspath('../../')

# Import salt libs
import integration
import salt.utils


@skipIf(NO_MOCK, NO_MOCK_REASON)
class TestWhich(integration.TestCase):
    '''
    Tests salt.utils.which function to ensure that it returns True as
    expected.
    '''

    # The mock patch bellow will make sure that ALL calls to the which function
    # returns None
    @patch('salt.utils.which', lambda exe: None)
    def test_missing_binary_in_linux(self):
        self.assertTrue(
            salt.utils.which('this-binary-does-not-exist') is None
        )

    # The mock patch bellow will make sure that ALL calls to the which function
    # return whatever is sent to it
    @patch('salt.utils.which', lambda exe: exe)
    def test_existing_binary_in_linux(self):
        self.assertTrue(salt.utils.which('this-binary-exists-under-linux'))

    # The mock patch bellow, since we're not providing the return value, we
    # will be able to tweak it within the test case. The testcase MUST accept
    # an arguemnt which is the MagicMock'ed object
    @patch('os.access')
    def test_existing_binary_in_windows(self, osaccess):
        # We define the side_effect attribute on the mocked object in order to
        # specify which calls return which values. First call to os.access
        # returns X, the second Y, the third Z, etc...
        osaccess.side_effect = [
            # The first os.access should return False(the abspath one)
            False,
            # The second, iterating through $PATH, should also return False,
            # still checking for Linux
            False,
            # Lastly return True, this is the windows check.
            True
        ]
        # Let's patch os.environ to provide a custom PATH variable
        with patch.dict(os.environ, {'PATH': '/bin'}):
            # Let's also patch is_windows to return True
            with patch('salt.utils.is_windows', lambda: True):
                self.assertEqual(
                    salt.utils.which('this-binary-exists-under-windows'),
                    # The returned path should return the .exe suffix
                    '/bin/this-binary-exists-under-windows.EXE'
                )

    @patch('os.access')
    def test_missing_binary_in_windows(self, osaccess):
        osaccess.side_effect = [
            # The first os.access should return False(the abspath one)
            False,
            # The second, iterating through $PATH, should also return False,
            # still checking for Linux
            False,
            # Lastly return True, this is the windows check.
            True
        ]
        # Let's patch os.environ to provide a custom PATH variable
        with patch.dict(os.environ, {'PATH': '/bin'}):
            # Let's also patch is_widows to return True
            with patch('salt.utils.is_windows', lambda: True):
                self.assertEqual(
                    # Since we're passing the .exe suffix, the last True above
                    # will not matter. The result will be None
                    salt.utils.which('this-binary-is-missing-in-windows.exe'),
                    None
                )

    # The mock patch bellow, since we're not providing the return value, we
    # will be able to tweak it within the test case. The testcase MUST accept
    # an arguemnt which is the MagicMock'ed object
    @patch('os.access')
    def test_existing_binary_in_windows_pathext(self, osaccess):
        # We define the side_effect attribute on the mocked object in order to
        # specify which calls return which values. First call to os.access
        # returns X, the second Y, the third Z, etc...
        osaccess.side_effect = [
            # The first os.access should return False(the abspath one)
            False,
            # The second, iterating through $PATH, should also return False,
            # still checking for Linux
            False,
            # We will now also return False 3 times so we get a .CMD back from
            # the function, see PATHEXT below.
            # Lastly return True, this is the windows check.
            False, False, False,
            True
        ]
        # Let's patch os.environ to provide a custom PATH variable
        with patch.dict(os.environ, {'PATH': '/bin',
                                     'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;'
                                     '.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY'}):
            # Let's also patch is_windows to return True
            with patch('salt.utils.is_windows', lambda: True):
                self.assertEqual(
                    salt.utils.which('this-binary-exists-under-windows'),
                    # The returned path should return the .exe suffix
                    '/bin/this-binary-exists-under-windows.CMD'
                )


if __name__ == '__main__':
    from integration import run_tests
    run_tests(TestWhich, needs_daemon=False)