From ba503592e1c499a25540f83fd16a7e143b023f2d Mon Sep 17 00:00:00 2001 From: Rob Dimsdale-Zucker Date: Mon, 11 Apr 2022 20:58:07 +0000 Subject: [PATCH] Add method to get container IP address Signed-off-by: Sophie Wigmore --- container.go | 27 ++++++++++++++++++++++++--- container_test.go | 22 ++++++++++++++++++++++ docker_test.go | 8 ++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/container.go b/container.go index 5bf3a46..47f9799 100644 --- a/container.go +++ b/container.go @@ -2,15 +2,17 @@ package occam import ( "encoding/json" + "fmt" "net/url" "os" "strings" ) type Container struct { - ID string - Ports map[string]string - Env map[string]string + ID string + Ports map[string]string + Env map[string]string + IPAddresses map[string]string } func NewContainerFromInspectOutput(output []byte) (Container, error) { @@ -23,6 +25,9 @@ func NewContainerFromInspectOutput(output []byte) (Container, error) { Ports map[string][]struct { HostPort string `json:"HostPort"` } `json:"Ports"` + Networks map[string]struct { + IPAddress string `json:"IPAddress"` + } `json:"Networks"` } `json:"NetworkSettings"` } @@ -50,6 +55,13 @@ func NewContainerFromInspectOutput(output []byte) (Container, error) { } } + if len(inspect[0].NetworkSettings.Networks) > 0 { + container.IPAddresses = make(map[string]string) + for networkName, network := range inspect[0].NetworkSettings.Networks { + container.IPAddresses[networkName] = network.IPAddress + } + } + return container, nil } @@ -70,3 +82,12 @@ func (c Container) Host() string { return url.Hostname() } + +func (c Container) IPAddressForNetwork(networkName string) (string, error) { + ipAddress, ok := c.IPAddresses[networkName] + if !ok { + return "", fmt.Errorf("invalid network name: %s", networkName) + } + + return ipAddress, nil +} diff --git a/container_test.go b/container_test.go index cbfc21f..2f3417a 100644 --- a/container_test.go +++ b/container_test.go @@ -22,4 +22,26 @@ func testContainer(t *testing.T, context spec.G, it spec.S) { Expect(container.HostPort("1234")).To(Equal("11111")) }) }) + + context("IPAddressForNetwork", func() { + it("returns the IP Address associated ", func() { + container := occam.Container{ + IPAddresses: map[string]string{ + "bridge": "10.172.0.2", + }, + } + Expect(container.IPAddressForNetwork("bridge")).To(Equal("10.172.0.2")) + }) + + context("failure cases", func() { + context("when the provided network does not exist", func() { + it("returns an error", func() { + container := occam.Container{} + + _, err := container.IPAddressForNetwork("some-non-existent-network") + Expect(err).To(HaveOccurred()) + }) + }) + }) + }) } diff --git a/docker_test.go b/docker_test.go index 7e10e40..36d5415 100644 --- a/docker_test.go +++ b/docker_test.go @@ -586,6 +586,11 @@ func testDocker(t *testing.T, context spec.G, it spec.S) { ] }, "NetworkSettings": { + "Networks": { + "bridge": { + "IPAddress": "10.172.0.2" + } + }, "Ports": { "8080/tcp": [ { @@ -613,6 +618,9 @@ func testDocker(t *testing.T, context spec.G, it spec.S) { Ports: map[string]string{ "8080": "12345", }, + IPAddresses: map[string]string{ + "bridge": "10.172.0.2", + }, })) Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{