Skip to content

Commit 077d5a9

Browse files
committed
fixup! fix(agent/agentcontainers): return host port instead of container port
1 parent f52142f commit 077d5a9

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

agent/agentcontainers/containers_dockercli.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,13 @@ func convertDockerInspect(in dockerInspect) (codersdk.WorkspaceAgentDevcontainer
400400
// Track host ports to avoid duplicates (same port on multiple interfaces)
401401
seenHostPorts := make(map[string]bool)
402402

403-
// Get all port mappings
403+
// Get port mappings in sorted order for deterministic output
404404
portKeys := maps.Keys(in.NetworkSettings.Ports)
405-
// Sort the ports for deterministic output
406405
sort.Strings(portKeys)
407406

408-
for containerPortSpec, bindings := range in.NetworkSettings.Ports {
407+
for _, containerPortSpec := range portKeys {
408+
bindings := in.NetworkSettings.Ports[containerPortSpec]
409+
409410
// Skip if no bindings exist (don't expose the container port)
410411
if len(bindings) == 0 {
411412
continue

agent/agentcontainers/containers_internal_test.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"os"
66
"slices"
7-
"sort"
87
"strconv"
98
"strings"
109
"testing"
1110
"time"
1211

1312
"go.uber.org/mock/gomock"
1413

14+
"github.com/google/go-cmp/cmp"
1515
"github.com/google/uuid"
1616
"github.com/ory/dockertest/v3"
1717
"github.com/ory/dockertest/v3/docker"
@@ -307,7 +307,6 @@ func TestContainersHandler(t *testing.T) {
307307
})
308308
}
309309

310-
// TestDockerPortBinding tests the port binding handling in convertDockerInspect
311310
func TestDockerPortBinding(t *testing.T) {
312311
t.Parallel()
313312

@@ -421,7 +420,6 @@ func TestDockerPortBinding(t *testing.T) {
421420
t.Run(tc.name, func(t *testing.T) {
422421
t.Parallel()
423422

424-
// Create a sample docker inspection result
425423
dockerData := dockerInspect{
426424
ID: "test-container",
427425
Created: time.Now(),
@@ -436,34 +434,11 @@ func TestDockerPortBinding(t *testing.T) {
436434
},
437435
}
438436

439-
// Process the docker data
440437
container, warns := convertDockerInspect(dockerData)
441-
442-
// Verify the ports
443-
assert.Len(t, container.Ports, len(tc.expectedPorts), "wrong number of ports")
444-
assert.Len(t, warns, tc.expectedWarns, "wrong number of warnings")
445-
446-
// Sort ports for consistent comparison (order may vary)
447-
sort.Slice(container.Ports, func(i, j int) bool {
448-
if container.Ports[i].Network == container.Ports[j].Network {
449-
return container.Ports[i].Port < container.Ports[j].Port
450-
}
451-
return container.Ports[i].Network < container.Ports[j].Network
452-
})
453-
sort.Slice(tc.expectedPorts, func(i, j int) bool {
454-
if tc.expectedPorts[i].Network == tc.expectedPorts[j].Network {
455-
return tc.expectedPorts[i].Port < tc.expectedPorts[j].Port
456-
}
457-
return tc.expectedPorts[i].Network < tc.expectedPorts[j].Network
458-
})
459-
460-
// Compare ports
461-
for i, expected := range tc.expectedPorts {
462-
if i < len(container.Ports) {
463-
assert.Equal(t, expected.Network, container.Ports[i].Network, "network mismatch")
464-
assert.Equal(t, expected.Port, container.Ports[i].Port, "port mismatch")
465-
}
438+
if diff := cmp.Diff(tc.expectedPorts, container.Ports); diff != "" {
439+
assert.Failf(t, "port mismatch", "(-want +got):\n%s", diff)
466440
}
441+
assert.Len(t, warns, tc.expectedWarns, "wrong number of warnings")
467442
})
468443
}
469444
}

0 commit comments

Comments
 (0)