示例#1
0
    def test_parse_operating_system_repos(self, mock_parse_schemes, mock_ub,
                                          mock_deb, mock_proxies):
        data = {'repos': REPOS_SAMPLE, 'codename': 'trusty'}

        mock_deb_expected_calls = []
        repos = []
        for r in REPOS_SAMPLE:
            kwargs = {
                'name': r['name'],
                'uri': r['uri'],
                'suite': r['suite'],
                'section': r['section'],
                'priority': r['priority']
            }
            mock_deb_expected_calls.append(mock.call(**kwargs))
            repos.append(objects.DEBRepo(**kwargs))
        driver = NailgunBuildImage(data)
        mock_ub_instance = mock_ub.return_value
        mock_ub_instance.repos = repos
        mock_ub.assert_called_once_with(
            repos=repos,
            packages=NailgunBuildImage.DEFAULT_TRUSTY_PACKAGES,
            major=14,
            minor=4,
            proxies=mock_proxies.return_value)
        self.assertEqual(mock_deb_expected_calls,
                         mock_deb.call_args_list[:len(REPOS_SAMPLE)])
        self.assertEqual(driver.operating_system.repos, repos)
示例#2
0
    def parse_operating_system(self):
        if self.data.get('codename').lower() != 'trusty':
            raise errors.WrongInputDataError(
                'Currently, only Ubuntu Trusty is supported, given '
                'codename is {0}'.format(self.data.get('codename')))

        packages = self.data.get('packages', self.DEFAULT_TRUSTY_PACKAGES)

        repos = []
        for repo in self.data['repos']:
            repos.append(objects.DEBRepo(
                name=repo['name'],
                uri=repo['uri'],
                suite=repo['suite'],
                section=repo['section'],
                priority=repo['priority']))

        self.operating_system = objects.Ubuntu(repos=repos, packages=packages)
示例#3
0
    def parse_operating_system(self):
        if self.data.get('codename').lower() != 'trusty':
            raise errors.WrongInputDataError(
                'Currently, only Ubuntu Trusty is supported, given '
                'codename is {0}'.format(self.data.get('codename')))

        packages = self.data.get('packages', self.DEFAULT_TRUSTY_PACKAGES)

        repos = []
        for repo in self.data['repos']:
            repos.append(objects.DEBRepo(
                name=repo['name'],
                uri=repo['uri'],
                suite=repo['suite'],
                section=repo['section'],
                priority=repo['priority']))

        proxies = objects.RepoProxies()

        proxy_dict = self.data.get('proxies', {})
        for protocol, uri in six.iteritems(proxy_dict.get('protocols', {})):
            proxies.add_proxy(protocol, uri)
        proxies.add_direct_repo_addrs(proxy_dict.get(
            'direct_repo_addr_list', []))

        os = objects.Ubuntu(repos=repos, packages=packages, major=14, minor=4,
                            proxies=proxies)

        # add root account
        root_password = self.data.get('root_password')
        hashed_root_password = self.data.get('hashed_root_password')

        # for backward compatibily set default password is no password provided
        if root_password is None and hashed_root_password is None:
            root_password = CONF.default_root_password

        os.add_user_account(
            name='root',
            password=root_password,
            homedir='/root',
            hashed_password=hashed_root_password,
        )
        return os
示例#4
0
    def test_do_build_image(self, mock_umount_target, mock_mount_target,
                            mock_yaml_dump, mock_mkdtemp, mock_open,
                            mock_shutil_move, mock_os, mock_utils, mock_fu,
                            mock_bu):

        loops = [objects.Loop(), objects.Loop()]

        self.mgr.driver.image_scheme = objects.ImageScheme([
            objects.Image('file:///fake/img.img.gz', loops[0], 'ext4', 'gzip'),
            objects.Image('file:///fake/img-boot.img.gz', loops[1], 'ext2',
                          'gzip')
        ])
        self.mgr.driver.partition_scheme = objects.PartitionScheme()
        self.mgr.driver.partition_scheme.add_fs(device=loops[0],
                                                mount='/',
                                                fs_type='ext4')
        self.mgr.driver.partition_scheme.add_fs(device=loops[1],
                                                mount='/boot',
                                                fs_type='ext2')
        self.mgr.driver.metadata_uri = 'file:///fake/img.yaml'
        self.mgr.driver.operating_system = objects.Ubuntu(
            repos=[
                objects.DEBRepo('ubuntu',
                                'http://fakeubuntu',
                                'trusty',
                                'fakesection',
                                priority=900),
                objects.DEBRepo('ubuntu_zero',
                                'http://fakeubuntu_zero',
                                'trusty',
                                'fakesection',
                                priority=None),
                objects.DEBRepo('mos',
                                'http://fakemos',
                                'mosX.Y',
                                'fakesection',
                                priority=1000)
            ],
            packages=['fakepackage1', 'fakepackage2'])

        mock_os.path.exists.return_value = False
        mock_os.path.join.return_value = '/tmp/imgdir/proc'
        mock_os.path.basename.side_effect = ['img.img.gz', 'img-boot.img.gz']
        mock_bu.create_sparse_tmp_file.side_effect = \
            ['/tmp/img', '/tmp/img-boot']
        mock_bu.get_free_loop_device.side_effect = ['/dev/loop0', '/dev/loop1']
        mock_mkdtemp.return_value = '/tmp/imgdir'
        getsize_side = [20, 2, 10, 1]
        mock_os.path.getsize.side_effect = getsize_side
        md5_side = [
            'fakemd5_raw', 'fakemd5_gzip', 'fakemd5_raw_boot',
            'fakemd5_gzip_boot'
        ]
        mock_utils.calculate_md5.side_effect = md5_side
        mock_bu.containerize.side_effect = ['/tmp/img.gz', '/tmp/img-boot.gz']
        mock_bu.stop_chrooted_processes.side_effect = [
            False, True, False, True
        ]

        self.mgr.do_build_image()
        self.assertEqual([
            mock.call('/fake/img.img.gz'),
            mock.call('/fake/img-boot.img.gz')
        ], mock_os.path.exists.call_args_list)
        self.assertEqual([
            mock.call(dir=CONF.image_build_dir, suffix=CONF.image_build_suffix)
        ] * 2, mock_bu.create_sparse_tmp_file.call_args_list)
        self.assertEqual([mock.call()] * 2,
                         mock_bu.get_free_loop_device.call_args_list)
        self.assertEqual([
            mock.call('/tmp/img', '/dev/loop0'),
            mock.call('/tmp/img-boot', '/dev/loop1')
        ], mock_bu.attach_file_to_loop.call_args_list)
        self.assertEqual([
            mock.call(
                fs_type='ext4', fs_options='', fs_label='', dev='/dev/loop0'),
            mock.call(
                fs_type='ext2', fs_options='', fs_label='', dev='/dev/loop1')
        ], mock_fu.make_fs.call_args_list)
        mock_mkdtemp.assert_called_once_with(dir=CONF.image_build_dir,
                                             suffix=CONF.image_build_suffix)
        mock_mount_target.assert_called_once_with('/tmp/imgdir',
                                                  treat_mtab=False,
                                                  pseudo=False)
        self.assertEqual([mock.call('/tmp/imgdir')] * 2,
                         mock_bu.suppress_services_start.call_args_list)
        mock_bu.run_debootstrap.assert_called_once_with(
            uri='http://fakeubuntu', suite='trusty', chroot='/tmp/imgdir')
        mock_bu.set_apt_get_env.assert_called_once_with()
        mock_bu.pre_apt_get.assert_called_once_with('/tmp/imgdir')
        self.assertEqual([
            mock.call(name='ubuntu',
                      uri='http://fakeubuntu',
                      suite='trusty',
                      section='fakesection',
                      chroot='/tmp/imgdir'),
            mock.call(name='ubuntu_zero',
                      uri='http://fakeubuntu_zero',
                      suite='trusty',
                      section='fakesection',
                      chroot='/tmp/imgdir'),
            mock.call(name='mos',
                      uri='http://fakemos',
                      suite='mosX.Y',
                      section='fakesection',
                      chroot='/tmp/imgdir')
        ], mock_bu.add_apt_source.call_args_list)

        # we don't call add_apt_preference for ubuntu_zero
        # because it has priority == None
        self.assertEqual([
            mock.call(name='ubuntu',
                      priority=900,
                      suite='trusty',
                      section='fakesection',
                      chroot='/tmp/imgdir',
                      uri='http://fakeubuntu'),
            mock.call(name='mos',
                      priority=1000,
                      suite='mosX.Y',
                      section='fakesection',
                      chroot='/tmp/imgdir',
                      uri='http://fakemos')
        ], mock_bu.add_apt_preference.call_args_list)
        mock_utils.makedirs_if_not_exists.assert_called_once_with(
            '/tmp/imgdir/proc')
        self.assertEqual([
            mock.call('tune2fs', '-O', '^has_journal', '/dev/loop0'),
            mock.call('tune2fs', '-O', 'has_journal', '/dev/loop0')
        ], mock_utils.execute.call_args_list)
        mock_fu.mount_bind.assert_called_once_with('/tmp/imgdir', '/proc')
        mock_bu.run_apt_get.assert_called_once_with(
            '/tmp/imgdir', packages=['fakepackage1', 'fakepackage2'])
        mock_bu.do_post_inst.assert_called_once_with('/tmp/imgdir')
        signal_calls = mock_bu.stop_chrooted_processes.call_args_list
        self.assertEqual(
            2 * [
                mock.call('/tmp/imgdir', signal=signal.SIGTERM),
                mock.call('/tmp/imgdir', signal=signal.SIGKILL)
            ], signal_calls)
        self.assertEqual([mock.call('/tmp/imgdir/proc')] * 2,
                         mock_fu.umount_fs.call_args_list)
        self.assertEqual(
            [mock.call('/tmp/imgdir', try_lazy_umount=False, pseudo=False)] *
            2, mock_umount_target.call_args_list)
        self.assertEqual([mock.call('/dev/loop0'),
                          mock.call('/dev/loop1')] * 2,
                         mock_bu.deattach_loop.call_args_list)
        self.assertEqual([mock.call('/tmp/img'),
                          mock.call('/tmp/img-boot')],
                         mock_bu.shrink_sparse_file.call_args_list)
        self.assertEqual([
            mock.call('/tmp/img'),
            mock.call('/fake/img.img.gz'),
            mock.call('/tmp/img-boot'),
            mock.call('/fake/img-boot.img.gz')
        ], mock_os.path.getsize.call_args_list)
        self.assertEqual([
            mock.call('/tmp/img', 20),
            mock.call('/fake/img.img.gz', 2),
            mock.call('/tmp/img-boot', 10),
            mock.call('/fake/img-boot.img.gz', 1)
        ], mock_utils.calculate_md5.call_args_list)
        self.assertEqual([
            mock.call('/tmp/img', 'gzip'),
            mock.call('/tmp/img-boot', 'gzip')
        ], mock_bu.containerize.call_args_list)
        mock_open.assert_called_once_with('/fake/img.yaml', 'w')
        self.assertEqual([
            mock.call('/tmp/img.gz', '/fake/img.img.gz'),
            mock.call('/tmp/img-boot.gz', '/fake/img-boot.img.gz')
        ], mock_shutil_move.call_args_list)

        metadata = {}
        for repo in self.mgr.driver.operating_system.repos:
            metadata.setdefault('repos', []).append({
                'type': 'deb',
                'name': repo.name,
                'uri': repo.uri,
                'suite': repo.suite,
                'section': repo.section,
                'priority': repo.priority,
                'meta': repo.meta
            })
        metadata['packages'] = self.mgr.driver.operating_system.packages
        metadata['images'] = [{
            'raw_md5':
            md5_side[0],
            'raw_size':
            getsize_side[0],
            'raw_name':
            None,
            'container_name':
            os.path.basename(self.mgr.driver.image_scheme.images[0].uri.split(
                'file://', 1)[1]),
            'container_md5':
            md5_side[1],
            'container_size':
            getsize_side[1],
            'container':
            self.mgr.driver.image_scheme.images[0].container,
            'format':
            self.mgr.driver.image_scheme.images[0].format
        }, {
            'raw_md5':
            md5_side[2],
            'raw_size':
            getsize_side[2],
            'raw_name':
            None,
            'container_name':
            os.path.basename(self.mgr.driver.image_scheme.images[1].uri.split(
                'file://', 1)[1]),
            'container_md5':
            md5_side[3],
            'container_size':
            getsize_side[3],
            'container':
            self.mgr.driver.image_scheme.images[1].container,
            'format':
            self.mgr.driver.image_scheme.images[1].format
        }]
        mock_yaml_dump.assert_called_once_with(metadata, stream=mock_open())