Пример #1
0
    def test_search_returns_matching_results(self):
        with unittest.mock.patch('libertine.service.apt.apt.Cache') as MockCache:
            MockCache.return_value = {
                "vim": build_mock_app("vim", "vi improved", "vim.com", "who even uses raw vi"),
                "gedit": build_mock_app("gedit", "text editor", "gedit.com", "visual text editor"),
                "gimp": build_mock_app("gimp", "foss photoshop", "gimp.com", "edit bitmap images"),
                "vim-common": build_mock_app("vim-common", "common vim stuff", "vim.common", "dependencies")
            }

            results = apt.AptCache('palpatine').search('vim')

            self.assertEqual(len(results), 2)
            results = sorted(results, key=lambda xx: xx['id'])
            self.assertEqual(results[0]['description'], 'who even uses raw vi')
            self.assertEqual(results[0]['name'], 'vim')
            self.assertEqual(results[0]['id'], 'vim')
            self.assertEqual(results[0]['summary'], 'vi improved')
            self.assertEqual(results[0]['website'], 'vim.com')
            self.assertEqual(results[0]['package'], 'vim')

            self.assertEqual(results[1]['description'], 'dependencies')
            self.assertEqual(results[1]['name'], 'vim-common')
            self.assertEqual(results[1]['id'], 'vim-common')
            self.assertEqual(results[1]['summary'], 'common vim stuff')
            self.assertEqual(results[1]['website'], 'vim.common')
            self.assertEqual(results[1]['package'], 'vim-common')

            MockCache.assert_called_once_with(rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #2
0
    def test_success_sends_data(self):
        self.called_with = None

        def callback(t):
            self.called_with = t

        self.monitor.done.return_value = False

        self.config.get_container_install_status.return_value = 'ready'
        self.config.get_container_name.return_value = 'Palpatine'
        task = tasks.ContainerInfoTask('palpatine', [1, 2, 3], self.config,
                                       self.monitor, callback)
        task._instant_callback = True
        task.start().join()

        self.monitor.data.assert_called_once_with(
            self.monitor.new_operation.return_value, unittest.mock.ANY)
        args, kwargs = self.monitor.data.call_args
        self.assertEqual(
            {
                'id': 'palpatine',
                'status': 'ready',
                'task_ids': [1, 2, 3],
                'name': 'Palpatine',
                'root': utils.get_libertine_container_rootfs_path('palpatine'),
                'home': utils.get_libertine_container_home_dir('palpatine')
            }, ast.literal_eval(args[1]))

        self.monitor.finished.assert_called_once_with(
            self.monitor.new_operation.return_value)

        self.assertEqual(task, self.called_with)
    def test_container_management(self):
        try:
            self.assertEqual([], ast.literal_eval(self._send(lambda: self._libertined.list())))
            self._send(lambda: self._libertined.create('rey', 'Rey', 'xenial', 'mock'))
            self.assertEqual(['rey'], ast.literal_eval(self._send(lambda: self._libertined.list())))

            self._send(lambda: self._libertined.create('kylo', 'Kylo Ren', 'xenial', 'mock'))
            self.assertEqual(['rey', 'kylo'], ast.literal_eval(self._send(lambda: self._libertined.list())))

            self._send(lambda: self._libertined.update('kylo'))

            self.assertEqual({'id': 'rey',
                              'status': 'ready',
                              'name': 'Rey',
                              'task_ids': [],
                              'root': utils.get_libertine_container_rootfs_path('rey'),
                              'home': '{}/libertine-container/user-data/rey'.format(TestLibertineService._tempdir.name)
                             }, ast.literal_eval(self._send(lambda: self._libertined.container_info('rey'))))

            self._send(lambda: self._libertined.destroy('kylo'))
            self.assertEqual(['rey'], ast.literal_eval(self._send(lambda: self._libertined.list())))
        except AssertionError as e:
            raise
        except Exception as e:
            self.fail('Exception thrown in test: %s' % str(e))
Пример #4
0
 def test_app_info_returns_empty_dict_when_no_such_app_exists(self):
     with unittest.mock.patch(
             'libertine.service.apt.apt.Cache') as MockCache:
         MockCache.return_value = {}
         self.assertEqual(apt.AptCache('palpatine').app_info("vim"), {})
         MockCache.assert_called_once_with(
             rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
    def test_success_sends_data(self):
        self.called_with = None
        def callback(t):
            self.called_with = t

        self.monitor.done.return_value = False

        self.config.get_container_install_status.return_value = 'ready'
        self.config.get_container_name.return_value = 'Palpatine'
        task = tasks.ContainerInfoTask('palpatine', [1, 2, 3], self.config, self.monitor, callback)
        task._instant_callback = True
        task.start().join()

        self.monitor.data.assert_called_once_with(self.monitor.new_operation.return_value, unittest.mock.ANY)
        args, kwargs = self.monitor.data.call_args
        self.assertEqual({'id': 'palpatine',
                          'status': 'ready',
                          'task_ids': [1, 2, 3],
                          'name': 'Palpatine',
                          'root': utils.get_libertine_container_rootfs_path('palpatine'),
                          'home': utils.get_libertine_container_home_dir('palpatine')},
                    ast.literal_eval(args[1]))

        self.monitor.finished.assert_called_once_with(self.monitor.new_operation.return_value)

        self.assertEqual(task, self.called_with)
Пример #6
0
    def _run(self):
        utils.get_logger().debug("Gathering info for container '{}'".format(self._container))
        container = {'id': str(self._container), 'task_ids': self._tasks}

        container['status'] = self._config.get_container_install_status(self._container) or ''
        container['name'] = self._config.get_container_name(self._container) or ''

        container_type = self._config.get_container_type(self._container)
        container['root'] = utils.get_libertine_container_rootfs_path(self._container)
        container['home'] = utils.get_libertine_container_home_dir(self._container)

        self._data(json.dumps(container))
Пример #7
0
    def test_loads_cache_only_once(self):
        with unittest.mock.patch('libertine.service.apt.apt.Cache') as MockCache:
            MockCache.return_value = {
                "vim": build_mock_app("vim", "vi improved", "vim.com", "who even uses raw vi"),
                "gimp": build_mock_app("gimp", "foss photoshop", "gimp.com", "visual text editor"),
            }

            cache = apt.AptCache('palpatine')
            cache.app_info("vim")
            cache.app_info("vim")

            MockCache.assert_called_once_with(rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #8
0
    def _load(self):
        with self._lock:
            if self._cache is None:
                try:
                    utils.get_logger().debug(
                        "Trying aptcache for container %s" % self._container)
                    container_path = utils.get_libertine_container_rootfs_path(
                        self._container)
                    if not container_path or not path.exists(container_path):
                        raise PermissionError

                    self._cache = apt.Cache(rootdir=container_path)
                except PermissionError:
                    utils.get_logger().debug("Trying system aptcache")
                    self._cache = apt.Cache()
Пример #9
0
 def test_app_info_returns_values_for_app(self):
     with unittest.mock.patch('libertine.service.apt.apt.Cache') as MockCache:
         MockCache.return_value = {
             "vim": build_mock_app("vim", "vi improved", "vim.com", "who even uses raw vi"),
             "gimp": build_mock_app("gimp", "foss photoshop", "gimp.com", "visual text editor"),
         }
         self.assertEqual(apt.AptCache('palpatine').app_info("vim"), {
             'name': 'vim',
             'id': 'vim',
             'package': 'vim',
             'summary': 'vi improved',
             'description': 'who even uses raw vi',
             'website': 'vim.com'
         })
         MockCache.assert_called_once_with(rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #10
0
    def test_container_management(self):
        try:
            self.assertEqual([],
                             ast.literal_eval(
                                 self._send(lambda: self._libertined.list())))
            self._send(lambda: self._libertined.create('rey', 'Rey', 'xenial',
                                                       'mock'))
            self.assertEqual(['rey'],
                             ast.literal_eval(
                                 self._send(lambda: self._libertined.list())))

            self._send(lambda: self._libertined.create('kylo', 'Kylo Ren',
                                                       'xenial', 'mock'))
            self.assertEqual(['rey', 'kylo'],
                             ast.literal_eval(
                                 self._send(lambda: self._libertined.list())))

            self._send(lambda: self._libertined.update('kylo'))

            self.assertEqual(
                {
                    'id':
                    'rey',
                    'status':
                    'ready',
                    'name':
                    'Rey',
                    'task_ids': [],
                    'root':
                    utils.get_libertine_container_rootfs_path('rey'),
                    'home':
                    '{}/libertine-container/user-data/rey'.format(
                        TestLibertineService._tempdir.name)
                },
                ast.literal_eval(
                    self._send(
                        lambda: self._libertined.container_info('rey'))))

            self._send(lambda: self._libertined.destroy('kylo'))
            self.assertEqual(['rey'],
                             ast.literal_eval(
                                 self._send(lambda: self._libertined.list())))
        except AssertionError as e:
            raise
        except Exception as e:
            self.fail('Exception thrown in test: %s' % str(e))
Пример #11
0
    def test_loads_cache_only_once(self):
        with unittest.mock.patch(
                'libertine.service.apt.apt.Cache') as MockCache:
            MockCache.return_value = {
                "vim":
                build_mock_app("vim", "vi improved", "vim.com",
                               "who even uses raw vi"),
                "gimp":
                build_mock_app("gimp", "foss photoshop", "gimp.com",
                               "visual text editor"),
            }

            cache = apt.AptCache('palpatine')
            cache.app_info("vim")
            cache.app_info("vim")

            MockCache.assert_called_once_with(
                rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #12
0
 def test_app_info_returns_values_for_app(self):
     with unittest.mock.patch(
             'libertine.service.apt.apt.Cache') as MockCache:
         MockCache.return_value = {
             "vim":
             build_mock_app("vim", "vi improved", "vim.com",
                            "who even uses raw vi"),
             "gimp":
             build_mock_app("gimp", "foss photoshop", "gimp.com",
                            "visual text editor"),
         }
         self.assertEqual(
             apt.AptCache('palpatine').app_info("vim"), {
                 'name': 'vim',
                 'id': 'vim',
                 'package': 'vim',
                 'summary': 'vi improved',
                 'description': 'who even uses raw vi',
                 'website': 'vim.com'
             })
         MockCache.assert_called_once_with(
             rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #13
0
    def test_search_returns_matching_results(self):
        with unittest.mock.patch(
                'libertine.service.apt.apt.Cache') as MockCache:
            MockCache.return_value = {
                "vim":
                build_mock_app("vim", "vi improved", "vim.com",
                               "who even uses raw vi"),
                "gedit":
                build_mock_app("gedit", "text editor", "gedit.com",
                               "visual text editor"),
                "gimp":
                build_mock_app("gimp", "foss photoshop", "gimp.com",
                               "edit bitmap images"),
                "vim-common":
                build_mock_app("vim-common", "common vim stuff", "vim.common",
                               "dependencies")
            }

            results = apt.AptCache('palpatine').search('vim')

            self.assertEqual(len(results), 2)
            results = sorted(results, key=lambda xx: xx['id'])
            self.assertEqual(results[0]['description'], 'who even uses raw vi')
            self.assertEqual(results[0]['name'], 'vim')
            self.assertEqual(results[0]['id'], 'vim')
            self.assertEqual(results[0]['summary'], 'vi improved')
            self.assertEqual(results[0]['website'], 'vim.com')
            self.assertEqual(results[0]['package'], 'vim')

            self.assertEqual(results[1]['description'], 'dependencies')
            self.assertEqual(results[1]['name'], 'vim-common')
            self.assertEqual(results[1]['id'], 'vim-common')
            self.assertEqual(results[1]['summary'], 'common vim stuff')
            self.assertEqual(results[1]['website'], 'vim.common')
            self.assertEqual(results[1]['package'], 'vim-common')

            MockCache.assert_called_once_with(
                rootdir=utils.get_libertine_container_rootfs_path('palpatine'))
Пример #14
0
 def test_app_info_returns_empty_dict_when_no_such_app_exists(self):
     with unittest.mock.patch('libertine.service.apt.apt.Cache') as MockCache:
         MockCache.return_value = {}
         self.assertEqual(apt.AptCache('palpatine').app_info("vim"), {})
         MockCache.assert_called_once_with(rootdir=utils.get_libertine_container_rootfs_path('palpatine'))