def test_bootstrap(config_files, monkeypatch):
with open(config_files, "r") as main_file:
def fakegetuid():
return 0
monkeypatch.setattr(os, 'getuid', fakegetuid)
def fakechown(*_):
pass
monkeypatch.setattr(shutil, 'chown', fakechown)
def fakerun(*popenargs, **kwargs):
if get_command(popenargs) == "chroot":
rootfs_path = get_command_parameter(popenargs, "chroot")
if not os.path.exists(rootfs_path):
os.mkdir(rootfs_path)
elif get_command(popenargs) == "debootstrap":
rootfs_path = popenargs[0][-2]
apt_dir = os.path.join(rootfs_path, 'etc', 'apt')
os.makedirs(apt_dir)
pass
elif get_command(popenargs) == "tar":
archive = get_command_parameter(popenargs, '-acf')
with open(archive, mode="w") as fakearchive:
fakearchive.write("fake archive")
elif popenargs[0][-2] == "dpkg" and popenargs[0][-1] == "--print-architecture":
return subprocess.CompletedProcess("fakerun", 0, 'amd64')
elif get_command(popenargs) == "lxd" and get_sub_command(popenargs) == "--version":
return subprocess.CompletedProcess("fakerun", 0, '2.18')
elif get_command(popenargs) == "printenv":
if get_sub_command(popenargs) == "HOME":
return subprocess.CompletedProcess("fakerun", 0, '/no/such/directory')
else:
return subprocess.CompletedProcess("fakerun", 0, '')
else:
print('Passthrough: {}'.format(get_command(popenargs)))
return subprocess.run(*popenargs, **kwargs)
return subprocess.CompletedProcess("fakerun", 0, '')
monkeypatch.setattr(mockablerun, 'run_mockable', fakerun)
monkeypatch.chdir(os.path.dirname(config_files))
bootstrap_cmd = Bootstrap()
with requests_mock.Mocker() as m:
m.get('https://ftp-master.debian.org/keys/archive-key-8.asc', text='key file mockup')
bootstrap_cmd.run(main_file)
expected_result = bootstrap_cmd._result()
assert os.path.exists(expected_result)
previous_result_text = "previous result"
with open(expected_result, mode="w") as previous_result:
previous_result.write(previous_result_text)
bootstrap_cmd2 = Bootstrap()
bootstrap_cmd2.run(main_file)
with open(expected_result, mode="r") as same_result:
assert same_result.read() == previous_result_text
评论列表
文章目录