Debian Codegraph @

tests/conftest.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2019-2020 Matthias Klumpp <[email protected]>
4 #
5 # Licensed under the GNU Lesser General Public License Version 3
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the license, or
10 # (at your option) any later version.
11 #
12 # This software is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this software. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22
23 import pytest
24
25 # pylint: disable=redefined-outer-name
26
27
28 @pytest.fixture(scope='session', autouse=True)
29 def gconfig():
30 '''
31 Ensure the global config object is set up properly for unit-testing.
32 '''
33 import shutil
34
35 import debspawn.cli
36
37 from . import source_root
38
39 debspawn.cli.__mainfile = os.path.join(source_root, 'debspawn.py')
40
41 class MockOptions:
42 config = None
43 no_unicode = False
44 owner = None
45
46 gconf = debspawn.cli.init_config(MockOptions())
47
48 test_tmp_dir = '/tmp/debspawn-test/'
49 shutil.rmtree(test_tmp_dir, ignore_errors=True)
50 os.makedirs(test_tmp_dir)
51
52 gconf._instance._osroots_dir = os.path.join(test_tmp_dir, 'containers/')
53 gconf._instance._results_dir = os.path.join(test_tmp_dir, 'results/')
54 gconf._instance._aptcache_dir = os.path.join(test_tmp_dir, 'aptcache/')
55 gconf._instance._injected_pkgs_dir = os.path.join(test_tmp_dir, 'injected-pkgs/')
56
57 return gconf
58
59
60 @pytest.fixture(scope='session', autouse=True)
61 def ensure_root():
62 '''
63 Ensure we run with superuser permissions.
64 '''
65
66 if os.geteuid() != 0:
67 print('The testsuite has to be run with superuser permissions in order to create nspawn instances.')
68 sys.exit(1)
69
70
71 @pytest.fixture(scope='session')
72 def build_arch():
73 '''
74 Retrieve the current architecture we should build packages for.
75 '''
76 from debspawn.utils.command import safe_run
77
78 out, _, ret = safe_run(['dpkg', '--print-architecture'])
79 assert ret == 0
80
81 arch = out.strip()
82 if not arch:
83 arch = 'amd64' # assume amd64 as default
84
85 return arch
86
87
88 @pytest.fixture(scope='session')
89 def testing_container(gconfig, build_arch):
90 '''
91 Create a container for Debian stable used for default tests
92 '''
93 from debspawn.osbase import OSBase
94
95 suite = 'stable'
96 variant = 'minbase'
97 components = ['main', 'contrib', 'non-free']
98 extra_suites = []
99
100 osbase = OSBase(gconfig, suite, build_arch, variant=variant, base_suite=None)
101 r = osbase.create(None, components, extra_suites=extra_suites)
102 assert r
103
104 return (suite, build_arch, variant)