From 8e38e559e2e99a20c73d3d96dea8fcddfa0a3927 Mon Sep 17 00:00:00 2001 From: Vincent van Dam Date: Sun, 3 Mar 2024 13:07:54 +0100 Subject: [PATCH] Fix config.exposedports value #76 --- internal/server/routes/docker/containers.go | 15 +++- .../server/routes/docker/containers_test.go | 75 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/internal/server/routes/docker/containers.go b/internal/server/routes/docker/containers.go index 2293229..8a7c39a 100644 --- a/internal/server/routes/docker/containers.go +++ b/internal/server/routes/docker/containers.go @@ -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") @@ -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{} { diff --git a/internal/server/routes/docker/containers_test.go b/internal/server/routes/docker/containers_test.go index cf93376..d3da0cf 100644 --- a/internal/server/routes/docker/containers_test.go +++ b/internal/server/routes/docker/containers_test.go @@ -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