|
1 | 1 | import os
|
2 | 2 | import os.path
|
| 3 | +import shutil |
3 | 4 | import tempfile
|
4 | 5 |
|
5 | 6 | from docker.client import Client
|
6 | 7 | from docker.constants import DEFAULT_DOCKER_API_VERSION
|
7 | 8 | from docker.errors import DockerException
|
8 | 9 | from docker.utils import (
|
9 | 10 | parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
|
10 |
| - create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file |
| 11 | + create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file, |
| 12 | + exclude_paths, |
11 | 13 | )
|
12 | 14 | from docker.utils.ports import build_port_bindings, split_port
|
13 | 15 | from docker.auth import resolve_repository_name, resolve_authconfig
|
14 | 16 |
|
15 | 17 | from . import base
|
| 18 | +from .helpers import make_tree |
16 | 19 |
|
17 | 20 | import pytest
|
18 | 21 |
|
@@ -466,3 +469,141 @@ def test_build_port_bindings_with_nonmatching_internal_port_ranges(self):
|
466 | 469 | ["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
|
467 | 470 | self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
|
468 | 471 | self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])
|
| 472 | + |
| 473 | + |
| 474 | +class ExcludePathsTest(base.BaseTestCase): |
| 475 | + dirs = [ |
| 476 | + 'foo', |
| 477 | + 'foo/bar', |
| 478 | + 'bar', |
| 479 | + ] |
| 480 | + |
| 481 | + files = [ |
| 482 | + 'Dockerfile', |
| 483 | + 'Dockerfile.alt', |
| 484 | + '.dockerignore', |
| 485 | + 'a.py', |
| 486 | + 'a.go', |
| 487 | + 'b.py', |
| 488 | + 'cde.py', |
| 489 | + 'foo/a.py', |
| 490 | + 'foo/b.py', |
| 491 | + 'foo/bar/a.py', |
| 492 | + 'bar/a.py', |
| 493 | + ] |
| 494 | + |
| 495 | + all_paths = set(dirs + files) |
| 496 | + |
| 497 | + def setUp(self): |
| 498 | + self.base = make_tree(self.dirs, self.files) |
| 499 | + |
| 500 | + def tearDown(self): |
| 501 | + shutil.rmtree(self.base) |
| 502 | + |
| 503 | + def exclude(self, patterns, dockerfile=None): |
| 504 | + return set(exclude_paths(self.base, patterns, dockerfile=dockerfile)) |
| 505 | + |
| 506 | + def test_no_excludes(self): |
| 507 | + assert self.exclude(['']) == self.all_paths |
| 508 | + |
| 509 | + def test_no_dupes(self): |
| 510 | + paths = exclude_paths(self.base, ['!a.py']) |
| 511 | + assert sorted(paths) == sorted(set(paths)) |
| 512 | + |
| 513 | + def test_wildcard_exclude(self): |
| 514 | + assert self.exclude(['*']) == set(['Dockerfile', '.dockerignore']) |
| 515 | + |
| 516 | + def test_exclude_dockerfile_dockerignore(self): |
| 517 | + """ |
| 518 | + Even if the .dockerignore file explicitly says to exclude |
| 519 | + Dockerfile and/or .dockerignore, don't exclude them from |
| 520 | + the actual tar file. |
| 521 | + """ |
| 522 | + assert self.exclude(['Dockerfile', '.dockerignore']) == self.all_paths |
| 523 | + |
| 524 | + def test_exclude_custom_dockerfile(self): |
| 525 | + """ |
| 526 | + If we're using a custom Dockerfile, make sure that's not |
| 527 | + excluded. |
| 528 | + """ |
| 529 | + assert self.exclude(['*'], dockerfile='Dockerfile.alt') == \ |
| 530 | + set(['Dockerfile.alt', '.dockerignore']) |
| 531 | + |
| 532 | + def test_single_filename(self): |
| 533 | + assert self.exclude(['a.py']) == self.all_paths - set(['a.py']) |
| 534 | + |
| 535 | + # As odd as it sounds, a filename pattern with a trailing slash on the |
| 536 | + # end *will* result in that file being excluded. |
| 537 | + def test_single_filename_trailing_slash(self): |
| 538 | + assert self.exclude(['a.py/']) == self.all_paths - set(['a.py']) |
| 539 | + |
| 540 | + def test_wildcard_filename_start(self): |
| 541 | + assert self.exclude(['*.py']) == self.all_paths - set([ |
| 542 | + 'a.py', 'b.py', 'cde.py', |
| 543 | + ]) |
| 544 | + |
| 545 | + def test_wildcard_with_exception(self): |
| 546 | + assert self.exclude(['*.py', '!b.py']) == self.all_paths - set([ |
| 547 | + 'a.py', 'cde.py', |
| 548 | + ]) |
| 549 | + |
| 550 | + def test_wildcard_with_wildcard_exception(self): |
| 551 | + assert self.exclude(['*.*', '!*.go']) == self.all_paths - set([ |
| 552 | + 'a.py', 'b.py', 'cde.py', 'Dockerfile.alt', |
| 553 | + ]) |
| 554 | + |
| 555 | + def test_wildcard_filename_end(self): |
| 556 | + assert self.exclude(['a.*']) == self.all_paths - set(['a.py', 'a.go']) |
| 557 | + |
| 558 | + def test_question_mark(self): |
| 559 | + assert self.exclude(['?.py']) == self.all_paths - set(['a.py', 'b.py']) |
| 560 | + |
| 561 | + def test_single_subdir_single_filename(self): |
| 562 | + assert self.exclude(['foo/a.py']) == self.all_paths - set(['foo/a.py']) |
| 563 | + |
| 564 | + def test_single_subdir_wildcard_filename(self): |
| 565 | + assert self.exclude(['foo/*.py']) == self.all_paths - set([ |
| 566 | + 'foo/a.py', 'foo/b.py', |
| 567 | + ]) |
| 568 | + |
| 569 | + def test_wildcard_subdir_single_filename(self): |
| 570 | + assert self.exclude(['*/a.py']) == self.all_paths - set([ |
| 571 | + 'foo/a.py', 'bar/a.py', |
| 572 | + ]) |
| 573 | + |
| 574 | + def test_wildcard_subdir_wildcard_filename(self): |
| 575 | + assert self.exclude(['*/*.py']) == self.all_paths - set([ |
| 576 | + 'foo/a.py', 'foo/b.py', 'bar/a.py', |
| 577 | + ]) |
| 578 | + |
| 579 | + def test_directory(self): |
| 580 | + assert self.exclude(['foo']) == self.all_paths - set([ |
| 581 | + 'foo', 'foo/a.py', 'foo/b.py', |
| 582 | + 'foo/bar', 'foo/bar/a.py', |
| 583 | + ]) |
| 584 | + |
| 585 | + def test_directory_with_trailing_slash(self): |
| 586 | + assert self.exclude(['foo']) == self.all_paths - set([ |
| 587 | + 'foo', 'foo/a.py', 'foo/b.py', |
| 588 | + 'foo/bar', 'foo/bar/a.py', |
| 589 | + ]) |
| 590 | + |
| 591 | + def test_directory_with_single_exception(self): |
| 592 | + assert self.exclude(['foo', '!foo/bar/a.py']) == self.all_paths - set([ |
| 593 | + 'foo/a.py', 'foo/b.py', |
| 594 | + ]) |
| 595 | + |
| 596 | + def test_directory_with_subdir_exception(self): |
| 597 | + assert self.exclude(['foo', '!foo/bar']) == self.all_paths - set([ |
| 598 | + 'foo/a.py', 'foo/b.py', |
| 599 | + ]) |
| 600 | + |
| 601 | + def test_directory_with_wildcard_exception(self): |
| 602 | + assert self.exclude(['foo', '!foo/*.py']) == self.all_paths - set([ |
| 603 | + 'foo/bar', 'foo/bar/a.py', |
| 604 | + ]) |
| 605 | + |
| 606 | + def test_subdirectory(self): |
| 607 | + assert self.exclude(['foo/bar']) == self.all_paths - set([ |
| 608 | + 'foo/bar', 'foo/bar/a.py', |
| 609 | + ]) |
0 commit comments