Skip to content

Commit

Permalink
Add Thread testing (#54)
Browse files Browse the repository at this point in the history
* Add manual local Thread commission test (#48)

* Remove obsolete LFS file, update file names

- Commission via code, without using BLE
- Use UTC time on target to ensure consistent queries regardless of local/remote timezones

Inline with doc updates: canonical/matter-docs#22

---------

Co-authored-by: Mengyi Wang <[email protected]>
  • Loading branch information
farshidtz and MonicaisHer authored May 2, 2024
1 parent b6dcc18 commit 2333f09
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 142 deletions.
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

11 changes: 11 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ where:
- `-failfast` makes the test stop after first failure
- `-count 1` is to avoid Go test caching for example when testing a rebuilt snap


## Run Thread tests
For running Thread tests, two Radio Co-Processors (RCPs) are needed for both local and remote machines.

For building and flashing RCP firmware, please refer to [Build and flash RCP firmware on nRF52480 dongle](https://github.com/canonical/openthread-border-router-snap/wiki/Setup-OpenThread-Border-Router-with-nRF52840-Dongle#build-and-flash-rcp-firmware-on-nrf52480-dongle).

```bash
LOCAL_INFRA_IF="eno1" REMOTE_INFRA_IF="eth0" REMOTE_USER="ubuntu" REMOTE_PASSWORD="abcdef" REMOTE_HOST="192.168.178.95" go test -v -failfast -count 1 ./thread_tests
```

## Environment variables

Some environment variables can modify the test functionality. Refer to these in
[the documentation](https://pkg.go.dev/github.com/canonical/matter-snap-testing/env)
of the `matter-snap-testing` Go package.

3 changes: 0 additions & 3 deletions tests/bin/chip-all-clusters-minimal-app-commit-1536ca2

This file was deleted.

33 changes: 33 additions & 0 deletions tests/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tests

import (
"testing"

"github.com/canonical/matter-snap-testing/utils"
"github.com/stretchr/testify/require"
)

func InstallChipTool(t *testing.T) {
const chipToolSnap = "chip-tool"

// clean
utils.SnapRemove(t, chipToolSnap)

if utils.LocalServiceSnap() {
require.NoError(t,
utils.SnapInstallFromFile(nil, utils.LocalServiceSnapPath),
)
} else {
require.NoError(t,
utils.SnapInstallFromStore(nil, chipToolSnap, utils.ServiceChannel),
)
}
t.Cleanup(func() {
utils.SnapRemove(t, chipToolSnap)
})

// connect interfaces
utils.SnapConnect(t, chipToolSnap+":avahi-observe", "")
utils.SnapConnect(t, chipToolSnap+":bluez", "")
utils.SnapConnect(t, chipToolSnap+":process-control", "")
}
2 changes: 2 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ go 1.21.6
require (
github.com/canonical/matter-snap-testing v1.0.0-beta.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.20.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
138 changes: 0 additions & 138 deletions tests/snap_test.go

This file was deleted.

93 changes: 93 additions & 0 deletions tests/thread_tests/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package thread_tests

import (
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/canonical/matter-snap-testing/utils"

tests "chip-tool-snap-tests"
)

const (
otbrSnap = "openthread-border-router"
OTCTL = otbrSnap + ".ot-ctl"
)

func setup(t *testing.T) {
tests.InstallChipTool(t)

const (
defaultInfraInterfaceValue = "wlan0"
infraInterfaceKey = "infra-if"
localInfraInterfaceEnv = "LOCAL_INFRA_IF"
)

// Clean
utils.SnapRemove(t, otbrSnap)

// Install OTBR
utils.SnapInstallFromStore(t, otbrSnap, utils.ServiceChannel)
t.Cleanup(func() {
utils.SnapRemove(t, otbrSnap)
})

// Connect interfaces
snapInterfaces := []string{"avahi-control", "firewall-control", "raw-usb", "network-control", "bluetooth-control", "bluez"}
for _, interfaceSlot := range snapInterfaces {
utils.SnapConnect(nil, otbrSnap+":"+interfaceSlot, "")
}

// Set infra interface
if v := os.Getenv(localInfraInterfaceEnv); v != "" {
infraInterfaceValue := v
utils.SnapSet(nil, otbrSnap, infraInterfaceKey, infraInterfaceValue)
} else {
utils.SnapSet(nil, otbrSnap, infraInterfaceKey, defaultInfraInterfaceValue)
}

// Start OTBR
start := time.Now()
utils.SnapStart(t, otbrSnap)
waitForLogMessage(t, otbrSnap, "Start Thread Border Agent: OK", start)

// Form Thread network
utils.Exec(t, "sudo "+OTCTL+" dataset init new")
utils.Exec(t, "sudo "+OTCTL+" dataset commit active")
utils.Exec(t, "sudo "+OTCTL+" ifconfig up")
utils.Exec(t, "sudo "+OTCTL+" thread start")
utils.WaitForLogMessage(t, otbrSnap, "Thread Network", start)
}

func getActiveDataset(t *testing.T) string {
activeDataset, _, _ := utils.Exec(t, "sudo "+OTCTL+" dataset active -x | awk '{print $NF}' | grep --invert-match \"Done\"")
trimmedActiveDataset := strings.TrimSpace(activeDataset)

return trimmedActiveDataset
}

// TODO: update the library function to print the tail before failing:
// https://github.com/canonical/matter-snap-testing/blob/abae29ac5e865f0c5208350bdab63cecb3bdcc5a/utils/config.go#L54-L69
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 := utils.SnapLogs(t, since, snap)
if strings.Contains(logs, expectedLog) {
t.Logf("Found expected content in logs: %s", expectedLog)
return
}
}

t.Logf("Time out: reached max %d retries.", maxRetry)
stdout, _, _ := utils.Exec(t,
fmt.Sprintf("sudo journalctl --lines=10 --no-pager --unit=snap.\"%s\".otbr-agent --priority=notice", snap))
t.Log(stdout)
t.FailNow()
}
Loading

0 comments on commit 2333f09

Please sign in to comment.