File: test_slave.py

package info (click to toggle)
python-molotov 2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,264 kB
  • sloc: python: 4,121; makefile: 60
file content (61 lines) | stat: -rw-r--r-- 1,985 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
import os
import subprocess
import tempfile
from shutil import copyfile, copytree
from unittest import mock

import pytest

from molotov import __version__
from molotov.slave import main
from molotov.tests.support import TestLoop, dedicatedloop, set_args

_REPO = "https://github.com/loads/molotov"
NO_INTERNET = os.environ.get("NO_INTERNET") is not None
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
CALLS = [0]


def check_call(cmd, *args, **kw):
    if CALLS[0] == 3:
        return
    if not cmd.startswith("git clone"):
        subprocess.check_call(cmd, *args, **kw)
    CALLS[0] += 1


@pytest.mark.skipif(NO_INTERNET, reason="This test requires internet access")
class TestSlave(TestLoop):
    @classmethod
    def setUpClass(cls):
        cls.dir = tempfile.mkdtemp()
        copytree(os.path.join(ROOT, "molotov"), os.path.join(cls.dir, "molotov"))
        for f in ("setup.py", "molotov.json", "requirements.txt"):
            copyfile(os.path.join(ROOT, f), os.path.join(cls.dir, f))

    @dedicatedloop
    @mock.patch("molotov.slave.check_call", new=check_call)
    def test_main(self):
        with set_args("moloslave", _REPO, "test", "--directory", self.dir) as out:
            main()
        if os.environ.get("CI") is not None:
            return
        output = out[0].read()
        self.assertTrue("This is the end" in output, output)

    @dedicatedloop
    @mock.patch("molotov.slave.check_call", new=check_call)
    def test_fail(self):
        with set_args("moloslave", _REPO, "fail", "--directory", self.dir):
            self.assertRaises(Exception, main)

    @dedicatedloop
    @mock.patch("molotov.slave.check_call", new=check_call)
    def test_version(self):
        with set_args("moloslave", "--version", "--directory", self.dir) as out:
            try:
                main()
            except SystemExit:
                pass
        version = out[0].read().strip()
        self.assertTrue(version, __version__)