Split checkDBHasEntries method into getDBEntries and assertEntriesAtLeast methods

This commit is contained in:
M. Mert Yildiran 2021-11-10 14:10:28 +03:00
parent 3e2102e262
commit 9ff9be9d68
No known key found for this signature in database
GPG Key ID: D42ADB236521BF7A
2 changed files with 24 additions and 14 deletions

View File

@ -66,7 +66,8 @@ func TestTap(t *testing.T) {
entriesCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, entriesCount)
entries := getDBEntries(t, timestamp, entriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
entry := entries[0]
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])
@ -431,7 +432,8 @@ func TestTapRedact(t *testing.T) {
redactCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, defaultEntriesCount)
entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
firstEntry := entries[0]
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
@ -527,7 +529,8 @@ func TestTapNoRedact(t *testing.T) {
redactCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, defaultEntriesCount)
entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
firstEntry := entries[0]
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
@ -623,7 +626,8 @@ func TestTapRegexMasking(t *testing.T) {
redactCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, defaultEntriesCount)
entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
firstEntry := entries[0]
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
@ -711,7 +715,8 @@ func TestTapIgnoredUserAgents(t *testing.T) {
ignoredUserAgentsCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, defaultEntriesCount)
entries := getDBEntries(t, timestamp, defaultEntriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
for _, entryInterface := range entries {
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entryInterface["id"])
@ -901,7 +906,8 @@ func TestDaemonSeeTraffic(t *testing.T) {
entriesCheckFunc := func() error {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
entries := checkDBHasEntries(t, timestamp, entriesCount)
entries := getDBEntries(t, timestamp, entriesCount, 1*time.Second)
assertEntriesAtLeast(t, entries, 1)
entry := entries[0]
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])

View File

@ -334,9 +334,17 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
}
}
// checkDBHasEntries checks whether there are any entries in the database
// before the given timestamp. Returns a slice of non-empty entries if it succeeds.
func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[string]interface{}) {
// assertEntriesAtLeast checks whether the number of entries greater than or equal to n
func assertEntriesAtLeast(t *testing.T, entries []map[string]interface{}, n int) {
if len(entries) < n {
t.Errorf("Unexpected entries result - Expected more than %d entries", n-1)
}
}
// getDBEntries retrieves the entries from the database before the given timestamp.
// Also limits the results according to the limit parameter.
// Timeout for the WebSocket connection is defined by the timeout parameter.
func getDBEntries(t *testing.T, timestamp int64, limit int, timeout time.Duration) (entries []map[string]interface{}) {
query := fmt.Sprintf("timestamp < %d and limit(%d)", timestamp, limit)
webSocketUrl := getWebSocketUrl(defaultApiServerPort)
@ -377,11 +385,7 @@ func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[
go handleWSConnection(&wg)
wg.Add(1)
waitTimeout(&wg, 1*time.Second)
if len(entries) == 0 {
t.Error("unexpected entries result - Expected more than 0 entries")
}
waitTimeout(&wg, timeout)
return
}