mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-07-20 19:22:20 +00:00
Split checkDBHasEntries
method into getDBEntries
and assertEntriesAtLeast
methods
This commit is contained in:
parent
3e2102e262
commit
9ff9be9d68
@ -66,7 +66,8 @@ func TestTap(t *testing.T) {
|
|||||||
entriesCheckFunc := func() error {
|
entriesCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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]
|
entry := entries[0]
|
||||||
|
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])
|
||||||
@ -431,7 +432,8 @@ func TestTapRedact(t *testing.T) {
|
|||||||
redactCheckFunc := func() error {
|
redactCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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]
|
firstEntry := entries[0]
|
||||||
|
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
||||||
@ -527,7 +529,8 @@ func TestTapNoRedact(t *testing.T) {
|
|||||||
redactCheckFunc := func() error {
|
redactCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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]
|
firstEntry := entries[0]
|
||||||
|
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
||||||
@ -623,7 +626,8 @@ func TestTapRegexMasking(t *testing.T) {
|
|||||||
redactCheckFunc := func() error {
|
redactCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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]
|
firstEntry := entries[0]
|
||||||
|
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"])
|
||||||
@ -711,7 +715,8 @@ func TestTapIgnoredUserAgents(t *testing.T) {
|
|||||||
ignoredUserAgentsCheckFunc := func() error {
|
ignoredUserAgentsCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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 {
|
for _, entryInterface := range entries {
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entryInterface["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entryInterface["id"])
|
||||||
@ -901,7 +906,8 @@ func TestDaemonSeeTraffic(t *testing.T) {
|
|||||||
entriesCheckFunc := func() error {
|
entriesCheckFunc := func() error {
|
||||||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
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]
|
entry := entries[0]
|
||||||
|
|
||||||
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])
|
entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entry["id"])
|
||||||
|
@ -334,9 +334,17 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkDBHasEntries checks whether there are any entries in the database
|
// assertEntriesAtLeast checks whether the number of entries greater than or equal to n
|
||||||
// before the given timestamp. Returns a slice of non-empty entries if it succeeds.
|
func assertEntriesAtLeast(t *testing.T, entries []map[string]interface{}, n int) {
|
||||||
func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[string]interface{}) {
|
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)
|
query := fmt.Sprintf("timestamp < %d and limit(%d)", timestamp, limit)
|
||||||
webSocketUrl := getWebSocketUrl(defaultApiServerPort)
|
webSocketUrl := getWebSocketUrl(defaultApiServerPort)
|
||||||
|
|
||||||
@ -377,11 +385,7 @@ func checkDBHasEntries(t *testing.T, timestamp int64, limit int) (entries []map[
|
|||||||
go handleWSConnection(&wg)
|
go handleWSConnection(&wg)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
waitTimeout(&wg, 1*time.Second)
|
waitTimeout(&wg, timeout)
|
||||||
|
|
||||||
if len(entries) == 0 {
|
|
||||||
t.Error("unexpected entries result - Expected more than 0 entries")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user