~curtin-dev/curtin/bionic

« back to all changes in this revision

Viewing changes to tests/unittests/helpers.py

  • Committer: Scott Moser
  • Date: 2017-08-03 19:51:16 UTC
  • mfrom: (1.1.50)
  • Revision ID: smoser@ubuntu.com-20170803195116-0xc6onji18peerm5
* New upstream snapshot.
  - tests: Add CiTestCase common parent for all curtin tests.
  - vmtests: Remove force flag for centos curthooks
  - tools/jenkins-runner: improve tgtd cleanup logic
  - tests: Drop EOL Wily Vivid and Yakkety tests.
  - Disable yum plugins when installing packages, update ca-certs for https
  - Rename centos_network_curthooks -> centos_apply_network_config.
  - tests: in centos_defaults use write_files for grub serial.
  - write_files: write files after extract, change write_files signature.
  - pass network configuration through to target for ubuntu and centos
  - tests: disable yakkety tests.
  - tools/launch: automatically pass on proxy settings to curtin
  - Add top level 'proxy' to config, deprecate top level http_proxy.
  - tools/curtainer: fix to enable deb-src for -proposed.
  - Use unshare to put chroot commands in own pid namespace.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import imp
20
20
import importlib
21
21
import mock
 
22
import os
 
23
import shutil
 
24
import tempfile
 
25
from unittest import TestCase
22
26
 
23
27
 
24
28
def builtin_module_name():
43
47
    m_patch = '{}.open'.format(mod_name)
44
48
    with mock.patch(m_patch, m_open, create=True):
45
49
        yield m_open
 
50
 
 
51
 
 
52
class CiTestCase(TestCase):
 
53
    """Common testing class which all curtin unit tests subclass."""
 
54
 
 
55
    def add_patch(self, target, attr, **kwargs):
 
56
        """Patches specified target object and sets it as attr on test
 
57
        instance also schedules cleanup"""
 
58
        if 'autospec' not in kwargs:
 
59
            kwargs['autospec'] = True
 
60
        m = mock.patch(target, **kwargs)
 
61
        p = m.start()
 
62
        self.addCleanup(m.stop)
 
63
        setattr(self, attr, p)
 
64
 
 
65
    def tmp_dir(self, dir=None, cleanup=True):
 
66
        """Return a full path to a temporary directory for the test run."""
 
67
        if dir is None:
 
68
            tmpd = tempfile.mkdtemp(
 
69
                prefix="curtin-ci-%s." % self.__class__.__name__)
 
70
        else:
 
71
            tmpd = tempfile.mkdtemp(dir=dir)
 
72
        self.addCleanup(shutil.rmtree, tmpd)
 
73
        return tmpd
 
74
 
 
75
    def tmp_path(self, path, _dir=None):
 
76
        # return an absolute path to 'path' under dir.
 
77
        # if dir is None, one will be created with tmp_dir()
 
78
        # the file is not created or modified.
 
79
        if _dir is None:
 
80
            _dir = self.tmp_dir()
 
81
        return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))