forked from paketo-buildpacks/occam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
125 lines (110 loc) · 3.18 KB
/
container_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package occam_test
import (
"testing"
occam "github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testContainer(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("HostPort", func() {
it("returns the external port the container is bound to", func() {
container := occam.Container{
Ports: map[string]string{
"1234": "11111",
},
}
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())
})
})
})
})
context("NewContainerFromInspectOutput", func() {
it("It creates a new container from inspect output ", func() {
output := []byte(`[
{
"Id": "container-id",
"Config": {
"Env": ["ENV_VAR1=value1", "ENV_VAR2=value2"]
},
"NetworkSettings": {
"Ports": {
"8080/tcp": [
{
"HostPort": "1234"
}
]
},
"Networks": {
"network1": {
"IPAddress": "192.168.0.1"
},
"network2": {
"IPAddress": "192.168.0.2"
}
}
}
}
]`)
container, err := occam.NewContainerFromInspectOutput(output)
Expect(err).NotTo(HaveOccurred())
Expect(container.ID).To(Equal("container-id"))
Expect(container.Ports).To(HaveLen(1))
Expect(container.Ports).To(HaveKeyWithValue("8080", "1234"))
Expect(container.Env).To(HaveLen(2))
Expect(container.Env).To(HaveKeyWithValue("ENV_VAR1", "value1"))
Expect(container.Env).To(HaveKeyWithValue("ENV_VAR2", "value2"))
Expect(container.IPAddresses).To(HaveLen(2))
Expect(container.IPAddresses["network1"]).To(Equal("192.168.0.1"))
Expect(container.IPAddresses["network2"]).To(Equal("192.168.0.2"))
})
context("When there are no host ports but only container ports", func() {
it("It creates a new container without the exposed ports included ", func() {
output := []byte(`[
{
"Id": "container-id",
"Config": {
"Env": ["ENV_VAR1=value1", "ENV_VAR2=value2"]
},
"NetworkSettings": {
"Ports": {
"8080/tcp": []
},
"Networks": {
"network1": {
"IPAddress": "192.168.0.1"
}
}
}
}
]`)
container, err := occam.NewContainerFromInspectOutput(output)
Expect(err).NotTo(HaveOccurred())
Expect(container.ID).To(Equal("container-id"))
Expect(container.Ports).To(HaveLen(0))
Expect(container.Env).To(HaveLen(2))
Expect(container.Env).To(HaveKeyWithValue("ENV_VAR1", "value1"))
Expect(container.Env).To(HaveKeyWithValue("ENV_VAR2", "value2"))
Expect(container.IPAddresses).To(HaveLen(1))
Expect(container.IPAddresses["network1"]).To(Equal("192.168.0.1"))
})
})
})
}