Skip to content

Commit

Permalink
Moved from github.com/skarllot/raiqub/test
Browse files Browse the repository at this point in the history
  • Loading branch information
skarllot committed Mar 24, 2016
0 parents commit e3fc3a1
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.out
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
sudo: required
language: go

go:
- 1.4
- 1.5
- 1.6
- tip

matrix:
allow_failures:
- go: tip
- go: 1.4

services:
- docker

before_install:
- docker pull mongo

before_script:
- go get -u github.com/golang/lint/golint
- go get -u github.com/axw/gocov/gocov
- go get -u github.com/mattn/goveralls
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi

script:
- go test -v --race ./...
- goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
- test -z "$(gofmt -s -l -w . | tee /dev/stderr)"
- test -z "$(golint ./... | tee /dev/stderr)"
- go vet ./...
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Eval

Eval is a library for the [Go Programming Language][go]. It provides some
operations to help testing operations.

## Status

[![Build Status](https://img.shields.io/travis/raiqub/eval/master.svg?style=flat&label=linux%20build)](https://travis-ci.org/raiqub/eval)
[![AppVeyor Build](https://img.shields.io/appveyor/ci/skarllot/eval/master.svg?style=flat&label=windows%20build)](https://ci.appveyor.com/project/skarllot/eval)
[![Coverage Status](https://coveralls.io/repos/raiqub/eval/badge.svg?branch=master&service=github)](https://coveralls.io/github/raiqub/eval?branch=master)
[![GoDoc](https://godoc.org/github.com/raiqub/eval?status.svg)](http://godoc.org/github.com/raiqub/eval)

## Features

* **Environment** type which provides a Docker environment for testing purposes.
* **MongoDBEnvironment** type which represents a MongoDB's Environment.

## Installation

To install raiqub/eval library run the following command:

```bash
go get gopkg.in/raiqub/eval.v0
```

To import this package, add the following line to your code:

```bash
import "gopkg.in/raiqub/eval.v0"
```

## Examples

Examples can be found on [library documentation][doc].

## Running tests

The tests can be run via the provided Bash script:

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

## License

raiqub/eval is made available under the [Apache Version 2.0 License][license].


[go]: http://golang.org/
[doc]: http://godoc.org/github.com/raiqub/eval
[license]: http://www.apache.org/licenses/LICENSE-2.0
20 changes: 20 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2015 Fabrício Godoy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
Package eval provides some testing operations.
*/
package eval
158 changes: 158 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2015 Fabrício Godoy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eval

import (
"testing"
"time"

"gopkg.in/raiqub/docker.v0"
)

const (
// ImageRedisName defines the default image name of Redis Docker image.
ImageRedisName = "redis"

// StartupTimeout defines the maximum duration of time waiting for container
// startup
StartupTimeout = 30 * time.Second
)

// A Environment represents a Docker testing environment.
type Environment struct {
dockerBin *docker.Docker
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 {
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()
return mongo.RunLight(cfg)
},
}
}

// NewRedisEnvironment creates a instance that allows to use Redis for testing.
func NewRedisEnvironment(tb testing.TB) *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()
return redis.Run(cfg)
},
}
}

// Applicability tests whether current testing environment can be run on current
// host.
func (s *Environment) Applicability() bool {
if !s.dockerBin.HasBin() {
return false
}

_, 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 err == nil
}

// Network returns network information from current running container.
func (s *Environment) Network() ([]docker.NetworkNode, error) {
if s.container == nil {
return nil, NotRunningError(s.image.Name())
}

nodes, err := s.container.NetworkNodes()
if err != nil {
return nil, err
}

return nodes, nil
}

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

var err error
s.container, err = s.run()
if err != nil {
if s.container != nil {
s.container.Kill()
s.container.Remove()
}

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
}
} else {
timeout := time.After(StartupTimeout)
for {
select {
case <-timeout:
inspect, err := s.container.Inspect()
if err != nil || !inspect[0].State.Running {
return false
}
}
}
}

return true
}

// Stop removes current running testing environment.
func (s *Environment) Stop() {
if s.container == nil {
return
}

s.container.Kill()
s.container.Remove()
}
30 changes: 30 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2015 Fabrício Godoy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eval

import (
"fmt"
)

// A NotRunningError represents a 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))
}
Loading

0 comments on commit e3fc3a1

Please sign in to comment.