Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to get container IP address #140

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
22 changes: 22 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
})
}
8 changes: 8 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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{
Expand Down