Skip to content

Commit

Permalink
add examples for automated-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bigwhite committed Mar 24, 2023
1 parent 968bd3c commit c327251
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 0 deletions.
Binary file added automated-testing/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions automated-testing/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
broker_addr=localhost:30083

all:
go test ./... -addr $(broker_addr) -json|go-test-report

clean:
rm -fr test_report.html


10 changes: 10 additions & 0 deletions automated-testing/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module bigwhite/autotester

go 1.18

require (
github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
)
14 changes: 14 additions & 0 deletions automated-testing/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed h1:ue9pVfIcP+QMEjfgo/Ez4ZjNZfonGgR6NgjMaJMu1Cg=
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
28 changes: 28 additions & 0 deletions automated-testing/scenarios/connection/connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package connection

import (
"testing"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

func Test_Connection_S0001_ConnectOKWithoutAuth(t *testing.T) {
opts := mqtt.NewClientOptions()
opts.AddBroker("tcp://" + addr)

// Create and start MQTT client
client := mqtt.NewClient(opts)
token := client.Connect()
token.Wait()

// Check if connection was successful
if token.Error() != nil {
t.Errorf("want ok, got %v", token.Error())
return
}
client.Disconnect(0)
}

func Test_Connection_S0002_ConnectOKWithAuth(t *testing.T) {
//... ...
}
36 changes: 36 additions & 0 deletions automated-testing/scenarios/connection/scenario_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package connection

import (
"flag"
"log"
"os"
"testing"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

var addr string

func init() {
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
}

func TestMain(m *testing.M) {
flag.Parse()

// setup for this scenario
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
/*
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
*/

// run this scenario test
r := m.Run()

// teardown for this scenario
// tbd if teardown is needed

os.Exit(r)
}
22 changes: 22 additions & 0 deletions automated-testing/scenarios/publish/publish_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package publish

import (
scenarios "bigwhite/autotester/scenarios"
"testing"
)

func Test_Publish_S0001_PublishOK(t *testing.T) {
client, testCaseTeardown, err := scenarios.TestCaseSetup(addr, nil)
if err != nil {
t.Errorf("want ok, got %v", err)
return
}
defer testCaseTeardown()

//TBD:xxx
_ = client
_ = err
}

func Test_Publish_S0002_PublishFail(t *testing.T) {
}
36 changes: 36 additions & 0 deletions automated-testing/scenarios/publish/scenario_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package publish

import (
"flag"
"log"
"os"
"testing"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

var addr string

func init() {
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
}

func TestMain(m *testing.M) {
flag.Parse()

// setup for this scenario
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
/*
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
*/

// run this scenario test
r := m.Run()

// teardown for this scenario
// tbd if teardown is needed

os.Exit(r)
}
23 changes: 23 additions & 0 deletions automated-testing/scenarios/scenarios.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package scenarios

import (
mqtt "github.com/eclipse/paho.mqtt.golang"
)

func TestCaseSetup(addr string, opts *mqtt.ClientOptions) (client mqtt.Client, testCaseTeardown func(), err error) {
if opts == nil {
opts = mqtt.NewClientOptions()
}
opts.AddBroker("tcp://" + addr)

// Create and start MQTT client
client = mqtt.NewClient(opts)
token := client.Connect()
token.Wait()

err = token.Error()
testCaseTeardown = func() {
client.Disconnect(0)
}
return
}
36 changes: 36 additions & 0 deletions automated-testing/scenarios/subscribe/scenario_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package subscribe

import (
"flag"
"log"
"os"
"testing"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

var addr string

func init() {
flag.StringVar(&addr, "addr", "", "the broker address(ip:port)")
}

func TestMain(m *testing.M) {
flag.Parse()

// setup for this scenario
mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
/*
mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0)
mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
*/

// run this scenario test
r := m.Run()

// teardown for this scenario
// tbd if teardown is needed

os.Exit(r)
}
62 changes: 62 additions & 0 deletions automated-testing/scenarios/subscribe/subscribe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package subscribe

import (
scenarios "bigwhite/autotester/scenarios"
"testing"
)

func Test_Subscribe_S0001_SubscribeOK(t *testing.T) {
t.Parallel() // indicate the case can be ran in parallel mode

tests := []struct {
name string
topic string
qos byte
}{
{
name: "Case_001: Subscribe with QoS 0",
topic: "a/b/c",
qos: 0,
},
{
name: "Case_002: Subscribe with QoS 1",
topic: "a/b/c",
qos: 1,
},
{
name: "Case_003: Subscribe with QoS 2",
topic: "a/b/c",
qos: 2,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel() // indicate the case can be ran in parallel mode
client, testCaseTeardown, err := scenarios.TestCaseSetup(addr, nil)
if err != nil {
t.Errorf("want ok, got %v", err)
return
}
defer testCaseTeardown()

token := client.Subscribe(tt.topic, tt.qos, nil)
token.Wait()

// Check if subscription was successful
if token.Error() != nil {
t.Errorf("want ok, got %v", token.Error())
}

token = client.Unsubscribe(tt.topic)
token.Wait()
if token.Error() != nil {
t.Errorf("want ok, got %v", token.Error())
}
})
}
}

func Test_Subscribe_S0002_SubscribeFail(t *testing.T) {
}
Loading

0 comments on commit c327251

Please sign in to comment.