def block_dataflow_by_port_protocol(self, inOrOut, protocol, port):
        """using iptables command to block specific port and protocol data
        
         Argument:
            inOrOut  : either 'INPUT' or 'OUTPUT'
            protocol : UDP, TCP
            port     : port number
        
        Returns    : 

        Examples:
        | block dataflow by port protocol | INPUT | UDP | 9002
        """

        cmd = "/sbin/iptables -A %s -p %s --destination-port %s -j DROP" % (
            inOrOut, protocol, port)

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        cmd = "/sbin/service iptables save"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
Exemple #2
0
def stop_smf(waittime=30, timeout=600):
    """Stop the Server Management Foundation process.
    Does not return a value.

    Example:
    | Stop SMF  |
    """
    cmd = 'service smf status'
    stdout, stderr, rc = _exec_command(cmd)
    print '*INFO* %s' %stdout
    if rc == 3: # rc==3 means SMF is not running
        return 0
    print '*INFO* Stopping SMF'
    cmd = 'service smf stop'
    stdout, stderr, rc = _exec_command(cmd)
    if rc !=0 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    
    #The smf stop may return before everything is stopped.  Make sure it is fully stopped.
    timeout = int(timeout)
    waittime = int(waittime)
    maxtime = time.time() + float(timeout)
    cmd = 'service smf status'
    while time.time() <= maxtime:
        stdout, stderr, rc = _exec_command(cmd)
        if rc == 3: # rc==3 means SMF is not running
            print '*INFO* %s' %stdout
            return 0
        time.sleep(waittime)
    raise AssertionError('*ERROR* Stop SMF did not complete within %s seconds' %(timeout))
Exemple #3
0
def get_stat_block_field(writerName,
                         blockName,
                         fieldName,
                         allowNotFound=False):
    """Returns the specified Stat Block field value.

    Example:
    | ${field}= | get stat block field  | ${mte}  | FMS  |  lastReorgType  |
    """

    cmd = "%s -f %s %s %s" % (utilpath.STATBLOCKFIELDREADER, writerName,
                              blockName, fieldName)
    stdout, stderr, rc = _exec_command(cmd)

    #   print 'DEBUG cmd=%s, rc=%s, stdout=%s stderr=%s' %(cmd,rc,stdout,stderr)
    if rc != 0 or stderr != '':
        if allowNotFound:
            return "Not Found"
        else:
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

    value = re.search(r'^Value:(.*)$', stdout, re.MULTILINE)
    if value:
        return value.group(1).strip()
    else:
        if allowNotFound:
            return "Not Found"
        else:
            raise AssertionError(
                '*ERROR* No value found for %s, %s, %s.  Received the following:%s'
                % (writerName, blockName, fieldName, stdout))
def run_dataview(dataType, multicastIP, interfaceIP, multicastPort, LineID,
                 RIC, domain, *optArgs):
    """ Run Dataview command with specified arguments and return stdout result.
        Argument :
            dataType : Could be TRWF2 or RWF
            multicastIP : multicast IP DataView listen to
            multicastPort : muticast port DataView used to get data
            interfaceIP : interface IP (DDNA or DDNB)
            LineID : lineID published by line handler
            RIC : published RIC by MTE
            Domain : published data domain
            optargs : a variable list of optional arguments for refresh request and DataView run time.
        Return: stdout.
        examples:
            DataView -TRWF2 -IM 232.2.19.229 -IH 10.91.57.71  -PM 7777 -L 4608 -R 1YWZ5_r -D MARKET_BY_PRICE  -O output_test.txt -REF -IMSG 232.2.9.0 -PMSG 9000 -S 0  -EXITDELAY 5
            DataView -TRWF2 -IM 232.2.19.229 -IH 10.91.57.71  -PM 7777 -L 4096 -R .[SPSCB1L2_I -D SERVICE_PROVIDER_STATUS -EXITDELAY 5
    """

    # use pipefail to detect failure of a command within a pipeline
    # remove non-printable chars; dataview COMP_NAME output contains binary characters that can cause utf-8 decode problems
    cmd = 'set -o pipefail; %s -%s -IM %s -IH %s -PM %s -L %s -R \'%s\' -D %s ' % (
        utilpath.DATAVIEW, dataType, multicastIP, interfaceIP, multicastPort,
        LineID, RIC, domain)
    cmd = cmd + ' ' + ' '.join(map(str, optArgs))
    cmd = cmd + ' | tr -dc \'[:print:],[:blank:],\\n\''
    print '*INFO* ' + cmd
    stdout, stderr, rc = _exec_command(cmd)

    if rc != 0:
        raise AssertionError('*ERROR* %s' % stderr)

    return stdout
def generate_persistence_backup(keepDays):
    """ based on the no. of keeping days generate dummy persistence backup files 
        
        params : keepDays = value found in MTE config tag <NumberOfDailyBackupsToKeep>

        return : N/A
        
        Examples :
            | generate persistence backup | 3
    """

    #Persistence backup filename format : PERSIST_${MTE}_YYYYMMDDTHHMMSS.DAT
    dummyRefFile = 'PERSIST_' + MTE + '.DAT'
    listOfPersistBackupFiles = LinuxFSUtilities().search_remote_files(
        VENUE_DIR, dummyRefFile, True)
    if (len(listOfPersistBackupFiles) == 0):
        raise AssertionError('*ERROR* Persistence file is missing')

    backupfileDir = os.path.dirname(listOfPersistBackupFiles[0])
    tdBoxDatetime = LinuxCoreUtilities().get_date_and_time()
    oneDayInSecond = 60 * 60 * 24 * -1
    for dayCount in range(0, int(keepDays) + 2):
        dummyDatetime = datetime(
            int(tdBoxDatetime[0]), int(
                tdBoxDatetime[1]), int(tdBoxDatetime[2]), int('01'), int('00'),
            int('00')) + timedelta(seconds=int(dayCount * oneDayInSecond))
        targetFile = 'PERSIST_%s_%s%02d%02dT010000.DAT' % (
            MTE, dummyDatetime.year, dummyDatetime.month, dummyDatetime.day)
        cmd = "cp -a %s %s" % (listOfPersistBackupFiles[0],
                               backupfileDir + '/' + targetFile)
        stdout, stderr, rc = _exec_command(cmd)

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
    def _get_all_interfaces_names(self):
        """Get the name for all avaliable interface from box
    
        Argument NIL
            
        Returns empty list or list of interface name
    
        Examples:
        | get all interfaces names |
        """

        listOfInterfaceName = []

        cmd = 'ip link show | awk \'/eth[0-9]/ {print $0}\''
        stdout, stderr, rc = _exec_command(cmd)

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        listOfContent = stdout.split('\n')
        for content in listOfContent:
            subcontent = content.split(':')
            if (len(subcontent) > 2):
                listOfInterfaceName.append(subcontent[1].lstrip())

        return listOfInterfaceName
    def _get_alias_ip(self, alias):
        """Get ip address by given alias name
    
        Argument alias specifies the alias name that we could find in /etc/hosts
        
        Returns string 'null' if not found or ip address that matched with alias
    
        Examples:
        | get alias ip | 'DDNA' |
        """
        cmd = 'getent hosts ' + alias
        stdout, stderr, rc = _exec_command(cmd)

        if rc == 2:
            print '*INFO* no alias found for given ip'
            return 'null'

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        if len(stdout) > 0:
            listOfContent = stdout.split()
            return listOfContent[0]

        return 'null'
    def get_interface_name_by_ip(self, ip):
        """Get network card interface name by given ip

        Argument ip specifies the ip address that used to find interface name
        
        Returns string interface name or prompt error

        Examples:
        | get interface name by ip | '192.168.56.10' |
        """

        #Checking if it is valid ip address
        ipComponents = ip.split('.')
        if (len(ipComponents) == 4):
            for component in ipComponents:
                if not (component.isdigit()):
                    raise AssertionError('*ERROR* Invalid IP address %s' % ip)

        cmd = 'ip addr' + '| grep "inet ' + ip + '/"' + '| awk \'BEGIN {FS=" "}{print $7}\''
        stdout, stderr, rc = _exec_command(cmd)

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        if len(stdout) > 0:
            return stdout.strip()

        raise AssertionError('*ERROR* Fail to get the interface name for %s' %
                             ip)
Exemple #9
0
def get_FID_ID_by_FIDName(fieldName):
    """get FID ID from TRWF2.DAT based on fidName
    
    fieldName is the FID Name. For example BID, ASK
    return the corresponding FID ID

    Examples:
    |get FID ID by FIDName | ASK |     
    """

    filelist = LinuxFSUtilities().search_remote_files(BASE_DIR, 'TRWF2.DAT',
                                                      True)
    if (len(filelist) == 0):
        raise AssertionError('no file is found, can not located the field ID')

    #sed 's/\"[^\"]*\"/ /' %s remove the string which begins with symbol " and end with symbol ",
    #for example in file /reuters/Config/TRWF2.DAT, remove the 2nd column
    #tr -s ' ' remove repeat symbol ' '(space)
    cmd = "sed 's/\"[^\"]*\"/ /' %s | grep \"^%s \" | tr -s ' '" % (
        filelist[0], fieldName)

    stdout, stderr, rc = _exec_command(cmd)
    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                             (cmd, rc, stdout, stderr))

    elements = stdout.split()

    if (len(elements) > 2):
        return elements[1]
    else:
        raise AssertionError('*ERROR* The FID can not be found')
Exemple #10
0
def get_FID_Name_by_FIDId(FidId):
    """get FID Name from TRWF2.DAT based on fidID
    
    fidID is the FID ID number. For example 22
    return the corresponding FID Name

    Examples:
    |get FID Name by FIDId | 22 |     
    """

    filelist = LinuxFSUtilities().search_remote_files(BASE_DIR, 'TRWF2.DAT',
                                                      True)
    if (len(filelist) == 0):
        raise AssertionError('no file is found, can not located the field ID')

        #sed -e '/^!/d' %s | sed 's/\"[^\"]*\"/ /'    the command is to remove the comments which begins with symbol !
        #and remove the string beginning with " and ending with ", for example in file /reuters/Config/TRWF2.DAT, the 2nd column
        #tr -s ' '  it is to remove repeat symbol ' '(space)
        #cut -d ' ' f1,2  Use symbol ' '(space) as delimiter to split the line and delete the filed f1 and f2
        cmd = "sed -e '/^!/d' %s | sed 's/\"[^\"]*\"/ /' | grep \" %s \" | tr -s ' ' | cut -d ' ' -f1,2 | grep \" %s$\"" % (
            filelist[0], FidId, FidId)

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        elements = stdout.split()

        if (len(elements) == 2):
            return elements[0]
        else:
            raise AssertionError('*ERROR* The FID can not be found')
Exemple #11
0
def Get_CHE_Config_Filepaths(filename, *ignorePathFile):
    """Get file path for specific filename from TD Box.
       Ignore files that contain any of the strings in list variable ignorePathFile
       if ignorePathFile is empty, then SCWatchdog and puppet directories will be ignored during search
     
    Argument: 
        filename : config filename
        ignorePathFile: list of strings to ignore
                 
    Returns: a list of full path of remote configuration files 
 
    Examples:
    | Get CHE Config Filepath | ddnPublishers.xml 
    | Get CHE Config Filepath | *_grs.json | config_grs.json | SCWatchdog
    """
    if len(ignorePathFile) == 0:
        ignorePathFile = ['/SCWatchdog/', '/puppet/']

    ignoreString = ' | grep -v '.join(map(str, ignorePathFile))
    ignoreString = ' | grep -v ' + ignoreString

    cmd = "find " + BASE_DIR + " -type f -name " + filename + "  " + ignoreString

    stdout, stderr, rc = _exec_command(cmd)
    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                             (cmd, rc, stdout, stderr))

    return stdout.strip().split()
Exemple #12
0
def backup_remote_cfg_file(searchdir, cfgfile, suffix='.backup'):
    """backup config file by create a new copy with filename append with suffix
    Argument : 
    searchdir  : directary where we search for the configuration file
    cfgfile    : configuration filename
    suffix     : suffix used to create the backup filename 
        
    Returns : a list with 1st item = full path config filename and 2nd itme = full path backup filename

    Examples:
    | backup remote cfg file | /ThomsonReuters/Venues | manglingConfiguration.xml |  
    """

    #Find configuration file
    foundfiles = _search_file(searchdir, cfgfile, True)
    if len(foundfiles) < 1:
        raise AssertionError('*ERROR* %s not found' % cfgfile)
    """elif len(foundfiles) > 1:
        raise AssertionError('*ERROR* Found more than one file: %s' %cfgfile)   """

    #backup config file
    backupfile = foundfiles[0] + suffix
    cmd = "cp -a %s %s" % (foundfiles[0], backupfile)
    stdout, stderr, rc = _exec_command(cmd)

    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                             (cmd, rc, stdout, stderr))

    return [foundfiles[0], backupfile]
def get_count_of_SHELL_RICs():
    """Returns a dictionary with Key : Context IDs and Values: the number of SHELL_RICs
    Examples:
    | get_count_of_SHELL_RICs |
    """
    cacheFile = dump_cache()
    fieldDict ={}
    
    # create hash of header values
    cmd = "head -1 %s | tr ',' '\n'" %cacheFile
    stdout, stderr, rc = _exec_command(cmd)
#         print 'DEBUG cmd=%s, rc=%s, stdout=%s stderr=%s' %(cmd,rc,stdout,stderr)
    if rc !=0 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))    

    headerList = stdout.strip().split()
    index = 1;
    headerDict = {}
    for fieldName in headerList:
        headerDict[fieldName] = index
        index += 1
    if not headerDict.has_key('HAS_SHELL_DATA'):
        raise AssertionError('*ERROR* Did not find HAS_SHELL_DATA column in cache file')
    
    #Verify if HAS_SHELL_DATA column has 'True' values
    ShellCol = headerDict['HAS_SHELL_DATA']
    contextIdCol = headerDict['CONTEXT_ID'] - 1
    cmd = "grep -v TEST %s | awk -F',' '($%s == \"TRUE\") {print}' " %(cacheFile, ShellCol)
    print '*INFO* cmd=%s' %cmd
    stdout, stderr, rc = _exec_command(cmd)
    if rc == 1:
        print '*INFO* HAS NO SHELL DATA'
        return fieldDict
    if rc > 1 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    rows = stdout.splitlines()
	 
    # get the requested fields    
    for row in rows:
        values = row.split(',')
        if len(values) != len(headerList):
            raise AssertionError('*ERROR* Number of values (%d) does not match number of headers (%d)' %(len(values), len(headerList)))
        cid = values[contextIdCol]
        fieldDict[cid] = fieldDict.get(cid,0) + 1       
    _delete_file(cacheFile,'',False)
    return fieldDict
def stop_dataview(pidDataView):
    """ Stop Dataview command.
        Argument :
            pidDataView: pid of the DataView which want to stop
        Return: None
    """
    stdout, stderr, rc = _exec_command('kill -SIGINT %s' % pidDataView)
    if rc != 0:
        raise AssertionError('*ERROR* %s' % stderr)
    wait_for_process_to_not_exist(' %s ' % pidDataView)
    def find_processes_by_pattern(self, pattern):
        """
        Find processes that match the given pattern.  Return the matching 'ps' lines.

        Examples:
        | ${result}= | find processes by pattern | MTE -c MFDS1M |
        """
        cmd = "ps -ef | grep '%s' | grep -v grep" % pattern
        stdout, stderr, rc = _exec_command(cmd)
        return stdout.rstrip()
def start_dataview(dataType, multicastIP, interfaceIP, multicastPort, LineID,
                   RIC, domain, outputfile, *optArgs):
    """ Start Dataview command to run in the background and return.  Dataview output will be written to the specified file.  The caller should use 'Stop Dataview' keyword to terminate the Dataview process.
        Argument :
            dataType : Could be TRWF2 or RWF
            multicastIP : multicast IP DataView listen to
            multicastPort : muticast port DataView used to get data
            interfaceIP : interface IP (DDNA or DDNB)
            LineID : lineID published by line handler
            RIC : published RIC by MTE, can be ??? for all rics capture
            Domain : published data domain
            outputfile: the file on the MTE machine which dataview output write to
            optargs : a variable list of optional arguments for refresh request and DataView run time.
        Return: pid of DataView.
        examples:
            ${pid}=    Start Dataview    TRWF2    @{multicastIPandPort}[0]    @{interfaceIPandPort}[0]    @{multicastIPandPort}[1]    ${lineID}
            ...    ${ric}    ${domain}    ${outputFile}    @{optargs}
            DataView -TRWF2 -IM 232.2.19.229 -IH 10.91.57.71 -PM 7777 -L 4608 -R ??? -D MARKET_BY_PRICE  -O output_test.txt -REF -IMSG 232.2.9.0 -PMSG 9000 -S 0 
            DataView -TRWF2 -IM 232.2.19.229 -IH 10.91.57.71 -PM 7777 -L 4096 -R .[SPSCB1L2_I -D SERVICE_PROVIDER_STATUS -O DV_output.txt
            ftp://ftp-coll.stldevnet.reuters.com/release/CI-CD/CATF/Installers/OL%20DataView%20Release%20Notes.txt
    """

    # cleanup all existing DataView process
    cmd = 'pkill DataView'
    _exec_command(cmd)

    cmd = '%s -%s -IM %s -IH %s -PM %s -L %s -R \'%s\' -D %s -O %s' % (
        utilpath.DATAVIEW, dataType, multicastIP, interfaceIP, multicastPort,
        LineID, RIC, domain, outputfile)
    cmd = cmd + ' ' + ' '.join(map(str, optArgs))
    print '*INFO* start Dataview:' + cmd
    _start_command(cmd)

    cmd = "ps -ef | grep -i 'dataview' | grep -v grep |grep '%s'" % RIC
    stdout, stderr, rc = _exec_command(cmd)
    pattern = re.compile(r'\w\s+(\d+)')
    pid = ''
    if re.findall(pattern, stdout) != []:
        pid = re.findall(pattern, stdout)[0]
        return pid
    else:
        raise AssertionError('*ERROR* Cannot find any DataView process')
Exemple #17
0
def start_smf():
    """Start the Server Management Foundation process.

    This keyword is normally invoked only in the test suite setup via the suite_setup keyword.
    
    Does not return a value.

    Example:
    | Start SMF  |
    """
    cmd = 'service smf status'
    stdout, stderr, rc = _exec_command(cmd)
    if rc != 3: # rc==3 means SMF is not running
        if rc !=0 or stderr !='':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
        print '*INFO* %s' %stdout
        return 0
    cmd = 'service smf start'
    stdout, stderr, rc = _exec_command(cmd)
    if rc !=0 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    print '*INFO* %s' %stdout
    def get_hostname(self):
        """
        Get the hostname of remote machine.

        Examples:
        | ${hostname} | get hostname |
        """
        cmd = 'hostname'
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        return stdout.strip()
    def get_memory_usage(self):
        """
        Find the memory usage(%) from system
        
        """

        cmd = "egrep \'MemTotal\' /proc/meminfo | sed \'s/[^0-9]*//g\'"
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        total = float(stdout)

        cmd = "egrep \'MemFree\' /proc/meminfo | sed \'s/[^0-9]*//g\'"
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        free = float(stdout)

        usage = math.ceil(((total - free) / total) * 100)

        return usage
    def unblock_dataflow(self):
        """using iptables command to unblock all ports
        
        Argument   :        
        Returns    : 

        Examples:
        | unblock dataflow | 
        """

        cmd = "iptables -F"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        cmd = "/sbin/service iptables save"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
Exemple #21
0
    def grep_remote_file(self, filePath, searchKeyWord, isPattern=True, isCaseSensitive=True, Fromline=0, timeout=0, retry_interval=0):
        """
        this keyword is used to grep keywords in a file, then return matching lines.\n

        filePath is the full path of the file.\n
        searchKeyWord is the keyword filter.\n
        isPattern is the flag indicate if searchKeyword is a normal search string or regular expression pattern.\n
        isCaseSensitive is the flag indicate if searchKeyword is case sensitive.\n
        timeout default to 0, mean not timeout wait.\n
        retry_interval is default to 20, mean every 20s will check the log once.\n
        Fromline default to 0, which mean from which line to search the content.\n
        
        The return value is a list for all the matched lines content.\n
        Empty list would be returned if no matched lines found.\n

        Examples:
        | @list | grep remote file | /result.txt | AB\\.C | ${true} | ${true} |
        | @list | grep remote file | /result.txt | timelimit \\\\d+ | ${true} | ${true} |
        | @list | grep remote file | /result.txt | AB.C | ${false} | ${false} | 60 | 
        """
        returnMatchLines = []
        current = time.time()
        timout_value = float(timeout)
        maxtime = current + timout_value
        while (current <= maxtime):
            if isPattern == False:
                if isCaseSensitive:
                    cmd = r'grep -n -F "%s" "%s"' % (searchKeyWord, filePath)
                else:
                    cmd = r'grep -n -i -F "%s" "%s"' % (searchKeyWord, filePath)  
            else:
                if not isCaseSensitive:
                    cmd = r'grep -n -i -P "%s" "%s"' % (searchKeyWord, filePath)  
                else:
                    cmd = r'grep -n -P "%s" "%s"' % (searchKeyWord, filePath)
            #stdout = _run_command(cmd)
            retInfo = _exec_command(cmd)
            stdout = retInfo[0]
            if len(stdout) < 1:
                if timout_value == 0:
                    break
                current = time.time()
                time.sleep(float(retry_interval))
            else:
                break
        for line in stdout.split('\n'):
            if line and int(line.split(':',1)[0]) >= int(Fromline):
                returnMatchLines.append(line.split(':',1)[1])
        return returnMatchLines
Exemple #22
0
def run_commander(application, command):
    """Runs the Commander tool to execute the specified CHE command.

    Returns the stdout from the executed command.

    Examples:
    | Run Commander  | process      | start ${mte}      |
    | Run Commander  | linehandler  | dumpcache ${mte}  |
    """
    cmd = r'%s -n %s -c "%s"' %(utilpath.COMMANDER,application,command)
    stdout, stderr, rc = _exec_command(cmd)
#         print 'DEBUG cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr)
    if rc !=0 or stderr !='' or stdout.lower().find('failed') != -1:
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    print '*INFO* cmd=%s, %s'%(cmd,stdout)
    return stdout
def wait_smf_log_does_not_contain(message,
                                  isCaseSensitive=False,
                                  waittime=2,
                                  timeout=60):
    """Wait until the SMF log file does not contain the specified message within the last 'waittime' interval

    Argument :
        message : target message in grep format to find in smf log
        isCaseSensitive : flag indicates if search message is case sensitive.
        waittime : specifies the time to wait between checks, in seconds.
        timeout : specifies the maximum time to wait, in seconds.
    
    Return : Nil if success or raise error

    Example:
    | wait smf log does not contain | Drop message sent for |
    """

    dt = LinuxCoreUtilities().get_date_and_time()

    # convert  unicode to int (it is unicode if it came from the Robot test)
    timeout = int(timeout)
    waittime = int(waittime)
    maxtime = time.time() + float(timeout)
    while time.time() <= maxtime:
        time.sleep(waittime)
        refDate = '%s-%s-%s' % (dt[0], dt[1], dt[2])
        refTime = '%s:%s:%s' % (dt[3], dt[4], dt[5])
        currentFile = '%s/smf-log-files.%s%s%s.txt' % (SMFLOGDIR, dt[0], dt[1],
                                                       dt[2])
        if isCaseSensitive:
            cmd = "grep '%s' %s | tail --lines=1" % (message, currentFile)
        else:
            cmd = "grep -i '%s' %s | tail --lines=1" % (message, currentFile)
        stdout, stderr, rc = _exec_command(cmd)
        retMessage = stdout.split(';')
        if (len(retMessage) < 2):
            return
        else:
            if retMessage[0].strip() <= refDate and retMessage[1].strip(
            ) <= refTime:
                return
        dt = LinuxCoreUtilities().get_date_and_time()
    raise AssertionError(
        '*ERROR* SMF log still contains pattern \'%s\' after timeout %ds' %
        (message, timeout))
def run_HostManger(*optArgs):
    """run HostManager with specific arguments
    
    optArgs    : argument that used to run HostManager
    Returns    : stdout of after the command has executed 

    Examples:
    | run HostManager  | -readparams /HKF02M/LiveStandby|
    """
    cmd = '%s ' % utilpath.HOSTMANAGER
    cmd = cmd + ' ' + ' '.join(map(str, optArgs))
    print '*INFO* ' + cmd
    stdout, stderr, rc = _exec_command(cmd)

    if rc != 0:
        raise AssertionError('*ERROR* %s' % stderr)

    return stdout
Exemple #25
0
def statBlock_should_be_equal(
        writerName,
        blockName,
        fieldName,
        expectedValue,
        msg='statBlock actual value does not match expected value'):
    """Checks whether the specified Stat Block field has the exected value.

    Example:
    | statBlock should be equal  | ${mte}  | FMS  |  lastReorgType  | 2 |
    """
    cmd = "%s -v %s %s %s %s" % (utilpath.STATBLOCKFIELDREADER, writerName,
                                 blockName, fieldName, expectedValue)
    stdout, stderr, rc = _exec_command(cmd)
    #         print '*DEBUG* cmd=%s, %s %s' %(cmd,stdout,stderr)
    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* ' + msg)
    return rc
Exemple #26
0
def restore_remote_cfg_file(cfgfile, backupfile):
    """restore config file by rename backupfile to cfgfile
    Argument : 
    cfgfile    : full path of configuration file
    backupfile : full path of backup file
        
    Returns : Nil

    Examples:
    | restore remote cfg file | /reuters/Venues/HKF/MTE/manglingConfiguration.xml | /reuters/Venues/HKF/MTE/manglingConfiguration.xml.backup |  
    """

    LinuxFSUtilities().remote_file_should_exist(cfgfile)
    LinuxFSUtilities().remote_file_should_exist(backupfile)

    #restore config file
    cmd = "mv -f %s %s" % (backupfile, cfgfile)
    stdout, stderr, rc = _exec_command(cmd)

    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                             (cmd, rc, stdout, stderr))
Exemple #27
0
def get_stat_blocks_for_category(writerName, categoryName):
    """Returns a list of Stat Blocks for the specified category.

    Examples:
    | $categories}= | get stat blocks for category  | ${mte}  | FMS       |
    | $categories}= | get stat blocks for category  | ${mte}  | Holidays  |
    """

    cmd = "%s -c %s %s " % (utilpath.STATBLOCKFIELDREADER, writerName,
                            categoryName)
    stdout, stderr, rc = _exec_command(cmd)
    #         print 'DEBUG cmd=%s, rc=%s, stdout=%s stderr=%s' %(cmd,rc,stdout,stderr)

    if rc != 0 or stderr != '':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                             (cmd, rc, stdout, stderr))
    lines = stdout.rstrip().split('\n')
    numLines = len(lines)
    for idx in range(0, numLines - 1):
        if lines[idx].find('All stat blocks for') != -1:
            return lines[idx + 1:numLines]

    raise AssertionError('*ERROR* No block found for %s, %s' %
                         (writerName, categoryName))
    def enable_disable_interface(self, interfaceName, status):
        """ Enable or disable the interface

            Argument : interfaceName - should be eth1 ... eth5
                       status - should be enable or disable

            Return :   None
            
            Examples :
            | enable disable interface| eth1 | enable |
        """
        if (status.lower() == 'enable'):
            cmd = 'ifup '
        elif (status.lower() == 'disable'):
            cmd = 'ifdown '
        else:
            raise AssertionError(
                '*ERROR* the status is %s, it should be enable or disable' %
                status)
        cmd = cmd + interfaceName
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0:
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
def get_ric_fields_from_cache(numrows, domain, contextID):
    """Get the first n rows' ric fields data for the specified domain or/and contextID from MTE cache.
    Ignore RICs that contain 'TEST' and non-publishable RICs.
    Returns an array of dictionary containing all fields for the match.  Returns empty dictionary if match are not found

    Arguments:
        numrows:   number of rows to return
        domain:    RIC must belong to this domain if domain is not NONE
        contextID: RIC must belong to this contextID if contextID is not NONE
                   If domain and contextID are NONE, first PUBLISHABLE=TRUE will be checked
                
    Returns an array of dictionaries containing fields for each RICs.
    E.g. [ {RIC : ric1, SIC sic1, DOMAIN MarketPrice, CONTEXT_ID : 1052 ...}, {RIC : ric2, SIC sic2, DOMAIN MarketPrice, CONTEXT_ID : 1052 ...} ]

    Example:
    | get_ric_fields_from_cache  | 1 | MARKET_PRICE |
    | get_ric_fields_from_cache  | 1 | ${EMPTY} | 1052 |
    | get_ric_fields_from_cache  | 2 | MARKET_PRICE | 1052 |
    | get_ric_fields_from_cache  | 2 |
    """
    if numrows != 'all':
        numrows = int(numrows)
        
    if domain:
        newDomain = _convert_domain_to_cache_format(domain)
        
    cacheFile = dump_cache()
    # create hash of header values
    cmd = "head -1 %s | tr ',' '\n'" %cacheFile
    stdout, stderr, rc = _exec_command(cmd)
#         print 'DEBUG cmd=%s, rc=%s, stdout=%s stderr=%s' %(cmd,rc,stdout,stderr)
    if rc !=0 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    
    headerList = stdout.strip().split()
    index = 1;
    headerDict = {}
    for fieldName in headerList:
        headerDict[fieldName] = index
        index += 1
    if not headerDict.has_key('DOMAIN') or not headerDict.has_key('PUBLISHABLE') or not headerDict.has_key('CONTEXT_ID'):
        raise AssertionError('*ERROR* Did not find required column names in cache file (DOMAIN, PUBLISHABLE, CONTEXT_ID') 
    
    # get all fields for selected RICs
    domainCol = headerDict['DOMAIN']
    publishableCol = headerDict['PUBLISHABLE']
    contextIDCol = headerDict['CONTEXT_ID']
    
    if contextID and domain:
        if numrows == 'all':
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" && $%d == \"%s\" {print}'" %(cacheFile, domainCol, newDomain, publishableCol, contextIDCol, contextID)
        else:
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" && $%d == \"%s\" {print}' | head -%d" %(cacheFile, domainCol, newDomain, publishableCol, contextIDCol,contextID, numrows)
            
    elif  domain: 
        if numrows == 'all':
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" {print}'" %(cacheFile, domainCol, newDomain, publishableCol)
        else:
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" {print}' | head -%d" %(cacheFile, domainCol, newDomain, publishableCol, numrows)
            
    elif  contextID:
        if numrows == 'all':
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" {print}'" %(cacheFile, contextIDCol, contextID, publishableCol)
        else:
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"%s\" && $%d == \"TRUE\" {print}' | head -%d" %(cacheFile, contextIDCol, contextID, publishableCol, numrows)
            
    else:
        if numrows == 'all':
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"TRUE\" {print}'" %(cacheFile, publishableCol)
        else:
            cmd = "grep -v TEST %s | awk -F',' '$%d == \"TRUE\" {print}' | head -%d" %(cacheFile, publishableCol, numrows)
            
    print '*INFO* cmd=%s' %cmd
    stdout, stderr, rc = _exec_command(cmd)
#     print 'DEBUG cmd=%s, rc=%s, stdout=%s stderr=%s' %(cmd,rc,stdout,stderr)
    if rc !=0 or stderr !='':
        raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
    rows = stdout.splitlines()
    if numrows != 'all' and len(rows) != numrows:
        raise AssertionError('*ERROR* Requested %d rows, Found %d rows' %(numrows,len(rows)))
    
    # get the requested fields
    result = []
    for row in rows:
        values = row.split(',')
        
        if len(values) != len(headerList):
            raise AssertionError('*ERROR* Number of values (%d) does not match number of headers (%d)' %(len(values), len(headerList)))
        
        fieldDict = {}
        for i in range(0, len(values)):
            if headerList[i] == 'DOMAIN':
                newdomain = _convert_cachedomain_to_normal_format(values[i]) 
                fieldDict[headerList[i]] = newdomain
            else:    
                fieldDict[headerList[i]] = values[i]
           
        result.append(fieldDict)
              
    _delete_file(cacheFile,'',False)
    return result 
Exemple #30
0
 def read_remote_file(self, path):
     cmd = "cat '" + path + "'"
     stdout, stderr, rc = _exec_command(cmd)
     if rc !=0 or stderr !='':
         raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
     return stdout