Skip to content

Commit

Permalink
Better example verification (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbszczepaniak authored and roblaszczak committed Aug 7, 2019
1 parent e46bea5 commit 95161a2
Show file tree
Hide file tree
Showing 34 changed files with 188 additions and 235 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_reconnect:

validate_examples:
go run dev/update-examples-deps/main.go
bash dev/validate_examples.sh
go run dev/validate-examples/main.go


fmt:
Expand Down
8 changes: 0 additions & 8 deletions _examples/cqrs-protobuf/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/cqrs-protobuf/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 30
expected_output: "beers ordered to room 3"
8 changes: 0 additions & 8 deletions _examples/http-to-kafka/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/http-to-kafka/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 30
expected_output: "Starting handler"
8 changes: 0 additions & 8 deletions _examples/kafka-to-http/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/kafka-to-http/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 60
expected_output: "POST /foo_or_bar: message"
8 changes: 0 additions & 8 deletions _examples/metrics/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/metrics/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 60
expected_output: "msg=\"Message acked\""
8 changes: 0 additions & 8 deletions _examples/simple-app/.validate_example_publishing.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/simple-app/.validate_example_publishing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 60
expected_output: "msg=\"Message sent to Kafka\""
8 changes: 0 additions & 8 deletions _examples/simple-app/.validate_example_subscribing.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/simple-app/.validate_example_subscribing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 60
expected_output: "msg=\"Starting handler\""
8 changes: 0 additions & 8 deletions _examples/your-first-app/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions _examples/your-first-app/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 60
expected_output: "received event [0-9]+"
5 changes: 5 additions & 0 deletions dev/validate-examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/ThreeDotsLabs/watermill/dev/validate-examples

go 1.12

require gopkg.in/yaml.v2 v2.2.2 // indirect
3 changes: 3 additions & 0 deletions dev/validate-examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
122 changes: 122 additions & 0 deletions dev/validate-examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

yaml "gopkg.in/yaml.v2"
)

type Config struct {
ValidationCmd string `yaml:"validation_cmd"`
TeardownCmd string `yaml:"teardown_cmd"`
Timeout int `yaml:"timeout"`
ExpectedOutput string `yaml:"expected_output"`
}

func (c *Config) LoadFrom(path string) error {
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(file, c)
if err != nil {
return err
}
return nil
}

func main() {
walkErr := filepath.Walk(".", func(exampleConfig string, f os.FileInfo, _ error) error {
matches, _ := filepath.Match(".validate_example*.yml", f.Name())
if !matches {
return nil
}

exampleDirectory := filepath.Dir(exampleConfig)
ok, err := validate(exampleConfig)

if err != nil {
fmt.Printf("validation for %s failed, err: %v\n", exampleDirectory, err)
return nil
}

if ok {
fmt.Printf("validation for %s succeeded\n", exampleDirectory)
return nil
}

fmt.Printf("validation for %s failed\n", exampleDirectory)
return nil
})
if walkErr != nil {
panic(walkErr)
}

}

func validate(path string) (bool, error) {
config := &Config{}
err := config.LoadFrom(path)
if err != nil {
return false, fmt.Errorf("could not load config, err: %v", err)
}

cmdAndArgs := strings.Fields(config.ValidationCmd)
validationCmd := exec.Command(cmdAndArgs[0], cmdAndArgs[1:]...)
validationCmd.Dir = filepath.Dir(path)
defer func() {
if config.TeardownCmd == "" {
return
}
cmdAndArgs := strings.Fields(config.TeardownCmd)
teardownCmd := exec.Command(cmdAndArgs[0], cmdAndArgs[1:]...)
teardownCmd.Dir = filepath.Dir(path)
_ = teardownCmd.Run()
}()

stdout, err := validationCmd.StdoutPipe()
if err != nil {
return false, fmt.Errorf("could not attach to stdout, err: %v", err)
}

err = validationCmd.Start()
if err != nil {
return false, fmt.Errorf("could not start validation, err: %v", err)
}

success := make(chan bool)

go func() {
output := bufio.NewReader(stdout)
for {
line, _, err := output.ReadLine()
if err != nil {
if err == io.EOF {
break
}
}
ok, _ := regexp.Match(config.ExpectedOutput, line)
if ok {
success <- true
return
}
}
success <- false
}()

select {
case success := <-success:
return success, nil
case <-time.After(time.Duration(config.Timeout) * time.Second):
return false, fmt.Errorf("validation command timed out")
}
}
122 changes: 0 additions & 122 deletions dev/validate_examples.sh

This file was deleted.

8 changes: 0 additions & 8 deletions docs/content/docs/getting-started/amqp/.validate_example.sh

This file was deleted.

4 changes: 4 additions & 0 deletions docs/content/docs/getting-started/amqp/.validate_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validation_cmd: "docker-compose up"
teardown_cmd: "docker-compose down"
timeout: 30
expected_output: "received message: [0-9a-f\\-]+, payload: Hello, world!"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validation_cmd: "docker-compose up"
timeout: 1
expected_output: "payload: Hello, world!"

This file was deleted.

Loading

0 comments on commit 95161a2

Please sign in to comment.