Skip to content

Commit

Permalink
Merge pull request #17 from pvormste/refactor/new-names-for-host-and-…
Browse files Browse the repository at this point in the history
…port

New names for DockerHost and DockerPort
  • Loading branch information
pvormste authored Mar 23, 2019
2 parents 08e9ba6 + cc7cbea commit 238b5a8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ images are covered by tests in this repository:

| name | value |
| -----| ----- |
| host | `postgresContainer.DockerHost` |
| port | `postgresContainer.HostPort` |
| host | `postgresContainer.Hostname` |
| port | `postgresContainer.Port` |
| user | postgres |
| password | postgres |
| database | postgres |
Expand All @@ -41,7 +41,8 @@ if err := postgresContainer.PurgeContainer(); err != nil {

### Custom Container

You can start any container with this library, just populate the `WrapperParams` struct and pass it to the `InitContainer` function
You can start any container with this library, just populate the `WrapperParams` struct and pass it to the `InitContainer` function.
The Hostname and Port will then be available via the fields `container.Hostname` and `container.Port`.

#### WrapperParams

Expand Down Expand Up @@ -73,11 +74,22 @@ if err != nil {
}


hostname := customContainer.Hostname
port := customContainer.Port


if err := customContainer.PurgeContainer(); err != nil {
// ...
}
```

## Using with Gitlab CI

If you want to use this library with Gitlab CI you may be interested in my fork of the
docker image: [pvormste/docker-go](https://github.com/pvormste/docker)
You can also find it on the DockerHub: [pvormste/docker-go](https://hub.docker.com/r/pvormste/docker-go)


## License

MIT License
Expand Down
26 changes: 15 additions & 11 deletions pkg/dockertestwrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const DefaultContainerExpiresAfterSeconds uint = 1800

// AfterInitActionFunc is a function type which will be executed after container initialization
type AfterInitActionFunc func(dockerHost string, hostPort int) error
type AfterInitActionFunc func(hostname string, port int) error

// WrapperParams contains all parameters needed to start a new custom container
type WrapperParams struct {
Expand All @@ -24,8 +24,10 @@ type WrapperParams struct {

// WrapperInstance holds all the information of the running container
type WrapperInstance struct {
DockerHost string
HostPort int
Hostname string
Port int
DockerHost string // deprecated
HostPort int // deprecated
Pool *dockertest.Pool
Resource *dockertest.Resource
}
Expand All @@ -47,16 +49,16 @@ func InitContainer(params WrapperParams) (instance *WrapperInstance, err error)
return nil, err
}

if err := instance.determineDockerHost(); err != nil {
if err := instance.determineHostname(); err != nil {
return nil, err
}

if err := instance.determineHostPort(params.ContainerPort); err != nil {
if err := instance.determinePort(params.ContainerPort); err != nil {
return nil, err
}

err = instance.Pool.Retry(func() error {
return params.AfterInitActionFunc(instance.DockerHost, instance.HostPort)
return params.AfterInitActionFunc(instance.Hostname, instance.Port)
})
if err != nil {
return nil, err
Expand All @@ -70,9 +72,9 @@ func (w WrapperInstance) PurgeContainer() error {
return w.Pool.Purge(w.Resource)
}

func (w *WrapperInstance) determineDockerHost() error {
func (w *WrapperInstance) determineHostname() error {
if strings.HasPrefix(w.Pool.Client.Endpoint(), "unix://") {
w.DockerHost = "localhost"
w.Hostname = "localhost"
return nil
}

Expand All @@ -82,16 +84,18 @@ func (w *WrapperInstance) determineDockerHost() error {
return err
}

w.DockerHost = endpointUrl.Hostname()
w.Hostname = endpointUrl.Hostname()
w.DockerHost = w.Hostname // will be removed in a future update
return nil
}

func (w *WrapperInstance) determineHostPort(containerPort string) (err error) {
func (w *WrapperInstance) determinePort(containerPort string) (err error) {
stringPort := w.Resource.GetPort(containerPort)
w.HostPort, err = strconv.Atoi(stringPort)
w.Port, err = strconv.Atoi(stringPort)
if err != nil {
return err
}

w.HostPort = w.Port // will be remove in a future update
return nil
}
28 changes: 14 additions & 14 deletions pkg/dockertestwrapper/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

// ===========================
// wrapper.determineDockerHost()
// wrapper.determineHostname()
// ===========================

var wrapperInstanceDetermineDockerHostTests = []struct {
var wrapperInstanceDetermineHostnameTests = []struct {
it string
inputDockerAddress string
doAssertions func(t *testing.T, actualInstance WrapperInstance, actualErr error)
Expand All @@ -22,21 +22,21 @@ var wrapperInstanceDetermineDockerHostTests = []struct {
inputDockerAddress: "unix:///var/run/docker.sock",
doAssertions: func(t *testing.T, actualInstance WrapperInstance, actualErr error) {
assert.NoError(t, actualErr)
assert.Equal(t, "localhost", actualInstance.DockerHost)
assert.Equal(t, "localhost", actualInstance.Hostname)
},
},
{
it: "should return the host from pool when not starting with unix://",
inputDockerAddress: "http://docker:2375",
doAssertions: func(t *testing.T, actualInstance WrapperInstance, actualErr error) {
assert.NoError(t, actualErr)
assert.Equal(t, "docker", actualInstance.DockerHost)
assert.Equal(t, "docker", actualInstance.Hostname)
},
},
}

func TestWrapperInstance_DetermineDockerHost(t *testing.T) {
for _, test := range wrapperInstanceDetermineDockerHostTests {
func TestWrapperInstance_DetermineHostname(t *testing.T) {
for _, test := range wrapperInstanceDetermineHostnameTests {
test := test
t.Run(test.it, func(t *testing.T) {
client, err := docker.NewClient(test.inputDockerAddress)
Expand All @@ -47,17 +47,17 @@ func TestWrapperInstance_DetermineDockerHost(t *testing.T) {
Client: client,
},
}
actualErr := actualWrapper.determineDockerHost()
actualErr := actualWrapper.determineHostname()
test.doAssertions(t, actualWrapper, actualErr)
})
}
}

// ===========================
// wrapper.determineHostPort()
// wrapper.determinePort()
// ===========================

var wrapperInstanceDetermineHostPortTests = []struct {
var wrapperInstanceDeterminePortTests = []struct {
it string
inputContainerPort string
internalHostPort string
Expand All @@ -69,7 +69,7 @@ var wrapperInstanceDetermineHostPortTests = []struct {
internalHostPort: "5432/tcp",
doAssertions: func(t *testing.T, instance WrapperInstance, actualErr error) {
assert.Error(t, actualErr)
assert.Equal(t, 0, instance.HostPort)
assert.Equal(t, 0, instance.Port)
},
},
{
Expand All @@ -78,13 +78,13 @@ var wrapperInstanceDetermineHostPortTests = []struct {
internalHostPort: "5432",
doAssertions: func(t *testing.T, instance WrapperInstance, actualErr error) {
assert.NoError(t, actualErr)
assert.Equal(t, 5432, instance.HostPort)
assert.Equal(t, 5432, instance.Port)
},
},
}

func TestWrapperInstance_DetermineHostPort(t *testing.T) {
for _, test := range wrapperInstanceDetermineHostPortTests {
func TestWrapperInstance_DeterminePort(t *testing.T) {
for _, test := range wrapperInstanceDeterminePortTests {
test := test
t.Run(test.it, func(t *testing.T) {
mockedPortBinding := map[docker.Port][]docker.PortBinding{}
Expand All @@ -106,7 +106,7 @@ func TestWrapperInstance_DetermineHostPort(t *testing.T) {
Resource: &mockedResource,
}

actualErr := actualWrapper.determineHostPort(test.inputContainerPort)
actualErr := actualWrapper.determinePort(test.inputContainerPort)
test.doAssertions(t, actualWrapper, actualErr)
})
}
Expand Down

0 comments on commit 238b5a8

Please sign in to comment.