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 <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Apr 22, 2017
1 parent 789a0d9 commit 7f679c4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
25 changes: 21 additions & 4 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,25 +44,34 @@ 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: build build-mirror
@test/integration/test.sh

gen-changelog:
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
1 change: 1 addition & 0 deletions test/integration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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/
Expand Down
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" ]]
}
8 changes: 8 additions & 0 deletions test/integration/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ 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
}
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)
}
}

0 comments on commit 7f679c4

Please sign in to comment.