예제 #1
0
    def test_apt_src_ppa_tri(self):
        """Test adding three ppa's"""
        cfg1 = {
            'source': 'ppa:smoser/cloud-init-test',
            'filename': self.aptlistfile
        }
        cfg2 = {
            'source': 'ppa:smoser/cloud-init-test2',
            'filename': self.aptlistfile2
        }
        cfg3 = {
            'source': 'ppa:smoser/cloud-init-test3',
            'filename': self.aptlistfile3
        }
        cfg = self.wrapv1conf([cfg1, cfg2, cfg3])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)
        calls = [
            call(['add-apt-repository', 'ppa:smoser/cloud-init-test'],
                 target=None),
            call(['add-apt-repository', 'ppa:smoser/cloud-init-test2'],
                 target=None),
            call(['add-apt-repository', 'ppa:smoser/cloud-init-test3'],
                 target=None)
        ]
        mockobj.assert_has_calls(calls, any_order=True)

        # adding ppa should ignore all filenames (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
        self.assertFalse(os.path.isfile(self.aptlistfile2))
        self.assertFalse(os.path.isfile(self.aptlistfile3))
예제 #2
0
    def apt_src_keyid(self, filename, cfg, keynum):
        """apt_src_keyid
        Test specification of a source + keyid
        """
        cfg = self.wrapv1conf(cfg)

        with mock.patch.object(cc_apt_configure, 'add_apt_key') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        # check if it added the right number of keys
        calls = []
        sources = cfg['apt']['sources']
        for src in sources:
            print(sources[src])
            calls.append(call(sources[src], None))

        mockobj.assert_has_calls(calls, any_order=True)

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

        contents = util.load_file(filename)
        self.assertTrue(
            re.search(r"%s %s %s %s\n" %
                      ("deb", ('http://ppa.launchpad.net/smoser/'
                               'cloud-init-test/ubuntu'), "xenial", "main"),
                      contents,
                      flags=re.IGNORECASE))
예제 #3
0
    def _apt_source_list(self, cfg, expected, distro):
        "_apt_source_list - Test rendering from template (generic)"

        # entry at top level now, wrap in 'apt' key
        cfg = {'apt': cfg}
        mycloud = self._get_cloud(distro)
        with mock.patch.object(util, 'write_file') as mockwf:
            with mock.patch.object(util,
                                   'load_file',
                                   return_value=MOCKED_APT_SRC_LIST) as mocklf:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mockisfile:
                    with mock.patch.object(util, 'rename'):
                        cc_apt_configure.handle("test", cfg, mycloud, LOG,
                                                None)

        # check if it would have loaded the distro template
        mockisfile.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mocklf.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        # check expected content in result
        mockwf.assert_called_once_with('/etc/apt/sources.list',
                                       expected,
                                       mode=0o644)
예제 #4
0
    def apt_src_keyid_real(self, cfg, expectedkey, is_hardened=None):
        """apt_src_keyid_real
        Test specification of a keyid without source including
        up to addition of the key (add_apt_key_raw mocked to keep the
        environment as is)
        """
        key = cfg["keyid"]
        keyserver = cfg.get("keyserver", "keyserver.ubuntu.com")
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(cc_apt_configure, "add_apt_key_raw") as mockkey:
            with mock.patch.object(gpg, "getkeybyid",
                                   return_value=expectedkey) as mockgetkey:
                cc_apt_configure.handle("test", cfg, self.fakecloud, None,
                                        None)
        if is_hardened is not None:
            mockkey.assert_called_with(expectedkey,
                                       self.aptlistfile,
                                       hardened=is_hardened)
        else:
            mockkey.assert_called_with(expectedkey, self.aptlistfile)
        mockgetkey.assert_called_with(key, keyserver)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #5
0
    def apt_src_keyid(self, filename, cfg, keynum):
        """apt_src_keyid
        Test specification of a source + keyid
        """
        cfg = self.wrapv1conf(cfg)

        with mock.patch.object(util, 'subp',
                               return_value=('fakekey 1234', '')) as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        # check if it added the right ammount of keys
        calls = []
        for _ in range(keynum):
            calls.append(call(['apt-key', 'add', '-'],
                              data=b'fakekey 1234',
                              target=None))
        mockobj.assert_has_calls(calls, any_order=True)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(re.search(r"%s %s %s %s\n" %
                                  ("deb",
                                   ('http://ppa.launchpad.net/smoser/'
                                    'cloud-init-test/ubuntu'),
                                   "xenial", "main"),
                                  contents, flags=re.IGNORECASE))
예제 #6
0
    def apt_src_key(self, filename, cfg):
        """apt_src_key
        Test specification of a source + key
        """
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(cc_apt_configure, "add_apt_key") as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        # check if it added the right amount of keys
        sources = cfg["apt"]["sources"]
        calls = []
        for src in sources:
            print(sources[src])
            calls.append(call(sources[src], None))

        mockobj.assert_has_calls(calls, any_order=True)

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

        contents = util.load_file(filename)
        self.assertTrue(
            re.search(
                r"%s %s %s %s\n" % (
                    "deb",
                    "http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu",
                    "xenial",
                    "main",
                ),
                contents,
                flags=re.IGNORECASE,
            ))
    def apt_source_list(self, distro, mirror, mirrorcheck=None):
        """apt_source_list
        Test rendering of a source.list from template for a given distro
        """
        if mirrorcheck is None:
            mirrorcheck = mirror

        if isinstance(mirror, list):
            cfg = {'apt_mirror_search': mirror}
        else:
            cfg = {'apt_mirror': mirror}
        mycloud = self._get_cloud(distro)

        with mock.patch.object(templater, 'render_to_file') as mocktmpl:
            with mock.patch.object(os.path, 'isfile',
                                   return_value=True) as mockisfile:
                with mock.patch.object(util, 'rename'):
                    cc_apt_configure.handle("notimportant", cfg, mycloud,
                                            LOG, None)

        mockisfile.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mocktmpl.assert_called_once_with(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro),
            '/etc/apt/sources.list',
            {'codename': '', 'primary': mirrorcheck, 'mirror': mirrorcheck})
예제 #8
0
    def apt_src_keyid(self, filename, cfg, keynum):
        """apt_src_keyid
        Test specification of a source + keyid
        """
        cfg = self.wrapv1conf(cfg)

        with mock.patch.object(util, 'subp',
                               return_value=('fakekey 1234', '')) as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        # check if it added the right ammount of keys
        calls = []
        for _ in range(keynum):
            calls.append(
                call(['apt-key', 'add', '-'],
                     data=b'fakekey 1234',
                     target=None))
        mockobj.assert_has_calls(calls, any_order=True)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(
            re.search(r"%s %s %s %s\n" %
                      ("deb", ('http://ppa.launchpad.net/smoser/'
                               'cloud-init-test/ubuntu'), "xenial", "main"),
                      contents,
                      flags=re.IGNORECASE))
예제 #9
0
    def test_apt_src_ppa_tri(self):
        """Test adding three ppa's"""
        cfg1 = {'source': 'ppa:smoser/cloud-init-test',
                'filename': self.aptlistfile}
        cfg2 = {'source': 'ppa:smoser/cloud-init-test2',
                'filename': self.aptlistfile2}
        cfg3 = {'source': 'ppa:smoser/cloud-init-test3',
                'filename': self.aptlistfile3}
        cfg = self.wrapv1conf([cfg1, cfg2, cfg3])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud,
                                    None, None)
        calls = [call(['add-apt-repository', 'ppa:smoser/cloud-init-test'],
                      target=None),
                 call(['add-apt-repository', 'ppa:smoser/cloud-init-test2'],
                      target=None),
                 call(['add-apt-repository', 'ppa:smoser/cloud-init-test3'],
                      target=None)]
        mockobj.assert_has_calls(calls, any_order=True)

        # adding ppa should ignore all filenames (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
        self.assertFalse(os.path.isfile(self.aptlistfile2))
        self.assertFalse(os.path.isfile(self.aptlistfile3))
    def test_apt_v3_source_list_ubuntu_snappy(self):
        """test_apt_v3_source_list_ubuntu_snappy - without custom sources or
        parms"""
        cfg = {'apt': {}}
        mycloud = self._get_cloud('ubuntu')

        with mock.patch.object(util, 'write_file') as mock_writefile:
            with mock.patch.object(util, 'system_is_snappy',
                                   return_value=True) as mock_issnappy:
                cc_apt_configure.handle("test", cfg, mycloud, LOG, None)

        self.assertEqual(0, mock_writefile.call_count)
        self.assertEqual(1, mock_issnappy.call_count)
예제 #11
0
    def apt_source_list(self, distro, mirror, mirrorcheck=None):
        """apt_source_list
        Test rendering of a source.list from template for a given distro
        """
        if mirrorcheck is None:
            mirrorcheck = mirror

        if isinstance(mirror, list):
            cfg = {"apt_mirror_search": mirror}
        else:
            cfg = {"apt_mirror": mirror}

        mycloud = get_cloud(distro)

        with mock.patch.object(util, "write_file") as mockwf:
            with mock.patch.object(
                util, "load_file", return_value="faketmpl"
            ) as mocklf:
                with mock.patch.object(
                    os.path, "isfile", return_value=True
                ) as mockisfile:
                    with mock.patch.object(
                        templater, "render_string", return_value="fake"
                    ) as mockrnd:
                        with mock.patch.object(util, "rename"):
                            cc_apt_configure.handle(
                                "test", cfg, mycloud, LOG, None
                            )

        mockisfile.assert_any_call(
            "/etc/cloud/templates/sources.list.%s.tmpl" % distro
        )
        mocklf.assert_any_call(
            "/etc/cloud/templates/sources.list.%s.tmpl" % distro
        )
        mockrnd.assert_called_once_with(
            "faketmpl",
            {
                "RELEASE": "fakerelease",
                "PRIMARY": mirrorcheck,
                "MIRROR": mirrorcheck,
                "SECURITY": mirrorcheck,
                "codename": "fakerelease",
                "primary": mirrorcheck,
                "mirror": mirrorcheck,
                "security": mirrorcheck,
            },
        )
        mockwf.assert_called_once_with(
            "/etc/apt/sources.list", "fake", mode=0o644
        )
예제 #12
0
    def test_apt_src_keyonly(self):
        """Test specifying key without source"""
        cfg = {'key': "fakekey 4242", 'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_once_with(['apt-key', 'add', '-'],
                                        data=b'fakekey 4242',
                                        target=None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #13
0
    def test_apt_src_keyonly(self):
        """Test specifying key without source"""
        cfg = {'key': "fakekey 4242",
               'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_once_with(['apt-key', 'add', '-'],
                                        data=b'fakekey 4242', target=None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #14
0
    def test_apt_src_ppa_tri(self):
        """Test adding three ppa's"""
        cfg1 = {
            "source": "ppa:smoser/cloud-init-test",
            "filename": self.aptlistfile,
        }
        cfg2 = {
            "source": "ppa:smoser/cloud-init-test2",
            "filename": self.aptlistfile2,
        }
        cfg3 = {
            "source": "ppa:smoser/cloud-init-test3",
            "filename": self.aptlistfile3,
        }
        cfg = self.wrapv1conf([cfg1, cfg2, cfg3])

        with mock.patch.object(subp, "subp") as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)
        calls = [
            call(
                [
                    "add-apt-repository",
                    "--no-update",
                    "ppa:smoser/cloud-init-test",
                ],
                target=None,
            ),
            call(
                [
                    "add-apt-repository",
                    "--no-update",
                    "ppa:smoser/cloud-init-test2",
                ],
                target=None,
            ),
            call(
                [
                    "add-apt-repository",
                    "--no-update",
                    "ppa:smoser/cloud-init-test3",
                ],
                target=None,
            ),
        ]
        mockobj.assert_has_calls(calls, any_order=True)

        # adding ppa should ignore all filenames (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
        self.assertFalse(os.path.isfile(self.aptlistfile2))
        self.assertFalse(os.path.isfile(self.aptlistfile3))
예제 #15
0
    def test_apt_src_ppa(self):
        """Test adding a ppa"""
        cfg = {'source': 'ppa:smoser/cloud-init-test',
               'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)
        mockobj.assert_called_once_with(['add-apt-repository',
                                         'ppa:smoser/cloud-init-test'],
                                        target=None)

        # adding ppa should ignore filename (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #16
0
    def apt_src_replacement(self, filename, cfg):
        """apt_src_replace
        Test Autoreplacement of MIRROR and RELEASE in source specs
        """
        cfg = self.wrapv1conf(cfg)
        params = self._get_default_params()
        cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(re.search(r"%s %s %s %s\n" %
                                  ("deb", params['MIRROR'], params['RELEASE'],
                                   "multiverse"),
                                  contents, flags=re.IGNORECASE))
예제 #17
0
    def test_apt_src_keyidonly(self):
        """Test specification of a keyid without source"""
        cfg = {'keyid': "03683F77", 'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp',
                               return_value=('fakekey 1212', '')) as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_with(['apt-key', 'add', '-'],
                                   data=b'fakekey 1212',
                                   target=None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #18
0
    def test_apt_src_keyonly(self):
        """Test specifying key without source"""
        cfg = {'key': "fakekey 4242", 'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])
        with mock.patch.object(cc_apt_configure, 'apt_key') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        calls = (call('add',
                      output_file=pathlib.Path(self.aptlistfile).stem,
                      data='fakekey 4242',
                      hardened=False), )
        mockobj.assert_has_calls(calls, any_order=True)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #19
0
    def test_apt_src_ppa(self):
        """Test adding a ppa"""
        cfg = {
            "source": "ppa:smoser/cloud-init-test",
            "filename": self.aptlistfile,
        }
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(subp, "subp") as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)
        mockobj.assert_called_once_with(
            ["add-apt-repository", "ppa:smoser/cloud-init-test"], target=None)

        # adding ppa should ignore filename (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #20
0
    def test_apt_src_keyidonly(self):
        """Test specification of a keyid without source"""
        cfg = {'keyid': "03683F77",
               'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp',
                               return_value=('fakekey 1212', '')) as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_with(['apt-key', 'add', '-'],
                                   data=b'fakekey 1212', target=None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #21
0
    def test_apt_src_ppa(self):
        """Test adding a ppa"""
        cfg = {
            'source': 'ppa:smoser/cloud-init-test',
            'filename': self.aptlistfile
        }
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)
        mockobj.assert_called_once_with(
            ['add-apt-repository', 'ppa:smoser/cloud-init-test'], target=None)

        # adding ppa should ignore filename (uses add-apt-repository)
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #22
0
    def apt_src_basic(self, filename, cfg):
        """apt_src_basic
        Test Fix deb source string, has to overwrite mirror conf in params
        """
        cfg = self.wrapv1conf(cfg)

        cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

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

        contents = load_tfile_or_url(filename)
        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_v1_srcl_custom(self):
        """Test rendering from a custom source.list template"""
        cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL)
        mycloud = self._get_cloud('ubuntu')

        # the second mock restores the original subp
        with mock.patch.object(util, 'write_file') as mockwrite:
            with mock.patch.object(util, 'subp', self.subp):
                with mock.patch.object(Distro, 'get_primary_arch',
                                       return_value='amd64'):
                    cc_apt_configure.handle("notimportant", cfg, mycloud,
                                            LOG, None)

        mockwrite.assert_called_once_with(
            '/etc/apt/sources.list',
            EXPECTED_CONVERTED_CONTENT,
            mode=420)
    def test_apt_v1_srcl_custom(self):
        """Test rendering from a custom source.list template"""
        cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL)
        mycloud = self._get_cloud('ubuntu')

        # the second mock restores the original subp
        with mock.patch.object(util, 'write_file') as mockwrite:
            with mock.patch.object(util, 'subp', self.subp):
                with mock.patch.object(Distro,
                                       'get_primary_arch',
                                       return_value='amd64'):
                    cc_apt_configure.handle("notimportant", cfg, mycloud, LOG,
                                            None)

        mockwrite.assert_called_once_with('/etc/apt/sources.list',
                                          EXPECTED_CONVERTED_CONTENT,
                                          mode=420)
예제 #25
0
    def apt_src_replacement(self, filename, cfg):
        """apt_src_replace
        Test Autoreplacement of MIRROR and RELEASE in source specs
        """
        cfg = self.wrapv1conf(cfg)
        params = self._get_default_params()
        cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(
            re.search(
                r"%s %s %s %s\n" %
                ("deb", params['MIRROR'], params['RELEASE'], "multiverse"),
                contents,
                flags=re.IGNORECASE))
예제 #26
0
    def apt_src_basic(self, filename, cfg):
        """apt_src_basic
        Test Fix deb source string, has to overwrite mirror conf in params
        """
        cfg = self.wrapv1conf(cfg)

        cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

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

        contents = load_tfile_or_url(filename)
        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 apt_source_list(self, distro, mirror, mirrorcheck=None):
        """apt_source_list
        Test rendering of a source.list from template for a given distro
        """
        if mirrorcheck is None:
            mirrorcheck = mirror

        if isinstance(mirror, list):
            cfg = {'apt_mirror_search': mirror}
        else:
            cfg = {'apt_mirror': mirror}

        mycloud = self._get_cloud(distro)

        with mock.patch.object(util, 'write_file') as mockwf:
            with mock.patch.object(util, 'load_file',
                                   return_value="faketmpl") as mocklf:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mockisfile:
                    with mock.patch.object(templater,
                                           'render_string',
                                           return_value='fake') as mockrnd:
                        with mock.patch.object(util, 'rename'):
                            cc_apt_configure.handle("test", cfg, mycloud, LOG,
                                                    None)

        mockisfile.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mocklf.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mockrnd.assert_called_once_with(
            'faketmpl', {
                'RELEASE': 'fakerelease',
                'PRIMARY': mirrorcheck,
                'MIRROR': mirrorcheck,
                'SECURITY': mirrorcheck,
                'codename': 'fakerelease',
                'primary': mirrorcheck,
                'mirror': mirrorcheck,
                'security': mirrorcheck
            })
        mockwf.assert_called_once_with('/etc/apt/sources.list',
                                       'fake',
                                       mode=0o644)
예제 #28
0
    def test_apt_src_keyidonly(self):
        """Test specification of a keyid without source"""
        cfg = {'keyid': "03683F77", 'filename': self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(subp, 'subp',
                               return_value=('fakekey 1212', '')):
            with mock.patch.object(cc_apt_configure, 'apt_key') as mockobj:
                cc_apt_configure.handle("test", cfg, self.fakecloud, None,
                                        None)

        calls = (call('add',
                      output_file=pathlib.Path(self.aptlistfile).stem,
                      data='fakekey 1212',
                      hardened=False), )
        mockobj.assert_has_calls(calls, any_order=True)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
    def test_apt_v3_srcl_custom(self):
        """test_apt_v3_srcl_custom - Test rendering a custom source template"""
        cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL)
        mycloud = get_cloud()

        # the second mock restores the original subp
        with mock.patch.object(util, 'write_file') as mockwrite:
            with mock.patch.object(subp, 'subp', self.subp):
                with mock.patch.object(Distro,
                                       'get_primary_arch',
                                       return_value='amd64'):
                    cc_apt_configure.handle("notimportant", cfg, mycloud, LOG,
                                            None)

        calls = [
            call('/etc/apt/sources.list',
                 EXPECTED_CONVERTED_CONTENT,
                 mode=0o644)
        ]
        mockwrite.assert_has_calls(calls)
예제 #30
0
    def test_apt_src_keyidonly(self):
        """Test specification of a keyid without source"""
        cfg = {"keyid": "03683F77", "filename": self.aptlistfile}
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(subp, "subp",
                               return_value=("fakekey 1212", "")):
            with mock.patch.object(cc_apt_configure, "apt_key") as mockobj:
                cc_apt_configure.handle("test", cfg, self.fakecloud, None,
                                        None)

        calls = (call(
            "add",
            output_file=pathlib.Path(self.aptlistfile).stem,
            data="fakekey 1212",
            hardened=False,
        ), )
        mockobj.assert_has_calls(calls, any_order=True)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #31
0
    def apt_src_keyid_real(self, cfg, expectedkey):
        """apt_src_keyid_real
        Test specification of a keyid without source including
        up to addition of the key (add_apt_key_raw mocked to keep the
        environment as is)
        """
        key = cfg['keyid']
        keyserver = cfg.get('keyserver', 'keyserver.ubuntu.com')
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(cc_apt_configure, 'add_apt_key_raw') as mockkey:
            with mock.patch.object(gpg, 'getkeybyid',
                                   return_value=expectedkey) as mockgetkey:
                cc_apt_configure.handle("test", cfg, self.fakecloud, None,
                                        None)

        mockgetkey.assert_called_with(key, keyserver)
        mockkey.assert_called_with(expectedkey, None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
예제 #32
0
    def apt_src_key(self, filename, cfg):
        """apt_src_key
        Test specification of a source + key
        """
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_with(['apt-key', 'add', '-'],
                                   data=b'fakekey 4321', target=None)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(re.search(r"%s %s %s %s\n" %
                                  ("deb",
                                   ('http://ppa.launchpad.net/smoser/'
                                    'cloud-init-test/ubuntu'),
                                   "xenial", "main"),
                                  contents, flags=re.IGNORECASE))
예제 #33
0
    def apt_src_keyid_real(self, cfg, expectedkey):
        """apt_src_keyid_real
        Test specification of a keyid without source including
        up to addition of the key (add_apt_key_raw mocked to keep the
        environment as is)
        """
        key = cfg['keyid']
        keyserver = cfg.get('keyserver', 'keyserver.ubuntu.com')
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(cc_apt_configure, 'add_apt_key_raw') as mockkey:
            with mock.patch.object(gpg, 'getkeybyid',
                                   return_value=expectedkey) as mockgetkey:
                cc_apt_configure.handle("test", cfg, self.fakecloud,
                                        None, None)

        mockgetkey.assert_called_with(key, keyserver)
        mockkey.assert_called_with(expectedkey, None)

        # filename should be ignored on key only
        self.assertFalse(os.path.isfile(self.aptlistfile))
    def _apt_source_list(self, distro, cfg, cfg_on_empty=False):
        """_apt_source_list - Test rendering from template (generic)"""
        # entry at top level now, wrap in 'apt' key
        cfg = {'apt': cfg}
        mycloud = self._get_cloud(distro)

        with mock.patch.object(util, 'write_file') as mock_writefile:
            with mock.patch.object(
                    util, 'load_file',
                    return_value=MOCKED_APT_SRC_LIST) as mock_loadfile:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mock_isfile:
                    cfg_func = ('cloudinit.config.cc_apt_configure.' +
                                '_should_configure_on_empty_apt')
                    with mock.patch(cfg_func,
                                    return_value=(cfg_on_empty,
                                                  "test")) as mock_shouldcfg:
                        cc_apt_configure.handle("test", cfg, mycloud, LOG,
                                                None)

        return mock_writefile, mock_loadfile, mock_isfile, mock_shouldcfg
    def apt_source_list(self, distro, mirror, mirrorcheck=None):
        """apt_source_list
        Test rendering of a source.list from template for a given distro
        """
        if mirrorcheck is None:
            mirrorcheck = mirror

        if isinstance(mirror, list):
            cfg = {'apt_mirror_search': mirror}
        else:
            cfg = {'apt_mirror': mirror}
        mycloud = self._get_cloud(distro)

        with mock.patch.object(util, 'write_file') as mockwf:
            with mock.patch.object(util, 'load_file',
                                   return_value="faketmpl") as mocklf:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mockisfile:
                    with mock.patch.object(templater, 'render_string',
                                           return_value="fake") as mockrnd:
                        with mock.patch.object(util, 'rename'):
                            cc_apt_configure.handle("test", cfg, mycloud,
                                                    LOG, None)

        mockisfile.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mocklf.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mockrnd.assert_called_once_with('faketmpl',
                                        {'RELEASE': 'fakerelease',
                                         'PRIMARY': mirrorcheck,
                                         'MIRROR': mirrorcheck,
                                         'SECURITY': mirrorcheck,
                                         'codename': 'fakerelease',
                                         'primary': mirrorcheck,
                                         'mirror': mirrorcheck,
                                         'security': mirrorcheck})
        mockwf.assert_called_once_with('/etc/apt/sources.list', 'fake',
                                       mode=0o644)
예제 #36
0
    def apt_src_key(self, filename, cfg):
        """apt_src_key
        Test specification of a source + key
        """
        cfg = self.wrapv1conf([cfg])

        with mock.patch.object(util, 'subp') as mockobj:
            cc_apt_configure.handle("test", cfg, self.fakecloud, None, None)

        mockobj.assert_called_with(['apt-key', 'add', '-'],
                                   data=b'fakekey 4321',
                                   target=None)

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

        contents = load_tfile_or_url(filename)
        self.assertTrue(
            re.search(r"%s %s %s %s\n" %
                      ("deb", ('http://ppa.launchpad.net/smoser/'
                               'cloud-init-test/ubuntu'), "xenial", "main"),
                      contents,
                      flags=re.IGNORECASE))
    def _apt_source_list(self, distro, cfg, cfg_on_empty=False):
        """_apt_source_list - Test rendering from template (generic)"""
        # entry at top level now, wrap in 'apt' key
        cfg = {'apt': cfg}
        mycloud = get_cloud(distro)

        with ExitStack() as stack:
            mock_writefile = stack.enter_context(
                mock.patch.object(util, 'write_file'))
            mock_loadfile = stack.enter_context(
                mock.patch.object(util,
                                  'load_file',
                                  return_value=MOCKED_APT_SRC_LIST))
            mock_isfile = stack.enter_context(
                mock.patch.object(os.path, 'isfile', return_value=True))
            stack.enter_context(mock.patch.object(util, 'del_file'))
            cfg_func = ('cloudinit.config.cc_apt_configure.'
                        '_should_configure_on_empty_apt')
            mock_shouldcfg = stack.enter_context(
                mock.patch(cfg_func, return_value=(cfg_on_empty, 'test')))
            cc_apt_configure.handle("test", cfg, mycloud, LOG, None)

            return mock_writefile, mock_loadfile, mock_isfile, mock_shouldcfg
예제 #38
0
    def _apt_source_list(self, cfg, expected, distro):
        "_apt_source_list - Test rendering from template (generic)"

        # entry at top level now, wrap in 'apt' key
        cfg = {'apt': cfg}
        mycloud = self._get_cloud(distro)
        with mock.patch.object(util, 'write_file') as mockwf:
            with mock.patch.object(util, 'load_file',
                                   return_value=MOCKED_APT_SRC_LIST) as mocklf:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mockisfile:
                    with mock.patch.object(util, 'rename'):
                        cc_apt_configure.handle("test", cfg, mycloud,
                                                LOG, None)

        # check if it would have loaded the distro template
        mockisfile.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        mocklf.assert_any_call(
            ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
        # check expected content in result
        mockwf.assert_called_once_with('/etc/apt/sources.list', expected,
                                       mode=0o644)