Example #1
0
    def test_get_action_config(
        self,
        mock_load_deployments,
        action_service,
        action_deploy,
    ):
        """Check resulting action config with various overrides from the action."""
        action_dict = {
            'command': 'echo first',
        }
        if action_service:
            action_dict['service'] = action_service
        if action_deploy:
            action_dict['deploy_group'] = action_deploy

        job_service = 'my_service'
        job_deploy = 'prod'
        expected_service = action_service or job_service
        expected_deploy = action_deploy or job_deploy
        expected_cluster = 'paasta-dev'

        job_dict = {
            'node': 'batch_server',
            'schedule': 'daily 12:10:00',
            'service': job_service,
            'deploy_group': job_deploy,
            'max_runtime': '2h',
            'actions': {
                'normal': action_dict
            },
        }

        soa_dir = '/other_dir'
        job_config = tron_tools.TronJobConfig('my_job',
                                              job_dict,
                                              expected_cluster,
                                              soa_dir=soa_dir)

        action_config = job_config._get_action_config('normal',
                                                      action_dict=action_dict)

        mock_load_deployments.assert_called_once_with(expected_service,
                                                      soa_dir)
        mock_deployments_json = mock_load_deployments.return_value
        mock_deployments_json.get_docker_image_for_deploy_group.assert_called_once_with(
            expected_deploy)
        mock_deployments_json.get_git_sha_for_deploy_group.assert_called_once_with(
            expected_deploy)
        expected_branch_dict = {
            'docker_image':
            mock_deployments_json.get_docker_image_for_deploy_group.
            return_value,
            'git_sha':
            mock_deployments_json.get_git_sha_for_deploy_group.return_value,
            'desired_state':
            'start',
            'force_bounce':
            None,
        }

        expected_input_action_config = {
            'command': 'echo first',
            'service': expected_service,
            'deploy_group': expected_deploy,
        }

        assert action_config == tron_tools.TronActionConfig(
            service=expected_service,
            instance=tron_tools.compose_instance('my_job', 'normal'),
            config_dict=expected_input_action_config,
            branch_dict=expected_branch_dict,
            soa_dir=soa_dir,
            cluster=expected_cluster,
        )
Example #2
0
    def test_format_tron_action_dict_paasta(self):
        action_dict = {
            "command":
            "echo something",
            "requires": ["required_action"],
            "retries":
            2,
            "retries_delay":
            "5m",
            "service":
            "my_service",
            "deploy_group":
            "prod",
            "executor":
            "paasta",
            "cpus":
            2,
            "mem":
            1200,
            "disk":
            42,
            "pool":
            "special_pool",
            "env": {
                "SHELL": "/bin/bash"
            },
            "extra_volumes": [{
                "containerPath": "/nail/tmp",
                "hostPath": "/nail/tmp",
                "mode": "RW"
            }],
            "trigger_downstreams":
            True,
            "triggered_by": ["foo.bar.{shortdate}"],
            "trigger_timeout":
            "5m",
        }
        branch_dict = {
            "docker_image": "my_service:paasta-123abcde",
            "git_sha": "aabbcc44",
            "desired_state": "start",
            "force_bounce": None,
        }
        action_config = tron_tools.TronActionConfig(
            service="my_service",
            instance=tron_tools.compose_instance("my_job", "do_something"),
            config_dict=action_dict,
            branch_dict=branch_dict,
            cluster="test-cluster",
        )

        with mock.patch.object(
                action_config,
                "get_docker_registry",
                return_value="docker-registry.com:400"), mock.patch(
                    "paasta_tools.utils.InstanceConfig.use_docker_disk_quota",
                    autospec=True,
                    return_value=False,
                ):
            result = tron_tools.format_tron_action_dict(action_config)

        assert result == {
            "command":
            "echo something",
            "requires": ["required_action"],
            "retries":
            2,
            "retries_delay":
            "5m",
            "docker_image":
            mock.ANY,
            "executor":
            "mesos",
            "cpus":
            2,
            "mem":
            1200,
            "disk":
            42,
            "env":
            mock.ANY,
            "extra_volumes": [{
                "container_path": "/nail/tmp",
                "host_path": "/nail/tmp",
                "mode": "RW"
            }],
            "docker_parameters":
            mock.ANY,
            "constraints": [{
                "attribute": "pool",
                "operator": "LIKE",
                "value": "special_pool"
            }],
            "trigger_downstreams":
            True,
            "triggered_by": ["foo.bar.{shortdate}"],
            "trigger_timeout":
            "5m",
        }
        expected_docker = "{}/{}".format("docker-registry.com:400",
                                         branch_dict["docker_image"])
        assert result["docker_image"] == expected_docker
        assert result["env"]["SHELL"] == "/bin/bash"
        assert isinstance(result["docker_parameters"], list)
Example #3
0
    def test_format_tron_action_dict_paasta_no_branch_dict(self):
        action_dict = {
            "command":
            "echo something",
            "requires": ["required_action"],
            "retries":
            2,
            "service":
            "my_service",
            "deploy_group":
            "prod",
            "executor":
            "paasta",
            "cpus":
            2,
            "mem":
            1200,
            "disk":
            42,
            "pool":
            "special_pool",
            "env": {
                "SHELL": "/bin/bash"
            },
            "extra_volumes": [{
                "containerPath": "/nail/tmp",
                "hostPath": "/nail/tmp",
                "mode": "RW"
            }],
        }
        action_config = tron_tools.TronActionConfig(
            service="my_service",
            instance=tron_tools.compose_instance("my_job", "do_something"),
            config_dict=action_dict,
            branch_dict=None,
            cluster="paasta-dev",
        )

        with mock.patch(
                "paasta_tools.utils.InstanceConfig.use_docker_disk_quota",
                autospec=True,
                return_value=False,
        ):
            result = tron_tools.format_tron_action_dict(action_config)

        assert result == {
            "command":
            "echo something",
            "requires": ["required_action"],
            "retries":
            2,
            "docker_image":
            "",
            "executor":
            "mesos",
            "cpus":
            2,
            "mem":
            1200,
            "disk":
            42,
            "env":
            mock.ANY,
            "extra_volumes": [{
                "container_path": "/nail/tmp",
                "host_path": "/nail/tmp",
                "mode": "RW"
            }],
            "docker_parameters":
            mock.ANY,
            "constraints": [{
                "attribute": "pool",
                "operator": "LIKE",
                "value": "special_pool"
            }],
        }
        assert result["env"]["SHELL"] == "/bin/bash"
        assert isinstance(result["docker_parameters"], list)
Example #4
0
 def test_compose_instance(self):
     result = tron_tools.compose_instance("great_job", "fast_action")
     assert result == "great_job.fast_action"
Example #5
0
    def test_get_action_config(self, mock_load_deployments, action_service,
                               action_deploy):
        """Check resulting action config with various overrides from the action."""
        action_dict = {"command": "echo first"}
        if action_service:
            action_dict["service"] = action_service
        if action_deploy:
            action_dict["deploy_group"] = action_deploy

        job_service = "my_service"
        job_deploy = "prod"
        expected_service = action_service or job_service
        expected_deploy = action_deploy or job_deploy
        expected_cluster = "paasta-dev"

        job_dict = {
            "node": "batch_server",
            "schedule": "daily 12:10:00",
            "service": job_service,
            "deploy_group": job_deploy,
            "max_runtime": "2h",
            "actions": {
                "normal": action_dict
            },
            "monitoring": {
                "team": "noop"
            },
        }

        soa_dir = "/other_dir"
        job_config = tron_tools.TronJobConfig("my_job",
                                              job_dict,
                                              expected_cluster,
                                              soa_dir=soa_dir)

        action_config = job_config._get_action_config("normal",
                                                      action_dict=action_dict)

        mock_load_deployments.assert_called_once_with(expected_service,
                                                      soa_dir)
        mock_deployments_json = mock_load_deployments.return_value
        mock_deployments_json.get_docker_image_for_deploy_group.assert_called_once_with(
            expected_deploy)
        mock_deployments_json.get_git_sha_for_deploy_group.assert_called_once_with(
            expected_deploy)
        expected_branch_dict = {
            "docker_image":
            mock_deployments_json.get_docker_image_for_deploy_group.
            return_value,
            "git_sha":
            mock_deployments_json.get_git_sha_for_deploy_group.return_value,
            "desired_state":
            "start",
            "force_bounce":
            None,
        }

        expected_input_action_config = {
            "command": "echo first",
            "service": expected_service,
            "deploy_group": expected_deploy,
            "monitoring": {
                "team": "noop"
            },
        }

        assert action_config == tron_tools.TronActionConfig(
            service=expected_service,
            instance=tron_tools.compose_instance("my_job", "normal"),
            config_dict=expected_input_action_config,
            branch_dict=expected_branch_dict,
            soa_dir=soa_dir,
            cluster=expected_cluster,
        )
Example #6
0
    def test_format_tron_action_dict_paasta_no_branch_dict(self):
        action_dict = {
            'name':
            'do_something',
            'command':
            'echo something',
            'requires': ['required_action'],
            'retries':
            2,
            'service':
            'my_service',
            'deploy_group':
            'prod',
            'executor':
            'paasta',
            'cpus':
            2,
            'mem':
            1200,
            'pool':
            'special_pool',
            'env': {
                'SHELL': '/bin/bash'
            },
            'extra_volumes': [
                {
                    'containerPath': '/nail/tmp',
                    'hostPath': '/nail/tmp',
                    'mode': 'RW'
                },
            ],
        }
        action_config = tron_tools.TronActionConfig(
            service='my_service',
            instance=tron_tools.compose_instance('my_job', 'do_something'),
            config_dict=action_dict,
            branch_dict=None,
            cluster="paasta-dev",
        )

        result = tron_tools.format_tron_action_dict(action_config,
                                                    '{cluster:s}.com')

        assert result == {
            'name':
            'do_something',
            'command':
            'echo something',
            'requires': ['required_action'],
            'retries':
            2,
            'mesos_address':
            'paasta-dev.com',
            'docker_image':
            '',
            'executor':
            'mesos',
            'cpus':
            2,
            'mem':
            1200,
            'env':
            mock.ANY,
            'extra_volumes': [{
                'container_path': '/nail/tmp',
                'host_path': '/nail/tmp',
                'mode': 'RW',
            }],
            'docker_parameters':
            mock.ANY,
            'constraints': [
                {
                    'attribute': 'pool',
                    'operator': 'LIKE',
                    'value': 'special_pool',
                },
            ],
        }
        assert result['env']['SHELL'] == '/bin/bash'
        assert isinstance(result['docker_parameters'], list)
Example #7
0
    def test_format_tron_action_dict_paasta(self):
        action_dict = {
            'name':
            'do_something',
            'command':
            'echo something',
            'requires': ['required_action'],
            'retries':
            2,
            'retries_delay':
            '5m',
            'service':
            'my_service',
            'deploy_group':
            'prod',
            'executor':
            'paasta',
            'cpus':
            2,
            'mem':
            1200,
            'pool':
            'special_pool',
            'env': {
                'SHELL': '/bin/bash'
            },
            'extra_volumes': [
                {
                    'containerPath': '/nail/tmp',
                    'hostPath': '/nail/tmp',
                    'mode': 'RW'
                },
            ],
        }
        branch_dict = {
            'docker_image': 'my_service:paasta-123abcde',
            'git_sha': 'aabbcc44',
            'desired_state': 'start',
            'force_bounce': None,
        }
        action_config = tron_tools.TronActionConfig(
            service='my_service',
            instance=tron_tools.compose_instance('my_job', 'do_something'),
            config_dict=action_dict,
            branch_dict=branch_dict,
            cluster="test-cluster",
        )

        with mock.patch.object(
                action_config,
                'get_docker_registry',
                return_value='docker-registry.com:400',
        ):
            result = tron_tools.format_tron_action_dict(
                action_config, '{cluster:s}.com')

        assert result == {
            'name':
            'do_something',
            'command':
            'echo something',
            'requires': ['required_action'],
            'retries':
            2,
            'retries_delay':
            '5m',
            'mesos_address':
            'test-cluster.com',
            'docker_image':
            mock.ANY,
            'executor':
            'mesos',
            'cpus':
            2,
            'mem':
            1200,
            'env':
            mock.ANY,
            'extra_volumes': [{
                'container_path': '/nail/tmp',
                'host_path': '/nail/tmp',
                'mode': 'RW',
            }],
            'docker_parameters':
            mock.ANY,
            'constraints': [
                {
                    'attribute': 'pool',
                    'operator': 'LIKE',
                    'value': 'special_pool',
                },
            ],
        }
        expected_docker = '{}/{}'.format('docker-registry.com:400',
                                         branch_dict['docker_image'])
        assert result['docker_image'] == expected_docker
        assert result['env']['SHELL'] == '/bin/bash'
        assert isinstance(result['docker_parameters'], list)
Example #8
0
 def test_compose_instance(self):
     result = tron_tools.compose_instance('great_job', 'fast_action')
     assert result == 'great_job.fast_action'