Skip to content

Commit

Permalink
Move wait for log function into log file (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpm-canonical authored Oct 15, 2024
1 parent 8bc32f8 commit df95c72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
21 changes: 1 addition & 20 deletions utils/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package utils

import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

type Config struct {
TestAutoStart bool
TestAutoStart bool
}

func TestConfig(t *testing.T, snapName string, conf Config) {
Expand Down Expand Up @@ -50,20 +48,3 @@ func TestAutostartGlobal(t *testing.T, snapName string) {
require.False(t, SnapServicesActive(t, snapName))
})
}

func WaitForLogMessage(t *testing.T, snap, expectedLog string, since time.Time) {
const maxRetry = 10

for i := 1; i <= maxRetry; i++ {
time.Sleep(1 * time.Second)
t.Logf("Retry %d/%d: Waiting for expected content in logs: %s", i, maxRetry, expectedLog)

logs := SnapLogs(t, since, snap)
if strings.Contains(logs, expectedLog) {
t.Logf("Found expected content in logs: %s", expectedLog)
return
}
}

t.Fatalf("Time out: reached max %d retries.", maxRetry)
}
18 changes: 18 additions & 0 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"strings"
"testing"
"time"
)

func logFileName(t *testing.T, label string) string {
Expand All @@ -28,3 +29,20 @@ func WriteLogFile(t *testing.T, label string, content string) error {
0644,
)
}

func WaitForLogMessage(t *testing.T, snap, expectedLog string, since time.Time) {
const maxRetry = 10

for i := 1; i <= maxRetry; i++ {
time.Sleep(1 * time.Second)
t.Logf("Retry %d/%d: Waiting for expected content in logs: %s", i, maxRetry, expectedLog)

logs := SnapLogs(t, since, snap)
if strings.Contains(logs, expectedLog) {
t.Logf("Found expected content in logs: %s", expectedLog)
return
}
}

t.Fatalf("Time out: reached max %d retries.", maxRetry)
}

0 comments on commit df95c72

Please sign in to comment.