- 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
…pect
- 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 |
---|---|---|
@@ -3,6 +3,7 @@ package agentcontainers | ||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
@@ -11,6 +12,7 @@ import ( | ||
"go.uber.org/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/uuid" | ||
"github.com/ory/dockertest/v3" | ||
"github.com/ory/dockertest/v3/docker" | ||
@@ -412,6 +414,216 @@ func TestConvertDockerVolume(t *testing.T) { | ||
} | ||
} | ||
// TestConvertDockerInspect tests the convertDockerInspect function using | ||
// fixtures from ./testdata. | ||
func TestConvertDockerInspect(t *testing.T) { | ||
t.Parallel() | ||
for _, tt := range []struct { | ||
name string | ||
expect []codersdk.WorkspaceAgentDevcontainer | ||
expectWarns []string | ||
expectError string | ||
}{ | ||
{ | ||
name: "container_simple", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 55, 58, 91280203, time.UTC), | ||
ID: "6b539b8c60f5230b8b0fde2502cd2332d31c0d526a3e6eb6eef1cc39439b3286", | ||
FriendlyName: "eloquent_kowalevski", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "container_labels", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 20, 3, 28, 71706536, time.UTC), | ||
ID: "bd8818e670230fc6f36145b21cf8d6d35580355662aa4d9fe5ae1b188a4c905f", | ||
FriendlyName: "fervent_bardeen", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{"baz": "zap", "foo": "bar"}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "container_binds", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 58, 43, 522505027, time.UTC), | ||
ID: "fdc75ebefdc0243c0fce959e7685931691ac7aede278664a0e2c23af8a1e8d6a", | ||
FriendlyName: "silly_beaver", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{ | ||
"/tmp/test/a": "/var/coder/a", | ||
"/tmp/test/b": "/var/coder/b", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "container_sameport", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 56, 34, 842164541, time.UTC), | ||
ID: "4eac5ce199d27b2329d0ff0ce1a6fc595612ced48eba3669aadb6c57ebef3fa2", | ||
FriendlyName: "modest_varahamihira", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{ | ||
{ | ||
Network: "tcp", | ||
Port: 12345, | ||
}, | ||
}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "container_differentport", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 57, 8, 862545133, time.UTC), | ||
ID: "3090de8b72b1224758a94a11b827c82ba2b09c45524f1263dc4a2d83e19625ea", | ||
FriendlyName: "boring_ellis", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{ | ||
{ | ||
Network: "tcp", | ||
Port: 23456, | ||
}, | ||
}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "container_volume", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 59, 42, 39484134, time.UTC), | ||
ID: "b3688d98c007f53402a55e46d803f2f3ba9181d8e3f71a2eb19b392cf0377b4e", | ||
FriendlyName: "upbeat_carver", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{}, | ||
Running: true, | ||
Status: "running", | ||
Ports: []codersdk.WorkspaceAgentListeningPort{}, | ||
Volumes: map[string]string{ | ||
"/var/lib/docker/volumes/testvol/_data": "/testvol", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "devcontainer_simple", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 1, 5, 751972661, time.UTC), | ||
ID: "0b2a9fcf5727d9562943ce47d445019f4520e37a2aa7c6d9346d01af4f4f9aed", | ||
FriendlyName: "optimistic_hopper", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{ | ||
"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{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "devcontainer_forwardport", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 3, 55, 22053072, time.UTC), | ||
ID: "4a16af2293fb75dc827a6949a3905dd57ea28cc008823218ce24fab1cb66c067", | ||
FriendlyName: "serene_khayyam", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{ | ||
"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{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "devcontainer_appport", | ||
expect: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
CreatedAt: time.Date(2025, 3, 11, 17, 2, 42, 613747761, time.UTC), | ||
ID: "52d23691f4b954d083f117358ea763e20f69af584e1c08f479c5752629ee0be3", | ||
FriendlyName: "suspicious_margulis", | ||
Image: "debian:bookworm", | ||
Labels: map[string]string{ | ||
"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", | ||
Port: 8080, | ||
}, | ||
}, | ||
Volumes: map[string]string{}, | ||
}, | ||
}, | ||
}, | ||
} { | ||
// nolint:paralleltest // variable recapture no longer required | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
bs, err := os.ReadFile(filepath.Join("testdata", tt.name+".json")) | ||
johnstcn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
require.NoError(t, err, "failed to read testdata file") | ||
actual, warns, err := convertDockerInspect(string(bs)) | ||
if len(tt.expectWarns) > 0 { | ||
assert.Len(t, warns, len(tt.expectWarns), "expected warnings") | ||
for _, warn := range tt.expectWarns { | ||
assert.Contains(t, warns, warn) | ||
} | ||
} | ||
if tt.expectError != "" { | ||
assert.Empty(t, actual, "expected no data") | ||
assert.ErrorContains(t, err, tt.expectError) | ||
return | ||
} | ||
require.NoError(t, err, "expected no error") | ||
if diff := cmp.Diff(tt.expect, actual); diff != "" { | ||
t.Errorf("unexpected diff (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
// TestDockerEnvInfoer tests the ability of EnvInfo to extract information from | ||
// running containers. Containers are deleted after the test is complete. | ||
// As this test creates containers, it is skipped by default. | ||
Uh oh!
There was an error while loading.Please reload this page.