Ejemplo n.º 1
0
    def test_lint_complex_dockerfile(self):
        dockerfile_path = os.path.join(get_mock_dir(), "Dockerfile.complete")
        dockerfile = Dockerfile.load_from_file_path(dockerfile_path)
        linter = DockerfileLint(dockerfile, exit_on_errors=False)

        with captured_output() as (out, err):
            linter.lint()
        output = out.getvalue().strip()

        self.assertIn("---> FAILED", output)
Ejemplo n.º 2
0
    def test_run_with_dry(self):
        args = ["--dry-run", "--no-lint", "-w", get_mock_dir()]
        make = self.create_make(args=args)

        with captured_output() as (out, err):
            make.run()
        output = out.getvalue().strip()

        self.assertIn("[DRY] Would execute command: docker build", output)
        self.assertIn("[DRY] Would execute command: docker push", output)
        self.assertNotIn("Markdown Summary", output)
        self.assertNotIn("Step", output)
Ejemplo n.º 3
0
    def test_create_docker_build_and_push_commands_with_overrides(self):
        config_file = os.path.join(get_mock_dir(), "docker-make-test.yaml")
        config = YamlLoader.safe_load_yaml(config_file)
        make = self.create_make(args=[
            "--no-pull", "--build-arg", "FIRST_VERSION=0.1", "--label",
            "testlabel"
        ],
                                dockerfile="Dockerfile.noargs",
                                config=config)
        build = make.config.get_builds()[0]

        with patch("dockermake.utils.helpers.System._run_command",
                   return_value=("", "", 0)) as mock:
            make._run_docker_build_and_push_commands(build)

        expected_build_command = (
            "docker build "
            '--build-arg DEFAULT_VERSION=1.0 --build-arg VERSION=overridden --build-arg FIRST_VERSION=0.1 '
            '--label testlabel '
            '--label ci.built_from_scratch=true '
            '--file ./Dockerfile '
            '--rm '
            '--tag registry.a.com/a-namespace/a-image-name '
            '--tag registry.a.com/a-namespace/a-image-name:latest '
            '--tag registry.a.com/a-namespace/a-image-name:0.1 '
            '--tag registry.a.com/a-namespace/a-image-name:overridden '
            '--tag registry.a.com/a-namespace/a-image-name:1.1 '
            '--tag registry.a.com/a-namespace/a-image-name:1.0 '
            '.')
        self.assertEqual(" ".join(mock.call_args_list[-8][0][0]),
                         expected_build_command)
        self.assertEqual(
            " ".join(mock.call_args_list[-7][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name")
        self.assertEqual(
            " ".join(mock.call_args_list[-6][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name:latest")
        self.assertEqual(
            " ".join(mock.call_args_list[-5][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name:0.1")
        self.assertEqual(
            " ".join(mock.call_args_list[-4][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name:overridden")
        self.assertEqual(
            " ".join(mock.call_args_list[-3][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name:1.1")
        self.assertEqual(
            " ".join(mock.call_args_list[-2][0][0]),
            "docker push registry.a.com/a-namespace/a-image-name:1.0")
        self.assertEqual(
            " ".join(mock.call_args_list[-1][0][0]),
            "docker inspect --format {{ json . }} registry.a.com/a-namespace/a-image-name"
        )
Ejemplo n.º 4
0
 def create_make(args=None, dockerfile="Dockerfile", config=None):
     args = args or []
     config = config or {}
     mock_registries()
     _, parsed_args = parse_arguments(args)
     with patch(
             "dockermake.docker.docker_cli_factory.DockerCliFactory.create",
             return_value=DockerCli112):
         make = Make(parsed_args)
     dockerfile_path = os.path.join(get_mock_dir(), dockerfile)
     make.dockerfile = Dockerfile.load_from_file_path(dockerfile_path)
     make.config = ConfigFactory.create_by_version(
         "1", config, make.args.docker_build_args)
     make._prepare_config()
     return make
Ejemplo n.º 5
0
 def test_load_config(self):
     config = ConfigLoader.load(get_mock_dir())
     self.assertEqual(config.get_registry_host(), 'registry.b.acme')
Ejemplo n.º 6
0
 def test_load_alternative_config(self):
     config = ConfigLoader.load(get_mock_dir(),
                                alternative_file="docker-make-test.yaml")
     self.assertEqual(config.get_registry_host(), 'registry.a.com')
Ejemplo n.º 7
0
    def test_load_valid_yaml(self):
        config_file = os.path.join(get_mock_dir(), "docker-make.yaml")
        config = YamlLoader.safe_load_yaml(config_file)

        self.assertEqual(config["version"], "1")
Ejemplo n.º 8
0
    def test_load_invalid_yaml(self):
        config_file = os.path.join(get_mock_dir(), "Dockerfile")

        with self.assertRaisesRegex(Exception,
                                    "Unable to parse .*Dockerfile.*"):
            YamlLoader.safe_load_yaml(config_file)
Ejemplo n.º 9
0
    def test_create_docker_build_and_push_commands_with_two_builds_including_buildargs_and_tags(
            self):
        config_file = os.path.join(get_mock_dir(), "docker-make-test2.yaml")
        config = YamlLoader.safe_load_yaml(config_file)
        make = self.create_make(args=[
            "--no-push", "--build-arg", "ansible_components_version=1.3",
            "--build-arg", "USERNAME=acme"
        ],
                                dockerfile="Dockerfile.args",
                                config=config)
        builds = make.config.get_builds()

        self.assertEqual(len(builds), 2)

        with patch("dockermake.utils.helpers.System._run_command",
                   return_value=("", "", 0)) as mock:
            make._run_docker_build_and_push_commands(builds[0])
        expected_build_command = (
            'docker build '
            '--build-arg ansible_components_version=1.3 --build-arg USERNAME=acme '
            '--build-arg ANSIBLE_VERSION=2.4.0.0 --build-arg BOTO_VERSION=2.47 '
            '--build-arg WINRM_VERSION=0.2.2 --build-arg KERBEROS_VERSION=1.2.5 '
            '--build-arg JINJA2_VERSION=2.8.1 --build-arg ANSIBLE_TOKEN=2.4.0.0 '
            '--label ci.built_from_scratch=false '
            '--file ./Dockerfile '
            '--pull '
            '--rm '
            '--tag registry.b.acme/acme/ansible:2.4.0-1.3-dev '
            '--tag registry.b.acme/acme/ansible:2.4.0-dev '
            '--tag registry.b.acme/acme/ansible:2.4-dev '
            '.')

        self.assertEqual(" ".join(mock.call_args_list[-1][0][0]),
                         expected_build_command)

        build_args = builds[0]['build-args']
        self.assertEqual(len(build_args), 6)
        self.assertEqual(build_args[0], "ANSIBLE_VERSION=2.4.0.0")
        self.assertEqual(build_args[1], "BOTO_VERSION=2.47")
        self.assertEqual(build_args[2], "WINRM_VERSION=0.2.2")
        self.assertEqual(build_args[3], "KERBEROS_VERSION=1.2.5")
        self.assertEqual(build_args[4], "JINJA2_VERSION=2.8.1")
        self.assertEqual(build_args[5], "ANSIBLE_TOKEN=2.4.0.0")

        make.args.no_push = False
        with patch("dockermake.utils.helpers.System._run_command",
                   return_value=("", "", 0)) as mock:
            make._run_docker_build_and_push_commands(builds[1])
        expected_build_command = (
            'docker build '
            '--build-arg ansible_components_version=1.3 --build-arg USERNAME=acme '
            '--build-arg ANSIBLE_VERSION=2.3.2.0 --build-arg BOTO_VERSION=2.47 '
            '--build-arg WINRM_VERSION=0.2.2 --build-arg KERBEROS_VERSION=1.2.5 '
            '--build-arg JINJA2_VERSION=2.8.1 --build-arg ANSIBLE_TOKEN=2.3.2.0 '
            '--label ci.built_from_scratch=false '
            '--file ./Dockerfile '
            '--pull '
            '--rm '
            '--tag registry.b.acme/acme/ansible:2.3.2-1.3-dev '
            '--tag registry.b.acme/acme/ansible:2.3.2-dev '
            '--tag registry.b.acme/acme/ansible:2.3-dev '
            '.')
        self.assertEqual(" ".join(mock.call_args_list[-5][0][-0]),
                         expected_build_command)
        self.assertEqual(
            " ".join(mock.call_args_list[-4][0][-0]),
            "docker push registry.b.acme/acme/ansible:2.3.2-1.3-dev")
        self.assertEqual(" ".join(mock.call_args_list[-3][0][-0]),
                         "docker push registry.b.acme/acme/ansible:2.3.2-dev")
        self.assertEqual(" ".join(mock.call_args_list[-2][0][-0]),
                         "docker push registry.b.acme/acme/ansible:2.3-dev")
        self.assertEqual(
            " ".join(mock.call_args_list[-1][0][0]),
            "docker inspect --format {{ json . }} registry.b.acme/acme/ansible:2.3.2-1.3-dev"
        )

        build_args = builds[1]['build-args']
        self.assertEqual(len(build_args), 6)
        self.assertEqual(build_args[0], "ANSIBLE_VERSION=2.3.2.0")
        self.assertEqual(build_args[1], "BOTO_VERSION=2.47")
        self.assertEqual(build_args[2], "WINRM_VERSION=0.2.2")
        self.assertEqual(build_args[3], "KERBEROS_VERSION=1.2.5")
        self.assertEqual(build_args[4], "JINJA2_VERSION=2.8.1")
        self.assertEqual(build_args[5], "ANSIBLE_TOKEN=2.3.2.0")