Skip to content

Commit

Permalink
Add integration test with Consul
Browse files Browse the repository at this point in the history
  • Loading branch information
VMitov committed Sep 29, 2017
1 parent 71d553c commit d93b14f
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 1 deletion.
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: go

services:
- docker

jobs:
include:

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions casper_test.go
Original file line number Diff line number Diff line change
@@ -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")
96 changes: 96 additions & 0 deletions cmd/casper/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ package main

import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package consul

import (
"encoding/json"
"flag"
"fmt"
"reflect"
"testing"

"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
Expand Down
6 changes: 6 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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}
6 changes: 6 additions & 0 deletions source/source_test.go
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 4 additions & 0 deletions storage/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"bytes"
"errors"
"flag"
"fmt"
"sort"
"strings"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions storage/file/file_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d93b14f

Please sign in to comment.