示例#1
0
    def _expand_repeat_tag(self, xml):
        """
        @param:
            user: the twister authenticated user
            xml: the project/last_edited xml in etree format. When going into recursion, xml becomes a suite.
        @summary:
            Suites can have a repeat tag. It duplicates the suite as many times as the repeat tag says.
        """
        for suite in xml.findall('TestSuite'):
            suite_name = suite.find('tsName').text
            repeat = None
            try:
                repeat = suite.find('Repeat')
                nb_repeat = int(repeat.text)
                logDebug("CeParser: Will repeat suite `{}`, {} times.".format(
                    suite_name, nb_repeat))
            except:
                nb_repeat = 1

            # before copying the suite for multiplication, remove the Repeat
            # tag because it MUST NOT be present in generated xml file
            if repeat is not None:
                suite.remove(repeat)

            index = xml.index(suite)
            for i in range(nb_repeat - 1):
                deep_copy = copy.deepcopy(suite)
                suite_st = etree.tostring(deep_copy)
                suite_copy = etree.fromstring(suite_st)
                xml.insert(index + 1, suite_copy)
                self._expand_repeat_tag_on_tc(suite_copy)
                self._expand_repeat_tag(suite_copy)
            self._expand_repeat_tag_on_tc(suite)
            self._expand_repeat_tag(suite)
示例#2
0
文件: CeFs.py 项目: varzan/Twister
    def _kill(self, user):
        """
        Kill all services for a user.
        """
        p_ps = local['ps']
        grep = local['grep']

        try:
            pids = (p_ps['aux'] | grep['/server/UserService.py']
                    | grep['^' + user] | grep[self.name])()
        except Exception:
            return

        # Kill all leftover processes
        for line in pids.strip().splitlines():
            std_li = line.strip().decode('utf').split()
            p_pid = int(std_li[1])
            del std_li[2:5]
            if '/bin/sh' in std_li:
                continue
            if '/bin/grep' in std_li:
                continue
            logDebug('User {}: Killing ugly zombie `{}`.'.format(
                user, ' '.join(std_li)))
            try:
                os.kill(p_pid, 9)
            except:
                pass
示例#3
0
    def save_release_reserved_tb(self, res_query, props={}):
        """
        Save the changes. Sync self.resources with self.reserved_resources
        and save to the disk
        """
        logDebug('CeTestBeds:save_release_reserved_tb {} {}'.format(res_query, props))

        # save changes
        result = self.save_reserved_tb(res_query, props)

        if result and not result.startswith("*ERROR*"):
            user_info = self.user_info(props)

            if ':' in res_query:
                res_query = res_query.split(':')[0]

            # get only the component
            resource_node = self.get_resource(res_query)
            if not resource_node or isinstance(resource_node, str):
                logFull("Can not find the resoruce {}".format(res_query))
                return None

            # get the entire TB
            if len(resource_node['path']) > 1:
                resource_node = self.get_path(resource_node['path'][0], self.resources)

            # delete this entry from reservedResources
            reserved_node = self.reservedResources[user_info[0]][resource_node['id']]
            self.reservedResources[user_info[0]].pop(reserved_node['id'])
            if not self.reservedResources[user_info[0]]:
                self.reservedResources.pop(user_info[0])
        else:
            return result

        return True
示例#4
0
    def del_binding(self, fpath):
        """
        Delete a binding between a CFG and a SUT.
        Return True/ False.
        """
        logFull('xmlparser:del_binding')
        cfg_file = '{}/twister/config/bindings.xml'.format(userHome(self.user))

        if not os.path.isfile(cfg_file):
            err = '*ERROR* Bindings Config file `{}`, for user `{}` does not exist!'.format(cfg_file, self.user)
            logError(err)
            return err

        bind_xml = parseXML(self.user, cfg_file)
        if bind_xml is None:
            err = '*ERROR* Config file `{}`, for user `{}` cannot be parsed!'.format(cfg_file, self.user)
            return err
        # Find the binding
        found = bind_xml.xpath('/root/binding/name[text()="{}"]/..'.format(fpath))

        # If found, delete it
        if found:
            bind_xml.remove(found[0])
            logDebug('Removed binding `{}`, for user `{}`.'.format(fpath, self.user))
        else:
            err = '*WARN* Invalid binding `{}`, user `{}`! Cannot unbind!'.format(fpath, self.user)
            # logDebug(err)
            return False

        return dumpXML(self.user, cfg_file, bind_xml)
示例#5
0
    def _expand_repeat_tag_on_tc(self, suite):
        """
        Expand the test cases by the repeat tag.
        """
        for tc in suite.findall('TestCase'):
            tcName = tc.find('tcName').text
            repeat = None
            try:
                repeat = tc.find('Repeat')
                nb_repeat = int(repeat.text)
                logDebug("CeParser: Will repeat suite `{}`, {} times.".format(
                    tcName, nb_repeat))
            except:
                nb_repeat = 1

            # before copying the suite for multiplication, remove the Repeat
            # tag because it MUST NOT be present in generated xml file
            if repeat is not None:
                tc.remove(repeat)

            index = suite.index(tc)
            for i in range(nb_repeat - 1):
                deep_copy = copy.deepcopy(tc)
                tc_st = etree.tostring(deep_copy)
                tc_copy = etree.fromstring(tc_st)
                suite.insert(index + 1, tc_copy)
示例#6
0
    def getBindingsConfig(self):
        """
        Parse the bindings file that connects Roots from a config file, with SUTs.
        """
        logFull('xmlparser:getBindingsConfig')
        cfg_file = '{}/twister/config/bindings.xml'.format(userHome(self.user))
        bindings = {}

        if not os.path.isfile(cfg_file):
            err = '*ERROR* Bindings Config file `{}`, for user `{}` does not exist!'.format(cfg_file, self.user)
            logError(err)
            return err

        bind_xml = parseXML(self.user, cfg_file)
        if bind_xml is None:
            err = '*ERROR* Config file `{}`, for user `{}` cannot be parsed!'.format(cfg_file, self.user)
            return err

        for binding in bind_xml.xpath('/root/binding'):
            name = binding.find('name')
            # Valid names ?
            if name is None:
                continue
            name = name.text.strip()
            if not name:
                continue
            bindings[name] = {}
            # All binds cfg -> sut
            for bind in binding.findall('bind'):
                cfg = bind.get('config')
                sut = bind.get('sut')
                bindings[name][cfg] = sut

        logDebug('Found `{}` bindings for user `{}`.'.format(len(bindings), self.user))
        return bindings
示例#7
0
    def get_tb(self, query, props={}):
        """
        Get the current version of the tb modified and unsaved or
        the version from the disk.
        """
        logDebug('CeTestBeds:get_tb {} {}'.format(query, props))
        user_info = self.user_info(props)

        result = None
        # If the resource is reserved, get the latest unsaved changes
        if user_info[0] in self.reservedResources:
            for i in range(len(self.reservedResources[user_info[0]].values())):
                result = self.get_resource(query, self.reservedResources[user_info[0]].values()[i])
                if isinstance(result, dict):
                    break
        # Or get it from the disk
        if not isinstance(result, dict):
            result = self.get_resource(query)

        if isinstance(result, dict):
            if ':' in query:
                meta = query.split(':')[1]
                ret = result['meta'].get(meta, '')
                return ret
            else:
                return self.format_resource(result, query)

        return "*ERROR* no such resource: {}".format(query)
示例#8
0
文件: CeWebUi.py 项目: varzan/Twister
    def json_save_project(self, user, epname):
        """ save project """
        logFull('CeWebUi:json_save_project user `{}`.'.format(user))
        if self.user_agent() == 'x':
            return 0

        c_l = cherrypy.request.headers['Content-Length']
        raw_data = cherrypy.request.body.read(int(c_l))
        json_data = json.loads(raw_data)
        del c_l, raw_data

        # Delete everything from XML Root
        self.project.del_settings_key(user, 'project', '//TestSuite')
        changes = 'Reset project file...\n'

        for suite_data in json_data:
            self.project.set_persistent_suite(user, suite_data['data'],
                                              {'ep': decode(epname)})
            changes += 'Created suite: {0}.\n'.format(suite_data['data'])
            for file_data in suite_data.get('children', []):
                changes += 'Created file: {0}.\n'.format(file_data['data'])
                self.project.set_persistent_file(user, suite_data['data'],
                                                 file_data['data'], {})

        changes += '>.<\n'
        logDebug(changes)
        return 'true'
示例#9
0
    def format_resource(self, result, query):
        '''
        We have to return data in a certain form - a formated dictionary.
        '''

        logDebug('CeCommonAllocator: format_resource query = {}'.format(query))

        meta = ""
        if ':' in query:
            meta = query.split(':')[1]
        if not meta:
            result['children'] = sorted([
                result['children'][node]['id']
                for node in result.get('children') or []
            ],
                                        key=lambda node: node.lower())

            result['path'] = '/'.join(result.get('path', []))

            if result['meta'] == '{}':
                result['meta'] = dict()
        else:
            result = result['meta'].get(meta, '')

        return result
示例#10
0
    def service_stop(self, service):
        """ stop service """
        logFull('CeServices:service_stop')

        rc = self.service_status(service)
        if not rc:
            logDebug('SM: Service name `{}` is not running.'.format(service['name']))
            return False

        tprocess = service.get('pid', 0)

        if isinstance(tprocess, int):
            logError('SM: Cannot stop service `{}`!'.format(service['name']))

        try:
            tprocess.terminate()
        except Exception as exp_err:
            logError('SM: Cannot stop service: `{}`, exception `{}`!'.format(service['name'], exp_err))
            return False

        try:
            time.sleep(0.1)
            os.killpg(tprocess.pid, signal.SIGTERM)
            time.sleep(0.1)
            tprocess.kill()
        except:
            pass

        logWarning('SM: Stopped service: `{}`.'.format(service['name']))
        return True
示例#11
0
    def __del__(self):

        logDebug('SM: Stopping Service Manager...')

        for service in self.twister_services:
            if self.service_status(service) == -1:
                self.service_stop(service)

        del self.twister_services
示例#12
0
    def get_reserved_resource(self, res_query, props={}):
        '''
        Returns the reserved resource.
        '''

        logDebug(
            'CeCommonAllocator:get_reserved_resource: res_query = {}'.format(
                res_query))

        resources = self.resources
        # If no resources...
        if not resources:
            msg = 'CeCommonAllocator:get_reserved_resource: There are no resources defined !'
            logError(msg)
            return False

        user_info = self.user_info(props)

        if not self.reservedResources.get(user_info[0]):
            msg = 'CeCommonAllocator: Resource `{}` is not reserved !'.format(
                res_query)
            logError(msg)
            return False

        # get only the root parent in case the path query contains components unsaved yet
        parts = [q for q in res_query.split('/') if q]
        if "/" in res_query:
            parent_path = parts
            node_path = self.get_resource("/" + parts[0])
        # if query is an saved component
        else:
            node_path = self.get_resource(res_query)
            if node_path and isinstance(node_path, dict):
                parent_path = node_path['path']

        #maybe query is an id of a component unsaved yet
        if not node_path:
            for p in self.reservedResources[user_info[0]]:
                node_path = self.get_resource(
                    res_query, self.reservedResources[user_info[0]][p])
                if node_path:
                    self.reservedResources[
                        user_info[0]][p]['path'] = node_path['path']
                    return self.reservedResources[user_info[0]][p]
            msg = 'CeCommonAllocator: Cannot find resource ID in reservedResources`{}` !'.format(
                res_query)
            logError(msg)
            return False

        if len(node_path['path']) > 1:
            node_path = self.get_path(node_path['path'][0], resources)

        self.reservedResources[user_info[0]][
            node_path['id']]['path'] = parent_path

        return self.reservedResources[user_info[0]][node_path['id']]
示例#13
0
    def _expand_global_configs(self, user, xml, config_list=[]):
        """
        @param:
            user: the twister authenticated user
            xml: the project/last_edited xml in etree format. When going into recursion, xml becomes a suite.
        @summary:
            Suites now support configs, seen as global configs for all test in a suite.
            It duplicates the suite as many times as the iterator says.
            The value from the top suite propagates to the subsuites.
            A property tag is added to store the iteration value.
            Between tc iterators and suite iterators, the last ones have priority.
        """
        for suite in xml.findall('TestSuite'):
            remove = True
            ts_name = suite.find('tsName').text
            cfg_prop = suite.find('ConfigFiles')
            config_info = cfg_prop.findall('Config')
            cartesian_list = self._get_cartesian_list(user, config_info)
            logDebug("CeParser: Will iterate test suite `{}`, {} times, from values: {}, user `{}`."\
                     .format(ts_name, len(cartesian_list), cartesian_list, user))

            inherited_val = ''
            parent_property = xml.findall('Property')
            if len(parent_property) > 0:
                parent_property = parent_property[0]
                inherited_val = parent_property.find('propValue').text
            index = xml.index(suite)
            # duplicate suites
            for item in reversed(cartesian_list):
                deep_copy = copy.deepcopy(suite)
                suite_st = etree.tostring(deep_copy)
                newSuite = etree.fromstring(suite_st)
                xml.insert(index + 1, newSuite)
                if isinstance(item, tuple):
                    value = ', '.join(str(x) for x in item)
                else:
                    value = str(item)
                self._add_property(newSuite, value, inherited_val)
                self._expand_global_configs(
                    user, newSuite, config_list + copy.deepcopy(config_info))
                self._expand_tc_configs(
                    user, newSuite, config_list + copy.deepcopy(config_info))
            if len(cartesian_list) == 0:
                remove = False
                if inherited_val:
                    self._add_property(suite, inherited_val)
                self._expand_global_configs(
                    user, suite, config_list + copy.deepcopy(config_info))
                self._expand_tc_configs(
                    user, suite, config_list + copy.deepcopy(config_info))
            # if the suite has iterators, remove the one that is being duplicated
            if remove:
                xml.remove(suite)
示例#14
0
    def get_reserved_resource(self, res_query, props={}):
        """
        Returns the reserved resource.
        """
        logDebug('Get reserved resource `{}`...'.format(res_query))

        resources = self.resources
        # If no resources...
        if not resources:
            msg = 'Get reserved resourcee: There are no resources defined !'
            logError(msg)
            return False

        user_info = self.user_info(props)
        user = user_info[0]

        if not self.reservedResources.get(user):
            msg = 'Get reserved resource: Resource `{}` is not reserved !'.format(
                res_query)
            logError(msg)
            return False

        # Maybe query is an ID of a component unsaved yet
        for p in self.reservedResources[user]:
            unsaved_res = self.get_resource(res_query,
                                            self.reservedResources[user][p])
            if unsaved_res:
                self.reservedResources[user][p]['path'] = unsaved_res['path']
                return self.reservedResources[user][p]

        # Get only the root parent in case the path query contains components unsaved yet
        parts = [q for q in res_query.split('/') if q]
        if '/' in res_query:
            parent_path = parts
            node_path = self.get_resource('/' + parts[0])
        # If query is an saved component
        else:
            node_path = self.get_resource(res_query)
            if node_path and isinstance(node_path, dict):
                parent_path = node_path['path']

        if not node_path:
            msg = 'Get reserved resource: Cannot find resource ID `{}` !'.format(
                res_query)
            logError(msg)
            return False

        if isinstance(node_path['path'], list) and len(node_path['path']) > 1:
            node_path = self.get_path(node_path['path'][0], resources)

        self.reservedResources[user][node_path['id']]['path'] = parent_path

        return self.reservedResources[user][node_path['id']]
示例#15
0
    def _expand_tc_configs(self, user, suite, config_list):
        """
        @param: 
            user: the authenticated twister user
            suite: the suite that contains the tc to duplicate
        @summary:
            Explodes the iterators and duplicates the test cases accordingly
            It is called after a suite is expanded, in _expand_global_configs
        """
        for tc in suite.findall('TestCase'):
            remove = True
            tc_name = tc.find('tcName').text
            cfg_prop = tc.find('ConfigFiles')
            config_info = cfg_prop.findall('Config')
            cartesian_list = self._get_cartesian_list(user, config_info)
            cfg_names = []
            for elem in config_list:
                new_cfg_name = elem.get('name')
                cfg_names = [
                    cfg.get('name') for cfg in cfg_prop.findall('Config')
                ]
                if new_cfg_name not in cfg_names:
                    cfg_prop.insert(-1, copy.deepcopy(elem))

            logDebug("CeParser: Will iterate test case `{}`, {} times, from values: {}, user `{}`."\
                     .format(tc_name, len(cartesian_list), cartesian_list, user))

            inherited_val = ''
            parent_property = suite.findall('Property')
            if len(parent_property) > 0:
                parent_property = parent_property[0]
                inherited_val = parent_property.find('propValue').text
            index = suite.index(tc)
            # duplicate suites
            for item in reversed(cartesian_list):
                deep_copy = copy.deepcopy(tc)
                tc_st = etree.tostring(deep_copy)
                new_tc = etree.fromstring(tc_st)
                suite.insert(index + 1, new_tc)
                if isinstance(item, tuple):
                    value = ', '.join(str(x) for x in item)
                else:
                    value = str(item)
                self._add_property(new_tc, value, inherited_val)
            if len(cartesian_list) == 0:
                remove = False
                if inherited_val:
                    self._add_property(tc, inherited_val)
            # if the tc has iterators, remove the one that is being duplicated
            if remove:
                suite.remove(tc)
示例#16
0
    def generate_index(self):
        '''
        Generate index when creating a new sut or test bed.
        '''

        logDebug('CeCommonAllocator: generate_index')

        res_id = False
        while not res_id:
            res_id = hexlify(os.urandom(5))
            # If by any chance, this ID already exists, generate another one!
            if self.get_id(res_id, self.resources):
                res_id = False
        return res_id
示例#17
0
    def send_command(self, command, name='', *args, **kwargs):
        """ send command to service """
        logFull('CeServices:send_command')

        if command == SM_LIST or command == SM_COMMAND_MAP[SM_LIST]:
            return self.list_services()

        found = False

        service = None
        for service_item in self.twister_services:
            if name == service_item['name']:
                found = True
                service = service_item
                break

        if not found:
            logDebug('SM: Invalid service name: `{}`!'.format(name))
            return False

        elif command == SM_STATUS or command == SM_COMMAND_MAP[SM_STATUS]:
            return self.service_status(service)

        elif command == SM_DESCRIP or command == SM_COMMAND_MAP[SM_DESCRIP]:
            return service.get('descrip')

        if command == SM_START or command == SM_COMMAND_MAP[SM_START]:
            return self.service_start(service)

        elif command == SM_STOP or command == SM_COMMAND_MAP[SM_STOP]:
            return self.service_stop(service)

        elif command == SM_GET_CONFIG or command == SM_COMMAND_MAP[SM_GET_CONFIG]:
            return self.read_config(service)

        elif command == SM_SET_CONFIG or command == SM_COMMAND_MAP[SM_SET_CONFIG]:
            try:
                return self.save_config(service, args[0][0])
            except:
                return 'SM: Invalid number of parameters for save config!'

        elif command == SM_GET_LOG or command == SM_COMMAND_MAP[SM_GET_LOG]:
            try:
                return self.get_console_log(service, read=args[0][0], fstart=args[0][1])
            except:
                return 'SM: Invalid number of parameters for read log!'

        else:
            return 'SM: Unknown command number: `{0}`!'.format(command)
示例#18
0
    def generate_index(self):
        """
        Generate index when creating a new sut, or test bed.
        """
        logDebug('CeCommonAllocator: generate_index')
        new_sut_id = False

        while not new_sut_id:
            new_sut_id = hexlify(os.urandom(5))
            # If by any chance, this ID already exists, generate another one!
            exists_id = self.get_id(new_sut_id, self.resources)
            if exists_id:
                new_sut_id = False

        return new_sut_id
示例#19
0
    def updateConfigTS(self, files_config=''):
        """
        Updates Test Suite Cofig file hash and recreates internal XML structure,
        only if the XML file is changed.
        The file number and suite number have to be unique.
        """

        if files_config and (type(files_config) == type('') or type(files_config) == type(u'')) \
                and (files_config[0] == '<' and files_config[-1] == '>'):

            # This is pure XML data
            config_ts = files_config
            # Hash check the XML file, to see if is changed
            newConfigHash = hashlib.md5(files_config).hexdigest()

        else:

            if not files_config or not os.path.isfile(files_config):
                # Get path to Test-Suites XML from Master config
                files_config = self.files_config

            if files_config.startswith('~/'):
                files_config = userHome(self.user) + files_config[1:]
            if not os.path.isfile(files_config):
                logError('User {}: Parser: Test-Suites XML file `{}` does '\
                'not exist! Please check framework config XML file!'.format(self.user, files_config))
                self.configTS = None
                return -1
            else:
                config_ts = localFs.read_user_file(self.user, files_config)

            # Hash check the XML file, to see if is changed
            newConfigHash = hashlib.md5(config_ts).hexdigest()

        if self.configHash != newConfigHash:
            logDebug('User {}: Parser: Test-Suites XML file changed, '\
            'rebuilding internal structure...\n'.format(self.user))
            # Use the new hash
            self.configHash = newConfigHash
            # Create XML Soup from the new XML file
            try:
                self.configTS = etree.fromstring(config_ts)
            except Exception:
                logError('User {}: Parser ERROR: Cannot access Test-Suites XML data!'.format(self.user))
                self.configTS = None
                return -1

        self.files_config = files_config
示例#20
0
    def discard_release_reserved_resource(self, res_query, props={}):
        """
        Discard changes for both SUTs and TestBeds
        """

        user_info = self.user_info(props)
        user = user_info[0]
        logDebug(
            'CeCommonAllocator: User {}: discard_release_reserved_resource: {}'
            .format(user, res_query))

        if ':' in res_query:
            res_query = res_query.split(':')[0]

        # Having the id, we can discard and release directly
        if user in self.reservedResources.keys():

            if res_query in self.reservedResources[user]:
                node_id = res_query
            # Having the name of the resource we have to get the id
            else:
                node = self.get_resource(res_query)
                if not isinstance(node, dict):
                    msg = "Could not found a result for {}".format(res_query)
                    logDebug(msg)
                    return False

                # get the root parent of this resource
                if len(node['path']) > 1:
                    node = self.get_path(node['path'][0], self.resources)

                if not node:
                    logError('User {}: Cannot find resource path or ID `{}` !'.
                             format(user, res_query))
                    return False
                node_id = node['id']
            # Delete the entry from reserved dict
            try:
                self.reservedResources[user].pop(node_id)
                if not self.reservedResources[user]:
                    self.reservedResources.pop(user)
            except Exception as exp_err:
                logError(
                    'CeCommonAllocator:discard_release_reserved_resource: `{}` for user {}!'
                    .format(exp_err, user))
                return False

        return True  #RESOURCE_FREE
示例#21
0
    def __init__(self):

        logDebug('SM: Starting Service Manager...')

        self.twister_services = []
        cfg_path = '{0}/config/services.ini'.format(TWISTER_PATH)
        cfg = iniparser.ConfigObj(cfg_path)

        for service in cfg:
            if service == 'DEFAULT':
                continue
            cfg[service]['name'] = service
            self.twister_services.append(cfg[service])

        logDebug('SM: Found `{0}` services: `{1}`.'.format(len(self.twister_services), ', '.join(cfg.keys())))
        del cfg, cfg_path
示例#22
0
    def list_all_tbs(self):
        """
        Fast list testbeds.
        """

        #maybe some resources changed meanwhile
        self.load_tb(verbose=False)

        res = []
        for k_val, v_val in self.resources.get('children').iteritems():
            res.append([k_val, v_val['id']])
        result = []

        def quick_find_path(dictionary, spath):
            """
            Find path.
            """

            for usr, locks in dictionary.iteritems():
                for id_tb, data in locks.iteritems():
                    path = data.get('path', [''])
                    if isinstance(path, str) or isinstance(path, unicode):
                        path = [path]
                    if path == [spath]:
                        return usr
            return None

        for tb_name, tb_id in sorted(res):
            ruser = quick_find_path(self.reservedResources, tb_name)
            luser = quick_find_path(self.lockedResources, tb_name)

            if (not ruser) and (not luser):
                result.append({'id': tb_id, 'name': tb_name, 'status': 'free'})
            elif ruser:
                result.append({'id': tb_id, 'name': tb_name, 'status': 'reserved', 'user': ruser})
            elif luser:
                result.append({'id': tb_id, 'name': tb_name, 'status': 'locked', 'user': luser})
            # Both reserved and locked ?
            else:
                result.append({'id': tb_id, 'name': tb_name, 'status': 'reserved', 'user': ruser})

        logDebug('Fast listing Resources... Found {}.'.format(res))

        return result
示例#23
0
    def _inherit_suite_dependency(self, xml):
        """
        All the tests under a suite inherits the suite's dependencies.
        """
        logDebug('Inherit dependencies from suites.')

        suites = xml.findall('.//TestSuite')
        for suite in suites:
            suitedependency = suite.find('Dependency')
            if suitedependency.text:
                tcs = suite.findall('.//TestCase')
                for tc in tcs:
                    tcdependency = tc.find('Dependency')
                    if tcdependency.text:
                        suitedeplist = suitedependency.text.split(';')
                        for suitedep in suitedeplist:
                            if not suitedep in tcdependency.text:
                                tcdependency.text += suitedep + ';'
                    else:
                        tcdependency.text = suitedependency.text
示例#24
0
    def save_tb(self, props={}):
        """
        Function used to write the changes on HDD.
        """
        logFull('CeTestBeds:_save {}'.format(props))
        log = []

        with self.save_lock:
            ver = self.resources.get('version', 0)
            self.resources['version'] = ver + 1
            try:
                logDebug('Saving test bed file.')
                with open(self.res_file, 'w') as f_p:
                    json.dump(self.resources, f_p, indent=4)
            except Exception as exp_err:
                log.append(exp_err)
        if log:
            logError(log)
            return '*ERROR* ' + str(log)

        return True
示例#25
0
    def system_command(self, user_view, cmd):
        """ Execute a system command """
        logDebug('System command {} {}'.format(user_view, cmd))

        # if the view string ends with : , it means the activity is not set
        # and we have to strip the last :
        if user_view.endswith(':'):
            user_view = user_view.rstrip(':')

        # make sure the CC service is started
        self._usr_service(user_view)

        proc = self._services.get(user_view, {}).get('proc')
        if proc:
            # Empty buffer
            plog = []
            while 1:
                try:
                    line = proc.readline().strip()
                    if not line:
                        continue
                    plog.append(line)
                except:
                    break
            # Send command
            proc.sendline(cmd)
            time.sleep(1)
            plog = []
            # Catch buffer
            while 1:
                try:
                    line = proc.readline().strip()
                    if not line:
                        continue
                    plog.append(line)
                except:
                    break
            return '\n'.join(plog)
        else:
            return False
示例#26
0
    def valid_props(self, props):
        """
        Verify if we have recevied valid props.
        """
        logDebug('CeCommonAllocator: valid_props: props = {} '.format(props))

        if not props:
            return dict()
        if isinstance(props, dict):
            return props
        elif isinstance(props, str) or isinstance(props, unicode):
            props = props.strip()
            try:
                props = ast.literal_eval(props)
            except Exception as exp_err:
                logError('Cannot parse properties: `{}`, `{}` !'.format(
                    props, exp_err))
                return None
        else:
            logError('Invalid properties for set method`{}` !'.format(props))
            return False
        return props
示例#27
0
    def unlock_resource(self, res_query, props={}):
        """
        unlock the resource, delete it from self.lockedResources dict
        """

        logDebug('CeCommonAllocator:unlock_resource {} {} '.format(
            res_query, props))
        user_info = self.user_info(props)
        #we need the id of the resource so we get it from self.resources

        node = self.get_resource(res_query)
        if not node or isinstance(node, str):
            msg = "No such resource {}".format(res_query)
            logError(msg)
            return "*ERROR* " + msg
        #we need to lock the root of the resource
        if len(node['path']) > 1:
            node = self.get_path(node['path'][0], self.resources)

        if isinstance(node, str):
            msg = 'User {}: Unlock resource: Cannot find resource path or ID `{}` !'.format(
                user_info[0], res_query)
            logError(msg)
            return '*ERROR* ' + msg

        with self.acc_lock:
            try:
                self.lockedResources[user_info[0]].pop(node['id'])
                if not self.lockedResources[user_info[0]]:
                    self.lockedResources.pop(user_info[0])
            except Exception as exp_err:
                msg = 'User {}: Unlock resource: `{}` !'.\
                format(user_info[0], exp_err)
                logError(msg)
                return "*ERROR* " + msg

        return True  #RESOURCE_FREE
示例#28
0
    def generate_xml(self, user, filename):
        '''
        Receives project file.
        Creates testsuites.xml file by multiplying tests depending
        on the suts number and eps.
        '''
        logDebug("CeParser: preparing to convert project file: `{}`,\
        user `{}`.".format(filename, user))

        data = self.project.read_project_file(user, filename)
        if data.startswith('*ERROR*'):
            logWarning(data)
            return data

        # try to parse the project file
        try:
            xml = etree.fromstring(data)
        except Exception as e:
            msg = "The file: '{}' it's not an xml file. Try again!\n{}".\
            format(filename, e)
            logDebug(msg)
            return '*ERROR* ' + msg

        self._expand_global_configs(user, xml)

        self._expand_repeat_tag(xml)

        self._expand_by_ep(user, xml)

        repeated_dict = {}
        self._change_ids(xml, repeated_dict)

        self._resolve_dependencies(xml, repeated_dict)

        self._inherit_suite_dependency(xml)

        self._remove_empty_suites_tc(xml)

        for suite in xml.findall('.//TestSuite'):
            prop = suite.find('Property')
            if prop:
                suite.remove(prop)
        # Final XML string
        xml_string = etree.tostring(xml, pretty_print=True)

        # write the xml file
        xml_file = userHome(user) + '/twister/config/testsuites.xml'

        resp = self.project.localFs.write_user_file(user, xml_file, xml_string,
                                                    'w')
        if resp != True:
            logError(resp)
            return '*ERROR* ' + resp

        logDebug('CeParser: Successfully generated: `{}`, user `{}`.'.\
        format(xml_file, user))
        return True
示例#29
0
def execScript(script_path):
    """
    Execute a user script and return the text printed on the screen.
    """
    logFull('helpers:execScript')
    if not os.path.exists(script_path):
        logWarning(
            'Exec script: The path `{}` does not exist!'.format(script_path))
        return False

    try:
        os.system('chmod +x {}'.format(script_path))
    except:
        pass

    logDebug('Executing script `{}`...'.format(script_path))

    try:
        txt = subprocess.check_output(script_path, shell=True)
        return txt.strip()
    except Exception as exp_err:
        logWarning('Exec script `{}`: Exception - `{}`.'.\
        format(script_path, exp_err))
        return False
示例#30
0
    def export_tb_xml(self, xml_file, props={}):
        """
        Export as XML file.
        """
        user_info = self.user_info(props)

        logDebug('User {}: exporting to XML file `{}`'.format(user_info[0], xml_file))

        try:
            f_p = open(xml_file, 'w')
        except:
            msg = 'User {}: export XML: XML file `{}` cannot be written !'.format(user_info[0], xml_file)
            logError(msg)
            return '*ERROR* ' + msg

        logDebug('User {}: exporting to XML file `{}`...'.format(user_info[0], xml_file))

        xml = etree.Element('root')
        res_to_xml(self.resources, xml, False)
        f_p.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n\n')
        f_p.write(etree.tostring(xml, pretty_print=True))
        f_p.close()

        return True