Skip to content

Commit

Permalink
Merge pull request #136 from rafecolton/fix-server-failure-if-no-basi…
Browse files Browse the repository at this point in the history
…c-auth-provided

Fix bug where server cannot be started w/o basic auth
  • Loading branch information
Rafe Colton committed Oct 6, 2014
2 parents 469d1b7 + 1b8d51c commit 14e2f62
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BATS_OUT_FORMAT=$(shell bash -c "echo $${CI+--tap}")
GOPATH := $(shell echo $${GOPATH%%:*})

# go build args
GO_TAG_ARGS := -tags netgo
GO_TAG_ARGS ?= -tags netgo

export GOPATH

Expand Down Expand Up @@ -68,8 +68,13 @@ gox-build: get $(GOPATH)/bin/gox
install-ginkgo:
go get -u github.com/onsi/ginkgo/ginkgo

.PHONY: .test
.test: fmtpolice ginkgo bats

.PHONY: test
test: build fmtpolice ginkgo bats
test:
@GO_TAG_ARGS="-tags netgo -tags integration" $(MAKE) build
@$(MAKE) .test

.PHONY: fmtpolice
fmtpolice: fmt lint
Expand Down
71 changes: 71 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main_test

import (
"os"
"os/exec"
"syscall"
"testing"
"time"
)

func TestServer(t *testing.T) {
cmd := exec.Cmd{
Path: os.Getenv("GOPATH") + "/bin/docker-builder",
Args: []string{"docker-builder", "serve"},
}
if err := cmd.Start(); err != nil {
t.Errorf("error start server: %s\n", err.Error())
}

go func() {
time.Sleep(100 * time.Millisecond)
syscall.Kill(cmd.Process.Pid, syscall.SIGUSR1)
time.Sleep(100 * time.Millisecond)
syscall.Kill(cmd.Process.Pid, syscall.SIGTERM)
}()

if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() != 166 {
t.Errorf("server exited unexpectedly and/or did not start correctly")
}
} else {
t.Errorf("unable to get process exit code")
}
} else {
t.Errorf("error waiting on server: %s\n", err.Error())
}
}
}

func TestServerWithBasicAuth(t *testing.T) {
cmd := exec.Cmd{
Path: os.Getenv("GOPATH") + "/bin/docker-builder",
Args: []string{"docker-builder", "serve", "--username", "foo", "--password", "bar"},
}
if err := cmd.Start(); err != nil {
t.Errorf("error start server: %s\n", err.Error())
}

go func() {
time.Sleep(100 * time.Millisecond)
syscall.Kill(cmd.Process.Pid, syscall.SIGUSR1)
time.Sleep(100 * time.Millisecond)
syscall.Kill(cmd.Process.Pid, syscall.SIGTERM)
}()

if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() != 166 {
t.Errorf("server exited unexpectedly and/or did not start correctly")
}
} else {
t.Errorf("unable to get process exit code")
}
} else {
t.Errorf("error waiting on server: %s\n", err.Error())
}
}
}
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func Serve(context *cli.Context) {
// set up auth functions
if shouldBasicAuth {
basicAuthFunc = auth.Basic(un, pwd)
} else {
basicAuthFunc = func(http.ResponseWriter, *http.Request) {}
}
if shouldTravisAuth {
travisAuthFunc = vauth.TravisCI(travisToken)
Expand Down
20 changes: 20 additions & 0 deletions test_signal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// +build integration

package main

import (
"os"
"os/signal"
"syscall"

"github.com/onsi/gocleanup"
)

func init() {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
<-c
gocleanup.Exit(166)
}()
}

0 comments on commit 14e2f62

Please sign in to comment.