Skip to content

Commit 238b5a8

Browse files
authored
Merge pull request #17 from pvormste/refactor/new-names-for-host-and-port
New names for DockerHost and DockerPort
2 parents 08e9ba6 + cc7cbea commit 238b5a8

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ images are covered by tests in this repository:
1919

2020
| name | value |
2121
| -----| ----- |
22-
| host | `postgresContainer.DockerHost` |
23-
| port | `postgresContainer.HostPort` |
22+
| host | `postgresContainer.Hostname` |
23+
| port | `postgresContainer.Port` |
2424
| user | postgres |
2525
| password | postgres |
2626
| database | postgres |
@@ -41,7 +41,8 @@ if err := postgresContainer.PurgeContainer(); err != nil {
4141

4242
### Custom Container
4343

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

4647
#### WrapperParams
4748

@@ -73,11 +74,22 @@ if err != nil {
7374
}
7475

7576

77+
hostname := customContainer.Hostname
78+
port := customContainer.Port
79+
80+
7681
if err := customContainer.PurgeContainer(); err != nil {
7782
// ...
7883
}
7984
```
8085

86+
## Using with Gitlab CI
87+
88+
If you want to use this library with Gitlab CI you may be interested in my fork of the
89+
docker image: [pvormste/docker-go](https://github.com/pvormste/docker)
90+
You can also find it on the DockerHub: [pvormste/docker-go](https://hub.docker.com/r/pvormste/docker-go)
91+
92+
8193
## License
8294

8395
MIT License

pkg/dockertestwrapper/wrapper.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
const DefaultContainerExpiresAfterSeconds uint = 1800
1212

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

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

2525
// WrapperInstance holds all the information of the running container
2626
type WrapperInstance struct {
27-
DockerHost string
28-
HostPort int
27+
Hostname string
28+
Port int
29+
DockerHost string // deprecated
30+
HostPort int // deprecated
2931
Pool *dockertest.Pool
3032
Resource *dockertest.Resource
3133
}
@@ -47,16 +49,16 @@ func InitContainer(params WrapperParams) (instance *WrapperInstance, err error)
4749
return nil, err
4850
}
4951

50-
if err := instance.determineDockerHost(); err != nil {
52+
if err := instance.determineHostname(); err != nil {
5153
return nil, err
5254
}
5355

54-
if err := instance.determineHostPort(params.ContainerPort); err != nil {
56+
if err := instance.determinePort(params.ContainerPort); err != nil {
5557
return nil, err
5658
}
5759

5860
err = instance.Pool.Retry(func() error {
59-
return params.AfterInitActionFunc(instance.DockerHost, instance.HostPort)
61+
return params.AfterInitActionFunc(instance.Hostname, instance.Port)
6062
})
6163
if err != nil {
6264
return nil, err
@@ -70,9 +72,9 @@ func (w WrapperInstance) PurgeContainer() error {
7072
return w.Pool.Purge(w.Resource)
7173
}
7274

73-
func (w *WrapperInstance) determineDockerHost() error {
75+
func (w *WrapperInstance) determineHostname() error {
7476
if strings.HasPrefix(w.Pool.Client.Endpoint(), "unix://") {
75-
w.DockerHost = "localhost"
77+
w.Hostname = "localhost"
7678
return nil
7779
}
7880

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

85-
w.DockerHost = endpointUrl.Hostname()
87+
w.Hostname = endpointUrl.Hostname()
88+
w.DockerHost = w.Hostname // will be removed in a future update
8689
return nil
8790
}
8891

89-
func (w *WrapperInstance) determineHostPort(containerPort string) (err error) {
92+
func (w *WrapperInstance) determinePort(containerPort string) (err error) {
9093
stringPort := w.Resource.GetPort(containerPort)
91-
w.HostPort, err = strconv.Atoi(stringPort)
94+
w.Port, err = strconv.Atoi(stringPort)
9295
if err != nil {
9396
return err
9497
}
9598

99+
w.HostPort = w.Port // will be remove in a future update
96100
return nil
97101
}

pkg/dockertestwrapper/wrapper_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
// ===========================
12-
// wrapper.determineDockerHost()
12+
// wrapper.determineHostname()
1313
// ===========================
1414

15-
var wrapperInstanceDetermineDockerHostTests = []struct {
15+
var wrapperInstanceDetermineHostnameTests = []struct {
1616
it string
1717
inputDockerAddress string
1818
doAssertions func(t *testing.T, actualInstance WrapperInstance, actualErr error)
@@ -22,21 +22,21 @@ var wrapperInstanceDetermineDockerHostTests = []struct {
2222
inputDockerAddress: "unix:///var/run/docker.sock",
2323
doAssertions: func(t *testing.T, actualInstance WrapperInstance, actualErr error) {
2424
assert.NoError(t, actualErr)
25-
assert.Equal(t, "localhost", actualInstance.DockerHost)
25+
assert.Equal(t, "localhost", actualInstance.Hostname)
2626
},
2727
},
2828
{
2929
it: "should return the host from pool when not starting with unix://",
3030
inputDockerAddress: "http://docker:2375",
3131
doAssertions: func(t *testing.T, actualInstance WrapperInstance, actualErr error) {
3232
assert.NoError(t, actualErr)
33-
assert.Equal(t, "docker", actualInstance.DockerHost)
33+
assert.Equal(t, "docker", actualInstance.Hostname)
3434
},
3535
},
3636
}
3737

38-
func TestWrapperInstance_DetermineDockerHost(t *testing.T) {
39-
for _, test := range wrapperInstanceDetermineDockerHostTests {
38+
func TestWrapperInstance_DetermineHostname(t *testing.T) {
39+
for _, test := range wrapperInstanceDetermineHostnameTests {
4040
test := test
4141
t.Run(test.it, func(t *testing.T) {
4242
client, err := docker.NewClient(test.inputDockerAddress)
@@ -47,17 +47,17 @@ func TestWrapperInstance_DetermineDockerHost(t *testing.T) {
4747
Client: client,
4848
},
4949
}
50-
actualErr := actualWrapper.determineDockerHost()
50+
actualErr := actualWrapper.determineHostname()
5151
test.doAssertions(t, actualWrapper, actualErr)
5252
})
5353
}
5454
}
5555

5656
// ===========================
57-
// wrapper.determineHostPort()
57+
// wrapper.determinePort()
5858
// ===========================
5959

60-
var wrapperInstanceDetermineHostPortTests = []struct {
60+
var wrapperInstanceDeterminePortTests = []struct {
6161
it string
6262
inputContainerPort string
6363
internalHostPort string
@@ -69,7 +69,7 @@ var wrapperInstanceDetermineHostPortTests = []struct {
6969
internalHostPort: "5432/tcp",
7070
doAssertions: func(t *testing.T, instance WrapperInstance, actualErr error) {
7171
assert.Error(t, actualErr)
72-
assert.Equal(t, 0, instance.HostPort)
72+
assert.Equal(t, 0, instance.Port)
7373
},
7474
},
7575
{
@@ -78,13 +78,13 @@ var wrapperInstanceDetermineHostPortTests = []struct {
7878
internalHostPort: "5432",
7979
doAssertions: func(t *testing.T, instance WrapperInstance, actualErr error) {
8080
assert.NoError(t, actualErr)
81-
assert.Equal(t, 5432, instance.HostPort)
81+
assert.Equal(t, 5432, instance.Port)
8282
},
8383
},
8484
}
8585

86-
func TestWrapperInstance_DetermineHostPort(t *testing.T) {
87-
for _, test := range wrapperInstanceDetermineHostPortTests {
86+
func TestWrapperInstance_DeterminePort(t *testing.T) {
87+
for _, test := range wrapperInstanceDeterminePortTests {
8888
test := test
8989
t.Run(test.it, func(t *testing.T) {
9090
mockedPortBinding := map[docker.Port][]docker.PortBinding{}
@@ -106,7 +106,7 @@ func TestWrapperInstance_DetermineHostPort(t *testing.T) {
106106
Resource: &mockedResource,
107107
}
108108

109-
actualErr := actualWrapper.determineHostPort(test.inputContainerPort)
109+
actualErr := actualWrapper.determinePort(test.inputContainerPort)
110110
test.doAssertions(t, actualWrapper, actualErr)
111111
})
112112
}

0 commit comments

Comments
 (0)