Commit 9a19551b authored by Maneschi Romain's avatar Maneschi Romain 🇫🇷

increase test coverage for testSuite package + fix some TODO

parent 9bdd5969
Pipeline #8305547 passed with stages
in 21 minutes and 1 second
......@@ -46,8 +46,6 @@ func Provision(uuid string, provisioning Model, sensorsToRegister model.Register
}
func DeProvision(uuid string) error {
//TODO this function is called twice (at stopTime and shutdownTime), the second time the instance
//is not there anymore so the error is logged. need to be fixed
if instance, ok := instances.Pop(uuid); ok {
return instance.(provisioner).DeProvision()
}
......
......@@ -8,7 +8,6 @@ import (
"lorhammer/src/orchestrator/provisioning"
"lorhammer/src/orchestrator/testType"
"lorhammer/src/tools"
"os"
"time"
)
......@@ -36,28 +35,26 @@ func (test *TestSuite) LaunchTest(consulClient tools.Consul, mqttClient tools.Mq
time.Sleep(test.StopAllLorhammerTime)
if test.StopAllLorhammerTime > 0 {
command.StopScenario(mqttClient)
if err := provisioning.DeProvision(test.Uuid); err != nil {
LOG.WithError(err).Error("Couldn't unprovision")
return nil, err
}
}
//wait until shutdown minus time we have already passed in stop (0 or negative value means no shutdown)
time.Sleep(test.ShutdownAllLorhammerTime - test.StopAllLorhammerTime)
success, errors := checkResults(check)
if test.ShutdownAllLorhammerTime > 0 {
command.ShutdownLorhammers(mqttClient)
if test.StopAllLorhammerTime > 0 || test.ShutdownAllLorhammerTime > 0 {
if err := provisioning.DeProvision(test.Uuid); err != nil {
LOG.WithError(err).Error("Couldn't unprovision")
return nil, err
}
}
if test.ShutdownAllLorhammerTime > 0 {
command.ShutdownLorhammers(mqttClient)
go func() {
//TODO add boolean in scenario to choose if we want kill also orchestrator (needed for ci)
time.Sleep(100 * time.Millisecond)
os.Exit(len(errors))
test.exiter(len(errors))
}()
}
endDate := time.Now()
......
package testSuite
import (
"errors"
"fmt"
"lorhammer/src/model"
"lorhammer/src/orchestrator/provisioning"
"lorhammer/src/tools"
"testing"
"time"
)
type testLaunch struct {
......@@ -22,6 +24,7 @@ type testLaunch struct {
needProvisioning bool
check string
deploy string
grafana tools.GrafanaClient
}
var testsLaunch = []testLaunch{
......@@ -37,6 +40,7 @@ var testsLaunch = []testLaunch{
needProvisioning: true,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: false,
......@@ -50,6 +54,7 @@ var testsLaunch = []testLaunch{
needProvisioning: true,
check: `{"type": "none"}`,
deploy: `{"type": "fake"}`,
grafana: nil,
},
{
testValid: false,
......@@ -63,6 +68,7 @@ var testsLaunch = []testLaunch{
needProvisioning: true,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: true,
......@@ -76,6 +82,7 @@ var testsLaunch = []testLaunch{
needProvisioning: true,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: false,
......@@ -89,6 +96,7 @@ var testsLaunch = []testLaunch{
needProvisioning: false,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: true,
......@@ -102,6 +110,7 @@ var testsLaunch = []testLaunch{
needProvisioning: false,
check: `{"type": "prometheus", "config": [{"query": "sum(lorhammer_long_request) + sum(lorhammer_durations_count)", "resultMin": 1, "resultMax": 1, "description": "nb messages"}]}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: true,
......@@ -115,6 +124,7 @@ var testsLaunch = []testLaunch{
needProvisioning: false,
check: `{"type": "prometheus", "config": [{"query": "sum(lorhammer_long_request) + sum(lorhammer_durations_count)", "resultMin": 0, "resultMax": 0, "description": "nb messages"}]}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: false,
......@@ -128,6 +138,21 @@ var testsLaunch = []testLaunch{
needProvisioning: false,
check: `{"type": "fake"}`,
deploy: `{"type": "none"}`,
grafana: nil,
},
{
testValid: true,
description: "Grafana error should not be reported",
test: `{"type": "oneShot", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "1ms",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
needProvisioning: true,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
grafana: fakeGrafana{err: errors.New("error grafana")},
},
}
......@@ -147,15 +172,16 @@ func TestLaunchTest(t *testing.T) {
if test.needProvisioning {
provisioning.Provision(tests[0].Uuid, tests[0].Provisioning, model.Register{})
}
report, err := tests[0].LaunchTest(fakeConsul{}, &fakeMqtt{}, nil)
tests[0].exiter = func(code int) {}
report, err := tests[0].LaunchTest(fakeConsul{}, &fakeMqtt{}, ct.grafana)
if ct.testValid && err != nil {
t.Fatal("valid test should not throw err")
t.Fatalf("valid test should not throw err %s", ct.description)
} else if ct.testValid && report == nil {
t.Fatal("valid test should return report")
t.Fatalf("valid test should return report %s", ct.description)
} else if !ct.testValid && err == nil {
t.Fatal("not valid test should throw err")
t.Fatalf("not valid test should throw err %s", ct.description)
} else if !ct.testValid && report != nil {
t.Fatal("not valid test should not return report")
t.Fatalf("not valid test should not return report %s", ct.description)
}
}
......@@ -182,3 +208,11 @@ func (f fakeConsul) ServiceFirst(name string, prefix string) (string, error) {
}
func (_ fakeConsul) DeRegister(string) error { return nil }
func (_ fakeConsul) AllServices() ([]tools.ConsulService, error) { return nil, nil }
type fakeGrafana struct {
err error
}
func (g fakeGrafana) MakeSnapshot(startTime time.Time, endTime time.Time) (string, error) {
return "", g.err
}
......@@ -64,3 +64,25 @@ func TestMultipleEntry(t *testing.T) {
t.Fatal("Bad formatted report")
}
}
func TestNilData(t *testing.T) {
err := WriteFile(nil, "/")
if err == nil {
t.Fatal("Nil report should throw an error")
}
}
func TestNotAuthorizedFilepath(t *testing.T) {
err := WriteFile(&TestReport{
StartDate: time.Now(),
EndDate: time.Now().Add(1 * time.Minute),
Input: nil,
ChecksSuccess: nil,
ChecksError: nil,
GrafanaSnapshotUrl: "",
}, "/")
if err == nil {
t.Fatal("/ filepath can't be written, WriteFile should return an error")
}
}
......@@ -8,6 +8,7 @@ import (
"lorhammer/src/orchestrator/deploy"
"lorhammer/src/orchestrator/provisioning"
"lorhammer/src/orchestrator/testType"
"os"
"time"
)
......@@ -22,6 +23,7 @@ type TestSuite struct {
Check checker.Model `json:"check"`
Provisioning provisioning.Model `json:"provisioning"`
Deploy deploy.Model `json:"deploy"`
exiter func(code int)
}
type jsonTestSuite struct {
......@@ -64,6 +66,7 @@ func FromFile(configFile []byte) ([]TestSuite, error) {
Check: test.Check,
Provisioning: test.Provisioning,
Deploy: test.Deploy,
exiter: os.Exit,
}
}
return res, nil
......
......@@ -6,112 +6,124 @@ import (
)
type testFromFile struct {
description string
testValid bool
test string
rampTime string
repeatTime string
stopAll string
shutdownAll string
sleep string
init string
provisioning string
prometheusCheck string
deploy string
description string
testValid bool
test string
rampTime string
repeatTime string
stopAll string
shutdownAll string
sleep string
init string
provisioning string
check string
deploy string
}
var testsFromFile = []testFromFile{
{
testValid: true,
description: "First simple valid test",
test: `{"type": "oneShot", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: true,
description: "First simple valid test",
test: `{"type": "oneShot", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: true,
description: "Test with good repeatTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "1m"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: true,
description: "Test with good repeatTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "1m"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because empty testType",
test: "",
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: false,
description: "Invalid test because empty testType",
test: "",
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because empty rampTime",
test: `{"type": "ramp", "rampTime": "", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: false,
description: "Invalid test because empty rampTime",
test: `{"type": "ramp", "rampTime": "", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because invalid repeatTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "not good"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: false,
description: "Invalid test because invalid repeatTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "not good"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because invalid stopAllTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "a",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: false,
description: "Invalid test because invalid stopAllTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "a",
shutdownAll: "0",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because invalid shutdownAllTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "a",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
prometheusCheck: `[]`,
deploy: `{"type": "none"}`,
testValid: false,
description: "Invalid test because invalid shutdownAllTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "a",
sleep: "0",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
{
testValid: false,
description: "Invalid test because invalid sleepAtEndTime",
test: `{"type": "repeat", "rampTime": "0", "repeatTime": "0"}`,
stopAll: "0",
shutdownAll: "0",
sleep: "a",
init: `{"nsAddress": "127.0.0.1:1700","nbGateway": 1,"nbNodePerGateway": [1, 1],"sleepTime": [100, 500]}`,
provisioning: `{"type": "none"}`,
check: `{"type": "none"}`,
deploy: `{"type": "none"}`,
},
}
var templateFromFile = `[{"test": %s,"rampTime": "%s","repeatTime": "%s","stopAllLorhammerTime": "%s","shutdownAllLorhammerTime": "%s","sleepAtEndTime": "%s","init": %s,"provisioning": %s,"prometheusCheck": %s, "deploy": %s}]`
var templateFromFile = `[{"test": %s,"rampTime": "%s","repeatTime": "%s","stopAllLorhammerTime": "%s","shutdownAllLorhammerTime": "%s","sleepAtEndTime": "%s","init": %s,"provisioning": %s,"check": %s, "deploy": %s}]`
func TestTransformFile(t *testing.T) {
for _, ct := range testsFromFile {
data := []byte(fmt.Sprintf(templateFromFile, ct.test, ct.rampTime, ct.repeatTime, ct.stopAll, ct.shutdownAll, ct.sleep, ct.init, ct.provisioning, ct.prometheusCheck, ct.deploy))
data := []byte(fmt.Sprintf(templateFromFile, ct.test, ct.rampTime, ct.repeatTime, ct.stopAll, ct.shutdownAll, ct.sleep, ct.init, ct.provisioning, ct.check, ct.deploy))
tests, err := FromFile(data)
if ct.testValid {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment