def step_an_ini_file_filepath_modified_with(ctx, filepath): """ Similar to :ref:`Given an INI file "{filepath}" with`, but accepts table with modifications that should be made in the respective INI file. Requires table with following headers: ========= ===== ======= Section Key Value ========= ===== ======= Examples: .. code-block:: gherkin Feature: Modifying an INI file Scenario: Editing /etc/dnf/dnf.conf Given an INI file "/etc/dnf/dnf.conf" modified with | Section | Key | Value | | main | debuglevel | 1 | | | -gpgcheck | | .. note:: Section or Key prefixed with '-' results in the removal of the respective record. """ updates = table_utils.parse_skv_table(ctx, HEADINGS_INI) sections = list( updates.keys()) # convert to list as py3 returns an iterator sections.sort() # sort so we have removal first conf = configparser.ConfigParser() conf.read(filepath) for section in sections: settings = updates[section] if section.startswith("-"): section = section[1:] ctx.assertion.assertTrue( conf.remove_section(section), "No such section '%s' in '%s'" % (section, filepath)) else: if not conf.has_section(section): conf.add_section(section) keys = settings.keys() for key in keys: if key.startswith("-"): key = key[1:] ctx.assertion.assertTrue( conf.remove_option(section, key), "No such key '%s' in section '%s' in '%s'" % (key, section, filepath)) else: conf.set(section, key, settings[key]) file_utils.create_file_with_contents(filepath, conf)
def step_a_file_filepath_with(ctx, filepath): """ Create/Re-Create file (``{filepath}``) and write multiline text inside with newline at the end of file. .. note:: Automatically creates all leading directories. """ ctx.assertion.assertIsNotNone(ctx.text, "Multiline text is not provided") file_utils.create_file_with_contents(filepath, ctx.text)
def step_an_ini_file_filepath_modified_with(ctx, filepath): """ Similar to :ref:`Given an INI file "{filepath}" with`, but accepts table with modifications that should be made in the respective INI file. Requires table with following headers: ========= ===== ======= Section Key Value ========= ===== ======= Examples: .. code-block:: gherkin Feature: Modifying an INI file Scenario: Editing /etc/dnf/dnf.conf Given an INI file "/etc/dnf/dnf.conf" modified with | Section | Key | Value | | main | debuglevel | 1 | | | -gpgcheck | | .. note:: Section or Key prefixed with '-' results in the removal of the respective record. """ updates = table_utils.parse_skv_table(ctx, HEADINGS_INI) sections = list(updates.keys()) # convert to list as py3 returns an iterator sections.sort() # sort so we have removal first conf = configparser.ConfigParser() conf.read(filepath) for section in sections: settings = updates[section] if section.startswith("-"): section = section[1:] ctx.assertion.assertTrue(conf.remove_section(section), "No such section '%s' in '%s'" % (section, filepath)) else: if not conf.has_section(section): conf.add_section(section) keys = settings.keys() for key in keys: if key.startswith("-"): key = key[1:] ctx.assertion.assertTrue(conf.remove_option(section, key), "No such key '%s' in section '%s' in '%s'" % (key, section, filepath)) else: conf.set(section, key, settings[key]) file_utils.create_file_with_contents(filepath, conf)
def step_an_ini_file_filepath_with(ctx, filepath): """ Same as :ref:`Given a file "{filepath}" with`, but accepts table with sections/keys/values structure to construct INI file. Requires table with following headers: ========= ===== ======= Section Key Value ========= ===== ======= Examples: .. code-block:: gherkin Feature: Creating INI files Scenario: Empty section Given an INI file "/etc/dnf/plugins/debuginfo-install.conf" with | Section | Key | Value | | main | | | Scenario: Section with one key Given an INI file "/etc/dnf/plugins/debuginfo-install.conf" with | Section | Key | Value | | main | enabled | False | Scenarion: Section with multiple keys Given an INI file "/etc/yum.repos.d/mnt.repo" with | Section | Key | Value | | mnt | name | Mounted repo - $basearch | | | baseurl | file:///mnt/repo/$basearch | | | enabled | True | | | gpgcheck | False | """ sections = table_utils.parse_skv_table(ctx, HEADINGS_INI) conf = configparser.ConfigParser() for section, settings in sections.items(): if six.PY2: conf.add_section(section) for key, value in settings.items(): conf.set(section, key, value) else: conf[section] = settings file_utils.create_file_with_contents(filepath, conf)
def given_repository_metadata_signed_by(ctx, repository, gpgkey): """ Signs repodata.xml for a given repository using the given GPG key and updates the repo file with gpgkey URL. Should be used after the repo is created or updated. .. note:: The default dnf settings is *repo_gpgcheck = False*. Examples: .. code-block:: gherkin Feature: Repodata signatures Scenario: Setup repository with signed metadata Given GPG key "JamesBond" And GPG key "JamesBond" imported in rpm database And repository "TestRepo" with packages signed by "JamesBond" | Package | Tag | Value | | TestA | | | And repository "TestRepo" metadata signed by "JamesBond" And a repo file of repository "TestRepo" modified with | Key | Value | | repo_gpgcheck | True | """ # sign the repomd.xml file repodir = repo_utils.get_repo_dir(repository) gpg = which("gpg2") cmd = "{!s} --detach-sig --armor --default-key '{!s}' {!s}/repodata/repomd.xml".format( gpg, gpgkey, repodir) step_i_successfully_run_command(ctx, cmd) # update the repo file with path to the gpg key pubkey = GPGKEY_FILEPATH_TMPL.format(gpgkey, "pubkey") keyurl = "file://{!s}".format(pubkey) repofile = REPO_TMPL.format(repository) conf = file_utils.read_ini_file(repofile) conf.set(repository, "gpgkey", keyurl) file_utils.create_file_with_contents(repofile, conf)
def given_repository_metadata_signed_by(ctx, repository, gpgkey): """ Signs repodata.xml for a given repository using the given GPG key and updates the repo file with gpgkey URL. Should be used after the repo is created or updated. .. note:: The default dnf settings is *repo_gpgcheck = False*. Examples: .. code-block:: gherkin Feature: Repodata signatures Scenario: Setup repository with signed metadata Given GPG key "JamesBond" And GPG key "JamesBond" imported in rpm database And repository "TestRepo" with packages signed by "JamesBond" | Package | Tag | Value | | TestA | | | And repository "TestRepo" metadata signed by "JamesBond" And a repo file of repository "TestRepo" modified with | Key | Value | | repo_gpgcheck | True | """ # sign the repomd.xml file repodir = repo_utils.get_repo_dir(repository) gpg = which("gpg2") cmd = "{!s} --detach-sig --armor --default-key '{!s}' {!s}/repodata/repomd.xml".format(gpg, gpgkey, repodir) step_i_successfully_run_command(ctx, cmd) # update the repo file with path to the gpg key pubkey = GPGKEY_FILEPATH_TMPL.format(gpgkey, "pubkey") keyurl = "file://{!s}".format(pubkey) repofile = REPO_TMPL.format(repository) conf = file_utils.read_ini_file(repofile) conf.set(repository, "gpgkey", keyurl) file_utils.create_file_with_contents(repofile, conf)