Beispiel #1
0
    def test_find_path_by_name_founds_path(self):
        """Assert that makefile.yml will be searched in RKD_PATH"""

        yaml_loader = YamlFileLoader([])

        d = tempfile.TemporaryDirectory()
        os.environ['RKD_PATH'] = d.name

        with open(d.name + '/makefile.yml', 'w') as f:
            f.write('''
        version: org.riotkit.rkd/yaml/v1
        imports: []
        tasks: 
            :join:iwa-ait:
                description: Subscribe to any local section of IWA-AIT, workers have common interest
                arguments:
                    - not a list
                    ''')

        try:
            path = yaml_loader.find_path_by_name('makefile.yml', '/')
            self.assertTrue(len(path) > 0)
        finally:
            d.cleanup()
            os.environ['RKD_PATH'] = ''
Beispiel #2
0
    def test_loads_from_file_is_searching_in_rkd_path(self):
        """Assert that makefile.yml will be searched in RKD_PATH"""

        yaml_loader = YamlFileLoader([])

        d = tempfile.TemporaryDirectory()
        os.environ['RKD_PATH'] = d.name

        with open(d.name + '/makefile.yml', 'w') as f:
            f.write('''
version: org.riotkit.rkd/yaml/v1
imports: []
tasks: 
    :join:iwa-ait:
        description: Subscribe to any local section of IWA-AIT, workers have common interest
        arguments:
            - not a list
            ''')

        try:
            self.assertRaises(
                YAMLFileValidationError, lambda: yaml_loader.load_from_file(
                    'makefile.yml', 'org.riotkit.rkd/yaml/v1'))
        finally:
            d.cleanup()
            os.environ['RKD_PATH'] = ''
Beispiel #3
0
    def test_invalid_file_path_is_causing_exception(self):
        """Test that invalid path will be reported quickly"""

        yaml_loader = YamlFileLoader([])
        self.assertRaises(
            FileNotFoundError, lambda: yaml_loader.load_from_file(
                'non-existing-file.yml', 'org.riotkit.rkd/yaml/v1'))
Beispiel #4
0
    def test_find_path_by_name_does_not_found_anything(self):
        """Verify that find_path_by_name() will not return anything if nothing searched was found"""

        yaml_loader = YamlFileLoader([])
        self.assertEqual(
            '',
            yaml_loader.find_path_by_name('some-file-that-does-not-exists',
                                          ''))
Beispiel #5
0
    def get_config(self) -> dict:
        """Loads and parses deployment.yml file.

        Supports:
            - Ansible Vault encryption of deployment.yml
            - SSH private key storage inside deployment.yml
        """

        deployment_filenames = ['deployment.yml', 'deployment.yaml']

        try:
            self._config
        except AttributeError:

            # try multiple files
            for filename in deployment_filenames:
                if os.path.isfile(filename):

                    #
                    # Check file contents before
                    #
                    with open(filename, 'rb') as f:
                        content = f.read().decode('utf-8')
                        #
                        # When file is encrypted, then decrypt it
                        #
                        if content.startswith('$ANSIBLE_VAULT;'):
                            tmp_vault_path, tmp_vault_filename = self.temp.create_tmp_file_path(
                            )

                            self.io().info('Decrypting deployment file')
                            self.sh('cp %s %s' % (filename, tmp_vault_path))

                            self.io().info_msg(
                                'Need a vault passphrase to decrypt "%s"' %
                                filename)
                            self.rkd([
                                ':harbor:vault:encrypt', '-d', tmp_vault_path
                            ] + self.vault_args)
                            self._config = YamlFileLoader(
                                self._ctx.directories).load_from_file(
                                    tmp_vault_filename,
                                    'org.riotkit.harbor/deployment/v1')

                            self._process_config_private_keys()
                            return self._config

                    self._config = YamlFileLoader(
                        self._ctx.directories).load_from_file(
                            filename, 'org.riotkit.harbor/deployment/v1')

                    self._process_config_private_keys()
                    return self._config

            raise MissingDeploymentConfigurationError()

        return self._config
Beispiel #6
0
    def test_validates_successfully(self):
        yaml_loader = YamlFileLoader([])

        parsed = yaml_loader.load('''
            version: org.riotkit.rkd/yaml/v1
            imports: []
            tasks: {}
            ''',
                                  schema_name='org.riotkit.rkd/yaml/v1')

        self.assertIn('version', parsed)
Beispiel #7
0
    def test_raises_error_when_type_does_not_match(self):
        """Expect OBJECT at .tasks path, but get ARRAY instead"""

        yaml_loader = YamlFileLoader([])

        self.assertRaises(
            YAMLFileValidationError,
            lambda: yaml_loader.load('''
                version: org.riotkit.rkd/yaml/v1
                imports: []
                tasks: []
                ''',
                                     schema_name='org.riotkit.rkd/yaml/v1'))
Beispiel #8
0
    def test_parse_env_preserves_variables_order(self):
        """Make sure that the environment variables are loaded in order they were defined
        """

        yaml_content = '''
environment:
    FIRST:  "Jolanta Brzeska"
    SECOND: "Maxwell Itoya"
    THIRD:  "August Spies"
    FOURTH: "Samuel Fielden"
        '''

        expected_order = [
            "Jolanta Brzeska", "Maxwell Itoya", "August Spies",
            "Samuel Fielden"
        ]

        for i in range(1, 10000):
            parsed = yaml.load(yaml_content, yaml.FullLoader)

            io = IO()
            factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
            envs = factory.parse_env(parsed, 'makefile.yaml')

            names_in_order = []

            for env_name, value in envs.items():
                names_in_order.append(env_name)

            self.assertEqual(expected_order, list(envs.values()))
Beispiel #9
0
    def test_expect_path_will_be_shown_in_exception_message(self):
        """Simply check if path to the attribute will be printed within the exception"""

        yaml_loader = YamlFileLoader([])

        try:
            yaml_loader.load('''
                version: org.riotkit.rkd/yaml/v1
                imports: []
                tasks: 
                    :join:iwa-ait: []
                ''',
                             schema_name='org.riotkit.rkd/yaml/v1')
        except YAMLFileValidationError as e:
            self.assertIn(
                "YAML schema validation failed at path \"tasks.:join:iwa-ait\" with error: [] is not of type 'object'",
                str(e))
            return

        self.fail('Expected an exception to be raised')
Beispiel #10
0
    def test_get_lookup_paths_includes_internal_path_as_well_as_rkd_path(self):
        """Verify that lookup paths includes RKD_PATH and internal RKD directories"""

        yaml_loader = YamlFileLoader([])
        os.environ['RKD_PATH'] = 'SOME-PATH-THERE'

        try:
            paths = yaml_loader.get_lookup_paths('harbor-internal/')
        finally:
            os.environ['RKD_PATH'] = ''

        defined_by_rkd_path = paths.index('SOME-PATH-THERE/harbor-internal/')

        internal_path = (os.path.realpath(SCRIPT_DIR_PATH) +
                         '/harbor-internal/').replace('test/', '')
        internal_path_index = paths.index(internal_path)

        self.assertGreater(defined_by_rkd_path,
                           internal_path_index,
                           msg='defined_by_rkd_path should be favored')
Beispiel #11
0
    def test_expect_deeper_validation_will_be_performed(self):
        """Expects that argparse arguments will be validated"""

        yaml_loader = YamlFileLoader([])

        try:
            yaml_loader.load('''
version: org.riotkit.rkd/yaml/v1
imports: []
tasks: 
    :join:iwa-ait:
        description: Subscribe to any local section of IWA-AIT, workers have common interest
        arguments:
            - not a list
                        ''',
                             schema_name='org.riotkit.rkd/yaml/v1')
        except YAMLFileValidationError as e:
            self.assertIn("tasks.:join:iwa-ait.arguments", str(e))
            self.assertIn("is not of type 'object'", str(e))
            return

        self.fail('Expected an exception to be raised')
Beispiel #12
0
    def test_parse_env_parses_environment_variables_added_manually(self):
        """Test "environment" block
        """

        io = IO()
        factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
        envs = factory.parse_env(
            {
                'environment': {
                    'EVENT_NAME':
                    'In memory of Maxwell Itoya, an Nigerian immigrant killed by police at flea market.'
                    + ' He was innocent, and left wife with 3 kids.'
                }
            }, 'make/file/path/makefile.yaml')

        self.assertIn('EVENT_NAME', envs)
        self.assertIn('Maxwell Itoya', envs['EVENT_NAME'])
Beispiel #13
0
    def test_parse_tasks_signals_error_instead_of_throwing_exception(self):
        """
        Test that error thrown by executed Python code will
        """

        input_tasks = {
            ':song': {
                'description':
                'Bella Ciao is an Italian protest folk song that originated in the hardships of the '
                +
                'mondina women, the paddy field workers in the late 19th century who sang it to '
                +
                'protest against harsh working conditions in the paddy fields of North Italy',
                'steps':
                ['''#!python
print(syntax-error-here)
                    ''']
            }
        }

        io = BufferedSystemIO()
        factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
        parsed_tasks = factory.parse_tasks(input_tasks, '', 'makefile.yaml',
                                           {})

        declaration = parsed_tasks[0]
        declaration.get_task_to_execute()._io = IO()
        task = declaration.get_task_to_execute()
        task._io = io

        # execute prepared task
        result = task.execute(ExecutionContext(declaration))

        self.assertEqual(
            False,
            result,
            msg='Expected that syntax error would result in a failure')
        self.assertIn("NameError: name 'syntax' is not defined",
                      io.get_value(),
                      msg='Error message should be attached')
        self.assertIn('File ":song@step 1", line 1',
                      io.get_value(),
                      msg='Stacktrace should be attached')
Beispiel #14
0
    def test_parse_env_parses_environment_variables_from_file(self):
        """Check if envs are loaded from file correctly
        """

        io = IO()
        factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
        envs = factory.parse_env(
            {
                'env_files': [
                    SCRIPT_DIR_PATH +
                    '/../docs/examples/env-in-yaml/.rkd/env/global.env'
                ]
            }, SCRIPT_DIR_PATH +
            '/../docs/examples/env-in-yaml/.rkd/makefile.yml')

        self.assertIn('TEXT_FROM_GLOBAL_ENV', envs)
        self.assertIn(
            'Jolanta Brzeska was a social activist against evictions, ' +
            'she was murdered - burned alive by reprivatization mafia',
            envs['TEXT_FROM_GLOBAL_ENV'])
Beispiel #15
0
    def test_parse_tasks_successful_case(self):
        """Successful case with description, arguments and bash steps
        """

        input_tasks = {
            ':resistentia': {
                'description':
                'Against moving the costs of the crisis to the workers!',
                'arguments': {
                    '--picket': {
                        'help': 'Picket form',
                        'required': False,
                        'action': 'store_true'
                    }
                },
                'steps': ['echo "Resistentia!"']
            }
        }

        io = IO()
        out = StringIO()
        factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
        parsed_tasks = factory.parse_tasks(input_tasks, '', './makefile.yaml',
                                           {})

        self.assertEqual(':resistentia',
                         parsed_tasks[0].to_full_name(),
                         msg='Expected that the task name will be present')

        declaration = parsed_tasks[0]
        declaration.get_task_to_execute()._io = NullSystemIO()

        with io.capture_descriptors(stream=out, enable_standard_out=False):
            declaration.get_task_to_execute().execute(
                ExecutionContext(declaration))

        self.assertIn('Resistentia!',
                      out.getvalue(),
                      msg='Expected that echo contents will be visible')