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
|
"""Tests for hooks.lxd_utils."""
import mock
import lxd_utils
import testing
class TestLXDUtilsDeterminePackages(testing.CharmTestCase):
"""Tests for hooks.lxd_utils.determine_packages."""
TO_PATCH = [
'config',
]
def setUp(self):
super(TestLXDUtilsDeterminePackages, self).setUp(
lxd_utils, self.TO_PATCH)
self.config.side_effect = self.test_config.get
def test_determine_packages(self):
"""A list of LXD packages should be returned."""
expected = [
'btrfs-tools',
'thin-provisioning-tools',
'criu',
'lvm2',
'lxd',
'lxd-client',
]
packages = lxd_utils.determine_packages()
self.assertEqual(expected, packages)
class TestLXDUtilsCreateAndImportBusyboxImage(testing.CharmTestCase):
"""Tests for hooks.lxd_utils.create_and_import_busybox_image."""
TO_PATCH = []
def setUp(self):
super(TestLXDUtilsCreateAndImportBusyboxImage, self).setUp(
lxd_utils, self.TO_PATCH)
@mock.patch('lxd_utils.open')
@mock.patch('lxd_utils.os.stat')
@mock.patch('lxd_utils.subprocess.Popen')
@mock.patch('lxd_utils.shutil.rmtree')
@mock.patch('lxd_utils.subprocess.call')
@mock.patch('lxd_utils.tarfile.open')
@mock.patch('lxd_utils.tempfile.mkdtemp')
@mock.patch('lxd_utils.check_call')
def test_create_and_import_busybox_image(
self, check_call, mkdtemp, tarfile_open, subprocess_call,
rmtree, Popen, stat, mock_open):
"""A busybox image is imported into lxd."""
mkdtemp.return_value = '/not/a/real/path'
tarfile_open.return_value = mock.Mock()
subprocess_call.return_value = False
Popen_rv = mock.Mock()
Popen_rv.stdout.read.return_value = '\n'
Popen.return_value = Popen_rv
stat_rv = mock.Mock()
stat_rv.st_ctime = 0
stat_rv.st_size = 0
stat.return_value = stat_rv
lxd_utils.create_and_import_busybox_image()
self.assertTrue(check_call.called)
args = check_call.call_args[0][0]
self.assertEqual(['lxc', 'image', 'import'], args[:3])
self.assertEqual(['--alias', 'busybox'], args[4:])
# Assert all other mocks *would* have been called.
mkdtemp.assert_called_once_with()
tarfile_open.assert_called_once_with(
'/not/a/real/path/busybox.tar', 'w:')
subprocess_call.assert_called_once_with(
['xz', '-9', '/not/a/real/path/busybox.tar'])
Popen.assert_called_once_with(
['/bin/busybox', '--list-full'], stdout=-1,
universal_newlines=True)
Popen_rv.stdout.read.assert_called_once_with()
stat.assert_called_with('/bin/busybox')
mock_open.assert_called_once_with('/bin/busybox', 'rb')
|