|
| 1 | +package agentcontainers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/go-chi/chi/v5" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "golang.org/x/xerrors" |
| 13 | + |
| 14 | + "github.com/coder/coder/v2/agent/agentcontainers" |
| 15 | + "github.com/coder/coder/v2/codersdk" |
| 16 | +) |
| 17 | + |
| 18 | +// fakeLister implements the agentcontainers.Lister interface for |
| 19 | +// testing. |
| 20 | +type fakeLister struct { |
| 21 | + containers codersdk.WorkspaceAgentListContainersResponse |
| 22 | + err error |
| 23 | +} |
| 24 | + |
| 25 | +func (f *fakeLister) List(_ context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) { |
| 26 | + return f.containers, f.err |
| 27 | +} |
| 28 | + |
| 29 | +// fakeDevcontainerCLI implements the agentcontainers.DevcontainerCLI |
| 30 | +// interface for testing. |
| 31 | +type fakeDevcontainerCLI struct { |
| 32 | + id string |
| 33 | + err error |
| 34 | +} |
| 35 | + |
| 36 | +func (f *fakeDevcontainerCLI) Up(_ context.Context, _, _ string, _ ...agentcontainers.DevcontainerCLIUpOptions) (string, error) { |
| 37 | + return f.id, f.err |
| 38 | +} |
| 39 | + |
| 40 | +func TestHandler(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + t.Run("Recreate", func(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + validContainer := codersdk.WorkspaceAgentContainer{ |
| 47 | + ID: "container-id", |
| 48 | + FriendlyName: "container-name", |
| 49 | + Labels: map[string]string{ |
| 50 | + agentcontainers.DevcontainerLocalFolderLabel: "/workspace", |
| 51 | + agentcontainers.DevcontainerConfigFileLabel: "/workspace/.devcontainer/devcontainer.json", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + missingFolderContainer := codersdk.WorkspaceAgentContainer{ |
| 56 | + ID: "missing-folder-container", |
| 57 | + FriendlyName: "missing-folder-container", |
| 58 | + Labels: map[string]string{}, |
| 59 | + } |
| 60 | + |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + containerID string |
| 64 | + lister *fakeLister |
| 65 | + devcontainerCLI *fakeDevcontainerCLI |
| 66 | + wantStatus int |
| 67 | + wantBody string |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "Missing ID", |
| 71 | + containerID: "", |
| 72 | + lister: &fakeLister{}, |
| 73 | + devcontainerCLI: &fakeDevcontainerCLI{}, |
| 74 | + wantStatus: http.StatusBadRequest, |
| 75 | + wantBody: "Missing container ID or name", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "List error", |
| 79 | + containerID: "container-id", |
| 80 | + lister: &fakeLister{ |
| 81 | + err: xerrors.New("list error"), |
| 82 | + }, |
| 83 | + devcontainerCLI: &fakeDevcontainerCLI{}, |
| 84 | + wantStatus: http.StatusInternalServerError, |
| 85 | + wantBody: "Could not list containers", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "Container not found", |
| 89 | + containerID: "nonexistent-container", |
| 90 | + lister: &fakeLister{ |
| 91 | + containers: codersdk.WorkspaceAgentListContainersResponse{ |
| 92 | + Containers: []codersdk.WorkspaceAgentContainer{validContainer}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + devcontainerCLI: &fakeDevcontainerCLI{}, |
| 96 | + wantStatus: http.StatusNotFound, |
| 97 | + wantBody: "Container not found", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "Missing workspace folder label", |
| 101 | + containerID: "missing-folder-container", |
| 102 | + lister: &fakeLister{ |
| 103 | + containers: codersdk.WorkspaceAgentListContainersResponse{ |
| 104 | + Containers: []codersdk.WorkspaceAgentContainer{missingFolderContainer}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + devcontainerCLI: &fakeDevcontainerCLI{}, |
| 108 | + wantStatus: http.StatusBadRequest, |
| 109 | + wantBody: "Missing workspace folder label", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Devcontainer CLI error", |
| 113 | + containerID: "container-id", |
| 114 | + lister: &fakeLister{ |
| 115 | + containers: codersdk.WorkspaceAgentListContainersResponse{ |
| 116 | + Containers: []codersdk.WorkspaceAgentContainer{validContainer}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + devcontainerCLI: &fakeDevcontainerCLI{ |
| 120 | + err: xerrors.New("devcontainer CLI error"), |
| 121 | + }, |
| 122 | + wantStatus: http.StatusInternalServerError, |
| 123 | + wantBody: "Could not recreate devcontainer", |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "OK", |
| 127 | + containerID: "container-id", |
| 128 | + lister: &fakeLister{ |
| 129 | + containers: codersdk.WorkspaceAgentListContainersResponse{ |
| 130 | + Containers: []codersdk.WorkspaceAgentContainer{validContainer}, |
| 131 | + }, |
| 132 | + }, |
| 133 | + devcontainerCLI: &fakeDevcontainerCLI{}, |
| 134 | + wantStatus: http.StatusNoContent, |
| 135 | + wantBody: "", |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + t.Parallel() |
| 142 | + |
| 143 | + // Setup router with the handler under test. |
| 144 | + r := chi.NewRouter() |
| 145 | + handler := agentcontainers.New( |
| 146 | + agentcontainers.WithLister(tt.lister), |
| 147 | + agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI), |
| 148 | + ) |
| 149 | + r.Post("/containers/{id}/recreate", handler.Recreate) |
| 150 | + |
| 151 | + // Simulate HTTP request to the recreate endpoint. |
| 152 | + req := httptest.NewRequest(http.MethodPost, "/containers/"+tt.containerID+"/recreate", nil) |
| 153 | + rec := httptest.NewRecorder() |
| 154 | + r.ServeHTTP(rec, req) |
| 155 | + |
| 156 | + // Check the response status code and body. |
| 157 | + require.Equal(t, tt.wantStatus, rec.Code, "status code mismatch") |
| 158 | + if tt.wantBody != "" { |
| 159 | + assert.Contains(t, rec.Body.String(), tt.wantBody, "response body mismatch") |
| 160 | + } else if tt.wantStatus == http.StatusNoContent { |
| 161 | + assert.Empty(t, rec.Body.String(), "expected empty response body") |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | + }) |
| 166 | +} |
0 commit comments