diff --git a/Makefile b/Makefile index 0f857fe..b0fa7a8 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/cli/cli.go b/cli/cli.go index 1a559a3..a07de0d 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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} diff --git a/kapacitorunit.go b/kapacitorunit.go index 9237b47..2f9e40e 100644 --- a/kapacitorunit.go +++ b/kapacitorunit.go @@ -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()) @@ -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 @@ -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 @@ -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 } diff --git a/kapacitorunit_test.go b/kapacitorunit_test.go index 24fc7b4..3ee2a08 100644 --- a/kapacitorunit_test.go +++ b/kapacitorunit_test.go @@ -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" { @@ -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) } } diff --git a/sample/test_case.yaml b/sample/test_cases/test_case.yaml similarity index 100% rename from sample/test_case.yaml rename to sample/test_cases/test_case.yaml diff --git a/sample/test_case_batch.yaml b/sample/test_cases/test_case_batch.yaml similarity index 100% rename from sample/test_case_batch.yaml rename to sample/test_cases/test_case_batch.yaml diff --git a/sample/alert_2.tick b/sample/tick_scripts/alert_2.tick similarity index 100% rename from sample/alert_2.tick rename to sample/tick_scripts/alert_2.tick diff --git a/sample/alert_weather.tick b/sample/tick_scripts/alert_weather.tick similarity index 100% rename from sample/alert_weather.tick rename to sample/tick_scripts/alert_weather.tick diff --git a/sample/alert_weather_batch.tick b/sample/tick_scripts/alert_weather_batch.tick similarity index 100% rename from sample/alert_weather_batch.tick rename to sample/tick_scripts/alert_weather_batch.tick