Skip to content

Commit

Permalink
Removing integration test dependency on internet access
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Apr 22, 2017
1 parent 789a0d9 commit 022e4dd
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 11 deletions.
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ define gocross
-o $(PREFIX)/bin/$(PKG_NAME)_$(1)-$(2)$(call extension,$(1));
endef

define gocross-tool
GOOS=$(1) GOARCH=$(2) \
$(GO) build \
-ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \
-o $(PREFIX)/bin/$(3)_$(1)-$(2)$(call extension,$(1)) \
$(PREFIX)/test/integration/$(3)svc;
endef

define compress
upx $(PREFIX)/bin/$(PKG_NAME)_$(1)-$(2)$(call extension,$(1)) \
-o $(PREFIX)/bin/$(PKG_NAME)_$(1)-$(2)-slim$(call extension,$(1))
Expand All @@ -36,26 +44,35 @@ compress-all:

build-release: clean build-x compress-all

$(PREFIX)/bin/$(PKG_NAME)_%: $(shell find . -type f -name '*.go')
$(PREFIX)/bin/$(PKG_NAME)_%: $(shell find $(PREFIX) -type f -name '*.go' -not -path "$(PREFIX)/test/*")
$(call gocross,$(shell echo $* | sed 's/\([^-]*\)-\([^.]*\).*/\1/'),$(shell echo $* | sed 's/\([^-]*\)-\([^.]*\).*/\2/'))

$(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS)): $(shell find . -type f -name '*.go')
$(PREFIX)/bin/mirror_%: $(shell find $(PREFIX)/test/integration/mirrorsvc -type f -name '*.go')
$(call gocross-tool,$(shell echo $* | sed 's/\([^-]*\)-\([^.]*\).*/\1/'),$(shell echo $* | sed 's/\([^-]*\)-\([^.]*\).*/\2/'),mirror)

$(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS)): $(shell find $(PREFIX) -type f -name '*.go' -not -path "$(PREFIX)/test/*")
$(GO) build -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" -o $@

$(PREFIX)/bin/mirror$(call extension,$(GOOS)): $(shell find $(PREFIX)/test/integration/mirrorsvc -type f -name '*.go')
$(GO) build -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" -o $@ $(PREFIX)/test/integration/mirrorsvc

build: $(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS))

build-mirror: $(PREFIX)/bin/mirror$(call extension,$(GOOS))

test:
$(GO) test -v -race `glide novendor`

build-integration-image: $(PREFIX)/bin/$(PKG_NAME)_linux-amd64$(call extension,$(GOOS))
build-integration-image: $(PREFIX)/bin/$(PKG_NAME)_linux-amd64$(call extension,$(GOOS)) $(PREFIX)/bin/mirror_linux-amd64$(call extension,$(GOOS))
cp $(PREFIX)/bin/$(PKG_NAME)_linux-amd64 test/integration/gomplate
cp $(PREFIX)/bin/mirror_linux-amd64 test/integration/mirror
docker build -f test/integration/Dockerfile -t gomplate-test test/integration/

test-integration-docker: build-integration-image
docker run -it --rm gomplate-test

test-integration: build
@test/integration/test.sh
test-integration: build build-mirror
@bats test/integration

gen-changelog:
github_changelog_generator --no-filter-by-milestone --exclude-labels duplicate,question,invalid,wontfix,admin
Expand Down
1 change: 1 addition & 0 deletions test/integration/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gomplate
mirror
3 changes: 2 additions & 1 deletion test/integration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ RUN mkdir /lib64 \
&& ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

COPY gomplate /bin/gomplate
COPY mirror /bin/mirror
COPY *.sh /tests/
COPY *.bash /tests/
COPY *.bats /tests/

CMD ["/tests/test.sh"]
CMD ["bats", "/tests"]
12 changes: 10 additions & 2 deletions test/integration/datasources_http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

load helper

function setup() {
start_mirror_svc
}

function teardown() {
stop_mirror_svc
}

@test "HTTP datasource with headers" {
gomplate \
-d foo=http://httpbin.org/get \
-d foo=http://127.0.0.1:8080/ \
-H foo=Foo:bar \
-i '{{ (datasource "foo").headers.Foo }}'
-i '{{ index (datasource "foo").headers.Foo 0 }}'
[ "$status" -eq 0 ]
[[ "${output}" == "bar" ]]
}
5 changes: 5 additions & 0 deletions test/integration/datasources_vault.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env bats

export VAULT_ADDR=http://127.0.0.1:8200
export VAULT_TOKEN=00000000-1111-2222-3333-444455556666

load helper

function setup () {
start_vault
cat <<EOF | vault policy-write pol - >& /dev/null
path "*" {
policy = "write"
Expand All @@ -18,6 +22,7 @@ function teardown () {
vault auth-disable approle2
vault auth-disable app-id
vault auth-disable app-id2
stop_vault
}

@test "Testing userpass vault auth" {
Expand Down
18 changes: 18 additions & 0 deletions test/integration/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ function __gomplate_stdin () {
shift 1
echo "$in" | bin/gomplate "$@"
}

function start_mirror_svc () {
bin/mirror &
}

function stop_mirror_svc () {
wget -q http://127.0.0.1:8080/quit
}

function start_vault () {
# fire up vault in dev mode for the vault tests
vault server -dev -dev-root-token-id=${VAULT_TOKEN} -log-level=err >&/dev/null &
VAULT_PID=$!
}

function stop_vault () {
kill ${VAULT_PID} || true
}
50 changes: 50 additions & 0 deletions test/integration/mirrorsvc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"encoding/json"
"flag"
"log"
"net"
"net/http"
)

// Req -
type Req struct {
Headers http.Header `json:"headers"`
}

var port string

func main() {
flag.StringVar(&port, "p", "8080", "Port to listen to")
flag.Parse()

l, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal(err)
}
// defer l.Close()
http.HandleFunc("/", rootHandler)

http.HandleFunc("/quit", quitHandler(l))

http.Serve(l, nil)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
req := Req{r.Header}
b, err := json.Marshal(req)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}

func quitHandler(l net.Listener) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
l.Close()
w.WriteHeader(http.StatusNoContent)
}
}
6 changes: 3 additions & 3 deletions test/integration/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ set -euo pipefail
# trap "kill 0" EXIT

# TODO: export these in a bats helper, as well as only launch vault in a vault helper
export VAULT_ADDR=http://127.0.0.1:8200
export VAULT_TOKEN=00000000-1111-2222-3333-444455556666
# export VAULT_ADDR=http://127.0.0.1:8200
# export VAULT_TOKEN=00000000-1111-2222-3333-444455556666

# fire up vault in dev mode for the vault tests
vault server -dev -dev-root-token-id=${VAULT_TOKEN} -log-level=err >&/dev/null &
# vault server -dev -dev-root-token-id=${VAULT_TOKEN} -log-level=err >&/dev/null &

bats $(dirname $0)

0 comments on commit 022e4dd

Please sign in to comment.