This repository was archived by the owner on Dec 16, 2024. It is now read-only.
forked from hkumarmk/python-dbuild
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.py
More file actions
203 lines (155 loc) · 8.93 KB
/
Copy pathtests.py
File metadata and controls
203 lines (155 loc) · 8.93 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import os.path
import shutil
import tarfile
import tempfile
import types
from unittest import TestCase
import mock
import dbuild
class DbuildTests(TestCase):
@mock.patch('dbuild.Client')
def test_client_connects_to_unix_socket_by_default(self, Client):
connection = dbuild.docker_client()
Client.assert_called_with('unix://var/run/docker.sock')
self.assertEquals(Client.return_value, connection)
def test_build_image(self):
docker_client = mock.MagicMock()
docker_client.build.return_value = iter([{'stream': 'line1'},
{'stream': 'line2'},
{'stream': 'line3'},
{'error': 'some error occurred',
'errorDetails': 'more details'}])
rv = dbuild.build_image(docker_client, 'some/path', 'sometag', False)
self.assertEquals(type(rv), types.GeneratorType)
expected_values = ['line3', 'line2', 'line1']
for line in rv:
self.assertEquals(expected_values.pop(), line)
if not expected_values:
break
with self.assertRaises(dbuild.exceptions.DbuildDockerBuildFailedException):
for line in rv:
assert False, 'returned more lines before raising exception'
docker_client.build.assert_called_with(path='some/path', rm=True, forcerm=True,
tag='sometag', decode=True, nocache=False)
def test_create_container_defaults(self):
docker_client = mock.MagicMock()
dbuild.create_container(docker_client, 'imagename')
docker_client.create_container.assert_called_with(image='imagename', name=None,
command=None, environment=None,
network_disabled=False, volumes=None,
working_dir=None, host_config=None)
def test_create_container_shared_volumes(self):
docker_client = mock.MagicMock()
dbuild.create_container(docker_client, 'imagename', shared_volumes={'/something': '/else'})
docker_client.create_host_config.assert_called_with(binds=['/something:/else'])
host_config = docker_client.create_host_config.return_value
docker_client.create_container.assert_called_with(image='imagename', name=None,
command=None, environment=None,
network_disabled=False, volumes=['/else'],
working_dir=None, host_config=host_config)
def test_start_container(self):
docker_client = mock.MagicMock()
container = {'Id': 1234}
dbuild.start_container(docker_client, container)
docker_client.start.assert_called_with(container=1234)
def test_wait_container(self):
docker_client = mock.MagicMock()
dbuild.wait_container(docker_client, 1234)
docker_client.wait.assert_called_with(container=1234)
def test_container_logs(self):
docker_client = mock.MagicMock()
docker_client.logs.return_value = iter(['line1', 'line2', 'line3'])
rv = dbuild.container_logs(docker_client, 1234)
self.assertEquals(type(rv), types.GeneratorType)
self.assertEquals(list(rv), ['line1', 'line2', 'line3'])
docker_client.logs.assert_called_with(container=1234, stream=True, timestamps=True)
def _test_remove_container(self, expected_force, **kwargs):
docker_client = mock.MagicMock()
dbuild.remove_container(docker_client, 1234, **kwargs)
docker_client.remove_container.assert_called_with(container=1234, force=expected_force)
def test_remove_container(self):
self._test_remove_container(False)
self._test_remove_container(False, force=False)
self._test_remove_container(True, force=True)
def test_create_dockerfile(self):
tmpdir = tempfile.mkdtemp()
try:
dbuild.create_dockerfile('ubuntu', 'trusty', tmpdir)
with open(os.path.join(tmpdir, 'Dockerfile'), 'r') as fp:
generated_content = fp.read()
with open(os.path.join(os.path.dirname(__file__), 'test_data', 'Dockerfile1'), 'r') as fp:
expected_content = fp.read()
self.assertEquals(expected_content, generated_content)
finally:
shutil.rmtree(tmpdir)
def test_build(self):
tmpdir = tempfile.mkdtemp()
try:
shutil.copytree(os.path.join(os.path.dirname(__file__), 'test_data', 'pkg1'),
os.path.join(tmpdir, 'source'))
dbuild.docker_build(tmpdir, build_type='source', build_owner=os.getuid())
create_container_real = dbuild.create_container
# Spy on the calls to create_container
with mock.patch('dbuild.create_container') as create_container:
create_container.side_effect = lambda *args, **kwargs: create_container_real(*args, **kwargs)
dbuild.docker_build(tmpdir, build_type='binary', build_owner=os.getuid(), parallel=7)
self.assertIn('-j7', create_container.call_args[1]['command'][2])
for f in ['buildsvctest_0.1.dsc', 'buildsvctest_0.1.tar.gz',
'buildsvctest_0.1_amd64.changes', 'buildsvctest_0.1_amd64.deb',
'buildsvctest_0.1_source.changes']:
assert os.path.exists(os.path.join(tmpdir, f)), '{} was missing'.format(f)
finally:
shutil.rmtree(tmpdir)
def test_discards_dot_git_dir(self):
tmpdir = tempfile.mkdtemp()
try:
shutil.copytree(os.path.join(os.path.dirname(__file__), 'test_data', 'pkg4'),
os.path.join(tmpdir, 'source'))
os.mkdir(os.path.join(tmpdir, 'source', '.git'))
with open(os.path.join(tmpdir, 'source', '.git', 'somefile'), 'w') as fp:
fp.write('this should be discarded')
dbuild.docker_build(tmpdir, build_type='source', build_owner=os.getuid())
tf = tarfile.open(os.path.join(tmpdir, 'pkg4_1.0-1.tar.gz'), 'r:*')
self.assertFalse(list(filter(lambda ti: '/.git/' in ti.name, tf.getmembers())),
'Tarball contained .git dir' + repr(tf.getmembers()))
finally:
shutil.rmtree(tmpdir)
def test_build_failed_source_build(self):
tmpdir = tempfile.mkdtemp()
try:
shutil.copytree(os.path.join(os.path.dirname(__file__), 'test_data', 'pkg2'),
os.path.join(tmpdir, 'source'))
self.assertRaises(dbuild.exceptions.DbuildSourceBuildFailedException,
dbuild.docker_build, tmpdir, build_type='source',
force_rm=True, build_owner=os.getuid())
finally:
shutil.rmtree(tmpdir)
def test_build_failed_binary_build(self):
tmpdir = tempfile.mkdtemp()
try:
shutil.copytree(os.path.join(os.path.dirname(__file__), 'test_data', 'pkg3'),
os.path.join(tmpdir, 'source'))
dbuild.docker_build(tmpdir, build_type='source', build_owner=os.getuid())
self.assertRaises(dbuild.exceptions.DbuildBinaryBuildFailedException,
dbuild.docker_build, tmpdir, build_type='binary',
force_rm=True, build_owner=os.getuid())
finally:
shutil.rmtree(tmpdir)
@mock.patch('dbuild.docker_build')
def test_build_cli(self, docker_build):
dbuild.main(['--no-include-timestamps', '/some/dir'])
self.assertEquals(docker_build.call_args_list,
[mock.call(build_cache=True, build_dir='/some/dir', build_owner=None,
build_type='source', dist='ubuntu',
docker_url='unix://var/run/docker.sock', extra_repo_keys_file='keys',
extra_repos_file='repos', force_rm=False, proxy='', release='trusty',
source_dir='source', no_default_sources=False,
include_timestamps=False),
mock.call(build_cache=True, build_dir='/some/dir', build_owner=None,
build_type='binary', dist='ubuntu',
docker_url='unix://var/run/docker.sock',
extra_repo_keys_file='keys', extra_repos_file='repos',
force_rm=False, proxy='', parallel=1, release='trusty',
source_dir='source', no_default_sources=False,
include_timestamps=False)])