|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "cdr.dev/coder-cli/ci/tcli" |
| 14 | + "cdr.dev/slog/sloggers/slogtest/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func build(path string) error { |
| 18 | + cmd := exec.Command( |
| 19 | + "sh", "-c", |
| 20 | + fmt.Sprintf("cd ../../ && go build -o %s ./cmd/coder", path), |
| 21 | + ) |
| 22 | + cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0") |
| 23 | + |
| 24 | + _, err := cmd.CombinedOutput() |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +var binpath string |
| 32 | + |
| 33 | +func init() { |
| 34 | + cwd, err := os.Getwd() |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + |
| 39 | + binpath = filepath.Join(cwd, "bin", "coder") |
| 40 | + err = build(binpath) |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// write session tokens to the given container runner |
| 47 | +func headlessLogin(ctx context.Context, t *testing.T, runner *tcli.ContainerRunner) { |
| 48 | + creds := login(ctx, t) |
| 49 | + cmd := exec.CommandContext(ctx, "sh", "-c", "mkdir -p ~/.config/coder && cat > ~/.config/coder/session") |
| 50 | + |
| 51 | + // !IMPORTANT: be careful that this does not appear in logs |
| 52 | + cmd.Stdin = strings.NewReader(creds.token) |
| 53 | + runner.RunCmd(cmd).Assert(t, |
| 54 | + tcli.Success(), |
| 55 | + ) |
| 56 | + runner.Run(ctx, fmt.Sprintf("echo -ne %s > ~/.config/coder/url", creds.url)).Assert(t, |
| 57 | + tcli.Success(), |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +func TestCoderCLI(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) |
| 64 | + defer cancel() |
| 65 | + |
| 66 | + c, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{ |
| 67 | + Image: "codercom/enterprise-dev", |
| 68 | + Name: "coder-cli-tests", |
| 69 | + BindMounts: map[string]string{ |
| 70 | + binpath: "/bin/coder", |
| 71 | + }, |
| 72 | + }) |
| 73 | + assert.Success(t, "new run container", err) |
| 74 | + defer c.Close() |
| 75 | + |
| 76 | + c.Run(ctx, "which coder").Assert(t, |
| 77 | + tcli.Success(), |
| 78 | + tcli.StdoutMatches("/usr/sbin/coder"), |
| 79 | + tcli.StderrEmpty(), |
| 80 | + ) |
| 81 | + |
| 82 | + c.Run(ctx, "coder version").Assert(t, |
| 83 | + tcli.StderrEmpty(), |
| 84 | + tcli.Success(), |
| 85 | + tcli.StdoutMatches("linux"), |
| 86 | + ) |
| 87 | + |
| 88 | + c.Run(ctx, "coder help").Assert(t, |
| 89 | + tcli.Success(), |
| 90 | + tcli.StderrMatches("Commands:"), |
| 91 | + tcli.StderrMatches("Usage: coder"), |
| 92 | + tcli.StdoutEmpty(), |
| 93 | + ) |
| 94 | + |
| 95 | + headlessLogin(ctx, t, c) |
| 96 | + |
| 97 | + c.Run(ctx, "coder envs").Assert(t, |
| 98 | + tcli.Success(), |
| 99 | + ) |
| 100 | + |
| 101 | + c.Run(ctx, "coder urls").Assert(t, |
| 102 | + tcli.Error(), |
| 103 | + ) |
| 104 | + |
| 105 | + c.Run(ctx, "coder sync").Assert(t, |
| 106 | + tcli.Error(), |
| 107 | + ) |
| 108 | + |
| 109 | + c.Run(ctx, "coder sh").Assert(t, |
| 110 | + tcli.Error(), |
| 111 | + ) |
| 112 | + |
| 113 | + c.Run(ctx, "coder logout").Assert(t, |
| 114 | + tcli.Success(), |
| 115 | + ) |
| 116 | + |
| 117 | + c.Run(ctx, "coder envs").Assert(t, |
| 118 | + tcli.Error(), |
| 119 | + ) |
| 120 | +} |
0 commit comments