Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run multiple tests configs #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ start-kapacitor:
docker-compose -f infra/docker-compose.yml up -d

sample1:
go install .
kapacitor-unit -dir ./sample/ -tests ./sample/test_case.yaml
go run kapacitorunit.go -dir ./sample/tick_scripts -tests ./sample/test_cases/test_case.yaml

sample1_debug:
go install .
kapacitor-unit -dir ./sample/ -tests ./sample/test_case.yaml -stderrthreshold=INFO
go run kapacitorunit.go -dir ./sample/tick_scripts -tests ./sample/test_cases/test_case.yaml -stderrthreshold=INFO

sample1_batch:
go install .
kapacitor-unit -dir ./sample/ -tests ./sample/test_case_batch.yaml
go run kapacitorunit.go -dir ./sample/tick_scripts -tests ./sample/test_cases/test_case_batch.yaml

sample1_batch_debug:
go install .
kapacitor-unit -dir ./sample/ -tests ./sample/test_case_batch.yaml -stderrthreshold=INFO
go run kapacitorunit.go -dir ./sample/tick_scripts -tests ./sample/test_cases/test_case_batch.yaml -stderrthreshold=INFO

sample_dir:
go run kapacitorunit.go -dir ./sample/tick_scripts -tests ./sample/test_cases
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Load() *Config {
}

if *scriptsDir == "" {
log.Fatal("ERROR: Path for where TICKscipts directory (--dir) must be defined")
log.Fatal("ERROR: Path for where TICKscripts directory (--dir) must be defined")
}

config := Config{*testsPath, *scriptsDir, *influxdbHost, *kapacitorHost}
Expand Down
81 changes: 62 additions & 19 deletions kapacitorunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)

//Structure that stored tests configuration
type C struct {
Tests []test.Test
}
type TestCollection []test.Test

func main() {
fmt.Println(renderWelcome())
Expand All @@ -25,17 +24,17 @@ func main() {
kapacitor := io.NewKapacitor(f.KapacitorHost)
influxdb := io.NewInfluxdb(f.InfluxdbHost)

c, err := testConfig(f.TestsPath)
tests, err := testConfig(f.TestsPath)
if err != nil {
log.Fatal("Test configuration parse failed")
log.Fatal("Error loading test configurations: ", err)
}
err = initTests(c, f.ScriptsDir)
err = initTests(tests, f.ScriptsDir)
if err != nil {
log.Fatal("Init Tests failed: ", err)
}

// Validates, runs tests in series and print results
for _, t := range c.Tests {
for _, t := range tests {
if err := t.Validate(); err != nil {
log.Println(err)
continue
Expand All @@ -48,7 +47,7 @@ func main() {
// Runs test
err = t.Run(kapacitor, influxdb)
if err != nil {
log.Println(err)
log.Println("Error running test: ", t, " Error: ", err)
continue
}
//Prints test output
Expand All @@ -67,28 +66,72 @@ func setColor(t test.Test) {
}
}

//Opens and parses test configuration file into a structure
func testConfig(p string) (*C, error) {
c, err := ioutil.ReadFile(p)
func loadYamlFile(fileName string) (TestCollection, error) {

type conf struct {
Tests TestCollection
}

b, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
cmap := C{}
err = yaml.Unmarshal([]byte(c), &cmap)

c := conf{}

err = yaml.Unmarshal(b, &c)

if err != nil {
return nil, err
}

return c.Tests, err

}

//Opens and parses test configuration file into a structure
func testConfig(fileName string) (TestCollection, error) {

stat, err := os.Stat(fileName)
if err != nil {
return &cmap, err
return nil, err
}
return &cmap, nil

files := make([]string, 0)

if stat.IsDir() {
filepath.Walk(fileName, func(path string, info os.FileInfo, err error) error {
if ext := filepath.Ext(path); ext == ".yml" || ext == ".yaml" {
files = append(files, path)
}
return nil
})

} else {
files = append(files, fileName)
}

tests := make(TestCollection, 0)

for _, file := range files {
fileTests, err := loadYamlFile(file)
if err != nil {
return nil, err
}
tests = append(tests, fileTests...)
}

return tests, nil
}

//Populates each of Test in Configuration struct with an initialized Task
func initTests(c *C, p string) error {
for i, t := range c.Tests {
func initTests(c TestCollection, p string) error {
for i, t := range c {
tk, err := task.New(t.TaskName, p)
if err != nil {
return err
}
c.Tests[i].Task = *tk
c[i].Task = *tk
}
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions kapacitorunit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ tests:
`
defer os.Remove(p)
createConfFile(p, c)
cmap, err := testConfig(p)
tests, err := testConfig(p)
if err != nil {
t.Error(err)
}

if cmap.Tests[0].Name != "test1" {
if tests[0].Name != "test1" {
t.Error("Test name not parsed as expected")
}
if cmap.Tests[0].Data[1] != "data 2" {
if tests[0].Data[1] != "data 2" {
t.Error("Data not parsed as expected")
}
//if cmap.Tests[1].Expects != "critical" {
Expand Down Expand Up @@ -115,17 +115,17 @@ tests:

defer os.Remove(p)
createConfFile(p, c)
cmap, err := testConfig(p)
tests, err := testConfig(p)
if err != nil {
t.Error(err)
}

err = initTests(cmap, "./sample/")
err = initTests(tests, "./sample/tick_scripts")
if err != nil {
t.Error(err)
}

if cmap.Tests[0].Task.Name != "alert_2.tick" {
t.Error(cmap.Tests[0].Task.Name)
if tests[0].Task.Name != "alert_2.tick" {
t.Error(tests[0].Task.Name)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.