Skip to content

Commit 27b59b1

Browse files
committed
agdcslog: imp tests
1 parent ca1d219 commit 27b59b1

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

internal/agdcslog/agdcslog_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
// testTimeout is the common timeout for tests.
2222
const testTimeout = 1 * time.Second
2323

24+
// testServiceName is the service name for integration tests.
2425
const testServiceName = "AdGuardDNSClientTest"
2526

2627
// requireIntegration skips the test unless TEST_AGDCSLOG parses to true.
@@ -29,14 +30,15 @@ func requireIntegration(tb testing.TB) {
2930

3031
const envName = "TEST_AGDCSLOG"
3132

32-
val := os.Getenv(envName)
33-
if val == "" {
34-
tb.Skip("skipping: env is not set")
35-
}
36-
37-
ok, err := strconv.ParseBool(val)
38-
if err != nil || !ok {
39-
tb.Skip("skipping: unexpected value")
33+
switch v := os.Getenv(envName); v {
34+
case "":
35+
tb.Skipf("skipping: %s is not set", envName)
36+
case "0":
37+
tb.Skip("skipping: integration tests are disabled")
38+
case "1":
39+
// Go on.
40+
default:
41+
tb.Skipf(`skipping: %s must be "1" or "0", got %q`, envName, v)
4042
}
4143
}
4244

@@ -74,19 +76,16 @@ func TestSystemLogger_integration(t *testing.T) {
7476

7577
l := integrationSystemLogger(t)
7678

77-
since := time.Now()
78-
sinceStr := since.Format(time.DateTime)
79-
80-
msg := strconv.FormatInt(since.UnixNano(), 10)
79+
msg := strconv.FormatInt(time.Now().UnixNano(), 10)
8180

8281
ctx := testutil.ContextWithTimeout(t, testTimeout)
83-
l.DebugContext(ctx, msg)
82+
l.InfoContext(ctx, msg)
8483

8584
require.EventuallyWithT(t, func(ct *assert.CollectT) {
8685
findCtx, cancel := context.WithTimeout(ctx, testTimeout)
8786
defer cancel()
8887

89-
ok, err := findInLog(findCtx, sinceStr, msg)
88+
ok, err := findInLog(findCtx, msg)
9089
require.NoError(ct, err)
9190

9291
assert.True(ct, ok)

internal/agdcslog/syslog_darwin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
const cmdLogReader = "log"
1616

1717
// findInLog searches the macOS unified log for the message.
18-
func findInLog(ctx context.Context, since, msg string) (ok bool, err error) {
18+
func findInLog(ctx context.Context, msg string) (ok bool, err error) {
1919
var stdOut, stdErr bytes.Buffer
2020

2121
err = executil.Run(ctx, executil.SystemCommandConstructor{}, &executil.CommandConfig{
2222
Path: cmdLogReader,
23-
Args: []string{"show", "--style", "syslog", "--start", since, "--debug"},
23+
Args: []string{"show", "--style", "syslog", "--last", testTimeout.String(), "--info"},
2424
Stdout: &stdOut,
2525
Stderr: &stdErr,
2626
})

internal/agdcslog/syslog_linux_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import (
1515
const cmdLogReader = "journalctl"
1616

1717
// findInLog searches the systemd journal for the message.
18-
func findInLog(ctx context.Context, since, msg string) (ok bool, err error) {
18+
func findInLog(ctx context.Context, msg string) (ok bool, err error) {
1919
var stdOut, stdErr bytes.Buffer
2020

21+
since := "-" + testTimeout.String()
22+
2123
err = executil.Run(ctx, executil.SystemCommandConstructor{}, &executil.CommandConfig{
2224
Path: cmdLogReader,
2325
Args: []string{"-o", "cat", "-t", testServiceName, "--since", since, "--no-pager"},

internal/agdcslog/syslog_windows_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"bytes"
77
"context"
88
"fmt"
9-
"strings"
109

1110
"github.com/AdguardTeam/golibs/osutil/executil"
1211
)
@@ -15,21 +14,25 @@ import (
1514
const cmdLogReader = "wevtutil"
1615

1716
// findInLog searches the Windows Application event log for the message.
18-
func findInLog(ctx context.Context, _, msg string) (ok bool, err error) {
17+
func findInLog(ctx context.Context, msg string) (ok bool, err error) {
1918
var stdOut, stdErr bytes.Buffer
2019

2120
ms := testTimeout.Milliseconds()
2221
query := fmt.Sprintf(`*[System[TimeCreated[timediff(@SystemTime) <= %d]]]`, ms)
2322

2423
err = executil.Run(ctx, executil.SystemCommandConstructor{}, &executil.CommandConfig{
2524
Path: cmdLogReader,
26-
Args: []string{"qe", "Application", "/rd:true", "/f:text", "/q:" + query},
25+
Args: []string{"qe", "Application", "/rd:true", "/f:text", "/uni", "/q:" + query},
2726
Stdout: &stdOut,
2827
Stderr: &stdErr,
2928
})
3029
if err != nil {
3130
return false, fmt.Errorf("log search failed: %w; stderr=%q", err, &stdErr)
3231
}
3332

34-
return strings.Contains(stdOut.String(), msg), nil
33+
// Strip NUL bytes from UTF-16LE output.
34+
out := stdOut.Bytes()
35+
out = bytes.ReplaceAll(out, []byte{0x00}, nil)
36+
37+
return bytes.Contains(out, []byte(msg)), nil
3538
}

0 commit comments

Comments
 (0)