def test_upload_plugins(self, _):
        with mock.patch('cloudify.manager.get_rest_client') as mock_client:
            plugin = mock.Mock()
            plugin.id = "CustomPlugin"
            self.cfy_mock_client.plugins.upload = mock.Mock(
                return_value=plugin)

            mock_client.return_value = self.cfy_mock_client
            with mock.patch(
                    'cloudify_types.component.component.get_local_path',
                    return_value='some_path') as get_local_path:
                with mock.patch('cloudify_types.component.component.zip_files',
                                return_value="_zip") as zip_files:
                    component = Component({
                        'plugins': {
                            'base_plugin': {
                                'wagon_path': '_wagon_path',
                                'plugin_yaml_path': '_plugin_yaml_path'
                            }
                        }
                    })
                    with mock.patch(
                            'cloudify_types.component.component.os')\
                            as os_mock:
                        component._upload_plugins()
                    zip_files.assert_called_with(["some_path", "some_path"])
                    get_local_path.assert_has_calls([
                        mock.call('_wagon_path', create_temp=True),
                        mock.call('_plugin_yaml_path', create_temp=True)
                    ])
                    os_mock.remove.assert_has_calls([
                        mock.call('some_path'),
                        mock.call('some_path'),
                        mock.call('_zip')
                    ])
    def test_upload_empty_plugins(self):
        get_local_path = mock.Mock(return_value="some_path")

        with mock.patch('cloudify.manager.get_rest_client'):
            zip_files = mock.Mock(return_value="_zip")
            with mock.patch('cloudify_types.component.component.zip_files',
                            zip_files):
                # empty plugins
                component = Component({'plugins': {}})
                component._upload_plugins()
                zip_files.assert_not_called()
                get_local_path.assert_not_called()
    def test_upload_empty_plugins(self):
        get_local_path = mock.Mock(return_value="some_path")

        with mock.patch('cloudify.manager.get_rest_client'):
            zip_files = mock.Mock(return_value="_zip")
            with mock.patch(
                'cloudify_types.component.component.zip_files',
                zip_files
            ):
                # empty plugins
                component = Component({'plugins': {}})
                component._upload_plugins()
                zip_files.assert_not_called()
                get_local_path.assert_not_called()
    def test_upload_plugins_with_wrong_format(self):
        with mock.patch('cloudify.manager.get_rest_client'):
            component = Component({'plugins': True})
            error = self.assertRaises(NonRecoverableError,
                                      component._upload_plugins)
            self.assertIn('Wrong type in plugins: True', error.message)

            component = Component({
                'plugins': {
                    'base_plugin': {
                        'wagon_path': '',
                        'plugin_yaml_path': ''
                    }
                }
            })
            error = self.assertRaises(NonRecoverableError,
                                      component._upload_plugins)
            self.assertIn(
                "You should provide both values wagon_path: '' "
                "and plugin_yaml_path: ''", error.message)
    def test_upload_plugins(self):
        get_local_path = mock.Mock(return_value="some_path")

        with mock.patch('cloudify.manager.get_rest_client') as mock_client:
            plugin = mock.Mock()
            plugin.id = "CustomPlugin"

            cfy_mock_client = MockCloudifyRestClient()
            cfy_mock_client.plugins.upload = mock.Mock(return_value=plugin)
            mock_client.return_value = cfy_mock_client
            with mock.patch(
                    'cloudify_types.component.component.get_local_path',
                    get_local_path):
                zip_files = mock.Mock(return_value="_zip")
                with mock.patch('cloudify_types.component.component.zip_files',
                                zip_files):
                    # dist of plugins
                    component = Component({
                        'plugins': {
                            'base_plugin': {
                                'wagon_path': '_wagon_path',
                                'plugin_yaml_path': '_plugin_yaml_path'
                            }
                        }
                    })
                    os_mock = mock.Mock()
                    with mock.patch('cloudify_types.component.component.os',
                                    os_mock):
                        component._upload_plugins()
                    zip_files.assert_called_with(["some_path", "some_path"])
                    get_local_path.assert_has_calls([
                        mock.call('_wagon_path', create_temp=True),
                        mock.call('_plugin_yaml_path', create_temp=True)
                    ])
                    os_mock.remove.assert_has_calls([
                        mock.call('some_path'),
                        mock.call('some_path'),
                        mock.call('_zip')
                    ])
 def test_upload_plugins_with_icon(self, *_mocks):
     plugin = mock.Mock()
     plugin.id = "CustomPlugin"
     component = Component({
         'plugins': {
             'base_plugin': {
                 'wagon_path': '_wagon_path',
                 'plugin_yaml_path': '_plugin_yaml_path',
                 'icon_png_path': '_icon_png_path',
             }
         }
     })
     get_local_path = mock.patch(
         'cloudify_types.component.component.get_local_path',
         side_effect=lambda filename, create_temp=True: filename)
     zip_files = mock.patch('cloudify_types.component.component.zip_files',
                            return_value='_zip')
     component_os = mock.patch('cloudify_types.component.component.os')
     with get_local_path as local_path_mock, \
             zip_files as zip_mock, \
             component_os as os_mock:
         component._upload_plugins()
     zip_mock.assert_called_with(
         ['_wagon_path', '_plugin_yaml_path', '_icon_png_path'])
     local_path_mock.assert_has_calls([
         mock.call('_wagon_path', create_temp=True),
         mock.call('_plugin_yaml_path', create_temp=True),
         mock.call('_icon_png_path', create_temp=True),
     ],
                                      any_order=True)
     os_mock.remove.assert_has_calls([
         mock.call('_wagon_path'),
         mock.call('_plugin_yaml_path'),
         mock.call('_icon_png_path'),
         mock.call('_zip'),
     ],
                                     any_order=True)
    def test_upload_plugins(self):
        get_local_path = mock.Mock(return_value="some_path")

        with mock.patch('cloudify.manager.get_rest_client') as mock_client:
            plugin = mock.Mock()
            plugin.id = "CustomPlugin"
            self.cfy_mock_client.plugins.upload = mock.Mock(
                return_value=plugin)

            mock_client.return_value = self.cfy_mock_client
            with mock.patch(
                'cloudify_types.component.component.get_local_path',
                get_local_path
            ):
                zip_files = mock.Mock(return_value="_zip")
                with mock.patch(
                    'cloudify_types.component.component.zip_files',
                    zip_files
                ):
                    # dist of plugins
                    component = Component({'plugins': {
                        'base_plugin': {
                            'wagon_path': '_wagon_path',
                            'plugin_yaml_path': '_plugin_yaml_path'}}})
                    os_mock = mock.Mock()
                    with mock.patch('cloudify_types.component.component.os',
                                    os_mock):
                        component._upload_plugins()
                    zip_files.assert_called_with(["some_path", "some_path"])
                    get_local_path.assert_has_calls([
                        mock.call('_wagon_path', create_temp=True),
                        mock.call('_plugin_yaml_path', create_temp=True)])
                    os_mock.remove.assert_has_calls([
                        mock.call('some_path'),
                        mock.call('some_path'),
                        mock.call('_zip')])