155
155
image.add_alias.assert_called_once_with(
156
156
"lp-xenial-amd64", "lp-xenial-amd64")
158
def assert_correct_profile(self, extra_raw_lxc_config=""):
158
def assert_correct_profile(self, extra_raw_lxc_config=None,
159
driver_version="2.0"):
160
if extra_raw_lxc_config is None:
161
extra_raw_lxc_config = []
159
163
client = pylxd.Client()
160
164
client.profiles.get.assert_called_once_with("lpbuildd")
167
("lxc.cap.drop", ""),
168
("lxc.cap.drop", "sys_time sys_module"),
169
("lxc.cgroup.devices.deny", ""),
170
("lxc.cgroup.devices.allow", ""),
171
("lxc.mount.auto", ""),
172
("lxc.mount.auto", "proc:rw sys:rw"),
175
major, minor = [int(v) for v in driver_version.split(".")[0:2]]
178
raw_lxc_config.extend([
179
("lxc.apparmor.profile", "unconfined"),
180
("lxc.net.0.ipv4.address", "10.10.10.2/24"),
181
("lxc.net.0.ipv4.gateway", "10.10.10.1"),
184
raw_lxc_config.extend([
185
("lxc.aa_profile", "unconfined"),
186
("lxc.network.0.ipv4", "10.10.10.2/24"),
187
("lxc.network.0.ipv4.gateway", "10.10.10.1"),
190
raw_lxc_config = "".join("{key}={val}\n".format(key=key, val=val)
191
for key, val in sorted(raw_lxc_config + extra_raw_lxc_config))
161
193
expected_config = {
162
194
"security.privileged": "true",
163
195
"security.nesting": "true",
164
"raw.lxc": dedent("""\
165
lxc.aa_profile=unconfined
167
lxc.cap.drop=sys_time sys_module
168
lxc.cgroup.devices.deny=
169
lxc.cgroup.devices.allow=
171
lxc.mount.auto=proc:rw sys:rw
172
lxc.network.0.ipv4=10.10.10.2/24
173
lxc.network.0.ipv4.gateway=10.10.10.1
174
""") + extra_raw_lxc_config,
196
"raw.lxc": raw_lxc_config,
176
198
expected_devices = {
206
if driver_version == "3.0":
207
expected_devices["root"] = {
184
212
client.profiles.create.assert_called_once_with(
185
213
"lpbuildd", expected_config, expected_devices)
187
215
def test_create_profile_amd64(self):
188
self.useFixture(MockPatch("pylxd.Client"))
189
client = pylxd.Client()
190
client.profiles.get.side_effect = FakeLXDAPIException
191
LXD("1", "xenial", "amd64").create_profile()
192
self.assert_correct_profile()
216
with MockPatch("pylxd.Client"):
217
for driver_version in ["2.0", "3.0"]:
218
client = pylxd.Client()
220
client.profiles.get.side_effect = FakeLXDAPIException
222
"environment": {"driver_version": driver_version}
224
LXD("1", "xenial", "amd64").create_profile()
225
self.assert_correct_profile(
226
driver_version=driver_version or "3.0")
194
228
def test_create_profile_powerpc(self):
195
self.useFixture(MockPatch("pylxd.Client"))
196
client = pylxd.Client()
197
client.profiles.get.side_effect = FakeLXDAPIException
198
LXD("1", "xenial", "powerpc").create_profile()
199
self.assert_correct_profile("lxc.seccomp=\n")
229
with MockPatch("pylxd.Client"):
230
for driver_version in ["2.0", "3.0"]:
231
client = pylxd.Client()
233
client.profiles.get.side_effect = FakeLXDAPIException
235
"environment": {"driver_version": driver_version}
237
LXD("1", "xenial", "powerpc").create_profile()
238
self.assert_correct_profile(
239
extra_raw_lxc_config=[("lxc.seccomp", ""),],
240
driver_version=driver_version or "3.0"
201
243
def test_start(self):
202
244
fs_fixture = self.useFixture(FakeFilesystem())
213
255
client.profiles.get.side_effect = FakeLXDAPIException
214
256
container = client.containers.create.return_value
215
257
client.containers.get.return_value = container
258
client.host_info = {"environment": {"driver_version": "2.0"}}
216
259
container.start.side_effect = (
217
260
lambda wait=False: setattr(container, "status_code", LXD_RUNNING))
218
261
files_api = container.api.files
299
342
b"127.0.0.1\tlocalhost\n\n"
300
343
b"127.0.1.1\texample.buildd example\n"),
301
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
344
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
302
345
files_api.post.assert_any_call(
303
346
params={"path": "/etc/hostname"},
304
347
data=b"example\n",
305
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
348
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
306
349
files_api.post.assert_any_call(
307
350
params={"path": "/etc/resolv.conf"},
308
351
data=b"host resolv.conf\n",
309
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
352
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
310
353
files_api.post.assert_any_call(
311
354
params={"path": "/usr/local/sbin/policy-rc.d"},
312
355
data=policy_rc_d.encode("UTF-8"),
313
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0755"})
356
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0755"})
314
357
files_api.session.get.assert_any_call(
315
358
"/1.0/containers/lp-xenial-amd64/files",
316
359
params={"path": "/etc/init/mounted-dev.conf"}, stream=True)
321
364
files_api.post.assert_any_call(
322
365
params={"path": "/etc/systemd/system/snapd.service.d/no-cdn.conf"},
323
366
data=b"[Service]\nEnvironment=SNAPPY_STORE_NO_CDN=1\n",
324
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
367
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
325
368
container.start.assert_called_once_with(wait=True)
326
369
self.assertEqual(LXD_RUNNING, container.status_code)
340
383
client.profiles.get.side_effect = FakeLXDAPIException
341
384
container = client.containers.create.return_value
342
385
client.containers.get.return_value = container
386
client.host_info = {"environment": {"driver_version": "2.0"}}
343
387
container.start.side_effect = (
344
388
lambda wait=False: setattr(container, "status_code", LXD_RUNNING))
345
389
files_api = container.api.files
362
406
"\n127.0.1.1\texample.buildd example\n").encode("UTF-8"),
363
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
407
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
365
409
def test_start_with_mounted_dev_conf(self):
366
410
fs_fixture = self.useFixture(FakeFilesystem())
375
419
self.useFixture(MockPatch("pylxd.Client"))
376
420
client = pylxd.Client()
377
421
client.profiles.get.side_effect = FakeLXDAPIException
422
client.host_info = {"environment": {"driver_version": "2.0"}}
378
423
container = client.containers.create.return_value
379
424
client.containers.get.return_value = container
380
425
container.start.side_effect = (
406
451
: # /sbin/MAKEDEV std fd ppp tun
408
453
""").encode("UTF-8"),
409
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
454
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
411
456
def test_run(self):
412
457
processes_fixture = self.useFixture(FakeProcesses())
456
501
container.api.files.post.assert_called_once_with(
457
502
params={"path": target_path},
459
headers={"X-LXD-uid": 0, "X-LXD-gid": 0, "X-LXD-mode": "0644"})
504
headers={"X-LXD-uid": "0", "X-LXD-gid": "0", "X-LXD-mode": "0644"})
461
506
def test_copy_in_error(self):
462
507
source_dir = self.useFixture(TempDir()).path