def rollover_MTE_start_date(GMTStartTime): """Change the MTE machine date to a few seconds before the next StartOfDay time. If current time < specified time, change to specified time today. If current time >= specified time, change to specified time tomorrow. Waits for start of day instrument update to complete. Argument: GMTStartTime : StartOfDay GMT time with format HH:MM. return nil Examples: | Rollover MTE Start Date | 09:00 | """ secondsBeforeStartTime = timedelta(seconds = 5) aDay = timedelta(days = 1 ) currTimeArray = LinuxCoreUtilities().get_date_and_time() # current time as array of strings C = map(int,currTimeArray) # current time as array of INTs T = map(int,GMTStartTime.split(':')) #start time as array of INTs currDateTime = datetime(*C) # current time as dateTime object newDateTime = datetime(C[0],C[1],C[2],T[0],T[1]) - secondsBeforeStartTime if currDateTime >= newDateTime: newDateTime += aDay LinuxCoreUtilities().set_date_and_time(newDateTime.year, newDateTime.month, newDateTime.day, newDateTime.hour, newDateTime.minute, newDateTime.second) currTimeArray = newDateTime.strftime('%Y,%m,%d,%H,%M,%S').split(',') logfiles.wait_smf_log_message_after_time('%s.*StartOfDay time occurred' %MTE, currTimeArray) logfiles.wait_smf_log_does_not_contain('dropped due to expiration' , waittime=5, timeout=300) logfiles.wait_smf_log_message_after_time('%s.*handleStartOfDayInstrumentUpdate.*Ending' %MTE, currTimeArray)
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 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 wait_for_persist_load_to_start(timeout=60): """Wait for the MTE/FTE to begin loading from the PERSIST file. Argument: timeout : the maximum time to wait, in seconds 1. This is not a generic function to search for a message from the log file. 2. Using read_until() or read_until_regexp() is problematic if there is a lot of output 3. To restrict the amount of output the routine grep's for only lines containing 'Persistence' """ dt = LinuxCoreUtilities().get_date_and_time() currentFile = '%s/smf-log-files.%s%s%s.txt' % (SMFLOGDIR, dt[0], dt[1], dt[2]) orig_timeout = G_SSHInstance.get_connection().timeout G_SSHInstance.set_client_configuration(timeout='%s seconds' % timeout) G_SSHInstance.write('tail --lines=0 -f %s | grep Persistence' % currentFile) G_SSHInstance.read_until_regexp('Persistence: Loading.*complete') G_SSHInstance.set_client_configuration(timeout=orig_timeout) LinuxCoreUtilities().kill_processes('tail')
def wait_smf_log_message_after_time(message, timeRef, isCaseSensitive=False, waittime=2, timeout=60): """Wait until the SMF log file contains the specified message with a timestamp newer than the specified reference time NOTE: This does not yet handle log file rollover at midnight Argument : message : target message in grep format to find in smf log timeRef : UTC time message must be after. It is a list of values as returned by the get_date_and_time Keyword [year, month, day, hour, min, second] 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 : the timestamp of the message found if success, or raise error Examples: | ${dt}= | get date and time | | ${logMsgTimestamp}= | wait smf log message after time | FMS REORG DONE | ${dt} | """ retLogTimestamp = None refDate = '%s-%s-%s' % (timeRef[0], timeRef[1], timeRef[2]) refTime = '%s:%s:%s' % (timeRef[3], timeRef[4], timeRef[5]) # print 'DEBUG refDate %s, refTime %s' %(refDate,refTime) dt = LinuxCoreUtilities().get_date_and_time() currentFile = '%s/smf-log-files.%s%s%s.txt' % (SMFLOGDIR, dt[0], dt[1], dt[2]) # print 'DEBUG checking SMF log file %s' %currentFile # 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: retMessages = LinuxFSUtilities().grep_remote_file( currentFile, message, isCaseSensitive=isCaseSensitive) # print 'DEBUG retMessages: %s' %retMessages if (len(retMessages) > 0): logContents = retMessages[-1].split(';') if (len(logContents) >= 2): if logContents[0].strip() >= refDate and logContents[1].strip( ) >= refTime: retLogTimestamp = logContents[0].strip( ) + ' ' + logContents[1].strip() return retLogTimestamp time.sleep(waittime) raise AssertionError( '*ERROR* Fail to get pattern \'%s\' from smf log before timeout %ds' % (message, timeout))
def rollover_MTE_time(GMTSysTime): """Change the MTE machine date to a few seconds before the configured time. Argument: GMTSysTime : GMT time with format HH:MM. Return: the set time array like ['2016', '11', '02', '08', '59', '55'] Examples: | Rollover MTE time | 2016-11-02 09:00 | """ secondsBeforeStartTime = timedelta(seconds = 5) newDateTime = datetime(int(GMTSysTime[0:4]),int(GMTSysTime[5:7]),int(GMTSysTime[8:10]),int(GMTSysTime[11:13]),int(GMTSysTime[14:16])) - secondsBeforeStartTime LinuxCoreUtilities().set_date_and_time(newDateTime.year, newDateTime.month, newDateTime.day, newDateTime.hour, newDateTime.minute, newDateTime.second) currTimeArray = newDateTime.strftime('%Y,%m,%d,%H,%M,%S').split(',') return currTimeArray
def dump_cache(waittime=2, timeout=60): """Dump the MTE cache to a file (on the MTE machine). Returns the full path name to the dumped file. Examples: | Dump Cache | | Dump Cache | 10 | 60 | """ stdout = CHEprocess.run_commander('linehandler', 'lhcommand %s dumpcache' %MTE) if stdout.lower().find('successfully processed command:') == -1: raise AssertionError('*ERROR* dumpcache %s failed, %s' %(MTE,stdout)) # get path to the cache file today = LinuxCoreUtilities().get_date_and_time() filename = '%s_%s%s%s.csv' %(MTE, today[0], today[1], today[2]) foundfiles = LinuxFSUtilities().wait_for_search_file(VENUE_DIR,filename,waittime,timeout) if len(foundfiles) > 1: raise AssertionError('*ERROR* Found more than one cache file: %s' %foundfiles) print '*INFO* cache file is %s' %foundfiles[0] LinuxFSUtilities().wait_for_file_write(foundfiles[0],waittime,timeout) return foundfiles[0]
def wait_for_process_to_not_exist(pattern, waittime=2, timeout=60): """Wait until no process matching the specified pattern exists. Argument pattern is the pattern to search for in the full process command line. Argument 'waittime' specifies the time to wait between checks, in seconds. Argument 'timeout' specifies the maximum time to wait, in seconds. Does not return a value; raises an error if the process still exists after timeout seconds. Examples: | Wait for process to not exist | MTE -c MFDS1M | """ # 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: result = LinuxCoreUtilities().find_processes_by_pattern(pattern) # print '*DEBUG* result=%s' %result if len(result) == 0: return time.sleep(waittime) raise AssertionError("*ERROR* Process matching pattern '%s' still exists (timeout %ds)" %(pattern,timeout))