Exemplo n.º 1
0
    def test_constructor2(self):
        '''!
        Test when we call the constructor function with incomplete or wrong parameters
        '''
        url = URL('http://localhost://2000/foo.txt')
        assert (url != None)
        assert (url.getSource() == 'http://localhost://2000/foo.txt')

        dl = [('foo', 'http://server:8080/file.txt')]
        d = dict(dl)
        with pytest.raises(TypeError):
            url = URL(d)

        dl = [('source', 'http://server:8080/file.txt')]
        d = dict(dl)
        url = URL(d)
        assert (url != None)

        dl = [('foo', 'http://server:8080/file.txt')]
        d = dict(dl)
        with pytest.raises(TypeError):
            url = URL(d)

        dl = [('source', 123)]
        d = dict(dl)
        with pytest.raises(TypeError):
            url = URL(d)

        url = URL('http://localhost://2000/foo.txt', 123)
        assert (url != None)

        with pytest.raises(TypeError):
            url = URL(None)

        dl = [('source', 'http://server:8080/file.txt'), ('destination', 123)]
        d = dict(dl)
        with pytest.raises(TypeError):
            url = URL(d)

        dl = [('source', 'http://server:8080/file.txt'),
              ('destination', 'abc')]
        d = dict(dl)
        url = URL(d)
        assert (url != None)

        dl = [('source', 'http://server:8080/file.txt')]
        d = dict(dl)
        url = URL(d, 'abc')
        assert (url != None)

        dl = [('source', 'http://server:8080/file.txt')]
        d = dict(dl)
        with pytest.raises(TypeError):
            url = URL(d, 123)
Exemplo n.º 2
0
    def plugin(self, section_name):
        '''!
         Resolve the plugin used to process a configuration section. If the plugin is specified
         as a url object, the plugin is downloaded.

         @param section_name (str) Configuration section name whose plugin needs to be resolved.

         @return
              If plugin is resolved using configuration section data: \n
                Expanded file path to plugin file used to process configuration section. \n
              If plugin is not found or error encountered: \n
                None
        '''

        if isString(section_name) is False:
            raise TypeError('Invalid argument used as section name')
        elif self.ztpDict.get(section_name) is None:
            logger.error('Configuration Section %s not found.' % section_name)
            return None

        plugin_data = self.ztpDict.get(section_name).get('plugin')
        name = None
        if plugin_data is not None and isinstance(plugin_data, dict):
            logger.debug(
                'User defined plugin detected for configuration section %s.' %
                section_name)
            plugin_file = getCfg(
                'ztp-tmp-persistent') + '/' + section_name + '/' + 'plugin'
            try:
                # Re-use the plugin if already present
                if os.path.isfile(plugin_file) is True:
                    return plugin_file

                if plugin_data.get('dynamic-url'):
                    dyn_url_data = plugin_data.get('dynamic-url')
                    if isinstance(dyn_url_data, dict) and dyn_url_data.get(
                            'destination') is not None:
                        objDynUrl = DynamicURL(dyn_url_data)
                    else:
                        objDynUrl = DynamicURL(dyn_url_data, plugin_file)
                    rc, plugin_file = objDynUrl.download()
                    return plugin_file
                elif plugin_data.get('url'):
                    url_data = plugin_data.get('url')
                    if isinstance(
                            url_data,
                            dict) and url_data.get('destination') is not None:
                        objUrl = URL(url_data)
                    else:
                        objUrl = URL(url_data, plugin_file)
                    updateActivity(
                        'Downloading plugin \'%s\' for configuration section %s'
                        % (objUrl.getSource(), section_name))
                    rc, plugin_file = objUrl.download()
                    if rc != 0:
                        logger.error(
                            'Failed to download plugin \'%s\' for configuration section %s.'
                            % (objUrl.getSource(), section_name))
                    return plugin_file
                elif plugin_data.get('name') is not None:
                    name = plugin_data.get('name')
            except (TypeError, ValueError, OSError, IOError) as e:
                logger.error(
                    'Exception [%s] encountered while determining plugin for configuration section %s.'
                    % (str(e), section_name))
                return None
        elif plugin_data is not None and isString(plugin_data):
            name = plugin_data
        elif plugin_data is not None:
            logger.error(
                'Invalid plugin data type used for configuration section %s.' %
                section_name)
            return None

        # plugin name is not provided in section data, use section name as plugin name
        if name is None:
            res = re.split("^[0-9]+-", section_name, maxsplit=1)
            if len(res) > 1:
                name = res[1]
            else:
                name = res[0]
        logger.debug(
            'ZTP provided plugin %s is being used for configuration section %s.'
            % (name, section_name))
        if os.path.isfile(getCfg('plugins-dir') + '/' + name) is True:
            return getCfg('plugins-dir') + '/' + name
        return None