Ejemplo n.º 1
0
    def test_install_plugin(self):
        with mock.patch('compose.plugin_manager.request.urlretrieve') as mock_urlretrieve, \
                mock.patch('compose.plugin_manager.os.path.isfile') as mock_isfile, \
                mock.patch('compose.plugin_manager.os.path.realpath') as mock_realpath, \
                mock.patch('compose.plugin_manager.os.path.isdir') as mock_isdir, \
                mock.patch('compose.plugin_manager.os.makedirs') as mock_makedirs, \
                mock.patch('compose.plugin_manager.zipfile.is_zipfile') as mock_is_zipfile, \
                mock.patch('compose.plugin_manager.tarfile.is_tarfile') as mock_is_tarfile, \
                mock.patch('compose.plugin_manager.zipfile.ZipFile') as mock_zipfile, \
                mock.patch('imp.load_source') as mock_load_source, \
                mock.patch('shutil.rmtree') as mock_rmtree, \
                mock.patch('os.mkdir') as mock_mkdir, \
                mock.patch('os.walk') as mock_walk:
            mock_walk.return_value = []
            mock_urlretrieve.side_effect = ValueError()
            mock_isfile.return_value = True
            mock_mkdir.return_value = True
            mock_realpath.return_value = '/real/path/to/plugin/plugin_name'

            mock_isdir.return_value = False

            mock_is_tarfile.return_value = False
            mock_is_zipfile.return_value = True
            mock_zipfile.return_value = self._get_archive_mock([
                'root_dir/',
                'root_dir/plugin.json'
            ])
            mock_load_source.return_value = None
            self._get_helper_class({
                'plugin': Plugin
            })

            plugin_manager = PluginManager('plugin_dir')

            with self.assertRaises(InvalidPluginError) as e:
                plugin_manager.install_plugin('plugin')
                self.assertTrue(mock_makedirs.called)
                self.assertTrue(mock_rmtree.called)

            self.assertEqual(
                str(e.exception),
                "Plugin 'root_dir' is not a plugin. Missing plugin attribute."
            )

            plugin_copy = Plugin
            plugin_copy.__init__ = lambda a, b, c: None
            plugin_copy.install = lambda a: False

            mock_load_source.return_value = self._get_helper_class({
                'plugin': plugin_copy
            })

            self.assertEquals(plugin_manager.install_plugin('plugin'), False)

            plugin_copy.install = lambda a: True

            mock_isdir.return_value = True
            mock_load_source.return_value = self._get_helper_class({
                'plugin': plugin_copy
            })

            self.assertEquals(plugin_manager.install_plugin('plugin'), True)