(t *testing.T)
| 2035 | } |
| 2036 | |
| 2037 | func TestSSH_Container(t *testing.T) { |
| 2038 | t.Parallel() |
| 2039 | if runtime.GOOS != "linux" { |
| 2040 | t.Skip("Skipping test on non-Linux platform") |
| 2041 | } |
| 2042 | |
| 2043 | t.Run("OK", func(t *testing.T) { |
| 2044 | t.Parallel() |
| 2045 | |
| 2046 | client, workspace, agentToken := setupWorkspaceForAgent(t) |
| 2047 | pool, err := dockertest.NewPool("") |
| 2048 | require.NoError(t, err, "Could not connect to docker") |
| 2049 | ct, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 2050 | Repository: "busybox", |
| 2051 | Tag: "latest", |
| 2052 | Cmd: []string{"sleep", "infnity"}, |
| 2053 | }, func(config *docker.HostConfig) { |
| 2054 | config.AutoRemove = true |
| 2055 | config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 2056 | }) |
| 2057 | require.NoError(t, err, "Could not start container") |
| 2058 | // Wait for container to start |
| 2059 | require.Eventually(t, func() bool { |
| 2060 | ct, ok := pool.ContainerByName(ct.Container.Name) |
| 2061 | return ok && ct.Container.State.Running |
| 2062 | }, testutil.WaitShort, testutil.IntervalSlow, "Container did not start in time") |
| 2063 | t.Cleanup(func() { |
| 2064 | err := pool.Purge(ct) |
| 2065 | require.NoError(t, err, "Could not stop container") |
| 2066 | }) |
| 2067 | |
| 2068 | _ = agenttest.New(t, client.URL, agentToken, func(o *agent.Options) { |
| 2069 | o.Devcontainers = true |
| 2070 | o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, |
| 2071 | agentcontainers.WithProjectDiscovery(false), |
| 2072 | agentcontainers.WithContainerLabelIncludeFilter("this.label.does.not.exist.ignore.devcontainers", "true"), |
| 2073 | ) |
| 2074 | }) |
| 2075 | _ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait() |
| 2076 | |
| 2077 | inv, root := clitest.New(t, "ssh", workspace.Name, "-c", ct.Container.ID) |
| 2078 | clitest.SetupConfig(t, client, root) |
| 2079 | ptty := ptytest.New(t).Attach(inv) |
| 2080 | |
| 2081 | ctx := testutil.Context(t, testutil.WaitLong) |
| 2082 | cmdDone := tGo(t, func() { |
| 2083 | err := inv.WithContext(ctx).Run() |
| 2084 | assert.NoError(t, err) |
| 2085 | }) |
| 2086 | |
| 2087 | ptty.ExpectMatchContext(ctx, " #") |
| 2088 | ptty.WriteLine("hostname") |
| 2089 | ptty.ExpectMatchContext(ctx, ct.Container.Config.Hostname) |
| 2090 | ptty.WriteLine("exit") |
| 2091 | <-cmdDone |
| 2092 | }) |
| 2093 | |
| 2094 | t.Run("NotFound", func(t *testing.T) { |
nothing calls this directly
no test coverage detected