-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_test.go
148 lines (119 loc) · 4.45 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tests
import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/canonical/matter-snap-testing/utils"
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
serviceWaitTimeout := 10
// Start clean
utils.SnapStop(t, otbrSnap)
t.Cleanup(func() {
utils.SnapStop(t, otbrSnap)
})
t.Run("Set infra-if", func(t *testing.T) {
configKey := infraInterfaceKey
configValue := "wpan1"
defaultConfigValue := infraInterfaceValue
expectedLog := infraInterfaceEnv + "=" + configValue
testSettingSnapOption(t, configKey, configValue, defaultConfigValue, otbrSetupApp, expectedLog)
})
t.Run("Set radio-url", func(t *testing.T) {
configKey := "radio-url"
configValue := "spinel+hdlc+uart:///dev/ttyACM1"
defaultConfigValue := defaultRadioURL
expectedLog := "RADIO_URL=" + configValue
testSettingSnapOption(t, configKey, configValue, defaultConfigValue, otbrAgentApp, expectedLog)
})
t.Run("Set invalid thread interface", func(t *testing.T) {
configKey := "thread-if"
defaultConfigValue := "wpan0"
invalidConfigValue := "wpan1"
t.Cleanup(func() {
utils.SnapSet(t, otbrSnap, configKey, defaultConfigValue)
utils.SnapStop(t, otbrSnap)
})
command := "sudo snap set openthread-border-router thread-if=" + invalidConfigValue
output, err := exec.Command("/bin/bash", "-c", command).CombinedOutput()
t.Logf("[exec] %s", command)
require.NotEmpty(t, output)
require.Error(t, err, "Expected an error while setting an invalid thread interface")
})
t.Run("Set webgui-listen-address", func(t *testing.T) {
configKey := "webgui-listen-address"
configValue := "127.0.0.1"
defaultConfigValue := "::"
t.Cleanup(func() {
utils.SnapSet(t, otbrSnap, configKey, defaultConfigValue)
utils.SnapStop(t, otbrSnap)
})
utils.RequirePortAvailable(t, defaultWebGUIPort)
utils.SnapSet(t, otbrSnap, configKey, configValue)
utils.SnapStart(t, otbrSnap)
utils.WaitServiceOnline(t, 10, defaultWebGUIPort)
})
t.Run("Set webgui-port", func(t *testing.T) {
configKey := "webgui-port"
configValue := "90"
defaultConfigValue := defaultWebGUIPort
t.Cleanup(func() {
utils.SnapSet(t, otbrSnap, configKey, defaultConfigValue)
utils.SnapStop(t, otbrSnap)
})
utils.RequirePortAvailable(t, configValue)
utils.SnapSet(t, otbrSnap, configKey, configValue)
utils.SnapStart(nil, otbrSnap)
utils.WaitServiceOnline(t, serviceWaitTimeout, configValue)
utils.WaitServiceOnline(t, 10, configValue)
})
t.Run("Set autostart", func(t *testing.T) {
t.Cleanup(func() {
utils.SnapStop(t, otbrSnap)
})
require.False(t, utils.SnapServicesEnabled(t, otbrSnap))
require.False(t, utils.SnapServicesActive(t, otbrSnap))
utils.SnapSet(t, otbrSnap, "autostart", "true")
require.True(t, utils.SnapServicesEnabled(t, otbrAgentApp))
require.True(t, utils.SnapServicesActive(t, otbrAgentApp))
require.True(t, utils.SnapServicesEnabled(t, otbrWebApp))
require.True(t, utils.SnapServicesActive(t, otbrWebApp))
require.True(t, utils.SnapServicesEnabled(t, otbrSetupApp))
require.False(t, utils.SnapServicesActive(t, otbrSetupApp))
})
}
func testSettingSnapOption(t *testing.T, configKey, configValue, defaultConfigValue, otbrService, expectedLog string) {
t.Helper()
// Start clean
utils.SnapStop(t, otbrSnap)
t.Cleanup(func() {
utils.SnapSet(t, otbrSnap, configKey, defaultConfigValue)
utils.SnapStop(t, otbrSnap)
})
utils.SnapSet(t, otbrSnap, configKey, configValue)
command := "sudo snap start openthread-border-router"
// The error below is not handled as intended
// because the configuration value passed here is invalid for the intended purpose
_, _ = exec.Command("/bin/bash", "-c", command).CombinedOutput()
t.Logf("[exec] %s", command)
snapLogLines := 200
maxRetry := 10
retryInterval := 1 * time.Second
waitForApplicationLogMessage(t, otbrService, expectedLog, snapLogLines, maxRetry, retryInterval)
}
func waitForApplicationLogMessage(t *testing.T, application, expectedLog string, snapLogLines, maxRetry int, retryInterval time.Duration) {
t.Helper()
for i := 1; i <= maxRetry; i++ {
time.Sleep(retryInterval)
t.Logf("Retry %d/%d: Waiting for expected content in application logs: %s", i, maxRetry, expectedLog)
logs, _, _ := utils.Exec(t, fmt.Sprintf("sudo snap logs -n=%d \"%s\"", snapLogLines, application))
if strings.Contains(logs, expectedLog) {
t.Logf("Found expected content in application logs: %s", expectedLog)
return
}
}
t.Fatalf("Time out: reached max %d retries.", maxRetry)
}