Skip to content

Commit

Permalink
Fix config.exposedports value #76
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed Mar 3, 2024
1 parent 09cfaef commit 8e38e55
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/server/routes/docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func getContainerInfo(cr *common.ContextRouter, tainr *types.Container, detail b
"Env": tainr.Env,
"Cmd": tainr.Cmd,
"Hostname": "localhost",
"ExposedPorts": getNetworkSettingsPorts(cr, tainr),
"ExposedPorts": getConfigExposedPorts(cr, tainr),
"Tty": false,
}
res["Created"] = tainr.Created.Format("2006-01-02T15:04:05Z")
Expand Down Expand Up @@ -336,6 +336,19 @@ func getNetworkSettingsPorts(cr *common.ContextRouter, tainr *types.Container) g
return res
}

// getConfigExposedPorts will return the available ports of the container
// as a gin.H json structure to be used in container config details.
func getConfigExposedPorts(cr *common.ContextRouter, tainr *types.Container) gin.H {
res := gin.H{}
if tainr.HostIP == "" {
return res
}
for dst := range getAvailablePorts(cr, tainr) {
res[fmt.Sprintf("%d/tcp", dst)] = gin.H{}
}
return res
}

// getContainerPorts will return the available ports of the container as
// a gin.H json structure to be used in container list.
func getContainerPorts(cr *common.ContextRouter, tainr *types.Container) []map[string]interface{} {
Expand Down
75 changes: 75 additions & 0 deletions internal/server/routes/docker/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,81 @@ func TestGetNetworkSettingsPorts(t *testing.T) {
}
}

func TestGetConfigExposedPorts(t *testing.T) {
tests := []struct {
tainr *types.Container
endp EndpointConfig
out gin.H
portfw bool
}{
{
tainr: &types.Container{
HostIP: "",
MappedPorts: map[int]int{303: 101},
},
out: gin.H{},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
MappedPorts: map[int]int{303: 101},
},
out: gin.H{"101/tcp": gin.H{}},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
HostPorts: map[int]int{303: 101},
},
out: gin.H{"101/tcp": gin.H{}},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
MappedPorts: map[int]int{303: 101},
HostPorts: map[int]int{303: 101},
},
out: gin.H{"101/tcp": gin.H{}},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
MappedPorts: map[int]int{-303: 303},
},
out: gin.H{},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
MappedPorts: map[int]int{303: 101},
HostPorts: map[int]int{202: 101},
},
out: gin.H{"101/tcp": gin.H{}},
portfw: true,
},
{
tainr: &types.Container{
HostIP: "127.0.0.1",
MappedPorts: map[int]int{303: 101},
},
out: gin.H{"101/tcp": gin.H{}},
portfw: false,
},
}
for i, tst := range tests {
cr := &common.ContextRouter{Config: common.Config{PortForward: tst.portfw}}
res := getConfigExposedPorts(cr, tst.tainr)
if !reflect.DeepEqual(res, tst.out) {
t.Errorf("failed test %d - expected %s, but got %s", i, tst.out, res)
}
}
}

func TestGetContainerPorts(t *testing.T) {
tests := []struct {
tainr *types.Container
Expand Down

0 comments on commit 8e38e55

Please sign in to comment.