diff --git a/CHANGELOG-9.0.0 b/CHANGELOG-9.0.0 index 59462a55201..2039f523188 100644 --- a/CHANGELOG-9.0.0 +++ b/CHANGELOG-9.0.0 @@ -1013,7 +1013,7 @@ Changes with Apache Traffic Server 9.0.0 #7004 - Fixed core when sending back a redirect and having an invalid server response #7005 - Added tasks and launch files for vscode, to configure, build and debug #7007 - Updates to thread scale factor - #7008 - slice plugin: fix throttle not work + #7008 - slice plugin: fix throttle not work #7012 - Remove incorrect assert in inactivity timeout handling #7031 - Fix code to eliminate warning and enable feature #7035 - Add a null check to avoid crashing diff --git a/tests/gold_tests/autest-site/when.test.ext b/tests/gold_tests/autest-site/when.test.ext index d44c9ba09cc..3f0a27600ca 100644 --- a/tests/gold_tests/autest-site/when.test.ext +++ b/tests/gold_tests/autest-site/when.test.ext @@ -19,18 +19,67 @@ When extensions. from autest.api import AddWhenFunction import hosts.output as host +import os +import re -def FileContains(haystack, needle): +def FileContains(haystack, needle, desired_count=1): + """ + Return whether the file haystack contains the string needle. + + Args: + haystack (str): The path to the file to be inspected. + needle (str): The content to look for in haystack. This can be a + regular expression which will be used in Python's re.search + function. + desired_count (int): How many times the caller desires to see needle in + haystack before considering the When condition fulfilled. + + Returns: + True if the haystack exists as a file and contains needle, False + otherwise. + """ + + if desired_count < 0: + raise ValueError("Cannot pass a negative desired_count value to FileContains.") + if desired_count == 0: + raise ValueError("Cannot pass a desired_count of 0 to FileContains.") + + if not os.path.exists(haystack): + host.WriteDebug( + ['FileContains', 'when'], + "Testing for file content '{0}' in file '{1}': file does not exist".format( + needle, haystack)) + return False + + needle_regex = re.compile(needle) with open(haystack) as f: - result = needle in f.read() + needle_seen_count = 0 + line_count = 0 + for line in f: + line_count += 1 + if needle_regex.search(line): + host.WriteDebug( + ['FileContains', 'when'], + "Found '{0}' in file '{1}' in line: '{2}', line number: {3}".format( + needle, haystack, line.rstrip(), line_count)) + needle_seen_count += 1 + + if needle_seen_count >= desired_count: + host.WriteDebug( + ['FileContains', 'when'], + "Testing for file content '{0}' in file '{1}', " + "successfully found it the desired {2} times".format( + needle, haystack, needle_seen_count)) + return True host.WriteDebug( - ['FileExists', 'when'], - "Testing for file content '{0}' in '{1}' : {2}".format( - needle, haystack, result)) + ['FileContains', 'when'], + "Testing for file content '{0}' in file '{1}', only seen {2} " + "out of the desired {3} times".format( + needle, haystack, needle_seen_count, desired_count)) - return result + return False AddWhenFunction(FileContains)