Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 8e8b215

Browse files
committed
Execute commands in container
1 parent 8b3f056 commit 8e8b215

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

ci/integration/integration_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
"time"
77

88
"cdr.dev/coder-cli/ci/tcli"
9+
"cdr.dev/slog/sloggers/slogtest/assert"
910
)
1011

1112
func TestTCli(t *testing.T) {
1213
ctx := context.Background()
1314

14-
container := tcli.NewRunContainer(ctx, "", "test-container")
15+
container, err := tcli.NewRunContainer(ctx, "ubuntu:latest", "test-container")
16+
assert.Success(t, "new run container", err)
17+
defer container.Close()
1518

1619
container.Run(ctx, "echo testing").Assert(t,
1720
tcli.Success(),

ci/tcli/tcli.go

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os/exec"
88
"regexp"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -14,35 +15,68 @@ import (
1415
)
1516

1617
type RunContainer struct {
18+
name string
19+
ctx context.Context
1720
}
1821

19-
func NewRunContainer(ctx context.Context, image, name string) *RunContainer {
20-
//exec.CommandContext(ctx, "docker", "start")
21-
// TODO: startup docker container
22-
return &RunContainer{}
22+
func NewRunContainer(ctx context.Context, image, name string) (*RunContainer, error) {
23+
cmd := exec.CommandContext(ctx,
24+
"docker", "run",
25+
"--name", name,
26+
"-it", "-d",
27+
image,
28+
)
29+
30+
out, err := cmd.CombinedOutput()
31+
if err != nil {
32+
return nil, xerrors.Errorf(
33+
"failed to start testing container %q, (%s): %w",
34+
name, string(out), err)
35+
}
36+
37+
return &RunContainer{
38+
name: name,
39+
ctx: ctx,
40+
}, nil
2341
}
2442

25-
func (r RunContainer) Teardown() error {
26-
// TODO: teardown run environment
43+
func (r *RunContainer) Close() error {
44+
cmd := exec.CommandContext(r.ctx,
45+
"sh", "-c", strings.Join([]string{
46+
"docker", "kill", r.name, "&&",
47+
"docker", "rm", r.name,
48+
}, " "))
49+
50+
out, err := cmd.CombinedOutput()
51+
if err != nil {
52+
return xerrors.Errorf(
53+
"failed to stop testing container %q, (%s): %w",
54+
r.name, string(out), err)
55+
}
2756
return nil
2857
}
2958

3059
type Assertable struct {
31-
cmd string
32-
ctx context.Context
60+
cmd string
61+
ctx context.Context
62+
container *RunContainer
3363
}
3464

35-
func (*RunContainer) Run(ctx context.Context, cmd string) *Assertable {
65+
func (r *RunContainer) Run(ctx context.Context, cmd string) *Assertable {
3666
return &Assertable{
37-
cmd: cmd,
38-
ctx: ctx,
67+
cmd: cmd,
68+
ctx: ctx,
69+
container: r,
3970
}
4071
}
4172

4273
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
4374
var cmdResult CommandResult
4475

45-
cmd := exec.CommandContext(a.ctx, "sh", "-c", a.cmd)
76+
cmd := exec.CommandContext(a.ctx,
77+
"docker", "exec", a.container.name,
78+
"sh", "-c", a.cmd,
79+
)
4680
var (
4781
stdout bytes.Buffer
4882
stderr bytes.Buffer

0 commit comments

Comments
 (0)