diff options
author | Ittay Stern <ittay.stern@att.com> | 2020-03-23 07:24:50 +0200 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2020-03-23 05:27:20 +0000 |
commit | 9c411472a8aa8a218665d258f52c3d436ee9b58e (patch) | |
tree | 410ebaffb7e050d212060f88598f71f1c12170a6 /vid-automation | |
parent | efefd95854efb51333dc36abe7bb33fca4d04e90 (diff) |
Add some timed grace for assertion on log-lines
It happens that logging is delayed, and rows are not available on disk
right after an inspected event.
Issue-ID: VID-647
Change-Id: Ic47cb2beff0be699f018ff6b5f264a57479bd7d0
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-automation')
4 files changed, 45 insertions, 38 deletions
diff --git a/vid-automation/src/main/java/org/onap/vid/api/AsyncInstantiationBase.java b/vid-automation/src/main/java/org/onap/vid/api/AsyncInstantiationBase.java index ee3982c19..f4dd6780a 100644 --- a/vid-automation/src/main/java/org/onap/vid/api/AsyncInstantiationBase.java +++ b/vid-automation/src/main/java/org/onap/vid/api/AsyncInstantiationBase.java @@ -17,13 +17,10 @@ import static vid.automation.test.utils.ExtendedHamcrestMatcher.hasItemsFromColl import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.util.concurrent.Uninterruptibles; -import java.time.Instant; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -199,20 +196,7 @@ public class AsyncInstantiationBase extends BaseMsoApiTest { } protected void assertAndRetryIfNeeded(Runnable asserter, long timeoutInSeconds) { - final Instant expiry = Instant.now().plusSeconds(timeoutInSeconds); - while (true) { - try { - asserter.run(); - break; // we're cool, assertion passed - } catch (AssertionError fail) { - Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); - if (Instant.now().isAfter(expiry)) { - throw fail; - } else { - System.out.println("retrying after: " + fail); - } - } - } + TestUtils.assertAndRetryIfNeeded(timeoutInSeconds, asserter); } protected ImmutableList<JobAuditStatus> vidAuditStatusesCompletedWithErrors(String jobId) { diff --git a/vid-automation/src/main/java/org/onap/vid/api/TestUtils.java b/vid-automation/src/main/java/org/onap/vid/api/TestUtils.java index cc292caa7..af3cc57b9 100644 --- a/vid-automation/src/main/java/org/onap/vid/api/TestUtils.java +++ b/vid-automation/src/main/java/org/onap/vid/api/TestUtils.java @@ -11,10 +11,13 @@ import static vid.automation.test.utils.RegExMatcher.matchesRegEx; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.google.common.util.concurrent.Uninterruptibles; import java.io.IOException; import java.io.InputStream; +import java.time.Instant; import java.util.Map; import java.util.Scanner; +import java.util.concurrent.TimeUnit; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; import org.apache.commons.text.RandomStringGenerator; @@ -99,6 +102,23 @@ public class TestUtils { return generator.generate(length); } + public static void assertAndRetryIfNeeded(long timeoutInSeconds, Runnable asserter) { + final Instant expiry = Instant.now().plusSeconds(timeoutInSeconds); + while (true) { + try { + asserter.run(); + break; // we're cool, assertion passed + } catch (AssertionError fail) { + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + if (Instant.now().isAfter(expiry)) { + throw fail; + } else { + System.out.println("retrying after: " + fail); + } + } + } + } + @DataProvider public static Object[][] trueAndFalse() { return new Object[][]{{true}, {false}}; diff --git a/vid-automation/src/test/java/org/onap/vid/more/AuditLoggerTest.java b/vid-automation/src/test/java/org/onap/vid/more/AuditLoggerTest.java index b27d6a8af..1cd6077ff 100644 --- a/vid-automation/src/test/java/org/onap/vid/more/AuditLoggerTest.java +++ b/vid-automation/src/test/java/org/onap/vid/more/AuditLoggerTest.java @@ -7,6 +7,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.collection.IsMapContaining.hasKey; +import static org.onap.vid.api.TestUtils.assertAndRetryIfNeeded; import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND; import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET; import static vid.automation.test.services.SimulatorApi.registerExpectationFromPreset; @@ -41,11 +42,11 @@ public class AuditLoggerTest extends BaseApiTest { registerExpectationFromPreset(new PresetAAIGetVpnsByType(), APPEND); String requestId = getRequestId(() -> restTemplate.getForEntity(buildUri("aai_get_vpn_list"), String.class)); - assertThat("request id must be found in exactly two rows", getRequestLogLines(requestId), + assertAndRetryIfNeeded(5, () -> assertThat("request id must be found in exactly two rows", getRequestLogLines(requestId), contains( allOf(containsString(requestId), containsString("Entering")), allOf(containsString(requestId), containsString("Exiting")) - )); + ))); } private List<String> getRequestLogLines(String requestId) { diff --git a/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java b/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java index 962a7860f..f148689d1 100644 --- a/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java +++ b/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java @@ -15,6 +15,7 @@ import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.matchesPattern; +import static org.onap.vid.api.TestUtils.assertAndRetryIfNeeded; import static vid.automation.test.services.SimulatorApi.retrieveRecordedRequests; import com.fasterxml.jackson.databind.JsonNode; @@ -166,30 +167,29 @@ public class LoggerFormatTest extends BaseApiTest { } public static void verifyExistenceOfIncomingReqsInAuditLogs (RestTemplate restTemplate, URI uri, String requestId, String path){ - List<String> logLines = getRequestLogLines(requestId, LogName.audit2019, restTemplate, uri); - String requestIdPrefix = "RequestID="; - assertThat("\nENTRY & EXIT logs are expected to include RequestId: " + requestId - + " \nAnd request path: " - + path + - "\nin exactly two rows - inside the audit log matching lines:\n" - + String.join("\n", logLines) + "\n", + assertAndRetryIfNeeded(5, () -> { + List<String> logLines = getRequestLogLines(requestId, LogName.audit2019, restTemplate, uri); + String requestIdPrefix = "RequestID="; + assertThat("\nENTRY & EXIT logs are expected to include RequestId: " + requestId + + " \nAnd request path: " + + path + + "\nin exactly two rows - inside the audit log matching lines:\n" + + String.join("\n", logLines) + "\n", logLines, contains( - allOf( - containsString(requestIdPrefix + requestId), - containsString("ENTRY"), - containsString(path)), - allOf( - containsString(requestIdPrefix + requestId), - containsString("EXIT"), - containsString(path)) + allOf( + containsString(requestIdPrefix + requestId), + containsString("ENTRY"), + containsString(path)), + allOf( + containsString(requestIdPrefix + requestId), + containsString("EXIT"), + containsString(path)) )); + }); } public static void assertHeadersAndMetricLogs (RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize){ - List<String> logLines = - getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri); - List<RecordedRequests> requests = retrieveRecordedRequests(); List<RecordedRequests> underTestRequests = requests.stream().filter(x -> x.path.contains(path)).collect(toList()); @@ -212,7 +212,9 @@ public class LoggerFormatTest extends BaseApiTest { String invocationId = invocationIds.get(0); allInvocationIds.add(invocationId); - assertIdsInMetricsLog(logLines, requestId, invocationId); + assertAndRetryIfNeeded(5, () -> assertIdsInMetricsLog( + getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri), requestId, invocationId) + ); }); //make sure no InvocationId is repeated twice |