Skip to content

fix: Fix properly selecting workspace apps by agent #3684

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 12 commits into from
Aug 29, 2022
Prev Previous commit
Next Next commit
Add unit test cases
  • Loading branch information
Emyrk committed Aug 24, 2022
commit 8b3f46d50a3713a9153d162364ecff090b304850
8 changes: 5 additions & 3 deletions coderd/httpmw/workspaceparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Ha
return
}

// If we have more than 1 workspace agent, we need to specify which one to use.
var agent database.WorkspaceAgent
var found bool
if len(agents) > 1 {
// If we have more than 1 workspace agent, we need to specify which one to use.
// If the user specified an agent, we need to make sure that agent
// actually exists.
if len(workspaceParts) > 1 || len(agents) > 1 {
for _, otherAgent := range agents {
if otherAgent.Name == workspaceParts[1] {
agent = otherAgent
Expand All @@ -141,7 +143,7 @@ func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Ha
}
if !found {
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("No agent exists with the name %s", workspaceParts[1]),
Message: fmt.Sprintf("No agent exists with the name %q", workspaceParts[1]),
})
return
}
Expand Down
30 changes: 30 additions & 0 deletions coderd/httpmw/workspaceparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ func TestWorkspaceAgentByNameParam(t *testing.T) {
ExpectedStatusCode: http.StatusBadRequest,
ExpectedError: "More than one agent exists, but no agent specified",
},
{
Name: "NotExistsOneAgent",
WorkspaceName: "dev",
Agents: map[string][]string{
"resource-a": {
"agent-one",
},
},
UrlParam: "dev.not-exists",
ExpectedStatusCode: http.StatusBadRequest,
ExpectedError: "No agent exists with the name",
},
{
Name: "NotExistsMultipleAgents",
WorkspaceName: "dev",
Agents: map[string][]string{
"resource-a": {
"agent-one",
},
"resource-b": {
"agent-two",
},
"resource-c": {
"agent-three",
},
},
UrlParam: "dev.not-exists",
ExpectedStatusCode: http.StatusBadRequest,
ExpectedError: "No agent exists with the name",
},

// OKs
{
Expand Down