Skip to content

Commit d67a685

Browse files
committed
add tests for remote levels of newlogd
The added test sets the remote log levels of EVE and checks the logs that arrive at the controller after a certain period of time. The test succeeds unless logs below the set remote log level are detected. Signed-off-by: Paul Gaiduk <paulg@zededa.com>
1 parent 00557c0 commit d67a685

File tree

6 files changed

+217
-15
lines changed

6 files changed

+217
-15
lines changed

tests/newlogd/Makefile

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
DEBUG ?= "debug"
2+
3+
# HOSTARCH is the host architecture
4+
# ARCH is the target architecture
5+
# we need to keep track of them separately
6+
HOSTARCH ?= $(shell uname -m)
7+
HOSTOS ?= $(shell uname -s | tr A-Z a-z)
8+
9+
# canonicalized names for host architecture
10+
override HOSTARCH := $(subst aarch64,arm64,$(subst x86_64,amd64,$(HOSTARCH)))
11+
12+
# unless otherwise set, I am building for my own architecture, i.e. not cross-compiling
13+
# and for my OS
14+
ARCH ?= $(HOSTARCH)
15+
OS ?= $(HOSTOS)
16+
17+
# canonicalized names for target architecture
18+
override ARCH := $(subst aarch64,arm64,$(subst x86_64,amd64,$(ARCH)))
19+
20+
WORKDIR ?= $(CURDIR)/../../dist
21+
TESTDIR := tests/$(shell basename $(CURDIR))
22+
BINDIR := $(WORKDIR)/bin
23+
DATADIR := $(WORKDIR)/$(TESTDIR)/
24+
BIN := eden
25+
LOCALBIN := $(BINDIR)/$(BIN)-$(OS)-$(ARCH)
26+
TESTNAME := eden.newlogd
27+
TESTBIN := $(TESTNAME).test
28+
TESTSCN := $(TESTNAME).tests.txt
29+
LOCALTESTBIN := $(TESTBIN)-$(OS)-$(ARCH)
30+
SCRIPTDIR := scripts
31+
LINKDIR := ../../tests/newlogd
32+
33+
.DEFAULT_GOAL := help
34+
35+
clean:
36+
rm -rf $(LOCALTESTBIN) $(BINDIR)/$(TESTBIN) $(WORKDIR)/$(TESTSCN) $(CURDIR)/$(TESTBIN) $(BINDIR)/$(TESTBIN)
37+
38+
$(BINDIR):
39+
mkdir -p $@
40+
$(DATADIR):
41+
mkdir -p $@
42+
43+
test:
44+
$(LOCALBIN) test $(CURDIR) -v $(DEBUG)
45+
46+
build: setup
47+
48+
testbin: $(TESTBIN)
49+
$(LOCALTESTBIN): $(BINDIR) *.go
50+
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go test -c -ldflags "-s -w" -o $@ *.go
51+
52+
$(TESTBIN): $(LOCALTESTBIN)
53+
ln -sf $(LOCALTESTBIN) $(CURDIR)/$(TESTBIN)
54+
55+
setup: testbin $(BINDIR) $(DATADIR)
56+
cp -a $(LOCALTESTBIN) $(CURDIR)/$(TESTBIN) $(BINDIR)
57+
cp -a *.yml $(TESTSCN) $(DATADIR)
58+
59+
debug:
60+
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go test -c -gcflags "all=-N -l" -o $@ *.go
61+
dlv dap --listen=:12345 --headless=true --api-version=2 exec ./debug -- -test.v
62+
63+
.PHONY: test build setup clean all testbin debug
64+
65+
help:
66+
@echo "EDEN is the harness for testing EVE and ADAM"
67+
@echo
68+
@echo "This Makefile automates commons tasks of EDEN testing"
69+
@echo
70+
@echo "Commonly used maintenance and development targets:"
71+
@echo " build build test-binary (OS and ARCH options supported, for ex. OS=linux ARCH=arm64)"
72+
@echo " setup setup of test environment"
73+
@echo " test run tests"
74+
@echo " clean cleanup of test harness"
75+
@echo
76+
@echo "You need install requirements for EVE (look at https://github.com/lf-edge/eve#install-dependencies)."
77+
@echo "You need access to docker socket and installed qemu packages."

tests/newlogd/eden-config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
eden:
3+
# test binary
4+
test-bin: "eden.newlogd.test"
5+
6+
# test scenario
7+
test-scenario: "eden.newlogd.tests.txt"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eden.newlogd.test

tests/newlogd/newlogd_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package newlogd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
"time"
8+
9+
"github.com/lf-edge/eden/pkg/controller/elog"
10+
tk "github.com/lf-edge/eden/pkg/evetestkit"
11+
"github.com/lf-edge/eden/pkg/utils"
12+
)
13+
14+
var eveNode *tk.EveNode
15+
var logT *testing.T
16+
17+
const (
18+
projectName = "newlogd"
19+
logTimeout = 2 * time.Minute
20+
)
21+
22+
func logFatalf(format string, args ...interface{}) {
23+
out := utils.AddTimestampf(format+"\n", args...)
24+
if logT != nil {
25+
logT.Fatal(out)
26+
} else {
27+
fmt.Print(out)
28+
os.Exit(1)
29+
}
30+
}
31+
32+
func logInfof(format string, args ...interface{}) {
33+
out := utils.AddTimestampf(format+"\n", args...)
34+
if logT != nil {
35+
logT.Log(out)
36+
} else {
37+
fmt.Print(out)
38+
}
39+
}
40+
41+
type stepCounter struct {
42+
count int
43+
}
44+
45+
func (s *stepCounter) AnnounceNext(msg string) {
46+
s.count++
47+
logInfof("STEP %d: %s", s.count, msg)
48+
}
49+
50+
func TestMain(m *testing.M) {
51+
logInfof("%s Test started", projectName)
52+
defer logInfof("%s Test finished", projectName)
53+
54+
node, err := tk.InitializeTest(projectName, tk.WithControllerVerbosity("debug"))
55+
if err != nil {
56+
logFatalf("Failed to initialize test: %v", err)
57+
}
58+
59+
eveNode = node
60+
res := m.Run()
61+
os.Exit(res)
62+
}
63+
64+
func TestLogLevelsDifferent(t *testing.T) {
65+
logT = t
66+
steppy := &stepCounter{}
67+
68+
logInfof("TestLogLevelsDifferent started")
69+
defer logInfof("TestLogLevelsDifferent finished")
70+
71+
steppy.AnnounceNext("secure the initial config")
72+
if err := eveNode.GetConfig("/tmp/initial_config"); err != nil {
73+
logFatalf("Failed to get initial config: %v", err)
74+
}
75+
defer func() {
76+
logInfof("revert to the initial config")
77+
if err := eveNode.SetConfig("/tmp/initial_config"); err != nil {
78+
logFatalf("Failed to get back to the initial config: %v", err)
79+
}
80+
}()
81+
82+
steppy.AnnounceNext("set log levels")
83+
localLogLevel := "debug"
84+
remoteLogLevel := "none"
85+
eveNode.UpdateNodeGlobalConfig(
86+
nil,
87+
map[string]string{
88+
"debug.default.loglevel": localLogLevel,
89+
"debug.default.remote.loglevel": remoteLogLevel,
90+
"debug.syslog.loglevel": localLogLevel,
91+
"debug.syslog.remote.loglevel": remoteLogLevel,
92+
"debug.kernel.loglevel": localLogLevel,
93+
"debug.kernel.remote.loglevel": remoteLogLevel,
94+
},
95+
)
96+
if err := eveNode.WaitForConfigApplied(60 * time.Second); err != nil {
97+
logFatalf("Failed to wait for config to be applied: %v", err)
98+
}
99+
100+
steppy.AnnounceNext("reboot EVE to generate fresh logs")
101+
if err := eveNode.EveRebootAndWait(180); err != nil {
102+
logFatalf("Failed to reboot EVE: %v", err)
103+
}
104+
105+
steppy.AnnounceNext(fmt.Sprintf("check for undesired logs (timeout %v)", logTimeout))
106+
query := map[string]string{
107+
"severity": ".*",
108+
}
109+
if err := eveNode.FindLogOnAdam(query, elog.LogNew, logTimeout); err != nil {
110+
logInfof("No logs found, as expected")
111+
} else {
112+
logFatalf("Logs found, but they should not be present with the remote log level '%s'", remoteLogLevel)
113+
}
114+
}

tests/workflow/smoke.tests.txt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Number of tests
2-
{{$tests := 24}}
2+
{{$tests := 25}}
33
# EDEN_TEST_SETUP env. var. -- "y"(default) performs the EDEN setup steps
44
{{$setup := "n"}}
55
{{$setup_env := EdenGetEnv "EDEN_TEST_SETUP"}}
@@ -50,35 +50,37 @@ eden.escript.test -testdata ../lim/testdata/ -test.run TestEdenScripts/info_test
5050
eden.escript.test -test.run TestEdenScripts/eden_hw_inventory
5151
/bin/echo Eden Metric test (10/{{$tests}})
5252
eden.escript.test -testdata ../lim/testdata/ -test.run TestEdenScripts/metric_test
53-
/bin/echo Eden Vector test (11/{{$tests}})
53+
/bin/echo Eden Newlogd test (11/{{$tests}})
54+
eden.escript.test -test.run TestEdenScripts/eden_newlogd
55+
/bin/echo Eden Vector test (12/{{$tests}})
5456
eden.escript.test -test.run TestEdenScripts/eden_vector
5557

56-
/bin/echo Escript args test (12/{{$tests}})
58+
/bin/echo Escript args test (13/{{$tests}})
5759
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/arg -args=test1=123,test2=456
58-
/bin/echo Escript template test (13/{{$tests}})
60+
/bin/echo Escript template test (14/{{$tests}})
5961
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/template
60-
/bin/echo Escript message test (14/{{$tests}})
62+
/bin/echo Escript message test (15/{{$tests}})
6163
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/message
62-
/bin/echo Escript nested scripts test (15/{{$tests}})
64+
/bin/echo Escript nested scripts test (16/{{$tests}})
6365
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/nested_scripts
64-
/bin/echo Escript time test (16/{{$tests}})
66+
/bin/echo Escript time test (17/{{$tests}})
6567
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/time
66-
/bin/echo Escript source test (17/{{$tests}})
68+
/bin/echo Escript source test (18/{{$tests}})
6769
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/source
68-
/bin/echo Escript fail scenario test (18/{{$tests}})
70+
/bin/echo Escript fail scenario test (19/{{$tests}})
6971
eden.escript.test -testdata ../escript/testdata/ -test.run TestEdenScripts/fail_scenario
7072

71-
/bin/echo Eden app metadata test (19/{{$tests}})
73+
/bin/echo Eden app metadata test (20/{{$tests}})
7274
eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/metadata
73-
/bin/echo Eden app userdata test (20/{{$tests}})
75+
/bin/echo Eden app userdata test (21/{{$tests}})
7476
eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/userdata
75-
/bin/echo Eden app log test (21/{{$tests}})
77+
/bin/echo Eden app log test (22/{{$tests}})
7678
eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/app_logs
77-
/bin/echo Eden change controller certificate test (22/{{$tests}})
79+
/bin/echo Eden change controller certificate test (23/{{$tests}})
7880
eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/ctrl_cert_change
7981

80-
/bin/echo Eden Shutdown test (23/{{$tests}})
82+
/bin/echo Eden Shutdown test (24/{{$tests}})
8183
eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/shutdown_test
8284

83-
/bin/echo EVE reset (24/{{$tests}})
85+
/bin/echo EVE reset (25/{{$tests}})
8486
eden.escript.test -test.run TestEdenScripts/eden_reset
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test eden.newlogd.test

0 commit comments

Comments
 (0)