Skip to content

Commit 2febb2a

Browse files
committed
agdcslog: integration tests
1 parent f09cc45 commit 2febb2a

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

internal/agdcslog/agdcslog_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"context"
66
"log/slog"
7+
"os"
8+
"strconv"
79
"strings"
810
"sync"
911
"testing"
@@ -15,6 +17,43 @@ import (
1517
"github.com/stretchr/testify/require"
1618
)
1719

20+
// testTimeout is the common timeout for tests.
21+
const testTimeout = 1 * time.Second
22+
23+
const testServiceName = "AdGuardDNSClientTest"
24+
25+
func requireIntegration(t *testing.T) {
26+
t.Helper()
27+
28+
const envName = "TEST_AGDCSLOG"
29+
30+
val := os.Getenv(envName)
31+
if val == "" {
32+
t.Skip()
33+
}
34+
35+
ok, err := strconv.ParseBool(val)
36+
if err != nil || !ok {
37+
t.Skip()
38+
}
39+
}
40+
41+
func integrationSystemLogger(t *testing.T) (l *slog.Logger) {
42+
t.Helper()
43+
44+
ctx := testutil.ContextWithTimeout(t, testTimeout)
45+
sl, err := agdcslog.NewSystemLogger(ctx, testServiceName)
46+
require.NoError(t, err)
47+
48+
testutil.CleanupAndRequireSuccess(t, sl.Close)
49+
50+
h := agdcslog.NewSyslogHandler(sl, &slog.HandlerOptions{
51+
Level: slog.LevelDebug,
52+
})
53+
54+
return slog.New(h)
55+
}
56+
1857
// testLogger is a mock implementation of [agdcslog.SystemLogger] interface for
1958
// tests.
2059
//
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build darwin
2+
3+
package agdcslog_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestSystemLogger_integration(t *testing.T) {
12+
requireIntegration(t)
13+
14+
l := integrationSystemLogger(t)
15+
require.NotNil(t, l)
16+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build linux
2+
3+
package agdcslog_test
4+
5+
import (
6+
"bytes"
7+
"context"
8+
"fmt"
9+
"strings"
10+
"testing"
11+
"time"
12+
13+
"github.com/AdguardTeam/golibs/osutil/executil"
14+
"github.com/AdguardTeam/golibs/testutil"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestSystemLogger_integration(t *testing.T) {
20+
requireIntegration(t)
21+
22+
l := integrationSystemLogger(t)
23+
24+
since := time.Now()
25+
sinceStr := since.Format(time.DateTime)
26+
27+
msg := fmt.Sprintf("%d", since.UnixNano())
28+
29+
ctx := testutil.ContextWithTimeout(t, testTimeout)
30+
l.DebugContext(ctx, msg)
31+
32+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
33+
findCtx, cancel := context.WithTimeout(ctx, testTimeout)
34+
defer cancel()
35+
36+
ok, err := findInJournald(findCtx, sinceStr, msg)
37+
require.NoError(ct, err)
38+
39+
assert.True(ct, ok)
40+
}, testTimeout, testTimeout/10)
41+
}
42+
43+
func findInJournald(ctx context.Context, since, msg string) (ok bool, err error) {
44+
var stdOut, stdErr bytes.Buffer
45+
46+
err = executil.Run(ctx, executil.SystemCommandConstructor{}, &executil.CommandConfig{
47+
Path: "journalctl",
48+
Args: []string{"-o", "cat", "-t", testServiceName, "--since", since, "--no-pager"},
49+
Stdout: &stdOut,
50+
Stderr: &stdErr,
51+
})
52+
if err != nil {
53+
return false, fmt.Errorf("journalctl failed: %w; stderr=%q", err, &stdErr)
54+
}
55+
56+
return strings.Contains(stdOut.String(), msg), nil
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build windows
2+
3+
package agdcslog_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestSystemLogger_integration(t *testing.T) {
12+
requireIntegration(t)
13+
14+
l := integrationSystemLogger(t)
15+
require.NotNil(t, l)
16+
}

0 commit comments

Comments
 (0)