test_cmd_validator_errors()   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 28
rs 8.5806
1
import os
2
import pytest
3
import groundwork
4
from groundwork_validation.patterns import GwCmdValidatorsPattern
5
from groundwork_validation.patterns.gw_cmd_validators_pattern.gw_cmd_validators_pattern \
6
    import NotAllowedReturnCode, CommandTimeoutExpired
7
8
9
def test_cmd_validator_init():
10
    """
11
    .. test:: GwCmdValidators Init test
12
       :tags: gwcmdvalidators
13
    """
14
    class My_Plugin(GwCmdValidatorsPattern):
15
        def __init__(self, app, **kwargs):
16
            self.name = "My_Plugin"
17
            super(My_Plugin, self).__init__(app, **kwargs)
18
19
        def activate(self):
20
            pass
21
22
        def deactivate(self):
23
            pass
24
25
    app = groundwork.App()
26
    plugin = My_Plugin(app)
27
    plugin.activate()
28
29
30 View Code Duplication
def test_cmd_validator_search(tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
    """
32
    .. test:: GwCmdValidators search test
33
       :tags: gwcmdvalidators
34
       :links: S_102F8
35
    """
36
    # Creating temporay folder with sub_folders
37
    tmpdir.mkdir("sub_a")
38
    tmpdir.mkdir("sub_b")
39
    tmpdir.mkdir("sub_c")
40
41
    # Change working directory to temporary folder
42
    old_cwd = os.getcwd()
43
    os.chdir(str(tmpdir))
44
45
    class My_Plugin(GwCmdValidatorsPattern):
46
        def __init__(self, app, **kwargs):
47
            self.name = "My_Plugin"
48
            super(My_Plugin, self).__init__(app, **kwargs)
49
50
        def activate(self):
51
            pass
52
53
        def deactivate(self):
54
            pass
55
56
    app = groundwork.App()
57
    plugin = My_Plugin(app)
58
    plugin.activate()
59
60
    assert plugin.validators.cmd.validate("dir", search="sub_a") is True
61
    assert plugin.validators.cmd.validate("dir", search="NO_KNOWN_FOLDER") is False
62
63
    # Let's change back the working dir, maybe this isn't done pytest itself after each test
64
    os.chdir(old_cwd)
65
66
67 View Code Duplication
def test_cmd_validator_regex(tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
68
    """
69
    .. test:: GwCmdValidators search regex test
70
       :tags: gwcmdvalidators
71
    """
72
73
    # Creating temporay folder with sub_folders
74
    tmpdir.mkdir("sub_a")
75
    tmpdir.mkdir("sub_b")
76
    tmpdir.mkdir("sub_c")
77
78
    # Change working directory to temporary folder
79
    old_cwd = os.getcwd()
80
    os.chdir(str(tmpdir))
81
82
    class My_Plugin(GwCmdValidatorsPattern):
83
        def __init__(self, app, **kwargs):
84
            self.name = "My_Plugin"
85
            super(My_Plugin, self).__init__(app, **kwargs)
86
87
        def activate(self):
88
            pass
89
90
        def deactivate(self):
91
            pass
92
93
    app = groundwork.App()
94
    plugin = My_Plugin(app)
95
    plugin.activate()
96
97
    assert plugin.validators.cmd.validate("dir", regex="sub*") is True
98
    assert plugin.validators.cmd.validate("dir", regex="sub_NO*") is False
99
100
    # Let's change back the working dir, maybe this isn't done pytest itself after each test
101
    os.chdir(old_cwd)
102
103
104
def test_cmd_validator_return_codes():
105
    """
106
    .. test:: GwCmdValidators return code test
107
       :tags: gwcmdvalidators
108
       :links: S_EB190
109
    """
110
    class My_Plugin(GwCmdValidatorsPattern):
111
        def __init__(self, app, **kwargs):
112
            self.name = "My_Plugin"
113
            super(My_Plugin, self).__init__(app, **kwargs)
114
115
        def activate(self):
116
            pass
117
118
        def deactivate(self):
119
            pass
120
121
    app = groundwork.App()
122
    plugin = My_Plugin(app)
123
    plugin.activate()
124
125
    plugin.validators.cmd.validate("dir", search="test", allowed_return_codes=0)
126
    plugin.validators.cmd.validate("dir", search="test", allowed_return_codes=[0])
127
    plugin.validators.cmd.validate("dir", search="test", allowed_return_codes=[1, 2, 3, 0])
128
    plugin.validators.cmd.validate("exit 2", search="test", allowed_return_codes=[2])
129
130
    with pytest.raises(NotAllowedReturnCode):
131
        plugin.validators.cmd.validate("UNKNOWN_COMMAND", search="test", allowed_return_codes=[0])
132
133
    with pytest.raises(NotAllowedReturnCode):
134
        plugin.validators.cmd.validate("UNKNOWN_COMMAND", search="test", allowed_return_codes=0)
135
136
137
def test_cmd_validator_errors():
138
    """
139
        .. test:: GwCmdValidators errors test
140
           :tags: gwcmdvalidators
141
    """
142
    class My_Plugin(GwCmdValidatorsPattern):
143
        def __init__(self, app, **kwargs):
144
            self.name = "My_Plugin"
145
            super(My_Plugin, self).__init__(app, **kwargs)
146
147
        def activate(self):
148
            pass
149
150
        def deactivate(self):
151
            pass
152
153
    app = groundwork.App()
154
    plugin = My_Plugin(app)
155
    plugin.activate()
156
157
    with pytest.raises(ValueError):
158
        plugin.validators.cmd.validate("dir")
159
160
    with pytest.raises(ValueError):
161
        plugin.validators.cmd.validate("dir", search="test", regex="test")
162
163
    with pytest.raises(TypeError):
164
        plugin.validators.cmd.validate("dir", search="test", allowed_return_codes="123")
165
166
167
def test_cmd_validator_timeout():
168
    """
169
    .. test:: GwCmdValidators timeout test
170
       :tags: gwcmdvalidators
171
       :links: S_8C1D8
172
    """
173
    class My_Plugin(GwCmdValidatorsPattern):
174
        def __init__(self, app, **kwargs):
175
            self.name = "My_Plugin"
176
            super(My_Plugin, self).__init__(app, **kwargs)
177
178
        def activate(self):
179
            pass
180
181
        def deactivate(self):
182
            pass
183
184
    app = groundwork.App()
185
    plugin = My_Plugin(app)
186
    plugin.activate()
187
188
    plugin.validators.cmd.validate(_sleep(1), search="")
189
    with pytest.raises(CommandTimeoutExpired):
190
        plugin.validators.cmd.validate(_sleep(3), search="")
191
192
    plugin.validators.cmd.validate(_sleep(1), search="", timeout=2)
193
    with pytest.raises(CommandTimeoutExpired):
194
        plugin.validators.cmd.validate(_sleep(1), search="", timeout=0.5)
195
196
197
def _sleep(seconds):
198
    """
199
    Helper functions, which generates a sleep like command depending on which operating system
200
    theses tests are running.
201
    :param seconds:
202
    :return:
203
    """
204
    if os.name == 'nt':
205
        command = "ping 127.0.0.1 -n %s " % seconds + 1
206
    else:
207
        command = "sleep %s" % seconds
208
209
    return command
210