def _generate_maven_settings(self):
     # ----- build settings.xml
     maven_servers = ConfigValue.convert_leaves_to_values(
         self.get_config_value('maven-servers'))
     maven_repositories = ConfigValue.convert_leaves_to_values(
         self.get_config_value('maven-repositories'))
     maven_mirrors = ConfigValue.convert_leaves_to_values(
         self.get_config_value('maven-mirrors'))
     return generate_maven_settings(self.get_working_dir(), maven_servers,
                                    maven_repositories, maven_mirrors)
    def test_convert_leaves_to_values_mixed_leaves(self):
        source_values = {
            Config.TSSC_CONFIG_KEY: {
                'step-foo': [{
                    'implementer': ConfigValue('foo1', None, None),
                    'config': {
                        'test1': ConfigValue('foo', None, None),
                        'test': 'not a tssc config value object'
                    }
                }]
            }
        }

        converted = ConfigValue.convert_leaves_to_values(source_values)

        self.assertEqual(
            converted, {
                Config.TSSC_CONFIG_KEY: {
                    'step-foo': [{
                        'implementer': 'foo1',
                        'config': {
                            'test1': 'foo',
                            'test': 'not a tssc config value object'
                        }
                    }]
                }
            })
    def test_convert_leaves_to_values_all_config_value_leaves(self):
        source_values = {
            Config.TSSC_CONFIG_KEY: {
                'step-foo': [{
                    'implementer': ConfigValue('foo1', None, None),
                    'config': {
                        'test1': ConfigValue('foo', None, None)
                    }
                }]
            }
        }

        converted = ConfigValue.convert_leaves_to_values(source_values)

        self.assertEqual(
            converted, {
                Config.TSSC_CONFIG_KEY: {
                    'step-foo': [{
                        'implementer': 'foo1',
                        'config': {
                            'test1': 'foo',
                        }
                    }]
                }
            })
    def test_global_environment_defaults(self):
        tssc_config = Config({
            Config.TSSC_CONFIG_KEY: {
                'global-environment-defaults': {
                    'env1': {
                        'test1': 'global-env1-default-1',
                        'test2': 'global-env1-default-2'
                    },
                    'env2': {
                        'test1': 'global-env2-default-1',
                        'test2': 'global-env2-default-2'
                    }
                },
                'step-foo': [{
                    'implementer': 'foo1',
                    'config': {
                        'test1': 'foo'
                    }
                }]
            }
        })

        step_config = tssc_config.get_step_config('step-foo')
        sub_step = step_config.get_sub_step('foo1')

        self.assertEqual(
            ConfigValue.convert_leaves_to_values(
                sub_step.get_global_environment_defaults('env1')), {
                    'environment-name': 'env1',
                    'test1': 'global-env1-default-1',
                    'test2': 'global-env1-default-2'
                })
        self.assertEqual(
            ConfigValue.convert_leaves_to_values(
                sub_step.get_global_environment_defaults('env2')), {
                    'environment-name': 'env2',
                    'test1': 'global-env2-default-1',
                    'test2': 'global-env2-default-2'
                })
    def _update_values_yaml(self, repo_directory,
                            values_file_repo_relative_path):  # pylint: disable=too-many-locals
        env = Environment(loader=FileSystemLoader(
            self.get_config_value('values-yaml-directory')),
                          trim_blocks=True,
                          lstrip_blocks=True)

        argocd_app_name = self._get_app_name()
        version = self._get_image_version()
        container_image_uri = self.__get_container_image_uri()
        timestamp = str(datetime.now())
        repo_branch = self._get_repo_branch()
        endpoint_url = self._get_endpoint_url()
        jinja_runtime_step_config = {
            'container_image_uri': container_image_uri,
            'image_version': version,
            'timestamp': timestamp,
            'repo_branch': repo_branch,
            'deployment_namespace': argocd_app_name,
            'endpoint_url': endpoint_url
        }

        copy_of_runtime_step_config = ConfigValue.convert_leaves_to_values(
            self.get_copy_of_runtime_step_config())
        for key in copy_of_runtime_step_config:
            jinja_runtime_step_config[key.replace(
                '-', '_')] = copy_of_runtime_step_config[key]

        template = env.get_template(
            self.get_config_value('values-yaml-template'))

        rendered_values_file = self.write_working_file(
            'values.yml',
            bytes(template.render(jinja_runtime_step_config), 'utf-8'))

        try:
            shutil.copyfile(
                rendered_values_file,
                os.path.join(repo_directory, values_file_repo_relative_path))
        except (shutil.SameFileError, OSError, IOError) as error:
            raise RuntimeError(
                "Error copying {values_file} file: {all}".format(
                    values_file=values_file_repo_relative_path,
                    all=error)) from error
Exemple #6
0
    def test_add_or_update_sub_step_config_exising_sub_step(self):
        tssc_config = Config([
            {
                Config.TSSC_CONFIG_KEY: {
                    'step-foo': [
                        {
                            'implementer': 'foo1',
                            'config': {
                                'test1': 'foo'
                            }
                        }
                    ]

                }
            },
            {
                Config.TSSC_CONFIG_KEY: {
                    'step-foo': [
                        {
                            'implementer': 'foo1',
                            'config': {
                                'test2': 'foo'
                            }
                        }
                    ]

                }
            },
        ])

        step_config = tssc_config.get_step_config('step-foo')
        self.assertEqual(len(step_config.sub_steps), 1)

        self.assertEqual(
            ConfigValue.convert_leaves_to_values(
                step_config.get_sub_step('foo1').sub_step_config
            ),
            {
                'test1': 'foo',
                'test2': 'foo'
            }
        )
    def test_get_sub_step_env_config(self):
        tssc_config = Config({
            Config.TSSC_CONFIG_KEY: {
                'step-foo': [{
                    'implementer': 'foo1',
                    'config': {
                        'test1': 'foo'
                    },
                    'environment-config': {
                        'env1': {
                            'test2': 'bar'
                        }
                    }
                }]
            }
        })

        step_config = tssc_config.get_step_config('step-foo')
        sub_step = step_config.get_sub_step('foo1')

        self.assertEqual(
            ConfigValue.convert_leaves_to_values(
                sub_step.get_sub_step_env_config('env1')), {'test2': 'bar'})
    def test_get_copy_of_runtime_step_config_default_global_defaults_global_env_defaults_sub_step_config_sub_step_env_config_step_config_overrides(
            self):
        tssc_config = Config({
            Config.TSSC_CONFIG_KEY: {
                'global-defaults': {
                    'global-default-unique-0': 'global-default',
                    'global-default-override-by-global-env-default-0':
                    'global-default-override-me',
                    'global-default-override-by-step-config-0':
                    'global-default-override-me',
                    'global-default-override-by-step-env-config-0':
                    'global-default-override-me',
                    'global-default-override-by-step-config-override-0':
                    'global-default-override-me',
                    'default-overriden-by-global-default': 'global-default',
                },
                'global-environment-defaults': {
                    'env1': {
                        'global-default-override-by-global-env-default-0':
                        'global-environment-defaults-1',
                        'global-env-default-env1-unique-0':
                        'global-environment-defaults-1',
                        'global-env-default-override-by-step-config':
                        'global-environment-defaults-1',
                        'default-overriden-by-global-env-default':
                        'global-environment-defaults-1'
                    },
                    'env2': {
                        'global-default-override-by-global-env-default-0':
                        'global-environment-defaults-2',
                        'global-env-default-env2-unique-0':
                        'global-environment-defaults-2',
                        'global-env-default-override-by-step-config':
                        'global-environment-defaults-2',
                        'default-overriden-by-global-env-default':
                        'global-environment-defaults-2'
                    }
                },
                'step-foo': [{
                    'implementer': 'foo1',
                    'config': {
                        'step-foo-foo1-unique-0': 'step-foo-foo1',
                        'step-foo-foo1-override-by-step-env-config':
                        'step-foo-foo-override-me',
                        'step-foo-foo1-override-by-step-override':
                        'step-foo-foo-override-me',
                        'global-default-override-by-step-config-0':
                        'step-foo-foo1',
                        'global-env-default-override-by-step-config':
                        'step-foo-foo1',
                        'default-overriden-by-step-config': 'step-foo-foo1'
                    },
                    'environment-config': {
                        'env1': {
                            'step-foo-foo1-override-by-step-env-config':
                            'step-foo-foo-env1',
                            'global-default-override-by-step-env-config-0':
                            'step-foo-foo-env1',
                            'step-foo-foo1-env1-unique-0':
                            'step-foo-foo-env1',
                            'step-foo-foo1-env-specific':
                            'step-foo-foo-env1',
                            'default-overriden-by-step-env-config':
                            'step-foo-foo-env1',
                        },
                        'env2': {
                            'step-foo-foo1-override-by-step-env-config':
                            'step-foo-foo-env2',
                            'global-default-override-by-step-env-config-0':
                            'step-foo-foo-env2',
                            'step-foo-foo1-env2-unique-0':
                            'step-foo-foo-env2',
                            'step-foo-foo1-env-specific':
                            'step-foo-foo-env2',
                            'default-overriden-by-step-env-config':
                            'step-foo-foo-env2',
                        }
                    }
                }]
            }
        })

        tssc_config.set_step_config_overrides(
            'step-foo', {
                'global-default-override-by-step-config-override-0':
                'step-foo-step-config-override',
                'step-foo-foo1-override-by-step-override':
                'step-foo-step-config-override',
                'step-config-override-unique-0':
                'step-config-override-unique',
                'default-overriden-by-step-config-overrides':
                'step-foo-step-config-override'
            })

        step_config = tssc_config.get_step_config('step-foo')
        sub_step = step_config.get_sub_step('foo1')

        defaults = {
            'default-unique': 'not-overriden-default',
            'default-overriden-by-global-default': 'override-me',
            'default-overriden-by-global-env-default': 'override-me',
            'default-overriden-by-step-config': 'override-me',
            'default-overriden-by-step-env-config': 'override-me',
            'default-overriden-by-step-config-overrides': 'override-me'
        }

        runtime_step_config_no_given_env = sub_step.get_copy_of_runtime_step_config(
            None, defaults)
        self.assertEqual(
            ConfigValue.convert_leaves_to_values(
                runtime_step_config_no_given_env), {
                    'global-default-unique-0':
                    'global-default',
                    'global-default-override-by-global-env-default-0':
                    'global-default-override-me',
                    'global-default-override-by-step-config-0':
                    'step-foo-foo1',
                    'global-default-override-by-step-env-config-0':
                    'global-default-override-me',
                    'step-foo-foo1-unique-0':
                    'step-foo-foo1',
                    'step-foo-foo1-override-by-step-env-config':
                    'step-foo-foo-override-me',
                    'global-default-override-by-step-config-override-0':
                    'step-foo-step-config-override',
                    'step-foo-foo1-override-by-step-override':
                    'step-foo-step-config-override',
                    'step-config-override-unique-0':
                    'step-config-override-unique',
                    'global-env-default-override-by-step-config':
                    'step-foo-foo1',
                    'default-unique':
                    'not-overriden-default',
                    'default-overriden-by-global-default':
                    'global-default',
                    'default-overriden-by-global-env-default':
                    'override-me',
                    'default-overriden-by-step-config':
                    'step-foo-foo1',
                    'default-overriden-by-step-env-config':
                    'override-me',
                    'default-overriden-by-step-config-overrides':
                    'step-foo-step-config-override'
                })

        runtime_step_config_env1 = sub_step.get_copy_of_runtime_step_config(
            'env1', defaults)
        self.assertEqual(
            ConfigValue.convert_leaves_to_values(runtime_step_config_env1), {
                'environment-name':
                'env1',
                'global-default-unique-0':
                'global-default',
                'global-default-override-by-global-env-default-0':
                'global-environment-defaults-1',
                'global-default-override-by-step-config-0':
                'step-foo-foo1',
                'global-default-override-by-step-env-config-0':
                'step-foo-foo-env1',
                'step-foo-foo1-unique-0':
                'step-foo-foo1',
                'step-foo-foo1-override-by-step-env-config':
                'step-foo-foo-env1',
                'global-default-override-by-step-config-override-0':
                'step-foo-step-config-override',
                'step-foo-foo1-override-by-step-override':
                'step-foo-step-config-override',
                'step-config-override-unique-0':
                'step-config-override-unique',
                'global-env-default-override-by-step-config':
                'step-foo-foo1',
                'global-env-default-env1-unique-0':
                'global-environment-defaults-1',
                'step-foo-foo1-env1-unique-0':
                'step-foo-foo-env1',
                'step-foo-foo1-env-specific':
                'step-foo-foo-env1',
                'default-unique':
                'not-overriden-default',
                'default-overriden-by-global-default':
                'global-default',
                'default-overriden-by-global-env-default':
                'global-environment-defaults-1',
                'default-overriden-by-step-config':
                'step-foo-foo1',
                'default-overriden-by-step-env-config':
                'step-foo-foo-env1',
                'default-overriden-by-step-config-overrides':
                'step-foo-step-config-override'
            })

        runtime_step_config_env2 = sub_step.get_copy_of_runtime_step_config(
            'env2', defaults)
        self.assertEqual(
            ConfigValue.convert_leaves_to_values(runtime_step_config_env2), {
                'environment-name':
                'env2',
                'global-default-unique-0':
                'global-default',
                'global-default-override-by-global-env-default-0':
                'global-environment-defaults-2',
                'global-default-override-by-step-config-0':
                'step-foo-foo1',
                'global-default-override-by-step-env-config-0':
                'step-foo-foo-env2',
                'step-foo-foo1-unique-0':
                'step-foo-foo1',
                'step-foo-foo1-override-by-step-env-config':
                'step-foo-foo-env2',
                'global-default-override-by-step-config-override-0':
                'step-foo-step-config-override',
                'step-foo-foo1-override-by-step-override':
                'step-foo-step-config-override',
                'step-config-override-unique-0':
                'step-config-override-unique',
                'global-env-default-override-by-step-config':
                'step-foo-foo1',
                'global-env-default-env2-unique-0':
                'global-environment-defaults-2',
                'step-foo-foo1-env2-unique-0':
                'step-foo-foo-env2',
                'step-foo-foo1-env-specific':
                'step-foo-foo-env2',
                'default-unique':
                'not-overriden-default',
                'default-overriden-by-global-default':
                'global-default',
                'default-overriden-by-global-env-default':
                'global-environment-defaults-2',
                'default-overriden-by-step-config':
                'step-foo-foo1',
                'default-overriden-by-step-env-config':
                'step-foo-foo-env2',
                'default-overriden-by-step-config-overrides':
                'step-foo-step-config-override'
            })