diff --git a/.travis.yml b/.travis.yml index 99f3b96..cf6c5c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: go +services: + - docker + jobs: include: @@ -8,12 +11,18 @@ jobs: os: linux install: - go get github.com/golang/lint/golint + - go get github.com/onsi/ginkgo/ginkgo + - go get github.com/modocache/gover - go get github.com/mattn/goveralls before_script: - go vet ./... - test -z "$(gofmt -s -l . 2>&1 | grep -v vendor | tee /dev/stderr)" - golint -set_exit_status $(go list ./...) - script: goveralls -service=travis-ci + - docker-compose up -d + script: + - ginkgo -r -cover -coverpkg=./... -race -- -full + - gover + - goveralls -coverprofile=gover.coverprofile -service=travis-ci - &test stage: test alternative environments diff --git a/casper_test.go b/casper_test.go new file mode 100644 index 0000000..5d4c54f --- /dev/null +++ b/casper_test.go @@ -0,0 +1,6 @@ +package casper + +import "flag" + +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") diff --git a/cmd/casper/main_test.go b/cmd/casper/main_test.go index 25678e9..70ba254 100644 --- a/cmd/casper/main_test.go +++ b/cmd/casper/main_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "flag" "fmt" "io" "io/ioutil" @@ -9,8 +10,15 @@ import ( "path/filepath" "strings" "testing" + + "github.com/miracl/casper/storage/consul" ) +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") + +var consulAddr = flag.String("consul-addr", "http://172.17.0.1:8500/?token=the_one_ring", "Consul instance to run tests agains") + func TestExample(t *testing.T) { wd, err := os.Getwd() if err != nil { @@ -113,6 +121,94 @@ func TestExample(t *testing.T) { } } +func TestConsulIntegration(t *testing.T) { + if !*full { + t.SkipNow() + } + + // Cleanup + defer func() { + s, err := consul.New(*consulAddr) + if err != nil { + t.Fatalf("cleanup failed: %v", err) + } + + c, err := s.GetChanges([]byte{}, "yaml", "") + if err != nil { + t.Fatalf("cleanup failed: %v", err) + } + + err = s.Push(c) + if err != nil { + t.Fatalf("cleanup failed: %v", err) + } + }() + + steps := []struct { + cmd string + exp string + }{ + { + cmd: fmt.Sprintf("casper fetch -format yaml -storage consul -consul-addr %v", *consulAddr), + exp: "{}\n\n", + }, + + { + cmd: "casper diff -plain" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=val1 -s placeholder2=val2", + exp: "+key1=val1\n+key2=val2\n\n", + }, + { + cmd: "casper push -plain -force" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=val1 -s placeholder2=val2", + exp: "+key1=val1\n+key2=val2\n\nApplying changes...\n", + }, + { + cmd: "casper diff -plain" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=val1 -s placeholder2=val2", + exp: "No changes\n", + }, + { + cmd: "casper fetch -format yaml -storage consul -consul-addr " + *consulAddr, + exp: "key1: val1\nkey2: val2\n\n", + }, + + { + cmd: "casper diff -plain" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=diffval1 -s placeholder2=diffval2", + exp: "-key1=val1\n+key1=diffval1\n-key2=val2\n+key2=diffval2\n\n", + }, + { + cmd: "casper push -plain -force" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=diffval1 -s placeholder2=diffval2", + exp: "-key1=val1\n+key1=diffval1\n-key2=val2\n+key2=diffval2\n\nApplying changes...\n", + }, + { + cmd: "casper diff -plain" + + " -storage consul -consul-addr " + *consulAddr + + " -template ../../example/template.yaml -s placeholder1=diffval1 -s placeholder2=diffval2", + exp: "No changes\n", + }, + { + cmd: "casper fetch -format yaml -storage consul -consul-addr " + *consulAddr, + exp: "key1: diffval1\nkey2: diffval2\n\n", + }, + } + + for i, step := range steps { + os.Args = strings.Split(step.cmd, " ") + out := getStdout(t, main) + if out != step.exp { + t.Errorf("\nstep%v:/$ %v\n%v;\nExpected:\n%v;", i, step.cmd, out, step.exp) + } + } +} + func TestAppErrors(t *testing.T) { cases := []struct { cmd string diff --git a/consul/consul_test.go b/consul/consul_test.go index 2350923..2dbace8 100644 --- a/consul/consul_test.go +++ b/consul/consul_test.go @@ -2,6 +2,7 @@ package consul import ( "encoding/json" + "flag" "fmt" "reflect" "testing" @@ -9,6 +10,9 @@ import ( "github.com/hashicorp/consul/api" ) +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") + func TestConsulToMap(t *testing.T) { testCases := []struct { pairs api.KVPairs diff --git a/diff/diff_test.go b/diff/diff_test.go new file mode 100644 index 0000000..7b9cf93 --- /dev/null +++ b/diff/diff_test.go @@ -0,0 +1,6 @@ +package diff + +import "flag" + +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..720cb1c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +# Compose file used for the integration test suite +version: '3' + +services: + + consul: + image: consul + command: agent -server -bind=127.0.0.1 -client=0.0.0.0 + ports: + - "8400:8400" + - "8500:8500/tcp" + - "8600:53/udp" + environment: + - CONSUL_LOCAL_CONFIG={"acl_datacenter":"dc1","acl_default_policy":"deny","acl_down_policy":"extend-cache","acl_master_token":"the_one_ring","bootstrap_expect":1,"datacenter":"dc1","data_dir":"/usr/local/bin/consul.d/data","server":true} diff --git a/source/source_test.go b/source/source_test.go new file mode 100644 index 0000000..b9f62b8 --- /dev/null +++ b/source/source_test.go @@ -0,0 +1,6 @@ +package source + +import "flag" + +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") diff --git a/storage/consul/consul_test.go b/storage/consul/consul_test.go index 741349c..b0ac5fd 100644 --- a/storage/consul/consul_test.go +++ b/storage/consul/consul_test.go @@ -3,6 +3,7 @@ package consul import ( "bytes" "errors" + "flag" "fmt" "sort" "strings" @@ -12,6 +13,9 @@ import ( "github.com/miracl/casper/diff" ) +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") + func TestNewConsulStorage(t *testing.T) { testCases := []struct { addr string diff --git a/storage/file/file_test.go b/storage/file/file_test.go index fdc84dd..63e46b3 100644 --- a/storage/file/file_test.go +++ b/storage/file/file_test.go @@ -1,12 +1,16 @@ package file import ( + "flag" "fmt" "io/ioutil" "os" "testing" ) +// It is defined in each package so you can run `go test ./...` +var full = flag.Bool("full", false, "Run all tests including integration") + func TestFileStorageString(t *testing.T) { testCases := []struct { data string