diff --git a/automated-testing/.DS_Store b/automated-testing/.DS_Store new file mode 100644 index 00000000..9d534fa8 Binary files /dev/null and b/automated-testing/.DS_Store differ diff --git a/automated-testing/Makefile b/automated-testing/Makefile new file mode 100644 index 00000000..ac0123f2 --- /dev/null +++ b/automated-testing/Makefile @@ -0,0 +1,9 @@ +broker_addr=localhost:30083 + +all: + go test ./... -addr $(broker_addr) -json|go-test-report + +clean: + rm -fr test_report.html + + diff --git a/automated-testing/go.mod b/automated-testing/go.mod new file mode 100644 index 00000000..dcbe7130 --- /dev/null +++ b/automated-testing/go.mod @@ -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 +) diff --git a/automated-testing/go.sum b/automated-testing/go.sum new file mode 100644 index 00000000..e7f9a64d --- /dev/null +++ b/automated-testing/go.sum @@ -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= diff --git a/automated-testing/scenarios/connection/connect_test.go b/automated-testing/scenarios/connection/connect_test.go new file mode 100644 index 00000000..14767352 --- /dev/null +++ b/automated-testing/scenarios/connection/connect_test.go @@ -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) { + //... ... +} diff --git a/automated-testing/scenarios/connection/scenario_test.go b/automated-testing/scenarios/connection/scenario_test.go new file mode 100644 index 00000000..b6cc5de5 --- /dev/null +++ b/automated-testing/scenarios/connection/scenario_test.go @@ -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) +} diff --git a/automated-testing/scenarios/publish/publish_test.go b/automated-testing/scenarios/publish/publish_test.go new file mode 100644 index 00000000..4e439d88 --- /dev/null +++ b/automated-testing/scenarios/publish/publish_test.go @@ -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) { +} diff --git a/automated-testing/scenarios/publish/scenario_test.go b/automated-testing/scenarios/publish/scenario_test.go new file mode 100644 index 00000000..8cd6e116 --- /dev/null +++ b/automated-testing/scenarios/publish/scenario_test.go @@ -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) +} diff --git a/automated-testing/scenarios/scenarios.go b/automated-testing/scenarios/scenarios.go new file mode 100644 index 00000000..bd8ab5f0 --- /dev/null +++ b/automated-testing/scenarios/scenarios.go @@ -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 +} diff --git a/automated-testing/scenarios/subscribe/scenario_test.go b/automated-testing/scenarios/subscribe/scenario_test.go new file mode 100644 index 00000000..69b659e8 --- /dev/null +++ b/automated-testing/scenarios/subscribe/scenario_test.go @@ -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) +} diff --git a/automated-testing/scenarios/subscribe/subscribe_test.go b/automated-testing/scenarios/subscribe/subscribe_test.go new file mode 100644 index 00000000..627cd534 --- /dev/null +++ b/automated-testing/scenarios/subscribe/subscribe_test.go @@ -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) { +} diff --git a/automated-testing/test_report.html b/automated-testing/test_report.html new file mode 100644 index 00000000..fba65bd0 --- /dev/null +++ b/automated-testing/test_report.html @@ -0,0 +1,468 @@ + + +
+ +