Skip to content

Commit

Permalink
Merge pull request #53 from testcontainers/feature/expose-container-id
Browse files Browse the repository at this point in the history
Exposed containerID with the function GetContainerID
  • Loading branch information
gianarb authored Mar 13, 2019
2 parents 4ed6500 + eb19c7e commit c193456
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ContainerProvider interface {

// Container allows getting info about and controlling a single container instance
type Container interface {
GetContainerID() string // get the container id from the provider
Endpoint(context.Context, string) (string, error) // get proto://ip:port string for the first exposed port
PortEndpoint(context.Context, nat.Port, string) (string, error) // get proto://ip:port string for the given exposed port
Host(context.Context) (string, error) // get host where the container port is exposed
Expand Down
4 changes: 4 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type DockerContainer struct {
terminationSignal chan bool
}

func (c *DockerContainer) GetContainerID() string {
return c.ID
}

// Endpoint gets proto://host:port string for the first exposed port
// Will returns just host:port if proto is ""
func (c *DockerContainer) Endpoint(ctx context.Context, proto string) (string, error) {
Expand Down
19 changes: 19 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)

func TestContainerReturnItsContainerID(t *testing.T) {
ctx := context.Background()
nginxA, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{
"80/tcp",
},
},
})
if err != nil {
t.Fatal(err)
}
defer nginxA.Terminate(ctx)
if nginxA.GetContainerID() == "" {
t.Errorf("expected a containerID but we got an empty string.")
}
}

func TestTwoContainersExposingTheSamePort(t *testing.T) {
ctx := context.Background()
nginxA, err := GenericContainer(ctx, GenericContainerRequest{
Expand Down

0 comments on commit c193456

Please sign in to comment.