summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_yum_add_repo.py
blob: d6de2ec21558eca02a5aa41788984deddecf7c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# This file is part of cloud-init. See LICENSE file for license information.

import configparser
import logging
import re
import shutil
import tempfile

import pytest

from cloudinit import util
from cloudinit.config import cc_yum_add_repo
from cloudinit.config.schema import (
    SchemaValidationError,
    get_schema,
    validate_cloudconfig_schema,
)
from tests.unittests import helpers

LOG = logging.getLogger(__name__)


class TestConfig(helpers.FilesystemMockingTestCase):
    def setUp(self):
        super(TestConfig, self).setUp()
        self.tmp = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.tmp)

    def test_bad_config(self):
        cfg = {
            "yum_repos": {
                "epel-testing": {
                    "name": "Extra Packages for Enterprise Linux 5 - Testing",
                    # Missing this should cause the repo not to be written
                    # 'baseurl': 'http://blah.org/pub/epel/testing/5/$barch',
                    "enabled": False,
                    "gpgcheck": True,
                    "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL",
                    "failovermethod": "priority",
                },
            },
        }
        self.patchUtils(self.tmp)
        cc_yum_add_repo.handle("yum_add_repo", cfg, None, LOG, [])
        self.assertRaises(
            IOError, util.load_file, "/etc/yum.repos.d/epel_testing.repo"
        )

    def test_write_config(self):
        cfg = {
            "yum_repos": {
                "epel-testing": {
                    "name": "Extra Packages for Enterprise Linux 5 - Testing",
                    "baseurl": "http://blah.org/pub/epel/testing/5/$basearch",
                    "enabled": False,
                    "gpgcheck": True,
                    "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL",
                    "failovermethod": "priority",
                },
            },
        }
        self.patchUtils(self.tmp)
        cc_yum_add_repo.handle("yum_add_repo", cfg, None, LOG, [])
        contents = util.load_file("/etc/yum.repos.d/epel_testing.repo")
        parser = configparser.ConfigParser()
        parser.read_string(contents)
        expected = {
            "epel_testing": {
                "name": "Extra Packages for Enterprise Linux 5 - Testing",
                "failovermethod": "priority",
                "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL",
                "enabled": "0",
                "baseurl": "http://blah.org/pub/epel/testing/5/$basearch",
                "gpgcheck": "1",
            }
        }
        for section in expected:
            self.assertTrue(
                parser.has_section(section),
                "Contains section {0}".format(section),
            )
            for k, v in expected[section].items():
                self.assertEqual(parser.get(section, k), v)

    def test_write_config_array(self):
        cfg = {
            "yum_repos": {
                "puppetlabs-products": {
                    "name": "Puppet Labs Products El 6 - $basearch",
                    "baseurl": (
                        "http://yum.puppetlabs.com/el/6/products/$basearch"
                    ),
                    "gpgkey": [
                        "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs",
                        "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppet",
                    ],
                    "enabled": True,
                    "gpgcheck": True,
                }
            }
        }
        self.patchUtils(self.tmp)
        cc_yum_add_repo.handle("yum_add_repo", cfg, None, LOG, [])
        contents = util.load_file("/etc/yum.repos.d/puppetlabs_products.repo")
        parser = configparser.ConfigParser()
        parser.read_string(contents)
        expected = {
            "puppetlabs_products": {
                "name": "Puppet Labs Products El 6 - $basearch",
                "baseurl": "http://yum.puppetlabs.com/el/6/products/$basearch",
                "gpgkey": (
                    "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs\n"
                    "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppet"
                ),
                "enabled": "1",
                "gpgcheck": "1",
            }
        }
        for section in expected:
            self.assertTrue(
                parser.has_section(section),
                "Contains section {0}".format(section),
            )
            for k, v in expected[section].items():
                self.assertEqual(parser.get(section, k), v)


class TestAddYumRepoSchema:
    @pytest.mark.parametrize(
        "config, error_msg",
        [
            # Happy path case
            ({"yum_repos": {"My-Repo 123": {"baseurl": "http://doit"}}}, None),
            # yum_repo_dir is a string
            (
                {"yum_repo_dir": True},
                "yum_repo_dir: True is not of type 'string'",
            ),
            (
                {"yum_repos": {}},
                re.escape("yum_repos: {} does not have enough properties"),
            ),
            # baseurl required
            (
                {"yum_repos": {"My-Repo": {}}},
                "yum_repos.My-Repo: 'baseurl' is a required",
            ),
            # patternProperties don't override type of explicit property names
            (
                {"yum_repos": {"My Repo": {"enabled": "nope"}}},
                "yum_repos.My Repo.enabled: 'nope' is not of type 'boolean'",
            ),
        ],
    )
    @helpers.skipUnlessJsonSchema()
    def test_schema_validation(self, config, error_msg):
        if error_msg is None:
            validate_cloudconfig_schema(config, get_schema(), strict=True)
        else:
            with pytest.raises(SchemaValidationError, match=error_msg):
                validate_cloudconfig_schema(config, get_schema(), strict=True)


# vi: ts=4 expandtab