|
37 | 37 | from docker.errors import APIError, NotFound
|
38 | 38 | from docker.utils import kwargs_from_env
|
39 | 39 |
|
| 40 | +from . import helpers |
40 | 41 | from .base import requires_api_version
|
41 | 42 | from .test import Cleanup
|
42 | 43 |
|
@@ -427,6 +428,90 @@ def test_valid_no_config_specified(self):
|
427 | 428 | self.assertEqual(container_log_config['Config'], {})
|
428 | 429 |
|
429 | 430 |
|
| 431 | +@requires_api_version('1.20') |
| 432 | +class GetArchiveTest(BaseTestCase): |
| 433 | + def test_get_file_archive_from_container(self): |
| 434 | + data = 'The Maid and the Pocket Watch of Blood' |
| 435 | + ctnr = self.client.create_container( |
| 436 | + BUSYBOX, 'sh -c "echo {0} > /vol1/data.txt"'.format(data), |
| 437 | + volumes=['/vol1'] |
| 438 | + ) |
| 439 | + self.tmp_containers.append(ctnr) |
| 440 | + self.client.start(ctnr) |
| 441 | + self.client.wait(ctnr) |
| 442 | + with tempfile.NamedTemporaryFile() as destination: |
| 443 | + strm, stat = self.client.get_archive(ctnr, '/vol1/data.txt') |
| 444 | + for d in strm: |
| 445 | + destination.write(d) |
| 446 | + destination.seek(0) |
| 447 | + retrieved_data = helpers.untar_file(destination, 'data.txt') |
| 448 | + if six.PY3: |
| 449 | + retrieved_data = retrieved_data.decode('utf-8') |
| 450 | + self.assertEqual(data, retrieved_data.strip()) |
| 451 | + |
| 452 | + def test_get_file_stat_from_container(self): |
| 453 | + data = 'The Maid and the Pocket Watch of Blood' |
| 454 | + ctnr = self.client.create_container( |
| 455 | + BUSYBOX, 'sh -c "echo -n {0} > /vol1/data.txt"'.format(data), |
| 456 | + volumes=['/vol1'] |
| 457 | + ) |
| 458 | + self.tmp_containers.append(ctnr) |
| 459 | + self.client.start(ctnr) |
| 460 | + self.client.wait(ctnr) |
| 461 | + strm, stat = self.client.get_archive(ctnr, '/vol1/data.txt') |
| 462 | + self.assertIn('name', stat) |
| 463 | + self.assertEqual(stat['name'], 'data.txt') |
| 464 | + self.assertIn('size', stat) |
| 465 | + self.assertEqual(stat['size'], len(data)) |
| 466 | + |
| 467 | + |
| 468 | +@requires_api_version('1.20') |
| 469 | +class PutArchiveTest(BaseTestCase): |
| 470 | + def test_copy_file_to_container(self): |
| 471 | + data = b'Deaf To All But The Song' |
| 472 | + with tempfile.NamedTemporaryFile() as test_file: |
| 473 | + test_file.write(data) |
| 474 | + test_file.seek(0) |
| 475 | + ctnr = self.client.create_container( |
| 476 | + BUSYBOX, |
| 477 | + 'cat {0}'.format( |
| 478 | + os.path.join('/vol1', os.path.basename(test_file.name)) |
| 479 | + ), |
| 480 | + volumes=['/vol1'] |
| 481 | + ) |
| 482 | + self.tmp_containers.append(ctnr) |
| 483 | + with helpers.simple_tar(test_file.name) as test_tar: |
| 484 | + self.client.put_archive(ctnr, '/vol1', test_tar) |
| 485 | + self.client.start(ctnr) |
| 486 | + self.client.wait(ctnr) |
| 487 | + logs = self.client.logs(ctnr) |
| 488 | + if six.PY3: |
| 489 | + logs = logs.decode('utf-8') |
| 490 | + data = data.decode('utf-8') |
| 491 | + self.assertEqual(logs.strip(), data) |
| 492 | + |
| 493 | + def test_copy_directory_to_container(self): |
| 494 | + files = ['a.py', 'b.py', 'foo/b.py'] |
| 495 | + dirs = ['foo', 'bar'] |
| 496 | + base = helpers.make_tree(dirs, files) |
| 497 | + ctnr = self.client.create_container( |
| 498 | + BUSYBOX, 'ls -p /vol1', volumes=['/vol1'] |
| 499 | + ) |
| 500 | + self.tmp_containers.append(ctnr) |
| 501 | + with docker.utils.tar(base) as test_tar: |
| 502 | + self.client.put_archive(ctnr, '/vol1', test_tar) |
| 503 | + self.client.start(ctnr) |
| 504 | + self.client.wait(ctnr) |
| 505 | + logs = self.client.logs(ctnr) |
| 506 | + if six.PY3: |
| 507 | + logs = logs.decode('utf-8') |
| 508 | + results = logs.strip().split() |
| 509 | + self.assertIn('a.py', results) |
| 510 | + self.assertIn('b.py', results) |
| 511 | + self.assertIn('foo/', results) |
| 512 | + self.assertIn('bar/', results) |
| 513 | + |
| 514 | + |
430 | 515 | class TestCreateContainerReadOnlyFs(BaseTestCase):
|
431 | 516 | def runTest(self):
|
432 | 517 | if not exec_driver_is_native():
|
|
0 commit comments