Skip to content

Commit c327251

Browse files
committed
add examples for automated-testing
1 parent 968bd3c commit c327251

File tree

12 files changed

+744
-0
lines changed

12 files changed

+744
-0
lines changed

automated-testing/.DS_Store

6 KB
Binary file not shown.

automated-testing/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
broker_addr=localhost:30083
2+
3+
all:
4+
go test ./... -addr $(broker_addr) -json|go-test-report
5+
6+
clean:
7+
rm -fr test_report.html
8+
9+

automated-testing/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module bigwhite/autotester
2+
3+
go 1.18
4+
5+
require (
6+
github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
7+
github.com/gorilla/websocket v1.4.2 // indirect
8+
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
9+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
10+
)

automated-testing/go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed h1:ue9pVfIcP+QMEjfgo/Ez4ZjNZfonGgR6NgjMaJMu1Cg=
2+
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
3+
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
4+
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
5+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
6+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8+
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
9+
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
10+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
11+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
12+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
13+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package connection
2+
3+
import (
4+
"testing"
5+
6+
mqtt "github.com/eclipse/paho.mqtt.golang"
7+
)
8+
9+
func Test_Connection_S0001_ConnectOKWithoutAuth(t *testing.T) {
10+
opts := mqtt.NewClientOptions()
11+
opts.AddBroker("tcp://" + addr)
12+
13+
// Create and start MQTT client
14+
client := mqtt.NewClient(opts)
15+
token := client.Connect()
16+
token.Wait()
17+
18+
// Check if connection was successful
19+
if token.Error() != nil {
20+
t.Errorf("want ok, got %v", token.Error())
21+
return
22+
}
23+
client.Disconnect(0)
24+
}
25+
26+
func Test_Connection_S0002_ConnectOKWithAuth(t *testing.T) {
27+
//... ...
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package connection
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
mqtt "github.com/eclipse/paho.mqtt.golang"
10+
)
11+
12+
var addr string
13+
14+
func init() {
15+
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
16+
}
17+
18+
func TestMain(m *testing.M) {
19+
flag.Parse()
20+
21+
// setup for this scenario
22+
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
23+
/*
24+
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
25+
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
26+
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
27+
*/
28+
29+
// run this scenario test
30+
r := m.Run()
31+
32+
// teardown for this scenario
33+
// tbd if teardown is needed
34+
35+
os.Exit(r)
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package publish
2+
3+
import (
4+
scenarios "bigwhite/autotester/scenarios"
5+
"testing"
6+
)
7+
8+
func Test_Publish_S0001_PublishOK(t *testing.T) {
9+
client, testCaseTeardown, err := scenarios.TestCaseSetup(addr, nil)
10+
if err != nil {
11+
t.Errorf("want ok, got %v", err)
12+
return
13+
}
14+
defer testCaseTeardown()
15+
16+
//TBD:xxx
17+
_ = client
18+
_ = err
19+
}
20+
21+
func Test_Publish_S0002_PublishFail(t *testing.T) {
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package publish
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
mqtt "github.com/eclipse/paho.mqtt.golang"
10+
)
11+
12+
var addr string
13+
14+
func init() {
15+
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
16+
}
17+
18+
func TestMain(m *testing.M) {
19+
flag.Parse()
20+
21+
// setup for this scenario
22+
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
23+
/*
24+
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
25+
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
26+
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
27+
*/
28+
29+
// run this scenario test
30+
r := m.Run()
31+
32+
// teardown for this scenario
33+
// tbd if teardown is needed
34+
35+
os.Exit(r)
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scenarios
2+
3+
import (
4+
mqtt "github.com/eclipse/paho.mqtt.golang"
5+
)
6+
7+
func TestCaseSetup(addr string, opts *mqtt.ClientOptions) (client mqtt.Client, testCaseTeardown func(), err error) {
8+
if opts == nil {
9+
opts = mqtt.NewClientOptions()
10+
}
11+
opts.AddBroker("tcp://" + addr)
12+
13+
// Create and start MQTT client
14+
client = mqtt.NewClient(opts)
15+
token := client.Connect()
16+
token.Wait()
17+
18+
err = token.Error()
19+
testCaseTeardown = func() {
20+
client.Disconnect(0)
21+
}
22+
return
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package subscribe
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
mqtt "github.com/eclipse/paho.mqtt.golang"
10+
)
11+
12+
var addr string
13+
14+
func init() {
15+
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
16+
}
17+
18+
func TestMain(m *testing.M) {
19+
flag.Parse()
20+
21+
// setup for this scenario
22+
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
23+
/*
24+
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
25+
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
26+
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
27+
*/
28+
29+
// run this scenario test
30+
r := m.Run()
31+
32+
// teardown for this scenario
33+
// tbd if teardown is needed
34+
35+
os.Exit(r)
36+
}

0 commit comments

Comments
 (0)