Skip to content

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 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
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make fmt lint
  • Loading branch information
johnstcn committed Mar 13, 2025
commit 8338af35d999f236b2faa108cd2a46de3fd464f0
8 changes: 5 additions & 3 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,12 @@ func TestWorkspaceAgentContainers(t *testing.T) {
Labels: testLabels,
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: 80,
Network: "tcp",
Port: 80,
HostIP: "0.0.0.0",
HostPort: 8000,
},
},
Volumes: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Meta, StoryObj } from "@storybook/react";
import { AgentDevcontainerCard } from "./AgentDevcontainerCard";
import {
MockWorkspace,
MockWorkspaceAgentDevcontainer,
MockWorkspaceAgentDevcontainerPorts,
} from "testHelpers/entities";
import { AgentDevcontainerCard } from "./AgentDevcontainerCard";

const meta: Meta<typeof AgentDevcontainerCard> = {
title: "modules/resources/AgentDevcontainerCard",
Expand Down
8 changes: 4 additions & 4 deletions site/src/modules/resources/AgentDevcontainerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from "@mui/material/Link";
import Tooltip, { type TooltipProps } from "@mui/material/Tooltip";
import type {
Workspace,
WorkspaceAgentDevcontainer,
Expand All @@ -10,7 +11,6 @@ import { portForwardURL } from "utils/portForward";
import { AgentButton } from "./AgentButton";
import { AgentDevcontainerSSHButton } from "./SSHButton/SSHButton";
import { TerminalLink } from "./TerminalLink/TerminalLink";
import Tooltip, { type TooltipProps } from "@mui/material/Tooltip";

type AgentDevcontainerCardProps = {
container: WorkspaceAgentDevcontainer;
Expand Down Expand Up @@ -52,13 +52,13 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
/>
{wildcardHostname !== "" &&
container.ports.map((port) => {
let portLabel = `${port.port}/${port.network.toUpperCase()}`;
let hasHostBind =
const portLabel = `${port.port}/${port.network.toUpperCase()}`;
const hasHostBind =
port.host_port !== undefined &&
port.host_port !== null &&
port.host_ip !== undefined &&
port.host_ip !== null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding this quite strange. Why not only return from the API null or undefined? Having the API returning both types look strange to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this seems like it was meant to be double-bookkeeping, but with how the server types are set up, the null conditionals won't ever trigger. The omitEmpty JSON tag will make sure that if the values of host_ip or host_port are missing, they'll always evaluate to undefined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Will remove the extraneous null checks.

let helperText = hasHostBind
const helperText = hasHostBind
? `${port.host_ip}:${port.host_port}`
: "Not bound to host";
return (
Expand Down
78 changes: 39 additions & 39 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4273,45 +4273,45 @@ function mockTwoDaysAgo() {
return date.toISOString();
}

export const MockWorkspaceAgentDevcontainerPorts: TypesGen.WorkspaceAgentDevcontainerPort[] = [
{
port: 1000,
network: "tcp",
host_port: 1000,
host_ip: "0.0.0.0"
},
{
port: 2001,
network: "tcp",
host_port: 2000,
host_ip: "::1"
},
export const MockWorkspaceAgentDevcontainerPorts: TypesGen.WorkspaceAgentDevcontainerPort[] =
[
{
port: 1000,
network: "tcp",
host_port: 1000,
host_ip: "0.0.0.0",
},
{
port: 2001,
network: "tcp",
host_port: 2000,
host_ip: "::1",
},
{
port: 8888,
network: "tcp",
},
Comment on lines +4290 to +4293
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure: is it possible for there to be variants of the port value that have host_port but not host_ip, or vice-versa?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it likely in practice.

];

export const MockWorkspaceAgentDevcontainer: TypesGen.WorkspaceAgentDevcontainer =
{
port: 8888,
network: "tcp",
}
]

export const MockWorkspaceAgentDevcontainer : TypesGen.WorkspaceAgentDevcontainer = {
created_at: "2024-01-04T15:53:03.21563Z",
id: "abcd1234",
name: "container-1",
image: "ubuntu:latest",
labels: {
"foo": "bar"
},
ports: [],
running: true,
status: "running",
volumes: {
"/mnt/volume1": "/volume1",
}
}
created_at: "2024-01-04T15:53:03.21563Z",
id: "abcd1234",
name: "container-1",
image: "ubuntu:latest",
labels: {
foo: "bar",
},
ports: [],
running: true,
status: "running",
volumes: {
"/mnt/volume1": "/volume1",
},
};

export const MockWorkspaceAgentListContainersResponse: TypesGen.WorkspaceAgentListContainersResponse =
{
containers: [
MockWorkspaceAgentDevcontainer,
],
"warnings": ["This is a warning"],
}
{
containers: [MockWorkspaceAgentDevcontainer],
warnings: ["This is a warning"],
};
Loading