How to use the molecule.util.write_file function in molecule

To help you get started, we’ve selected a few molecule examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ansible / molecule / test / unit / lint / test_trailing.py View on Github external
project_directory = trailing_instance._config.project_directory

    for d in [
            '.foo',
            '.bar',
    ]:
        os.mkdir(os.path.join(project_directory, d))

    for f in [
            'foo.yml',
            'foo.yaml',
            'foo.py',
            '.foo/foo.yml',
            '.bar/foo.yml',
    ]:
        util.write_file(os.path.join(project_directory, f), '')

    # NOTE(retr0h): Unit tests add a molecule.yml automatically.
    assert 4 == len(trailing_instance._get_tests())
github ansible / molecule / test / unit / command / test_prepare.py View on Github external
def test_execute(
    mocker,
    patched_logger_info,
    _patched_ansible_prepare,
    patched_config_validate,
    config_instance,
):
    pb = os.path.join(config_instance.scenario.directory, 'prepare.yml')
    util.write_file(pb, '')

    p = prepare.Prepare(config_instance)
    p.execute()

    x = [mocker.call("Scenario: 'default'"), mocker.call("Action: 'prepare'")]
    assert x == patched_logger_info.mock_calls

    _patched_ansible_prepare.assert_called_once_with()

    assert config_instance.state.prepared
github ansible / molecule / test / unit / command / test_prepare.py View on Github external
def test_execute_when_instances_already_prepared_but_force_provided(
    mocker, patched_logger_warn, _patched_ansible_prepare, config_instance
):
    pb = os.path.join(config_instance.scenario.directory, 'prepare.yml')
    util.write_file(pb, '')

    config_instance.state.change_state('prepared', True)
    config_instance.command_args = {'force': True}

    p = prepare.Prepare(config_instance)
    p.execute()

    _patched_ansible_prepare.assert_called_once_with()
github ansible / molecule / test / unit / test_util.py View on Github external
def test_write_file(temp_dir):
    dest_file = os.path.join(temp_dir.strpath, 'test_util_write_file.tmp')
    contents = binascii.b2a_hex(os.urandom(15)).decode('utf-8')
    util.write_file(dest_file, contents)
    with util.open_file(dest_file) as stream:
        data = stream.read()
    x = '# Molecule managed\n\n{}'.format(contents)

    assert x == data
github ansible / molecule / molecule / provisioner / ansible.py View on Github external
def write_config(self):
        """
        Writes the provisioner's config file to disk and returns None.

        :return: None
        """
        template = util.render_template(
            self._get_config_template(), config_options=self.config_options
        )
        util.write_file(self.config_file, template)
github ansible / molecule / molecule / core.py View on Github external
if target in self.config.config['ansible']:
            vars_target = self.config.config['ansible'][target]
        else:
            return

        molecule_dir = self.config.config['molecule']['molecule_dir']
        target_vars_path = os.path.join(molecule_dir, target)

        if not os.path.exists(os.path.abspath(target_vars_path)):
            os.mkdir(os.path.abspath(target_vars_path))

        for target in vars_target.keys():
            target_var_content = vars_target[target]
            path = os.path.join(os.path.abspath(target_vars_path), target)

            util.write_file(
                path,
                yaml.dump(
                    target_var_content,
                    default_flow_style=False,
                    explicit_start=True))
github ansible / molecule / molecule / config.py View on Github external
def write(self):
        util.write_file(self.config_file, util.safe_dump(self.config))
github ansible / molecule / molecule / core.py View on Github external
if self.args.get('platform') == 'all':
            self.driver.platform = 'all'

        for group, subgroups in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for subgroup in subgroups:
                instance_name = util.format_instance_name(
                    subgroup, self.driver.platform, self.driver.instances)
                if instance_name:
                    inventory += '{}\n'.format(instance_name)
                else:
                    inventory += '{}\n'.format(subgroup)

        inventory_file = self.config.config['ansible']['inventory_file']
        try:
            util.write_file(inventory_file, inventory)
        except IOError:
            msg = 'WARNING: could not write inventory file {}.'.format(
                inventory_file)
            util.print_warn(msg)