- Notifications
You must be signed in to change notification settings - Fork1k
fix(agent/agentcontainers): improve testing of convertDockerInspect, return correct host port#16887
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
80ac9a3
0ecceb0
55998d0
fb78d33
a7d1ea4
393f6e9
95b156e
f8f3000
999469f
8338af3
2f0180e
1ae6015
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -319,22 +319,23 @@ func runDockerInspect(ctx context.Context, execer agentexec.Execer, ids ...strin | ||
// To avoid a direct dependency on the Docker API, we use the docker CLI | ||
// to fetch information about containers. | ||
type dockerInspect struct { | ||
IDstring `json:"Id"` | ||
Createdtime.Time `json:"Created"` | ||
ConfigdockerInspectConfig `json:"Config"` | ||
Name string`json:"Name"` | ||
Mounts[]dockerInspectMount `json:"Mounts"` | ||
State dockerInspectState`json:"State"` | ||
NetworkSettings dockerInspectNetworkSettings`json:"NetworkSettings"` | ||
} | ||
type dockerInspectConfig struct { | ||
Image string `json:"Image"` | ||
Labels map[string]string `json:"Labels"` | ||
} | ||
type dockerInspectPort struct { | ||
HostIP string `json:"HostIp"` | ||
HostPort string `json:"HostPort"` | ||
} | ||
type dockerInspectMount struct { | ||
@@ -349,6 +350,10 @@ type dockerInspectState struct { | ||
Error string `json:"Error"` | ||
} | ||
type dockerInspectNetworkSettings struct { | ||
Ports map[string][]dockerInspectPort `json:"Ports"` | ||
} | ||
func (dis dockerInspectState) String() string { | ||
if dis.Running { | ||
return "running" | ||
@@ -388,20 +393,41 @@ func convertDockerInspect(raw string) ([]codersdk.WorkspaceAgentDevcontainer, [] | ||
Volumes: make(map[string]string, len(in.Mounts)), | ||
} | ||
if in.NetworkSettings.Ports == nil { | ||
in.NetworkSettings.Ports = make(map[string][]dockerInspectPort) | ||
} | ||
portKeys := maps.Keys(in.NetworkSettings.Ports) | ||
// Sort the ports for deterministic output. | ||
sort.Strings(portKeys) | ||
// Each port binding may have multiple entries mapped to the same interface. | ||
// Keep track of the ports we've already seen. | ||
johnstcn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
seen := make(map[int]struct{}, len(in.NetworkSettings.Ports)) | ||
| ||
for _, pk := range portKeys { | ||
for _, p := range in.NetworkSettings.Ports[pk] { | ||
_, network, err := convertDockerPort(pk) | ||
if err != nil { | ||
warns = append(warns, fmt.Sprintf("convert docker port: %s", err.Error())) | ||
// Default network to "tcp" if we can't parse it. | ||
network = "tcp" | ||
} | ||
hp, err := strconv.Atoi(p.HostPort) | ||
if err != nil { | ||
warns = append(warns, fmt.Sprintf("convert docker port: %s", err.Error())) | ||
continue | ||
} | ||
if hp > 65535 || hp < 0 { // invalid port | ||
warns = append(warns, fmt.Sprintf("convert docker port: invalid host port %d", hp)) | ||
continue | ||
} | ||
if _, ok := seen[hp]; ok { | ||
// We've already seen this port, so skip it. | ||
continue | ||
} | ||
out.Ports = append(out.Ports, codersdk.WorkspaceAgentListeningPort{ | ||
Network: network, | ||
Port:uint16(hp), | ||
}) | ||
seen[hp] = struct{}{} | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -357,7 +357,7 @@ func TestConvertDockerPort(t *testing.T) { | ||
expectError: "invalid port", | ||
}, | ||
} { | ||
//nolint: paralleltest //variable recapture no longer required | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
actualPort, actualNetwork, actualErr := convertDockerPort(tc.in) | ||
@@ -511,7 +511,7 @@ func TestConvertDockerInspect(t *testing.T) { | ||
Ports: []codersdk.WorkspaceAgentListeningPort{ | ||
{ | ||
Network: "tcp", | ||
Port:12345, | ||
}, | ||
}, | ||
Volumes: map[string]string{}, | ||
@@ -548,10 +548,10 @@ func TestConvertDockerInspect(t *testing.T) { | ||
"devcontainer.config_file": "/home/coder/src/coder/coder/agent/agentcontainers/testdata/devcontainer_simple.json", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. A little bit of coder inflation in this path 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. TBH there's probably a lot of "pruning" we can do in those fixtures, but I'd like to avoid changing them if possible :) | ||
"devcontainer.metadata": "[]", | ||
}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
@@ -567,10 +567,10 @@ func TestConvertDockerInspect(t *testing.T) { | ||
"devcontainer.config_file": "/home/coder/src/coder/coder/agent/agentcontainers/testdata/devcontainer_forwardport.json", | ||
"devcontainer.metadata": "[]", | ||
}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
@@ -586,12 +586,13 @@ func TestConvertDockerInspect(t *testing.T) { | ||
"devcontainer.config_file": "/home/coder/src/coder/coder/agent/agentcontainers/testdata/devcontainer_appport.json", | ||
"devcontainer.metadata": "[]", | ||
}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{ | ||
{ | ||
Network: "tcp", | ||
// Container port 8080 is mapped to 32768 on the host. | ||
Port: 32768, | ||
}, | ||
}, | ||
Volumes: map[string]string{}, | ||