From 457dcf1313f62d67087c9ee23d13d73430c61e43 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Wed, 3 Feb 2021 20:34:12 -0500 Subject: [PATCH] [stable-2.10] Make yum_repository test more reliable (#73467) * Improve setup_rpm_repo - add handlers to remove repos - add variable to control whethere or not repos are created * Use local repo for all distros * Change repo creation script to module. (cherry picked from commit 997b2d2a1955ccb4e70f805c18dc3e227e86c678) Co-authored-by: Sam Doran --- .../targets/setup_rpm_repo/defaults/main.yml | 1 + .../targets/setup_rpm_repo/handlers/main.yml | 5 + .../setup_rpm_repo/library/create_repo.py | 94 +++++++++++++++++++ .../targets/setup_rpm_repo/meta/main.yml | 2 + .../targets/setup_rpm_repo/tasks/main.yml | 39 ++++---- .../targets/yum_repository/defaults/main.yml | 5 + .../targets/yum_repository/handlers/main.yml | 4 + .../targets/yum_repository/meta/main.yml | 4 + .../targets/yum_repository/tasks/main.yml | 33 +------ .../targets/yum_repository/vars/CentOS-8.yml | 10 -- .../targets/yum_repository/vars/CentOS.yml | 10 -- .../targets/yum_repository/vars/Fedora.yml | 5 - .../targets/yum_repository/vars/default.yml | 0 13 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 test/integration/targets/setup_rpm_repo/handlers/main.yml create mode 100644 test/integration/targets/setup_rpm_repo/library/create_repo.py create mode 100644 test/integration/targets/setup_rpm_repo/meta/main.yml create mode 100644 test/integration/targets/yum_repository/defaults/main.yml create mode 100644 test/integration/targets/yum_repository/handlers/main.yml create mode 100644 test/integration/targets/yum_repository/meta/main.yml delete mode 100644 test/integration/targets/yum_repository/vars/CentOS-8.yml delete mode 100644 test/integration/targets/yum_repository/vars/CentOS.yml delete mode 100644 test/integration/targets/yum_repository/vars/Fedora.yml delete mode 100644 test/integration/targets/yum_repository/vars/default.yml diff --git a/test/integration/targets/setup_rpm_repo/defaults/main.yml b/test/integration/targets/setup_rpm_repo/defaults/main.yml index e69de29bb2d..19c033b9296 100644 --- a/test/integration/targets/setup_rpm_repo/defaults/main.yml +++ b/test/integration/targets/setup_rpm_repo/defaults/main.yml @@ -0,0 +1 @@ +install_repos: yes diff --git a/test/integration/targets/setup_rpm_repo/handlers/main.yml b/test/integration/targets/setup_rpm_repo/handlers/main.yml new file mode 100644 index 00000000000..a0af3c92d75 --- /dev/null +++ b/test/integration/targets/setup_rpm_repo/handlers/main.yml @@ -0,0 +1,5 @@ +- name: remove repos + yum_repository: + state: absent + name: "{{ item }}" + loop: "{{ repos }}" diff --git a/test/integration/targets/setup_rpm_repo/library/create_repo.py b/test/integration/targets/setup_rpm_repo/library/create_repo.py new file mode 100644 index 00000000000..6dc1e457c55 --- /dev/null +++ b/test/integration/targets/setup_rpm_repo/library/create_repo.py @@ -0,0 +1,94 @@ +#!/usr/bin/python + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import sys +import tempfile + +from collections import namedtuple + +from ansible.module_utils.basic import AnsibleModule + +try: + from rpmfluff import SimpleRpmBuild + from rpmfluff import YumRepoBuild +except ImportError: + from rpmfluff.rpmbuild import SimpleRpmBuild + from rpmfluff.yumrepobuild import YumRepoBuild + +try: + from rpmfluff import can_use_rpm_weak_deps +except ImportError: + try: + from rpmfluff.utils import can_use_rpm_weak_deps + except ImportError: + can_use_rpm_weak_deps = None + +RPM = namedtuple('RPM', ['name', 'version', 'release', 'epoch', 'recommends']) + + +SPECS = [ + RPM('dinginessentail', '1.0', '1', None, None), + RPM('dinginessentail', '1.0', '2', '1', None), + RPM('dinginessentail', '1.1', '1', '1', None), + RPM('dinginessentail-olive', '1.0', '1', None, None), + RPM('dinginessentail-olive', '1.1', '1', None, None), + RPM('landsidescalping', '1.0', '1', None, None), + RPM('landsidescalping', '1.1', '1', None, None), + RPM('dinginessentail-with-weak-dep', '1.0', '1', None, ['dinginessentail-weak-dep']), + RPM('dinginessentail-weak-dep', '1.0', '1', None, None), +] + + +def create_repo(arch='x86_64'): + pkgs = [] + for spec in SPECS: + pkg = SimpleRpmBuild(spec.name, spec.version, spec.release, [arch]) + pkg.epoch = spec.epoch + + if spec.recommends: + # Skip packages that require weak deps but an older version of RPM is being used + if not can_use_rpm_weak_deps or not can_use_rpm_weak_deps(): + continue + + for recommend in spec.recommends: + pkg.add_recommends(recommend) + + pkgs.append(pkg) + + repo = YumRepoBuild(pkgs) + repo.make(arch) + + for pkg in pkgs: + pkg.clean() + + return repo.repoDir + + +def main(): + module = AnsibleModule( + argument_spec={ + 'arch': {'required': True}, + 'tempdir': {'type': 'path'}, + } + ) + + arch = module.params['arch'] + tempdir = module.params['tempdir'] + + # Save current temp dir so we can set it back later + original_tempdir = tempfile.tempdir + tempfile.tempdir = tempdir + + try: + repo_dir = create_repo(arch) + finally: + tempfile.tempdir = original_tempdir + + module.exit_json(repo_dir=repo_dir, tmpfile=tempfile.gettempdir()) + + +if __name__ == "__main__": + main() diff --git a/test/integration/targets/setup_rpm_repo/meta/main.yml b/test/integration/targets/setup_rpm_repo/meta/main.yml new file mode 100644 index 00000000000..1810d4bec98 --- /dev/null +++ b/test/integration/targets/setup_rpm_repo/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_remote_tmp_dir diff --git a/test/integration/targets/setup_rpm_repo/tasks/main.yml b/test/integration/targets/setup_rpm_repo/tasks/main.yml index 14addf79a83..a6766f2053c 100644 --- a/test/integration/targets/setup_rpm_repo/tasks/main.yml +++ b/test/integration/targets/setup_rpm_repo/tasks/main.yml @@ -31,18 +31,22 @@ - ansible_facts.distribution in ['RedHat', 'CentOS'] - ansible_facts.distribution_major_version is version('8', '>=') - - name: Copy script for creating a repo - copy: - src: create-repo.py - dest: /tmp/create-repo.py - mode: 0755 + - set_fact: + repos: + - "fake-{{ ansible_architecture }}" + - "fake-i686" + - "fake-ppc64" + changed_when: yes + notify: remove repos - name: Create RPMs and put them into a repo - shell: "{{ansible_python_interpreter}} /tmp/create-repo.py {{ ansible_architecture }}" + create_repo: + arch: "{{ ansible_architecture }}" + tempdir: "{{ remote_tmp_dir }}" register: repo - set_fact: - repodir: "{{ repo.stdout_lines[-1] }}" + repodir: "{{ repo.repo_dir }}" - name: Install the repo yum_repository: @@ -50,6 +54,7 @@ description: "fake-{{ ansible_architecture }}" baseurl: "file://{{ repodir }}" gpgcheck: no + when: install_repos | bool - name: Copy comps.xml file copy: @@ -61,11 +66,13 @@ command: createrepo -g {{ repodir_comps.dest | quote }} {{ repodir | quote }} - name: Create RPMs and put them into a repo (i686) - shell: "{{ansible_python_interpreter}} /tmp/create-repo.py i686" + create_repo: + arch: i686 + tempdir: "{{ remote_tmp_dir }}" register: repo_i686 - set_fact: - repodir_i686: "{{ repo_i686.stdout_lines[-1] }}" + repodir_i686: "{{ repo_i686.repo_dir }}" - name: Install the repo (i686) yum_repository: @@ -73,13 +80,16 @@ description: "fake-i686" baseurl: "file://{{ repodir_i686 }}" gpgcheck: no + when: install_repos | bool - name: Create RPMs and put them into a repo (ppc64) - shell: "{{ansible_python_interpreter}} /tmp/create-repo.py ppc64" + create_repo: + arch: ppc64 + tempdir: "{{ remote_tmp_dir }}" register: repo_ppc64 - set_fact: - repodir_ppc64: "{{ repo_ppc64.stdout_lines[-1] }}" + repodir_ppc64: "{{ repo_ppc64.repo_dir }}" - name: Install the repo (ppc64) yum_repository: @@ -87,11 +97,6 @@ description: "fake-ppc64" baseurl: "file://{{ repodir_ppc64 }}" gpgcheck: no - - - set_fact: - repos: - - "fake-{{ ansible_architecture }}" - - "fake-i686" - - "fake-ppc64" + when: install_repos | bool when: ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux', 'Fedora'] diff --git a/test/integration/targets/yum_repository/defaults/main.yml b/test/integration/targets/yum_repository/defaults/main.yml new file mode 100644 index 00000000000..4c1fbc652e6 --- /dev/null +++ b/test/integration/targets/yum_repository/defaults/main.yml @@ -0,0 +1,5 @@ +yum_repository_test_package: dinginessentail +yum_repository_test_repo: + name: fakerepo + description: Fake Repo + baseurl: "file://{{ repodir }}" diff --git a/test/integration/targets/yum_repository/handlers/main.yml b/test/integration/targets/yum_repository/handlers/main.yml new file mode 100644 index 00000000000..f96c23917b7 --- /dev/null +++ b/test/integration/targets/yum_repository/handlers/main.yml @@ -0,0 +1,4 @@ +- name: remove listtest repo + yum_repository: + name: listtest + state: absent diff --git a/test/integration/targets/yum_repository/meta/main.yml b/test/integration/targets/yum_repository/meta/main.yml new file mode 100644 index 00000000000..56539a4f557 --- /dev/null +++ b/test/integration/targets/yum_repository/meta/main.yml @@ -0,0 +1,4 @@ +dependencies: + - role: setup_rpm_repo + vars: + install_repos: no diff --git a/test/integration/targets/yum_repository/tasks/main.yml b/test/integration/targets/yum_repository/tasks/main.yml index 3884d46c0d7..0ff0bcff67b 100644 --- a/test/integration/targets/yum_repository/tasks/main.yml +++ b/test/integration/targets/yum_repository/tasks/main.yml @@ -1,17 +1,6 @@ - name: Run tests when: ansible_facts.distribution in ['CentOS', 'Fedora'] block: - - name: Include distribution specific variables - include_vars: "{{ lookup('first_found', params) }}" - vars: - params: - files: - - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml" - - "{{ ansible_facts.distribution }}.yml" - - default.yml - paths: - - vars - - name: ensure {{ yum_repository_test_package }} is uninstalled to begin with action: "{{ ansible_facts.pkg_mgr }}" args: @@ -181,6 +170,7 @@ includepkgs: - ccc - ddd + notify: remove listtest repo - name: Assert that lists were properly inserted assert: @@ -195,24 +185,3 @@ repofile: "{{ lookup('file', '/etc/yum.repos.d/listtest.repo') }}" url_hostname: "{{ yum_repository_test_repo.baseurl | urlsplit('hostname') }}" url_hostname2: "{{ url_hostname | replace('download[0-9]?\\.', 'download2\\.', 1) }}" - - - name: CLEANUP | Remove list test repo - yum_repository: - name: listtest - state: absent - - - name: CLEANUP | Remove {{ yum_repository_test_repo.name }} - yum_repository: - name: "{{ yum_repository_test_repo.name }}" - state: absent - - - name: CLEANUP | Enable EPEL - yum_repository: - name: epel - state: present - description: "{{ yum_repository_epel.description | default(omit) }}" - metalink: "{{ yum_repository_epel.metalink | default(omit) }}" - mirrorlist: "{{ yum_repository_epel.mirrorlist | default(omit) }}" - gpgkey: "{{ yum_repository_epel.gpgkey }}" - gpgcheck: yes - when: ansible_facts.distribution == 'CentOS' diff --git a/test/integration/targets/yum_repository/vars/CentOS-8.yml b/test/integration/targets/yum_repository/vars/CentOS-8.yml deleted file mode 100644 index 22d4d13e722..00000000000 --- a/test/integration/targets/yum_repository/vars/CentOS-8.yml +++ /dev/null @@ -1,10 +0,0 @@ -yum_repository_test_package: sshpass -yum_repository_test_repo: - name: epel - description: EPEL yum repo - baseurl: https://download.fedoraproject.org/pub/epel/$releasever/Everything/$basearch - -yum_repository_epel: - description: Extra Packages for Enterprise Linux $releasever - $basearch - metalink: https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch&infra=$infra&content=$contentdir - gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{{ ansible_facts.distribution_major_version }} diff --git a/test/integration/targets/yum_repository/vars/CentOS.yml b/test/integration/targets/yum_repository/vars/CentOS.yml deleted file mode 100644 index db9947d5ea3..00000000000 --- a/test/integration/targets/yum_repository/vars/CentOS.yml +++ /dev/null @@ -1,10 +0,0 @@ -yum_repository_test_package: sl -yum_repository_test_repo: - name: epel - description: EPEL yum repo - baseurl: https://archives.fedoraproject.org/pub/archive/epel/{{ ansible_facts.distribution_major_version }}/$basearch - -yum_repository_epel: - description: Extra Packages for Enterprise Linux {{ ansible_facts.distribution_major_version }} - $basearch - mirrorlist: https://mirrors.fedoraproject.org/metalink?repo=epel-{{ ansible_facts.distribution_major_version }}&arch=$basearch - gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{{ ansible_facts.distribution_major_version }} diff --git a/test/integration/targets/yum_repository/vars/Fedora.yml b/test/integration/targets/yum_repository/vars/Fedora.yml deleted file mode 100644 index 8c37eaa1076..00000000000 --- a/test/integration/targets/yum_repository/vars/Fedora.yml +++ /dev/null @@ -1,5 +0,0 @@ -yum_repository_test_package: libbdplus -yum_repository_test_repo: - name: rpmfusion-free - description: RPM Fusion for Fedora {{ ansible_distribution_major_version }} - Free - baseurl: http://download1.rpmfusion.org/free/fedora/releases/{{ ansible_distribution_major_version }}/Everything/{{ ansible_architecture }}/os/ diff --git a/test/integration/targets/yum_repository/vars/default.yml b/test/integration/targets/yum_repository/vars/default.yml deleted file mode 100644 index e69de29bb2d..00000000000