File: test_manager.py

package info (click to toggle)
rally 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,368 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (369 lines) | stat: -rw-r--r-- 15,834 bytes parent folder | download | duplicates (4)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import os
import sys
from unittest import mock

from rally import exceptions
from rally.verification import manager
from tests.unit import test


DEFAULT_REPO = "https://git.example.com"
DEFAULT_VERSION = 3.14159


@manager.configure("some_fake_verifier", default_repo=DEFAULT_REPO,
                   default_version=DEFAULT_VERSION)
class FakeVerifier(manager.VerifierManager):

    @classmethod
    def _get_default_meta(cls):
        return {"fake_key1": "fake_value"}

    def run(self, verification, pattern=None, load_list=None, skip_list=None,
            xfail_list=None, concurrency=None, failed=False, extra_args=None):
        pass

    def list_tests(self, pattern=""):
        pass


class VerifierManagerTestCase(test.TestCase):
    def setUp(self):
        super(VerifierManagerTestCase, self).setUp()
        check_output_p = mock.patch("rally.verification.manager.utils."
                                    "check_output")
        self.check_output = check_output_p.start()
        self.addCleanup(check_output_p.stop)

    @mock.patch.dict(os.environ, values={"PATH": ""}, clear=True)
    def test_environ(self):
        verifier = mock.Mock(system_wide=False)
        vmanager = FakeVerifier(verifier)

        self.assertEqual({"PATH": "%s/bin:" % vmanager.venv_dir,
                          "VIRTUAL_ENV": vmanager.venv_dir},
                         vmanager.environ)

        verifier.system_wide = True
        self.assertEqual({"PATH": ""}, vmanager.environ)

    @mock.patch("rally.verification.manager.VerifierManager.validate_args")
    @mock.patch("rally.verification.context.ContextManager.validate")
    def test_validate(self, mock_context_manager_validate, mock_validate_args):
        fvmanager = FakeVerifier(mock.Mock())
        args = mock.Mock()
        with mock.patch.object(FakeVerifier, "_meta_get") as mock__meta_get:
            fvmanager.validate(args)
            mock__meta_get.assert_called_once_with("context")
            mock_validate_args.assert_called_once_with(args)
            mock_context_manager_validate.assert_called_once_with(
                mock__meta_get.return_value)

    @mock.patch("rally.verification.manager.os.path.exists",
                side_effect=[False, True])
    def test__clone(self, mock_exists):
        verifier = mock.Mock(version=None)
        vmanager = FakeVerifier(verifier)

        # Check source validation
        verifier.source = "some_source"
        e = self.assertRaises(exceptions.RallyException, vmanager._clone)
        self.assertEqual("Source path 'some_source' is not valid.", "%s" % e)

        verifier.source = None

        # Version to switch repo is provided
        verifier.version = "1.0.0"
        vmanager._clone()
        self.assertEqual(
            [mock.call(["git", "clone", DEFAULT_REPO,
                        vmanager.repo_dir, "-b", DEFAULT_VERSION]),
             mock.call(["git", "checkout", "1.0.0"], cwd=vmanager.repo_dir)],
            self.check_output.call_args_list)
        verifier.update_properties.assert_not_called()

        # Version to switch repo is not provided
        verifier.version = None
        self.check_output.side_effect = [
            "Output from cloning", "heads/master", "Output from cloning",
            "0.1.0-72-g4a39bd4", "4a39bd4qwerty12345", "Output from cloning",
            "2.0.0", "12345qwerty4a39bd4"]

        # Case 1: verifier is switched to a branch
        self.check_output.reset_mock()
        verifier.update_properties.reset_mock()
        vmanager._clone()
        self.assertEqual(
            [mock.call(["git", "clone", DEFAULT_REPO,
                        vmanager.repo_dir, "-b", DEFAULT_VERSION]),
             mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir)],
            self.check_output.call_args_list)
        verifier.update_properties.assert_called_once_with(version="master")

        # Case 2: verifier is switched to a commit ID
        self.check_output.reset_mock()
        verifier.update_properties.reset_mock()
        vmanager._clone()
        self.assertEqual(
            [mock.call(["git", "clone", DEFAULT_REPO,
                        vmanager.repo_dir, "-b", DEFAULT_VERSION]),
             mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir),
             mock.call(["git", "rev-parse", "HEAD"], cwd=vmanager.repo_dir)],
            self.check_output.call_args_list)
        verifier.update_properties.assert_called_once_with(
            version="4a39bd4qwerty12345")

        # Case 3: verifier is switched to a tag
        self.check_output.reset_mock()
        verifier.update_properties.reset_mock()
        vmanager._clone()
        self.assertEqual(
            [mock.call(["git", "clone", DEFAULT_REPO,
                        vmanager.repo_dir, "-b", DEFAULT_VERSION]),
             mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir),
             mock.call(["git", "rev-parse", "HEAD"], cwd=vmanager.repo_dir)],
            self.check_output.call_args_list)
        verifier.update_properties.assert_called_once_with(version="2.0.0")

    @mock.patch("rally.verification.manager.VerifierManager.install_venv")
    @mock.patch("rally.verification.manager.VerifierManager.check_system_wide")
    @mock.patch("rally.verification.manager.VerifierManager._clone")
    @mock.patch("rally.verification.utils.create_dir")
    def test_install(self, mock_create_dir, mock__clone,
                     mock_check_system_wide, mock_install_venv):
        verifier = mock.Mock()
        vmanager = FakeVerifier(verifier)

        # venv case
        verifier.system_wide = False

        vmanager.install()

        mock__clone.assert_called_once_with()
        self.assertFalse(mock_check_system_wide.called)
        mock_install_venv.assert_called_once_with()

        # system-wide case
        mock__clone.reset_mock()
        mock_check_system_wide.reset_mock()
        mock_install_venv.reset_mock()
        verifier.system_wide = True

        vmanager.install()

        mock__clone.assert_called_once_with()
        mock_check_system_wide.assert_called_once_with()
        self.assertFalse(mock_install_venv.called)

    @mock.patch("rally.verification.manager.shutil.rmtree")
    @mock.patch("rally.verification.manager.os.path.exists", return_value=True)
    def test_uninstall(self, mock_exists, mock_rmtree):
        vmanager = FakeVerifier(mock.MagicMock())

        vmanager.uninstall()
        mock_exists.assert_called_once_with(vmanager.home_dir)
        mock_rmtree.assert_called_once_with(vmanager.home_dir)

        mock_exists.reset_mock()
        mock_rmtree.reset_mock()

        vmanager.uninstall(full=True)
        mock_exists.assert_called_once_with(vmanager.base_dir)
        mock_rmtree.assert_called_once_with(vmanager.base_dir)

    @mock.patch("rally.verification.manager.shutil.rmtree")
    @mock.patch("rally.verification.manager.os.path.exists")
    def test_install_venv(self, mock_exists, mock_rmtree):
        mock_exists.return_value = False
        vmanager = FakeVerifier(mock.Mock())

        vmanager.install_venv()
        self.assertEqual(
            [mock.call(["virtualenv", "-p", sys.executable, vmanager.venv_dir],
                       cwd=vmanager.repo_dir,
                       msg_on_err="Failed to initialize virtual env in %s "
                                  "directory." % vmanager.venv_dir),
             mock.call(["pip", "install", "-e", "./"],
                       cwd=vmanager.repo_dir, env=vmanager.environ)
             ],
            self.check_output.call_args_list)
        self.assertFalse(mock_rmtree.called)

        # case: venv was created previously
        mock_exists.return_value = True
        self.check_output.reset_mock()

        vmanager.install_venv()

        self.assertEqual(
            [mock.call(["virtualenv", "-p", sys.executable, vmanager.venv_dir],
                       cwd=vmanager.repo_dir,
                       msg_on_err="Failed to initialize virtual env in %s "
                                  "directory." % vmanager.venv_dir),
             mock.call(["pip", "install", "-e", "./"],
                       cwd=vmanager.repo_dir, env=vmanager.environ)
             ],
            self.check_output.call_args_list)
        mock_rmtree.assert_called_once_with(vmanager.venv_dir)

    @mock.patch("rally.verification.manager.open")
    def test_check_system_wide(self, mock_open):
        r_file = mock_open.return_value.__enter__.return_value
        r_file.read.return_value = "\n#comment\nrequests>1.2   # Licence\n"

        vmanager = FakeVerifier(mock.Mock())

        vmanager.check_system_wide()
        mock_open.assert_called_once_with(
            "%s/requirements.txt" % vmanager.repo_dir)
        mock_open.reset_mock()

        # failure
        r_file.read.return_value = "\n#comment\nNumPy>1.2   # Licence\n"
        e = self.assertRaises(manager.VerifierSetupFailure,
                              vmanager.check_system_wide)
        self.assertIn("NumPy>1.2", "%s" % e)

    def test_checkout(self):
        vmanager = FakeVerifier(mock.Mock())
        version = "3.14159"

        vmanager.checkout(version)

        self.assertEqual(
            [mock.call(["git", "checkout", "master"], cwd=vmanager.repo_dir),
             mock.call(["git", "remote", "update"], cwd=vmanager.repo_dir),
             mock.call(["git", "pull"], cwd=vmanager.repo_dir),
             mock.call(["git", "checkout", version], cwd=vmanager.repo_dir)],
            self.check_output.call_args_list)

    def test_configure(self):
        vmanager = FakeVerifier(mock.Mock())
        self.assertRaises(NotImplementedError, vmanager.configure,
                          extra_options={"key": "value"})

    def test_is_configured(self):
        vmanager = FakeVerifier(mock.Mock())
        self.assertTrue(vmanager.is_configured())

    def test_override_configuration(self):
        # coverage should be 100%...
        self.assertRaises(NotImplementedError,
                          FakeVerifier(mock.Mock()).override_configuration,
                          "something")

    def test_extend_configuration(self):
        # coverage should be 100%...
        self.assertRaises(NotImplementedError,
                          FakeVerifier(mock.Mock()).extend_configuration,
                          "something")

    def test_get_configuration(self):
        self.assertEqual("", FakeVerifier(mock.Mock()).get_configuration())

    def test_install_extension(self):
        # coverage should be 100%...
        self.assertRaises(NotImplementedError,
                          FakeVerifier(mock.Mock()).install_extension,
                          "source")

    def test_list_extensions(self):
        self.assertEqual([], FakeVerifier(mock.Mock()).list_extensions())

    def test_uninstall_extension(self):
        # coverage should be 100%...
        self.assertRaises(NotImplementedError,
                          FakeVerifier(mock.Mock()).uninstall_extension,
                          "name")

    @mock.patch("rally.verification.manager.io.StringIO")
    @mock.patch("rally.verification.manager.subunit_v2")
    def test_parse_results(self, mock_subunit_v2, mock_string_io):
        data = "123123"
        self.assertEqual(mock_subunit_v2.parse.return_value,
                         FakeVerifier(mock.Mock()).parse_results(data))
        mock_subunit_v2.parse.assert_called_once_with(
            mock_string_io.return_value)
        mock_string_io.assert_called_once_with(data)

    def test_validate_args(self):
        # validating "pattern" argument
        fvmanager = FakeVerifier(mock.Mock())
        fvmanager.validate_args({"pattern": "it is string"})
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args, {"pattern": 2})
        self.assertEqual("'pattern' argument should be a string.",
                         e.kwargs["message"])

        # validating "concurrency" argument
        fvmanager.validate_args({"concurrency": 1})
        fvmanager.validate_args({"concurrency": 5})
        fvmanager.validate_args({"concurrency": 0})
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args, {"concurrency": -1})
        self.assertEqual("'concurrency' argument should be a positive integer "
                         "or zero.", e.kwargs["message"])
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args, {"concurrency": "bla"})
        self.assertEqual("'concurrency' argument should be a positive integer "
                         "or zero.", e.kwargs["message"])

        # validating "load_list" argument
        fvmanager.validate_args({"load_list": []})
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args, {"load_list": "str"})
        self.assertEqual("'load_list' argument should be a list of tests.",
                         e.kwargs["message"])

        # validating "skip_list" argument
        fvmanager.validate_args({"skip_list": {}})
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args, {"skip_list": "str"})
        self.assertEqual("'skip_list' argument should be a dict of tests where"
                         " keys are test names and values are reasons.",
                         e.kwargs["message"])

        # validating "xfail_list" argument
        fvmanager.validate_args({"xfail_list": {}})
        e = self.assertRaises(exceptions.ValidationError,
                              fvmanager.validate_args,
                              {"xfail_list": "str"})
        self.assertEqual("'xfail_list' argument should be a dict of tests "
                         "where keys are test names and values are reasons.",
                         e.kwargs["message"])

    def test__get_doc(self):
        self.assertEqual(
            "\n"
            "**Running arguments**:\n\n"
            "* *concurrency*: Number of processes to be used for launching "
            "tests. In case of 0 value, number of processes will be equal to "
            "number of CPU cores.\n"
            "* *load_list*: a list of tests to launch.\n"
            "* *pattern*: a regular expression of tests to launch.\n"
            "* *skip_list*: a list of tests to skip (actually, it is a dict "
            "where keys are names of tests, values are reasons).\n"
            "* *xfail_list*: a list of tests that are expected to fail "
            "(actually, it is a dict where keys are names of tests, values "
            "are reasons).\n\n"
            "**Installation arguments**:\n\n"
            "* *system_wide*: Whether or not to use the system-wide "
            "environment for verifier instead of a virtual environment. "
            "Defaults to False.\n"
            "* *source*: Path or URL to the repo to clone verifier from. "
            "Defaults to https://git.example.com\n"
            "* *version*: Branch, tag or commit ID to checkout before "
            "verifier installation. Defaults to '%s'.\n" % DEFAULT_VERSION,
            FakeVerifier._get_doc())