-
Notifications
You must be signed in to change notification settings - Fork 875
feat(agent): implement recreate for devcontainers #17308
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6d18522
feat(agent): implement recreate for devcontainers
mafredri 6b08fd1
remove ai comments from test
mafredri db6157e
update comments
mafredri c214476
oopsie
mafredri 486bb3f
consistent var naming
mafredri 379b262
restructure test
mafredri ca791d6
wait medium just in case
mafredri 2692e55
mark testdata as generated
mafredri 2687bfd
allow paralleltest
mafredri b850b07
rename log writer
mafredri 6b8ad8c
fixup! allow paralleltest
mafredri 1d937a0
fixup! fixup! allow paralleltest
mafredri f5e2328
fixup! fixup! fixup! allow paralleltest
mafredri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package agentcontainers_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/agent/agentcontainers" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
// fakeLister implements the agentcontainers.Lister interface for | ||
// testing. | ||
type fakeLister struct { | ||
containers codersdk.WorkspaceAgentListContainersResponse | ||
err error | ||
} | ||
|
||
func (f *fakeLister) List(_ context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) { | ||
return f.containers, f.err | ||
} | ||
|
||
// fakeDevcontainerCLI implements the agentcontainers.DevcontainerCLI | ||
// interface for testing. | ||
type fakeDevcontainerCLI struct { | ||
id string | ||
err error | ||
} | ||
|
||
func (f *fakeDevcontainerCLI) Up(_ context.Context, _, _ string, _ ...agentcontainers.DevcontainerCLIUpOptions) (string, error) { | ||
return f.id, f.err | ||
} | ||
|
||
func TestHandler(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Recreate", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
validContainer := codersdk.WorkspaceAgentContainer{ | ||
ID: "container-id", | ||
FriendlyName: "container-name", | ||
Labels: map[string]string{ | ||
agentcontainers.DevcontainerLocalFolderLabel: "/workspace", | ||
agentcontainers.DevcontainerConfigFileLabel: "/workspace/.devcontainer/devcontainer.json", | ||
}, | ||
} | ||
|
||
missingFolderContainer := codersdk.WorkspaceAgentContainer{ | ||
ID: "missing-folder-container", | ||
FriendlyName: "missing-folder-container", | ||
Labels: map[string]string{}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
containerID string | ||
lister *fakeLister | ||
devcontainerCLI *fakeDevcontainerCLI | ||
wantStatus int | ||
wantBody string | ||
}{ | ||
{ | ||
name: "Missing ID", | ||
containerID: "", | ||
lister: &fakeLister{}, | ||
devcontainerCLI: &fakeDevcontainerCLI{}, | ||
wantStatus: http.StatusBadRequest, | ||
wantBody: "Missing container ID or name", | ||
}, | ||
{ | ||
name: "List error", | ||
containerID: "container-id", | ||
lister: &fakeLister{ | ||
err: xerrors.New("list error"), | ||
}, | ||
devcontainerCLI: &fakeDevcontainerCLI{}, | ||
wantStatus: http.StatusInternalServerError, | ||
wantBody: "Could not list containers", | ||
}, | ||
{ | ||
name: "Container not found", | ||
containerID: "nonexistent-container", | ||
lister: &fakeLister{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{validContainer}, | ||
}, | ||
}, | ||
devcontainerCLI: &fakeDevcontainerCLI{}, | ||
wantStatus: http.StatusNotFound, | ||
wantBody: "Container not found", | ||
}, | ||
{ | ||
name: "Missing workspace folder label", | ||
containerID: "missing-folder-container", | ||
lister: &fakeLister{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{missingFolderContainer}, | ||
}, | ||
}, | ||
devcontainerCLI: &fakeDevcontainerCLI{}, | ||
wantStatus: http.StatusBadRequest, | ||
wantBody: "Missing workspace folder label", | ||
}, | ||
{ | ||
name: "Devcontainer CLI error", | ||
containerID: "container-id", | ||
lister: &fakeLister{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{validContainer}, | ||
}, | ||
}, | ||
devcontainerCLI: &fakeDevcontainerCLI{ | ||
err: xerrors.New("devcontainer CLI error"), | ||
}, | ||
wantStatus: http.StatusInternalServerError, | ||
wantBody: "Could not recreate devcontainer", | ||
}, | ||
{ | ||
name: "OK", | ||
containerID: "container-id", | ||
lister: &fakeLister{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{validContainer}, | ||
}, | ||
}, | ||
devcontainerCLI: &fakeDevcontainerCLI{}, | ||
wantStatus: http.StatusNoContent, | ||
wantBody: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Setup router with the handler under test. | ||
r := chi.NewRouter() | ||
handler := agentcontainers.New( | ||
agentcontainers.WithLister(tt.lister), | ||
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI), | ||
) | ||
r.Post("/containers/{id}/recreate", handler.Recreate) | ||
|
||
// Simulate HTTP request to the recreate endpoint. | ||
req := httptest.NewRequest(http.MethodPost, "/containers/"+tt.containerID+"/recreate", nil) | ||
rec := httptest.NewRecorder() | ||
r.ServeHTTP(rec, req) | ||
|
||
// Check the response status code and body. | ||
require.Equal(t, tt.wantStatus, rec.Code, "status code mismatch") | ||
if tt.wantBody != "" { | ||
assert.Contains(t, rec.Body.String(), tt.wantBody, "response body mismatch") | ||
} else if tt.wantStatus == http.StatusNoContent { | ||
assert.Empty(t, rec.Body.String(), "expected empty response body") | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential follow-up (non-blocking): I wonder if we should also add an
Inspect()
method to theLister
; listing the entire containers endpoint is probably wasteful.