Files @ d752715bb533
Branch filter:

Location: majic-ansible-roles/roles/preseed/molecule/default/tests/test_parameters_mandatory.py

branko
MAR-149: Switch to using File.content_string instead of File.content in tests for all roles:

- The .content variant returns contents as bytes, while the
content_string returns a string (which is what is wanted in
practically all cases).
import os

import testinfra.utils.ansible_runner


testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-mandatory')


def test_preseed_directory(host):
    """
    Tests presence and permissions on created preseed directory.
    """

    with host.sudo():

        # Preseed directory created at same level as inventory.
        # @TODO: At this point, this path is getting uglier and
        #        uglier. It might be actually better to make the
        #        preseed_directory a mandatory parameter instead. Same
        #        goes for the ansible_key role parameter as well.
        preseed_directory = host.file(os.path.join('/tmp/molecule/preseed/default/inventory', 'preseed_files'))

        assert preseed_directory.is_directory
        assert preseed_directory.mode == 0o750


def test_preseed_configuration_files(host):
    """
    Tests presence and permissions on created preseed configuration files.
    """

    with host.sudo():

        # Preseed directory created at same level as inventory.
        preseed_directory_path = os.path.join('/tmp/molecule/preseed/default/inventory', 'preseed_files')

        # Verify that preseed configuration files are created for all hosts.
        for testinfra_host in testinfra_hosts:

            preseed_file = host.file(os.path.join(preseed_directory_path, "%s.cfg" % testinfra_host))

            assert preseed_file.is_file
            assert preseed_file.mode == 0o640


def test_preseed_configuration_file_content(host):
    """
    Tests content of generated preseed configuration file.
    """

    hostname = host.run('hostname').stdout.strip()

    with host.sudo():
        preseed_file = host.file(os.path.join("/tmp/molecule/preseed/default/inventory", "preseed_files", "%s.cfg" % hostname))
        preseed_file_content = preseed_file.content_string_string
        ssh_public_key = open(os.path.join(os.path.expanduser("~"), ".ssh", "id_rsa.pub")).read().strip()

    assert "d-i debian-installer/language string en" in preseed_file_content
    assert "d-i debian-installer/country string SE" in preseed_file_content
    assert "d-i debian-installer/locale string en_US.UTF-8" in preseed_file_content
    assert "d-i keyboard-configuration/xkb-keymap select us" in preseed_file_content
    assert "d-i netcfg/choose_interface select eth0" in preseed_file_content

    assert """# DHCP network configuration.
d-i netcfg/disable_autoconfig boolean false
d-i netcfg/get_hostname string ignored-value
d-i netcfg/get_domain string ignored-value""" in preseed_file_content

    assert "d-i mirror/http/hostname string ftp.se.debian.org" in preseed_file_content
    assert "d-i mirror/http/directory string /debian" in preseed_file_content
    assert "d-i mirror/http/proxy string " in preseed_file_content
    assert "d-i passwd/root-password password root" in preseed_file_content
    assert "d-i passwd/root-password-again password root" in preseed_file_content
    assert "d-i time/zone string Europe/Stockholm" in preseed_file_content
    assert ssh_public_key in preseed_file_content