Skip to content

Commit 8e38e55

Browse files
committed
Fix config.exposedports value #76
1 parent 09cfaef commit 8e38e55

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

internal/server/routes/docker/containers.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func getContainerInfo(cr *common.ContextRouter, tainr *types.Container, detail b
296296
"Env": tainr.Env,
297297
"Cmd": tainr.Cmd,
298298
"Hostname": "localhost",
299-
"ExposedPorts": getNetworkSettingsPorts(cr, tainr),
299+
"ExposedPorts": getConfigExposedPorts(cr, tainr),
300300
"Tty": false,
301301
}
302302
res["Created"] = tainr.Created.Format("2006-01-02T15:04:05Z")
@@ -336,6 +336,19 @@ func getNetworkSettingsPorts(cr *common.ContextRouter, tainr *types.Container) g
336336
return res
337337
}
338338

339+
// getConfigExposedPorts will return the available ports of the container
340+
// as a gin.H json structure to be used in container config details.
341+
func getConfigExposedPorts(cr *common.ContextRouter, tainr *types.Container) gin.H {
342+
res := gin.H{}
343+
if tainr.HostIP == "" {
344+
return res
345+
}
346+
for dst := range getAvailablePorts(cr, tainr) {
347+
res[fmt.Sprintf("%d/tcp", dst)] = gin.H{}
348+
}
349+
return res
350+
}
351+
339352
// getContainerPorts will return the available ports of the container as
340353
// a gin.H json structure to be used in container list.
341354
func getContainerPorts(cr *common.ContextRouter, tainr *types.Container) []map[string]interface{} {

internal/server/routes/docker/containers_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,81 @@ func TestGetNetworkSettingsPorts(t *testing.T) {
9090
}
9191
}
9292

93+
func TestGetConfigExposedPorts(t *testing.T) {
94+
tests := []struct {
95+
tainr *types.Container
96+
endp EndpointConfig
97+
out gin.H
98+
portfw bool
99+
}{
100+
{
101+
tainr: &types.Container{
102+
HostIP: "",
103+
MappedPorts: map[int]int{303: 101},
104+
},
105+
out: gin.H{},
106+
portfw: true,
107+
},
108+
{
109+
tainr: &types.Container{
110+
HostIP: "127.0.0.1",
111+
MappedPorts: map[int]int{303: 101},
112+
},
113+
out: gin.H{"101/tcp": gin.H{}},
114+
portfw: true,
115+
},
116+
{
117+
tainr: &types.Container{
118+
HostIP: "127.0.0.1",
119+
HostPorts: map[int]int{303: 101},
120+
},
121+
out: gin.H{"101/tcp": gin.H{}},
122+
portfw: true,
123+
},
124+
{
125+
tainr: &types.Container{
126+
HostIP: "127.0.0.1",
127+
MappedPorts: map[int]int{303: 101},
128+
HostPorts: map[int]int{303: 101},
129+
},
130+
out: gin.H{"101/tcp": gin.H{}},
131+
portfw: true,
132+
},
133+
{
134+
tainr: &types.Container{
135+
HostIP: "127.0.0.1",
136+
MappedPorts: map[int]int{-303: 303},
137+
},
138+
out: gin.H{},
139+
portfw: true,
140+
},
141+
{
142+
tainr: &types.Container{
143+
HostIP: "127.0.0.1",
144+
MappedPorts: map[int]int{303: 101},
145+
HostPorts: map[int]int{202: 101},
146+
},
147+
out: gin.H{"101/tcp": gin.H{}},
148+
portfw: true,
149+
},
150+
{
151+
tainr: &types.Container{
152+
HostIP: "127.0.0.1",
153+
MappedPorts: map[int]int{303: 101},
154+
},
155+
out: gin.H{"101/tcp": gin.H{}},
156+
portfw: false,
157+
},
158+
}
159+
for i, tst := range tests {
160+
cr := &common.ContextRouter{Config: common.Config{PortForward: tst.portfw}}
161+
res := getConfigExposedPorts(cr, tst.tainr)
162+
if !reflect.DeepEqual(res, tst.out) {
163+
t.Errorf("failed test %d - expected %s, but got %s", i, tst.out, res)
164+
}
165+
}
166+
}
167+
93168
func TestGetContainerPorts(t *testing.T) {
94169
tests := []struct {
95170
tainr *types.Container

0 commit comments

Comments
 (0)