Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffa51ec

Browse files
committed
[ci skip] extract variable, remove TODOs
1 parent 7e2ff9a commit ffa51ec

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

agent/containers.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
const (
2323
defaultGetContainersCacheDuration = 10 * time.Second
24+
dockerCreatedAtTimeFormat = "2006-01-02 15:04:05 -0700 MST"
2425
getContainersTimeout = 5 * time.Second
2526
)
2627

@@ -56,6 +57,8 @@ func (ch *containersHandler) getContainers(ctx context.Context) ([]codersdk.Work
5657
ch.cacheDuration = defaultGetContainersCacheDuration
5758
}
5859
if ch.cl == nil {
60+
// TODO(cian): we may need some way to select the desired
61+
// implementation, but for now there is only one.
5962
ch.cl = &dockerCLIContainerLister{}
6063
}
6164
if ch.containers == nil {
@@ -94,6 +97,8 @@ type ContainerLister interface {
9497
// dockerCLIContainerLister is a ContainerLister that lists containers using the docker CLI
9598
type dockerCLIContainerLister struct{}
9699

100+
var _ ContainerLister = &dockerCLIContainerLister{}
101+
97102
type dockerCLIList struct {
98103
CreatedAt string `json:"CreatedAt"`
99104
ID string `json:"ID"`
@@ -107,7 +112,7 @@ func (*dockerCLIContainerLister) List(ctx context.Context) ([]codersdk.Workspace
107112
cmd := exec.CommandContext(ctx, "docker", "ps", "--all", "--no-trunc", "--format=json")
108113
cmd.Stdout = &buf
109114
if err := cmd.Run(); err != nil {
110-
return nil, xerrors.Errorf("list containers: %w", err)
115+
return nil, xerrors.Errorf("run docker ps: %w", err)
111116
}
112117

113118
// the output is returned with a single item per line, so we have to decode it
@@ -119,7 +124,7 @@ func (*dockerCLIContainerLister) List(ctx context.Context) ([]codersdk.Workspace
119124
continue
120125
}
121126
if err := json.NewDecoder(strings.NewReader(line)).Decode(&tmp); err != nil {
122-
return nil, xerrors.Errorf("list containers: %w", err)
127+
return nil, xerrors.Errorf("decode docker ps output: %w", err)
123128
}
124129
out = append(out, convertDockerCLIList(tmp))
125130
}
@@ -134,17 +139,17 @@ func convertDockerCLIList(in dockerCLIList) codersdk.WorkspaceAgentContainer {
134139
Labels: map[string]string{},
135140
}
136141

137-
createdAt, err := time.Parse("2006-01-02 15:04:05 -0700 MST", in.CreatedAt)
142+
createdAt, err := time.Parse(dockerCreatedAtTimeFormat, in.CreatedAt)
138143
if err != nil {
139-
createdAt = time.Time{} // TODO: how to handle invalid createdAt?
144+
createdAt = time.Unix(0, 0)
140145
}
141146
out.CreatedAt = createdAt
142147

143148
labels := strings.Split(in.Labels, ",")
144149
for _, label := range labels {
145150
kvs := strings.Split(label, "=")
146151
if len(kvs) != 2 {
147-
continue // TODO: how should we handle this weirdness?
152+
continue // skip invalid labels
148153
}
149154
out.Labels[kvs[0]] = kvs[1]
150155
}

agent/containers_internal_test.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDockerCLIContainerLister(t *testing.T) {
3131
pool, err := dockertest.NewPool("")
3232
require.NoError(t, err, "Could not connect to docker")
3333
testLabelValue := uuid.New().String()
34-
res, err := pool.RunWithOptions(&dockertest.RunOptions{
34+
ct, err := pool.RunWithOptions(&dockertest.RunOptions{
3535
Repository: "busybox",
3636
Tag: "latest",
3737
Cmd: []string{"sleep", "infnity"},
@@ -42,30 +42,23 @@ func TestDockerCLIContainerLister(t *testing.T) {
4242
})
4343
require.NoError(t, err, "Could not start test docker container")
4444
t.Cleanup(func() {
45-
assert.NoError(t, pool.Purge(res), "Could not purge resource")
45+
assert.NoError(t, pool.Purge(ct), "Could not purge resource")
4646
})
4747

48-
expectedCt := codersdk.WorkspaceAgentContainer{
49-
CreatedAt: res.Container.Created.Local().Truncate(time.Second),
50-
ID: res.Container.ID,
51-
// For some reason, ory/dockertest pre-pends a forward slash to the container name.
52-
FriendlyName: strings.TrimPrefix(res.Container.Name, "/"),
53-
// ory/dockertest returns the sha256 digest of the image.
54-
Image: "busybox:latest",
55-
// Labels: res.Container.Config.Labels, // unused
56-
}
5748
dcl := dockerCLIContainerLister{}
5849
ctx := testutil.Context(t, testutil.WaitShort)
5950
actual, err := dcl.List(ctx)
6051
require.NoError(t, err, "Could not list containers")
6152
var found bool
62-
for _, ct := range actual {
63-
if ct.Labels != nil && ct.Labels["com.coder.test"] == testLabelValue {
53+
for _, foundContainer := range actual {
54+
if foundContainer.ID == ct.Container.ID {
6455
found = true
65-
assert.Equal(t, expectedCt.CreatedAt, ct.CreatedAt)
66-
assert.Equal(t, expectedCt.FriendlyName, ct.FriendlyName)
67-
assert.Equal(t, expectedCt.ID, ct.ID)
68-
assert.Equal(t, expectedCt.Image, ct.Image)
56+
assert.Equal(t, ct.Container.Created.Local().Truncate(time.Second), foundContainer.CreatedAt)
57+
// ory/dockertest pre-pends a forward slash to the container name.
58+
assert.Equal(t, strings.TrimPrefix(ct.Container.Name, "/"), foundContainer.FriendlyName)
59+
// ory/dockertest returns the sha256 digest of the image.
60+
assert.Equal(t, "busybox:latest", foundContainer.Image)
61+
assert.Equal(t, ct.Container.Config.Labels, foundContainer.Labels)
6962
break
7063
}
7164
}

0 commit comments

Comments
 (0)