summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_apt_source.py
blob: 22a4accf25ca2571ae792279e20c397b0302c65a (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
""" test_handler_apt_source
Testing various config variations of the apt_source config
"""
import os
import shutil
import tempfile
import re

from cloudinit import distros
from cloudinit import util
from cloudinit.config import cc_apt_configure

from ..helpers import TestCase

UNKNOWN_ARCH_INFO = {
    'arches': ['default'],
    'failsafe': {'primary': 'http://fs-primary-default',
                 'security': 'http://fs-security-default'}
}

PACKAGE_MIRRORS = [
    {'arches': ['i386', 'amd64'],
     'failsafe': {'primary': 'http://fs-primary-intel',
                  'security': 'http://fs-security-intel'},
     'search': {
         'primary': ['http://%(ec2_region)s.ec2/',
                     'http://%(availability_zone)s.clouds/'],
         'security': ['http://security-mirror1-intel',
                      'http://security-mirror2-intel']}},
    {'arches': ['armhf', 'armel'],
     'failsafe': {'primary': 'http://fs-primary-arm',
                  'security': 'http://fs-security-arm'}},
    UNKNOWN_ARCH_INFO
]

GAPMI = distros._get_arch_package_mirror_info

def load_tfile_or_url(*args, **kwargs):
    """ load_tfile_or_url
    load file and return content after decoding
    """
    return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents)

class TestAptSourceConfig(TestCase):
    """ TestAptSourceConfig
    Main Class to test apt_source configs
    """
    def setUp(self):
        super(TestAptSourceConfig, self).setUp()
        self.tmp = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.tmp)
        self.aptlistfile = os.path.join(self.tmp, "single-deb.list")


    @staticmethod
    def _get_default_params():
        """ get_default_params
        Get the most basic default mrror and release info to be used in tests
        """
        params = {}
        params['RELEASE'] = cc_apt_configure.get_release()
        params['MIRROR'] = "http://archive.ubuntu.com/ubuntu"
        return params


    def test_apt_source_basic(self):
        """ test_apt_source_basic
        Test Fix deb source string, has to overwrite mirror conf in params
        """
        params = self._get_default_params()
        cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu'
                          ' karmic-backports'
                          ' main universe multiverse restricted'),
               'filename': self.aptlistfile}

        cc_apt_configure.add_sources([cfg], params)

        self.assertTrue(os.path.isfile(self.aptlistfile))

        contents = load_tfile_or_url(self.aptlistfile)
        self.assertTrue(re.search(r"%s %s %s %s\n" %
                                  ("deb", "http://archive.ubuntu.com/ubuntu",
                                   "karmic-backports",
                                   "main universe multiverse restricted"),
                                  contents, flags=re.IGNORECASE))


    def test_apt_source_replacement(self):
        """ test_apt_source_replace
        Test Autoreplacement of MIRROR and RELEASE in source specs
        """
        params = self._get_default_params()
        cfg = {'source': 'deb $MIRROR $RELEASE multiverse',
               'filename': self.aptlistfile}

        cc_apt_configure.add_sources([cfg], params)

        self.assertTrue(os.path.isfile(self.aptlistfile))

        contents = load_tfile_or_url(self.aptlistfile)
        self.assertTrue(re.search(r"%s %s %s %s\n" %
                                  ("deb", params['MIRROR'], params['RELEASE'],
                                   "multiverse"),
                                  contents, flags=re.IGNORECASE))


    def test_apt_source_ppa(self):
        """ test_apt_source_ppa
        Test specification of a ppa
        """
        params = self._get_default_params()
        cfg = {'source': 'ppa:smoser/cloud-init-test',
               'filename': self.aptlistfile}

        cc_apt_configure.add_sources([cfg], params)

        self.assertTrue(os.path.isfile(self.aptlistfile))

        # report content before making regex
        # FAIL ? goes in "untranslated"
        # should become e.g. deb http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu xenial main
        contents = load_tfile_or_url(self.aptlistfile)
        print(contents)
        self.assertTrue(1 == 2)


# vi: ts=4 expandtab