Skip to content

Commit

Permalink
feat: dockerized for contribution and add automated testing with Trav…
Browse files Browse the repository at this point in the history
…isCI (#14)

* chore: dockerized and use dep to manage dependencies

* fix: add 'Source' option to Config

* rebase mixed code

* chore: Use golang:alpine image to shrink final size

* feat: Add travis.yml

* chore: Use native go environment

* fix: Add mongodb for travis

* fix: switch back to docker-compose for travis

* Update readme for #11

* chore: Add TravisCI badge
  • Loading branch information
moehlone authored Feb 7, 2018
1 parent 5fca75a commit e159d08
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 6 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ _testmain.go
*.prof

# Intellij
.idea
.idea

# godep
vendor

# data storage
.data

.env
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
- DOCKER_COMPOSE_VERSION=1.18.0

before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin

script:
- make test
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:alpine

ENV APP_DIR $GOPATH/src/mongodm

RUN apk add --update --no-cache git && \
mkdir -p $APP_DIR && \
go get -u github.com/golang/dep/cmd/dep

ADD . $APP_DIR

WORKDIR $APP_DIR
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
branch = "v2"
name = "gopkg.in/mgo.v2"
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
IMAGE = mongodm
APP_DIR = /go/src/mongodm
RUN = docker run -it --rm -v $(PWD):$(APP_DIR) $(IMAGE)
ARGS = $(filter-out $@,$(MAKECMDGOALS))

test:
docker-compose run test

dep:
echo $(ARGS)
$(RUN) dep $(ARGS)

%:
@:

.PHONY:
test dep
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Build Status](https://travis-ci.org/zebresel-com/mongodm.svg?branch=master)](https://travis-ci.org/zebresel-com/mongodm)
[![GoDoc](https://godoc.org/github.com/zebresel-com/mongodm?status.svg)](https://godoc.org/github.com/zebresel-com/mongodm)

## What is mongodm?
Expand Down Expand Up @@ -490,9 +491,38 @@ User := self.db.Model("User")
In this case we retrieve a `requestMap` and forward the `password` attribute to our `Validate` method (example above).
If you want to use your own regular expression as attribute tags then use the following format: `validation:"/YOUR_REGEX/YOUR_FLAG(S)"` - for example: `validation:"/[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}/"`

## Contribute

Feel free! Start pull requests or issues if anything is missing for you :)

### Dockerized

With docker, you do not need to install `go` and `mongodb` on your local machine, it's easy to setup a development environment for this repository. Thanks to @centsent who helped to dockerize this project.

### Prerequisites

Make sure you already installed below programs on your local machine:

* `git`
* `docker`
* `docker-compose`

### Usage

Just run `make test`, it will build the docker container and run `go test`.

### dep

[dep](https://github.com/golang/dep) is a prototype dependency management tool for Go.
To use `dep` in the container, prefix `make` for all `dep` commands, for example:

```bash
$ make dep "ensure -add github.com/some/repos"
```

Beware that the double quotes are required after `make dep` command.

## Questions?

Are there any questions or is something not clear enough? Simply open up a ticket or send me an email :)


**Also feel free to contribute! Start pull requests against the `develop` branch.**
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

version: '3'
services:
test:
depends_on:
- mongodb
build:
context: .
image: mongodm
volumes:
- "$PWD:/go/src/mongodm"
command: sh -c "dep ensure -v && go test"

mongodb:
image: mongo
container_name: mongo
ports:
- "27017:27017"
volumes:
- "$PWD/.data:/data/db"
command: mongod
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
6 changes: 4 additions & 2 deletions mongodm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import (
"strings"
"time"

"gopkg.in/mgo.v2"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

Expand All @@ -102,6 +102,7 @@ type (
DatabaseName string
DatabaseUser string
DatabasePassword string
Source string
Locals map[string]string
}

Expand Down Expand Up @@ -300,8 +301,9 @@ func (self *Connection) Open() (err error) {
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
Source: self.Config.Source,
}

session, err := mgo.DialWithInfo(info)

if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion mongodm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

const (
DBHost string = "127.0.0.1"
// It must be a container name to connect to mongodb correctly
DBHost string = "mongo"
DBName string = "mongodm_test"
DBUser string = "admin"
DBPass string = "admin"
Expand Down Expand Up @@ -58,6 +59,7 @@ func init() {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}

}

func TestConnection(t *testing.T) {
Expand All @@ -70,6 +72,11 @@ func TestConnection(t *testing.T) {
DatabaseName: DBName,
DatabaseUser: DBUser,
DatabasePassword: DBPass,
// Source is the database used to establish credentials and privileges
// with a MongoDB server. Defaults to the value of Database, if that is
// set, or "admin" otherwise.
// see https://godoc.org/labix.org/v2/mgo#DialInfo
Source: "admin",
Locals: localMap["en-US"],
}

Expand Down

0 comments on commit e159d08

Please sign in to comment.