Skip to content

Commit

Permalink
Change to not handle testing.TB
Browse files Browse the repository at this point in the history
  • Loading branch information
skarllot committed May 5, 2016
1 parent e3fc3a1 commit 5cfe86e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 57 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ before_script:
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi

script:
- go get -v -t ./...
- go test -v --race ./...
- goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
- test -z "$(gofmt -s -l -w . | tee /dev/stderr)"
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.PHONY: all build test benchmark test-cover test-cover-html generate list-imports
PACKAGES = $(shell find ./ -type d -not -path '*/\.*')

all: test build

build:
go build ./...

test:
go test -v ./...
test -z "`gofmt -s -l -w . | tee /dev/stderr`"
test -z "`golint ./... | grep -v ffjson | tee /dev/stderr`"
go vet ./...

benchmark:
go test -bench . -benchmem -run=^a ./... | grep "Benchmark" > bench_result.txt

test-cover:
@go test -cover `go list ./... | grep -v /vendor/` | grep "%"

test-cover-html:
echo "mode: count" > coverage-all.out
$(foreach pkg,$(PACKAGES),\
go test -coverprofile=coverage.out -covermode=count $(pkg);\
tail -n +2 coverage.out >> coverage-all.out;)
go tool cover -html=coverage-all.out
rm -f coverage.out
rm -f coverage-all.out

generate:
go generate `go list ./...`

list-imports:
go list -f {{.Imports}} ./...
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Examples can be found on [library documentation][doc].

## Running tests

The tests can be run via the provided Bash script:
The tests can be run via the provided Makefile:

```bash
./test.sh
make test
```

## License
Expand Down
60 changes: 34 additions & 26 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package eval

import (
"testing"
"fmt"
"time"

"gopkg.in/raiqub/docker.v0"
Expand All @@ -38,19 +38,17 @@ type Environment struct {
image *docker.Image
container *docker.Container
run func() (*docker.Container, error)
test testing.TB
}

// NewMongoDBEnvironment creates a instance that allows to use MongoDB for
// testing
func NewMongoDBEnvironment(tb testing.TB) *Environment {
func NewMongoDBEnvironment() *Environment {
d := docker.NewDocker()
mongo := docker.NewImageMongoDB(d)

return &Environment{
dockerBin: d,
image: &mongo.Image,
test: tb,
run: func() (*docker.Container, error) {
cfg := docker.NewRunConfig()
cfg.Detach()
Expand All @@ -60,14 +58,13 @@ func NewMongoDBEnvironment(tb testing.TB) *Environment {
}

// NewRedisEnvironment creates a instance that allows to use Redis for testing.
func NewRedisEnvironment(tb testing.TB) *Environment {
func NewRedisEnvironment() *Environment {
d := docker.NewDocker()
redis := docker.NewImage(d, ImageRedisName)

return &Environment{
dockerBin: d,
image: redis,
test: tb,
run: func() (*docker.Container, error) {
cfg := docker.NewRunConfig()
cfg.Detach()
Expand All @@ -78,17 +75,24 @@ func NewRedisEnvironment(tb testing.TB) *Environment {

// Applicability tests whether current testing environment can be run on current
// host.
func (s *Environment) Applicability() bool {
func (s *Environment) Applicability() (bool, *ErrUser) {
if !s.dockerBin.HasBin() {
return false
return false, &ErrUser{
Warn,
"Docker binary was not found",
}
}

_, err := s.dockerBin.Run("ps")
if err != nil {
s.test.Log("Docker is installed but is not running or current user " +
"is lacking permissions")
return false, &ErrUser{
Warn,
"Docker is installed but is not running or current user " +
"is lacking permissions",
}
}
return err == nil

return true, nil
}

// Network returns network information from current running container.
Expand All @@ -106,30 +110,31 @@ func (s *Environment) Network() ([]docker.NetworkNode, error) {
}

// Run starts a new Docker instance for testing environment.
func (s *Environment) Run() bool {
func (s *Environment) Run() (bool, *ErrUser) {
if err := s.image.Setup(); err != nil {
s.test.Fatalf("Error setting up Docker: %v", err)
return false
return false, &ErrUser{
Fatal,
fmt.Sprintf("Error setting up Docker: %v", err),
}
}

var err error
s.container, err = s.run()
if err != nil {
if s.container != nil {
s.container.Kill()
s.container.Remove()
s.Stop()
return false, &ErrUser{
Fatal,
fmt.Sprintf("Error running a new Docker container: %v", err),
}

s.test.Fatal("Error running a new Docker container:", err)
return false
}

if s.container.HasExposedPorts() {
if err := s.container.WaitStartup(StartupTimeout); err != nil {
s.container.Kill()
s.container.Remove()
s.test.Fatal("Timeout waiting Docker instance to respond:", err)
return false
s.Stop()
return false, &ErrUser{
Fatal,
fmt.Sprintf("Timeout waiting Docker instance to respond: %v", err),
}
}
} else {
timeout := time.After(StartupTimeout)
Expand All @@ -138,13 +143,16 @@ func (s *Environment) Run() bool {
case <-timeout:
inspect, err := s.container.Inspect()
if err != nil || !inspect[0].State.Running {
return false
return false, &ErrUser{
Fatal,
fmt.Sprint("Timeout waiting container startup"),
}
}
}
}
}

return true
return true, nil
}

// Stop removes current running testing environment.
Expand Down
27 changes: 26 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,36 @@ import (
"fmt"
)

// A NotRunningError represents a error when a Environment function is called
const (
// Info defines a informational event.
Info = 0

// Warn defines a event which indicates wrong parameters.
Warn = 1

// Error defines a failed event.
Error = 2

// Fatal defines a unrecoverable event.
Fatal = 3
)

// A NotRunningError represents an error when a Environment function is called
// before it is running.
type NotRunningError string

// Error returns string representation of current instance error.
func (e NotRunningError) Error() string {
return fmt.Sprintf("The environment '%s' is not running", string(e))
}

// A ErrUser represents an event that needs user attention.
type ErrUser struct {
Severity int
Message string
}

// Error returns string representation of current instance error.
func (e ErrUser) Error() string {
return e.Message
}
41 changes: 23 additions & 18 deletions mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package eval

import (
"fmt"
"testing"

"gopkg.in/mgo.v2"
)
Expand All @@ -35,46 +34,52 @@ type MongoDBEnvironment struct {

// PrepareMongoDBEnvironment creates a new MongoDB container, starts it and open
// a new session to database.
func PrepareMongoDBEnvironment(tb testing.TB) *MongoDBEnvironment {
mongo := NewMongoDBEnvironment(tb)
if !mongo.Applicability() {
tb.Skip("This test cannot be run because Docker is not acessible")
return nil
func PrepareMongoDBEnvironment() (*MongoDBEnvironment, *ErrUser) {
mongo := NewMongoDBEnvironment()
if ok, err := mongo.Applicability(); !ok {
return nil, err
}

if !mongo.Run() {
tb.Fatal("Could not start MongoDB server")
return nil
if ok, err := mongo.Run(); !ok {
return nil, err
}

net, err := mongo.Network()
if err != nil {
mongo.Stop()
tb.Fatalf("Error getting MongoDB IP address: %s\n", err)
return nil
return nil, &ErrUser{
Fatal,
fmt.Sprintf("Error getting MongoDB IP address: %s", err),
}
}

mgourl := fmt.Sprintf(mongoURLTpl, net[0].IPAddress, net[0].Port)

session, err := newDBSession(mgourl)
if err != nil {
mongo.Stop()
tb.Fatalf("Error opening a MongoDB session: %s\n", err)
return nil
return nil, &ErrUser{
Fatal,
fmt.Sprintf("Error opening a MongoDB session: %s", err),
}
}

return &MongoDBEnvironment{
mongo,
session,
}
}, nil
}

// Dispose closes database session and removes current environment.
func (e *MongoDBEnvironment) Dispose() {
e.session.Close()
e.env.Stop()
e.session = nil
e.env = nil
if e.session != nil {
e.session.Close()
e.session = nil
}
if e.env != nil {
e.env.Stop()
e.env = nil
}
}

// Session returns the database session for current environment.
Expand Down
13 changes: 10 additions & 3 deletions mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ func TestMongoDBEnvironmentSession(t *testing.T) {
true,
}

env := PrepareMongoDBEnvironment(t)
if env == nil {
return
env, err := PrepareMongoDBEnvironment()
if err != nil {
switch err.Severity {
case Info:
t.Log(err.Message)
case Warn:
t.Skip(err.Message)
case Error, Fatal:
t.Fatal(err.Message)
}
}
defer env.Dispose()

Expand Down
7 changes: 0 additions & 7 deletions test.sh

This file was deleted.

0 comments on commit 5cfe86e

Please sign in to comment.