Improve unit test coverage and molecule verifications

This patch adds more unit test to yum-config module and validations
to molecule verify step.

Change-Id: Icf4cd8463bcc5b466a6b0882e072874639ce117a
Signed-off-by: Douglas Viroel <dviroel@redhat.com>
This commit is contained in:
Douglas Viroel 2021-09-27 16:40:12 -03:00
parent bbb81ff8d4
commit a34103fffd
8 changed files with 371 additions and 22 deletions

View File

@ -0,0 +1,23 @@
---
- name: Get the content of {{ item.name }} repo
command: "cat {{ item.path }}"
register: file_output
- name: Print {{ item.name }} repo content
debug:
msg:
- "Content of {{ item.name }} repo located at '{{ item.path }}'"
- "{{ file_output.stdout_lines }}"
- set_fact:
tmp_repo_file: /tmp/{{ item.name|lower }}.temp
- name: Retrieve remote repo ini file
fetch:
src: "{{ item.path }}"
dest: "{{ tmp_repo_file }}"
flat: yes
- assert:
that:
- "{{ lookup('ini', '{{ item.key }} section={{ item.section|lower }} file={{ tmp_repo_file }}') }} == {{ item.value }}"

View File

@ -2,19 +2,32 @@
- name: Converge
hosts: all
tasks:
- name: "Test yum_config repo config"
- name: "Check get_hash"
tripleo.repos.get_hash:
release: master
- name: "Check get_hash with invalid url"
tripleo.repos.get_hash:
release: master
dlrn_url: 'https://httpbin.org/status/404'
register: result
failed_when: result is success
- name: "Disable one of the system repos"
become: true
tripleo.repos.yum_config:
type: repo
name: appstream
enabled: true
name: rt
file_path: /etc/yum.repos.d/CentOS-Stream-RealTime.repo
enabled: false
tags:
# TODO: fix yum_config to correctly report changed state and uncomment
# the line below which disables molecule idemptotence test.
- molecule-idempotence-notest
# TODO: code needs a fix to be compatible with CentOS-7
when: ansible_distribution_major_version is version(8, '>=')
- name: "Test yum_config global config"
- name: "Update yum_config global config"
become: true
tripleo.repos.yum_config:
type: global
@ -26,17 +39,8 @@
# TODO: fix yum_config to correctly report changed state and uncomment
# the line below which disables molecule idemptotence test.
- molecule-idempotence-notest
- name: "Check get_hash"
tripleo.repos.get_hash:
release: master
- name: "Check get_hash with invalid url"
tripleo.repos.get_hash:
release: master
dlrn_url: 'https://httpbin.org/status/404'
register: result
failed_when: result is success
# TODO: code needs a fix to be compatible with CentOS-7
when: ansible_distribution_major_version is version(8, '>=')
- name: "Test yum_config enable-compose-repos"
become: true
@ -52,3 +56,5 @@
- /etc/yum.repos.d/CentOS-Stream-BaseOS.repo
tags:
- molecule-idempotence-notest
# NOTE: operation available only for CentOS >= 8
when: ansible_distribution_major_version is version(8, '>=')

View File

@ -1,10 +1,38 @@
---
# This is an example playbook to execute Ansible tests.
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Example assertion
assert:
that: true
- name: Validate if RealTime repo is disabled
include_tasks: assert_ini_key_value.yml
with_items:
- name: RealTime
path: /etc/yum.repos.d/CentOS-Stream-RealTime.repo
section: rt
key: enabled
value: "0"
# TODO: code needs a fix to be compatible with CentOS-7
when: ansible_distribution_major_version is version(8, '>=')
- name: Validate yum/dnf conf file
include_tasks: assert_ini_key_value.yml
with_items:
- name: dnf.conf
path: /etc/dnf/dnf.conf
section: main
key: skip_if_unavailable
value: "False"
- name: dnf.conf
path: /etc/dnf/dnf.conf
section: main
key: keepcache
value: "0"
# TODO: code needs a fix to be compatible with CentOS-7
when: ansible_distribution_major_version is version(8, '>=')
- name: Validate compose repos outputs
include_tasks: verify_compose_repos.yml
with_items:
- "AppStream"
- "BaseOS"
# NOTE: operation available only for CentOS >= 8
when: ansible_distribution_major_version is version(8, '>=')

View File

@ -0,0 +1,26 @@
---
- name: "Search for {{ item }} repos"
find:
paths: "/etc/yum.repos.d"
file_type: file
use_regex: yes
patterns:
- "^CentOS-Stream.*{{ item }}.*.repo$"
excludes:
- "CentOS-Stream-{{ item }}.repo"
register: compose_repos
failed_when: compose_repos.files|length != 1
- name: Validate repo file configuration
include_tasks: assert_ini_key_value.yml
with_items:
- name: "{{ item }}"
path: "{{ compose_repos.files[0].path }}"
section: "{{ item|lower }}"
key: enabled
value: "1"
- name: "{{ item }}"
path: "/etc/yum.repos.d/CentOS-Stream-{{ item }}.repo"
section: "{{ item|lower }}"
key: enabled
value: "0"

View File

@ -19,6 +19,65 @@ FAKE_OPTION1 = 'fake_option1'
FAKE_DIR_FILES = ['fake_file1.conf', 'fake_file2.conf', 'fake.md']
FAKE_SECTIONS = ['fake_section1', 'fake_section2']
FAKE_SECTION1 = 'fake_section1'
FAKE_SECTION2 = 'fake_section2'
FAKE_SET_DICT = {
'key1': 'value1',
'key2': 'value2',
}
FAKE_COMPOSE_URL = (
'https://composes.centos.org/fake-CentOS-Stream/compose/')
FAKE_REPO_PATH = '/etc/yum.repos.d/fake.repo'
FAKE_COMPOSE_INFO = {
"header": {
"version": "1.2",
},
"payload": {
"compose": {
"id": "fake_compose_id",
},
"release": {
"name": "CentOS Stream",
"short": "CentOS-Stream",
"version": "8",
},
"variants": {
"AppStream": {
"arches": [
"aarch64",
"ppc64le",
"x86_64"
],
"id": "AppStream",
"name": "AppStream",
"paths": {
"packages": {
"aarch64": "AppStream/aarch64/os/Packages",
"ppc64le": "AppStream/ppc64le/os/Packages",
"x86_64": "AppStream/x86_64/os/Packages",
},
},
},
"BaseOS": {
"arches": [
"aarch64",
"ppc64le",
"x86_64",
],
"id": "BaseOS",
"name": "BaseOS",
"paths": {
"packages": {
"aarch64": "BaseOS/aarch64/os/Packages",
"ppc64le": "BaseOS/ppc64le/os/Packages",
"x86_64": "BaseOS/x86_64/os/Packages",
},
},
},
},
},
}
class FakeConfigParser(dict):
@ -33,4 +92,7 @@ class FakeConfigParser(dict):
pass
def add_section(self, section):
pass
self[section] = {}
def sections(self):
return self.keys()

View File

@ -0,0 +1,119 @@
# Copyright 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
import json
import os
from unittest import mock
import urllib.request
from . import fakes
from . import test_main
import tripleo_repos.yum_config.constants as const
import tripleo_repos.yum_config.exceptions as exc
import tripleo_repos.yum_config.compose_repos as repos
import tripleo_repos.yum_config.yum_config as yum_config
class TestTripleOComposeRepos(test_main.TestTripleoYumConfigBase):
"""Tests for TripleComposeRepos class and its methods."""
def setUp(self):
super(TestTripleOComposeRepos, self).setUp()
self.repos = self._create_compose_repos_obj(
dir_path='/tmp'
)
def _create_compose_repos_obj(
self,
compose_url=fakes.FAKE_COMPOSE_URL,
release=const.COMPOSE_REPOS_RELEASES[0],
dir_path=None,
arch=const.COMPOSE_REPOS_SUPPORTED_ARCHS[0]):
url_res = mock.Mock()
json_data = json.dumps(fakes.FAKE_COMPOSE_INFO)
self.mock_object(urllib.request, "urlopen",
mock.Mock(return_value=url_res))
self.mock_object(url_res, 'read',
mock.Mock(return_value=json_data))
return repos.TripleOYumComposeRepoConfig(
compose_url, release, dir_path=dir_path, arch=arch)
def test_tripleo_compose_repos_invalid_release(self):
self.assertRaises(exc.TripleOYumConfigComposeError,
repos.TripleOYumComposeRepoConfig,
fakes.FAKE_COMPOSE_URL,
'invalid_release')
def test_tripleo_compose_repos_invalid_url(self):
self.assertRaises(exc.TripleOYumConfigComposeError,
repos.TripleOYumComposeRepoConfig,
"http://invalid_url.org",
const.COMPOSE_REPOS_RELEASES[0])
def test__get_compose_info_exc(self):
self.mock_object(urllib.request, "urlopen",
mock.Mock(side_effect=Exception))
self.assertRaises(exc.TripleOYumConfigComposeError,
self.repos._get_compose_info)
def test_enable_compose_repos(self):
self.mock_object(self.repos, 'add_section')
self.repos.enable_compose_repos(
variants=fakes.FAKE_COMPOSE_INFO['payload']['variants'].keys(),
override_repos=False
)
@mock.patch('builtins.open')
def test_add_section(self, open):
self.mock_object(os.path, 'isfile', mock.Mock(return_value=False))
mock_add_section = self.mock_object(yum_config.TripleOYumConfig,
"add_section")
self.repos.add_section(fakes.FAKE_SECTION1, fakes.FAKE_SET_DICT,
fakes.FAKE_FILE_PATH)
mock_add_section.assert_called_once_with(
fakes.FAKE_SECTION1, fakes.FAKE_SET_DICT, fakes.FAKE_FILE_PATH
)
def test_update_section(self):
mock_update = self.mock_object(yum_config.TripleOYumConfig,
"update_section")
expected_set_dict = copy.deepcopy(fakes.FAKE_SET_DICT)
expected_set_dict['enabled'] = '1'
self.repos.update_section(fakes.FAKE_SECTION1,
set_dict=fakes.FAKE_SET_DICT,
enabled=True,
file_path=fakes.FAKE_FILE_PATH)
mock_update.assert_called_once_with(fakes.FAKE_SECTION1,
expected_set_dict,
file_path=fakes.FAKE_FILE_PATH)
def test_update_all_sections(self):
mock_update = self.mock_object(yum_config.TripleOYumConfig,
"update_all_sections")
expected_set_dict = copy.deepcopy(fakes.FAKE_SET_DICT)
expected_set_dict['enabled'] = '0'
self.repos.update_all_sections(fakes.FAKE_FILE_PATH,
set_dict=fakes.FAKE_SET_DICT,
enabled=False)
mock_update.assert_called_once_with(expected_set_dict,
fakes.FAKE_FILE_PATH)

View File

@ -20,6 +20,7 @@ from unittest import mock
from . import fakes
from . import mock_modules # noqa: F401
import tripleo_repos.yum_config.__main__ as main
import tripleo_repos.yum_config.compose_repos as repos
import tripleo_repos.yum_config.constants as const
import tripleo_repos.yum_config.dnf_manager as dnf_mgr
import tripleo_repos.yum_config.yum_config as yum_cfg
@ -118,3 +119,33 @@ class TestTripleoYumConfigMain(TestTripleoYumConfigBase):
main.main()
self.assertEqual(2, command.exception.code)
def test_main_enable_compose_repos(self):
sys.argv[1:] = [
'enable-compose-repos', '--compose-url', fakes.FAKE_COMPOSE_URL,
'--release', const.COMPOSE_REPOS_RELEASES[0],
'--variants', 'fake_variant',
'--disable-repos', fakes.FAKE_REPO_PATH,
'--arch', const.COMPOSE_REPOS_SUPPORTED_ARCHS[0],
]
repos_obj = mock.Mock()
mock_yum_global_obj = self.mock_object(
repos, 'TripleOYumComposeRepoConfig',
mock.Mock(return_value=repos_obj))
mock_enable_composes = self.mock_object(
repos_obj, 'enable_compose_repos')
mock_update_all = self.mock_object(
repos_obj, 'update_all_sections')
main.main()
mock_yum_global_obj.assert_called_once_with(
fakes.FAKE_COMPOSE_URL,
const.COMPOSE_REPOS_RELEASES[0],
dir_path=const.YUM_REPO_DIR,
arch=const.COMPOSE_REPOS_SUPPORTED_ARCHS[0])
mock_enable_composes.assert_called_once_with(
variants=['fake_variant'], override_repos=False)
mock_update_all.assert_called_once_with(
fakes.FAKE_REPO_PATH, enabled=False
)

View File

@ -172,6 +172,60 @@ class TestTripleOYumConfig(test_main.TestTripleoYumConfigBase):
updates)
mock_get_configs.assert_called_once_with(fakes.FAKE_SECTION1)
@mock.patch('builtins.open')
def test_add_section(self, open):
yum_config = self._create_yum_config_obj(
valid_options=fakes.FAKE_SUPP_OPTIONS)
config_parser = fakes.FakeConfigParser({fakes.FAKE_SECTION1: {}})
mock_read_config = self.mock_object(
yum_config, '_read_config_file',
mock.Mock(return_value=(config_parser, fakes.FAKE_FILE_PATH)))
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
yum_config.add_section(fakes.FAKE_SECTION2, updates,
file_path=fakes.FAKE_FILE_PATH)
mock_read_config.assert_called_once_with(
file_path=fakes.FAKE_FILE_PATH)
@mock.patch('builtins.open')
def test_add_section_already_exists(self, open):
yum_config = self._create_yum_config_obj(
valid_options=fakes.FAKE_SUPP_OPTIONS)
config_parser = fakes.FakeConfigParser({fakes.FAKE_SECTION1: {}})
mock_read_config = self.mock_object(
yum_config, '_read_config_file',
mock.Mock(return_value=(config_parser, fakes.FAKE_FILE_PATH)))
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
self.assertRaises(exc.TripleOYumConfigInvalidSection,
yum_config.add_section,
fakes.FAKE_SECTION1, updates,
file_path=fakes.FAKE_FILE_PATH)
mock_read_config.assert_called_once_with(
file_path=fakes.FAKE_FILE_PATH)
@mock.patch('builtins.open')
def test_add_update_all_sections(self, open):
yum_config = self._create_yum_config_obj(
valid_options=fakes.FAKE_SUPP_OPTIONS)
config_parser = fakes.FakeConfigParser({fakes.FAKE_SECTION1: {}})
mock_read_config = self.mock_object(
yum_config, '_read_config_file',
mock.Mock(return_value=(config_parser, fakes.FAKE_FILE_PATH)))
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
yum_config.update_all_sections(updates, fakes.FAKE_FILE_PATH)
mock_read_config.assert_called_once_with(fakes.FAKE_FILE_PATH)
@ddt.ddt
class TestTripleOYumRepoConfig(test_main.TestTripleoYumConfigBase):