def step_a_file_with_type_added_into_repository(ctx, filepath, mdtype, repository): """ Example: Given repository "base" with packages | Package | Tag | Value | | TestA | Version | 1 | And a file "metadata.ini" with type "newmd" added into repository "base" \"\"\" [example] TestA = 1 \"\"\" """ # verify that modifyrepo_c is present modifyrepo = which("modifyrepo_c") ctx.assertion.assertIsNotNone(modifyrepo, "modifyrepo_c is required") repodir = repo_utils.get_repo_dir(repository) if not os.path.isfile(filepath): ctx.assertion.assertIsNotNone(ctx.text, "Multiline text is not provided") tmpdir = tempfile.mkdtemp() filepath = os.path.join(tmpdir, os.path.basename(filepath)) with open(filepath, 'w') as fw: fw.write(ctx.text) file_utils.set_dir_content_ownership(ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{} --mdtype={} {} {}".format(modifyrepo, mdtype, filepath, os.path.join(repodir, "repodata")) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def then_update_repository_with_packages(ctx, repository): """ Examples: Feature: Working with repositories Given I update http repository base with packages | Package | Tag | Value | | foo | | | Given I update http repository "updates" with packages | Package | Tag | Value | | foo | Version | 2.1 | | foo v3 | Version | 3.1 | """ packages = table_utils.parse_skv_table(ctx, HEADINGS_REPO, PKG_TAGS, PKG_TAGS_REPEATING) rpmbuild = which("rpmbuild") ctx.assertion.assertIsNotNone(rpmbuild, "rpmbuild is required") createrepo = which("createrepo_c") ctx.assertion.assertIsNotNone(createrepo, "createrepo_c is required") repodir = repo_utils.get_repo_dir(repository) tmpdir = repodir srpm_tmpdir = '{}-source'.format(tmpdir.rstrip('/')) template = JINJA_ENV.from_string(PKG_TMPL) for name, settings in packages.items(): name = name.split()[0] # cut-off the pkg name _suffix_ to allow defining multiple package versions disttag = "" if '/' in name: # using the module/pkgname notation, module would be placed to a disttag (module, name) = name.split('/', 1) disttag = ".{}".format(module) # before processing the template # lower all characters # replace '%' in Tag name with '_' # replace '(' in Tag name with '_' # delete all ')' in Tag settings = {k.lower().replace('%', '_').replace('(', '_').replace(')', ''): v for k, v in settings.items()} ctx.text = template.render(name=name, disttag=disttag, **settings) fname = "{!s}/{!s}.spec".format(tmpdir, name) step_a_file_filepath_with(ctx, fname) buildname = '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm' if 'arch' not in settings or settings['arch'] == 'noarch': cmd = "{!s} --define '_rpmdir {!s}' --define '_srcrpmdir {!s}' \ --define '_build_name_fmt {!s}' -ba {!s}" \ .format(rpmbuild, tmpdir, srpm_tmpdir, buildname, fname) else: cmd = "setarch {!s} {!s} --define '_rpmdir {!s}' --define '_srcrpmdir {!s}' \ --define '_build_name_fmt {!s}' --target {!s} -ba {!s}" \ .format(settings['arch'], rpmbuild, tmpdir, srpm_tmpdir, buildname, settings['arch'], fname) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} --update {!s}".format(createrepo, repodir) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
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)
def step_a_file_with_type_added_into_repository(ctx, filepath, mdtype, repository): """ Example: Given repository "base" with packages | Package | Tag | Value | | TestA | Version | 1 | And a file "metadata.ini" with type "newmd" added into repository "base" \"\"\" [example] TestA = 1 \"\"\" """ # verify that modifyrepo_c is present modifyrepo = which("modifyrepo_c") ctx.assertion.assertIsNotNone(modifyrepo, "modifyrepo_c is required") repodir = repo_utils.get_repo_dir(repository) if not os.path.isfile(filepath): ctx.assertion.assertIsNotNone(ctx.text, "Multiline text is not provided") tmpdir = tempfile.mkdtemp() filepath = os.path.join(tmpdir, os.path.basename(filepath)) with open(filepath, 'w') as fw: fw.write(ctx.text) file_utils.set_dir_content_ownership( ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{} --mdtype={} {} {}".format(modifyrepo, mdtype, filepath, os.path.join(repodir, "repodata")) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def step_i_successfully_run_command_in_repository(ctx, command, repository): repo = repo_utils.get_repo_dir(repository) ctx.assertion.assertIsNotNone(repo, "repository does not exist") ctx.cmd_result = command_utils.run(ctx, command, cwd=repo) ctx.assertion.assertEqual(ctx.cmd_result.returncode, 0)
def given_package_groups_defined_in_repository(ctx, repository): """ For a given repository creates comps.xml file with described package groups and recreates the repo .. note:: Requires *createrepo_c* and the repo to be already created. Requires table with following headers: ========= ===== ======= Group Tag Value ========= ===== ======= *Tag* is describing characteristics of the respective package group.Supported tags are: ============== =============== Tag Default value ============== =============== is_default false is_uservisible true description "" mandatory [] default [] optional [] conditional [] ============== =============== Examples: .. code-block:: gherkin Feature: Installing a package group @setup Scenario: Repository base with package group minimal Given repository "base" with packages | Package | Tag | Value | | foo | | | | bar | | | | baz | | | | qux | | | And package groups defined in repository "base" | Group | Tag | Value | | minimal | mandatory | foo | | | default | bar | | | conditional | baz qux | Scenario: Installing package group from background When I enable repository "base" Then I successfully run "dnf -y group install minimal" .. note:: Conditional packages are described in a form PKG REQUIREDPKG """ HEADINGS_GROUP = ['Group', 'Tag', 'Value'] GROUP_TAGS_REPEATING = ['mandatory', 'default', 'optional', 'conditional'] GROUP_TAGS = ['is_default', 'is_uservisible', 'description' ] + GROUP_TAGS_REPEATING pkg_groups = table_utils.parse_skv_table(ctx, HEADINGS_GROUP, GROUP_TAGS, GROUP_TAGS_REPEATING) createrepo = which("createrepo_c") ctx.assertion.assertIsNotNone(createrepo, "createrepo_c is required") createrepo = "{!s} {!s}".format(createrepo, "--no-database") # prepare the comps.xml comps_xml = COMPS_PREFIX template = JINJA_ENV.from_string(COMPS_TMPL) for name, settings in pkg_groups.items(): settings = {k.lower(): v for k, v in settings.items()} comps_xml += template.render(name=name, **settings) comps_xml += COMPS_SUFFIX # save comps.xml and recreate the repo repodir = repo_utils.get_repo_dir(repository) with open(os.path.join(repodir, "comps.xml"), "w") as f_comps: f_comps.write(comps_xml) file_utils.set_dir_content_ownership( ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} -g comps.xml --update {!s}".format(createrepo, repodir) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def step_updateinfo_defined_in_repository(ctx, repository): """ For a given repository creates updateinfo.xml file with described updates and recreates the repo .. note:: Requires *modifyrepo_c* and the repo to be already created. Requires table with following headers: ==== ===== ======= Id Tag Value ==== ===== ======= *Tag* is describing attributes of the respective update. Supported tags are: ============ ========================= Tag Default value ============ ========================= Title Default title of Id Type security Description Default description of Id Summary Default summary of Id Severity Low Solution Default solution of Id Rights nobody Issued 2017-01-01 00:00:01 Updated 2017-01-01 00:00:01 Reference none Package none ============ ========================= Examples: .. code-block:: gherkin Feature: Defining updateinfo in a repository @setup Scenario: Repository base with updateinfo defined Given repository "base" with packages | Package | Tag | Value | | foo | Version | 2 | | bar | Version | 2 | And updateinfo defined in repository "base" | Id | Tag | Value | | RHSA-2017-001 | Title | foo bar security update | | | Type | security | | | Description | Fixes buffer overflow | | | Summary | Critical bug is fixed | | | Severity | Critical | | | Solution | Update to the new version | | | Rights | Copyright 2017 Baz Inc | | | Reference | CVE-2017-0001 | | | Reference | BZ123456 | | | Package | foo-2 | | | Package | bar-2 | .. note:: Specifying Version or Release in Package tag is not necessary, however when multiple RPMs matches the string the last one from the sorted list is used. """ HEADINGS_GROUP = ['Id', 'Tag', 'Value'] UPDATEINFO_TAGS_REPEATING = ['Package', 'Reference'] UPDATEINFO_TAGS = ['Title', 'Type', 'Description', 'Solution', 'Summary', 'Severity', 'Rights', 'Issued', 'Updated'] + \ UPDATEINFO_TAGS_REPEATING updateinfo_table = table_utils.parse_skv_table(ctx, HEADINGS_GROUP, UPDATEINFO_TAGS, UPDATEINFO_TAGS_REPEATING) # verify that modifyrepo_c is present modifyrepo = which("modifyrepo_c") ctx.assertion.assertIsNotNone(modifyrepo, "modifyrepo_c is required") # prepare updateinfo.xml content repodir = repo_utils.get_repo_dir(repository) updateinfo_xml = repo_utils.get_updateinfo_xml(repository, updateinfo_table) # save it to the updateinfo.xml file and recreate the repodata tmpdir = tempfile.mkdtemp() with open(os.path.join(tmpdir, "updateinfo.xml"), 'w') as fw: fw.write(updateinfo_xml) file_utils.set_dir_content_ownership( ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} {!s} {!s}".format(modifyrepo, os.path.join(tmpdir, 'updateinfo.xml'), os.path.join(repodir, 'repodata')) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def given_package_groups_defined_in_repository(ctx, repository): """ For a given repository creates comps.xml file with described package groups and recreates the repo .. note:: Requires *createrepo_c* and the repo to be already created. Requires table with following headers: ========= ===== ======= Group Tag Value ========= ===== ======= *Tag* is describing characteristics of the respective package group.Supported tags are: ============== =============== Tag Default value ============== =============== is_default false is_uservisible true description "" mandatory [] default [] optional [] conditional [] ============== =============== Examples: .. code-block:: gherkin Feature: Installing a package group @setup Scenario: Repository base with package group minimal Given repository "base" with packages | Package | Tag | Value | | foo | | | | bar | | | | baz | | | | qux | | | And package groups defined in repository "base" | Group | Tag | Value | | minimal | mandatory | foo | | | default | bar | | | conditional | baz qux | Scenario: Installing package group from background When I enable repository "base" Then I successfully run "dnf -y group install minimal" .. note:: Conditional packages are described in a form PKG REQUIREDPKG """ HEADINGS_GROUP = ['Group', 'Tag', 'Value'] GROUP_TAGS_REPEATING = ['mandatory', 'default', 'optional', 'conditional'] GROUP_TAGS = ['is_default', 'is_uservisible', 'description'] + GROUP_TAGS_REPEATING pkg_groups = table_utils.parse_skv_table(ctx, HEADINGS_GROUP, GROUP_TAGS, GROUP_TAGS_REPEATING) createrepo = which("createrepo_c") ctx.assertion.assertIsNotNone(createrepo, "createrepo_c is required") # prepare the comps.xml comps_xml = COMPS_PREFIX template = JINJA_ENV.from_string(COMPS_TMPL) for name, settings in pkg_groups.items(): settings = {k.lower(): v for k, v in settings.items()} comps_xml += template.render(name=name, **settings) comps_xml += COMPS_SUFFIX # save comps.xml and recreate the repo repodir = repo_utils.get_repo_dir(repository) with open(os.path.join(repodir, "comps.xml"), "w") as f_comps: f_comps.write(comps_xml) file_utils.set_dir_content_ownership(ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} -g comps.xml --update {!s}".format(createrepo, repodir) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def step_updateinfo_defined_in_repository(ctx, repository): """ For a given repository creates updateinfo.xml file with described updates and recreates the repo .. note:: Requires *modifyrepo_c* and the repo to be already created. Requires table with following headers: ==== ===== ======= Id Tag Value ==== ===== ======= *Tag* is describing attributes of the respective update. Supported tags are: ============ ========================= Tag Default value ============ ========================= Title Default title of Id Type security Description Default description of Id Summary Default summary of Id Severity Low Solution Default solution of Id Rights nobody Issued 2017-01-01 00:00:01 Updated 2017-01-01 00:00:01 Reference none Package none ============ ========================= Examples: .. code-block:: gherkin Feature: Defining updateinfo in a repository @setup Scenario: Repository base with updateinfo defined Given repository "base" with packages | Package | Tag | Value | | foo | Version | 2 | | bar | Version | 2 | And updateinfo defined in repository "base" | Id | Tag | Value | | RHSA-2017-001 | Title | foo bar security update | | | Type | security | | | Description | Fixes buffer overflow | | | Summary | Critical bug is fixed | | | Severity | Critical | | | Solution | Update to the new version | | | Rights | Copyright 2017 Baz Inc | | | Reference | CVE-2017-0001 | | | Reference | BZ123456 | | | Package | foo-2 | | | Package | bar-2 | .. note:: Specifying Version or Release in Package tag is not necessary, however when multiple RPMs matches the string the last one from the sorted list is used. """ HEADINGS_GROUP = ['Id', 'Tag', 'Value'] UPDATEINFO_TAGS_REPEATING = ['Package', 'Reference'] UPDATEINFO_TAGS = ['Title', 'Type', 'Description', 'Solution', 'Summary', 'Severity', 'Rights', 'Issued', 'Updated'] + \ UPDATEINFO_TAGS_REPEATING updateinfo_table = table_utils.parse_skv_table(ctx, HEADINGS_GROUP, UPDATEINFO_TAGS, UPDATEINFO_TAGS_REPEATING) # verify that modifyrepo_c is present modifyrepo = which("modifyrepo_c") ctx.assertion.assertIsNotNone(modifyrepo, "modifyrepo_c is required") # prepare updateinfo.xml content repodir = repo_utils.get_repo_dir(repository) updateinfo_xml = repo_utils.get_updateinfo_xml(repository, updateinfo_table) # save it to the updateinfo.xml file and recreate the repodata tmpdir = tempfile.mkdtemp() with open(os.path.join(tmpdir, "updateinfo.xml"), 'w') as fw: fw.write(updateinfo_xml) file_utils.set_dir_content_ownership(ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} {!s} {!s}".format(modifyrepo, os.path.join(tmpdir, 'updateinfo.xml'), os.path.join(repodir, 'repodata')) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership
def step_i_successfully_run_command_in_repository(ctx, command, repository): repo = repo_utils.get_repo_dir(repository) ctx.assertion.assertIsNotNone(repo, "repository does not exist") ctx.cmd_result = command_utils.run(ctx, command, cwd=repo) ctx.assertion.assertEqual(ctx.cmd_result.returncode, 0)
def then_update_repository_with_packages(ctx, repository): """ Examples: Feature: Working with repositories Given I update http repository base with packages | Package | Tag | Value | | foo | | | Given I update http repository "updates" with packages | Package | Tag | Value | | foo | Version | 2.1 | | foo v3 | Version | 3.1 | """ packages = table_utils.parse_skv_table(ctx, HEADINGS_REPO, PKG_TAGS, PKG_TAGS_REPEATING) rpmbuild = which("rpmbuild") ctx.assertion.assertIsNotNone(rpmbuild, "rpmbuild is required") createrepo = which("createrepo_c") ctx.assertion.assertIsNotNone(createrepo, "createrepo_c is required") repodir = repo_utils.get_repo_dir(repository) tmpdir = repodir srpm_tmpdir = '{}-source'.format(tmpdir.rstrip('/')) template = JINJA_ENV.from_string(PKG_TMPL) for name, settings in packages.items(): name = name.split( )[0] # cut-off the pkg name _suffix_ to allow defining multiple package versions disttag = "" if '/' in name: # using the module/pkgname notation, module would be placed to a disttag (module, name) = name.split('/', 1) disttag = ".{}".format(module) # before processing the template # lower all characters # replace '%' in Tag name with '_' # replace '(' in Tag name with '_' # delete all ')' in Tag settings = { k.lower().replace('%', '_').replace('(', '_').replace(')', ''): v for k, v in settings.items() } ctx.text = template.render(name=name, disttag=disttag, **settings) fname = "{!s}/{!s}.spec".format(tmpdir, name) step_a_file_filepath_with(ctx, fname) buildname = '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm' if 'arch' not in settings or settings['arch'] == 'noarch': cmd = "{!s} --define '_rpmdir {!s}' --define '_srcrpmdir {!s}' \ --define '_build_name_fmt {!s}' -ba {!s}" \ .format(rpmbuild, tmpdir, srpm_tmpdir, buildname, fname) else: cmd = "setarch {!s} {!s} --define '_rpmdir {!s}' --define '_srcrpmdir {!s}' \ --define '_build_name_fmt {!s}' --target {!s} -ba {!s}" \ .format(settings['arch'], rpmbuild, tmpdir, srpm_tmpdir, buildname, settings['arch'], fname) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership( ctx, repodir, 'root') # change file ownership to root so we can change it cmd = "{!s} --update {!s}".format(createrepo, repodir) step_i_successfully_run_command(ctx, cmd) file_utils.set_dir_content_ownership(ctx, repodir) # restore file ownership