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

Commit bf31666

Browse files
committed
Add host runner
1 parent 50c4e75 commit bf31666

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

ci/integration/integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func init() {
4646
func TestTCli(t *testing.T) {
4747
ctx := context.Background()
4848

49-
container, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
49+
container, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
5050
Image: "ubuntu:latest",
5151
Name: "test-container",
5252
BindMounts: map[string]string{
@@ -94,7 +94,7 @@ func TestTCli(t *testing.T) {
9494
func TestCoderCLI(t *testing.T) {
9595
ctx := context.Background()
9696

97-
c, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
97+
c, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
9898
Image: "ubuntu:latest",
9999
Name: "test-container",
100100
BindMounts: map[string]string{

ci/tcli/tcli.go

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"os/exec"
89
"regexp"
910
"strings"
@@ -14,13 +15,18 @@ import (
1415
"golang.org/x/xerrors"
1516
)
1617

17-
// RunContainer specifies a runtime container for performing command tests
18-
type RunContainer struct {
19-
name string
20-
ctx context.Context
18+
var (
19+
_ runnable = &ContainerRunner{}
20+
_ runnable = &HostRunner{}
21+
)
22+
23+
type runnable interface {
24+
Run(ctx context.Context, command string) *Assertable
25+
RunCmd(cmd *exec.Cmd) *Assertable
26+
io.Closer
2127
}
2228

23-
// ContainerConfig describes the RunContainer configuration schema for initializing a testing environment
29+
// ContainerConfig describes the ContainerRunner configuration schema for initializing a testing environment
2430
type ContainerConfig struct {
2531
Name string
2632
Image string
@@ -42,8 +48,14 @@ func preflightChecks() error {
4248
return nil
4349
}
4450

45-
// NewRunContainer starts a new docker container for executing command tests
46-
func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContainer, error) {
51+
// ContainerRunner specifies a runtime container for performing command tests
52+
type ContainerRunner struct {
53+
name string
54+
ctx context.Context
55+
}
56+
57+
// NewContainerRunner starts a new docker container for executing command tests
58+
func NewContainerRunner(ctx context.Context, config *ContainerConfig) (*ContainerRunner, error) {
4759
if err := preflightChecks(); err != nil {
4860
return nil, err
4961
}
@@ -65,14 +77,14 @@ func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContaine
6577
config.Name, string(out), err)
6678
}
6779

68-
return &RunContainer{
80+
return &ContainerRunner{
6981
name: config.Name,
7082
ctx: ctx,
7183
}, nil
7284
}
7385

7486
// Close kills and removes the command execution testing container
75-
func (r *RunContainer) Close() error {
87+
func (r *ContainerRunner) Close() error {
7688
cmd := exec.CommandContext(r.ctx,
7789
"sh", "-c", strings.Join([]string{
7890
"docker", "kill", r.name, "&&",
@@ -88,43 +100,72 @@ func (r *RunContainer) Close() error {
88100
return nil
89101
}
90102

103+
type HostRunner struct{}
104+
105+
func (r *HostRunner) Run(ctx context.Context, command string) *Assertable {
106+
var (
107+
args []string
108+
path string
109+
parts = strings.Split(command, " ")
110+
)
111+
if len(parts) > 0 {
112+
path = parts[0]
113+
}
114+
if len(parts) > 1 {
115+
args = parts[1:]
116+
}
117+
return &Assertable{
118+
cmd: exec.CommandContext(ctx, path, args...),
119+
tname: command,
120+
}
121+
}
122+
123+
func (r *HostRunner) RunCmd(cmd *exec.Cmd) *Assertable {
124+
return &Assertable{
125+
cmd: cmd,
126+
tname: strings.Join(cmd.Args, " "),
127+
}
128+
}
129+
130+
func (r *HostRunner) Close() error {
131+
return nil
132+
}
133+
91134
// Assertable describes an initialized command ready to be run and asserted against
92135
type Assertable struct {
93-
cmd *exec.Cmd
94-
ctx context.Context
95-
container *RunContainer
136+
cmd *exec.Cmd
137+
tname string
96138
}
97139

98140
// Run executes the given command in the runtime container with reasonable defaults
99-
func (r *RunContainer) Run(ctx context.Context, command string) *Assertable {
141+
func (r *ContainerRunner) Run(ctx context.Context, command string) *Assertable {
100142
cmd := exec.CommandContext(ctx,
101143
"docker", "exec", "-i", r.name,
102144
"sh", "-c", command,
103145
)
104146

105147
return &Assertable{
106-
cmd: cmd,
107-
ctx: ctx,
108-
container: r,
148+
cmd: cmd,
149+
tname: command,
109150
}
110151
}
111152

112153
// RunCmd lifts the given *exec.Cmd into the runtime container
113-
func (r *RunContainer) RunCmd(cmd *exec.Cmd) *Assertable {
154+
func (r *ContainerRunner) RunCmd(cmd *exec.Cmd) *Assertable {
114155
path, _ := exec.LookPath("docker")
115156
cmd.Path = path
116157
command := strings.Join(cmd.Args, " ")
117-
cmd.Args = append([]string{"docker", "exec", "-i", r.name, "sh", "-c", command})
158+
cmd.Args = []string{"docker", "exec", "-i", r.name, "sh", "-c", command}
118159

119160
return &Assertable{
120-
cmd: cmd,
121-
container: r,
161+
cmd: cmd,
162+
tname: command,
122163
}
123164
}
124165

125166
// Assert runs the Assertable and
126167
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
127-
t.Run(strings.Join(a.cmd.Args[6:], " "), func(t *testing.T) {
168+
t.Run(a.tname, func(t *testing.T) {
128169
var cmdResult CommandResult
129170

130171
var (
@@ -225,7 +266,7 @@ func StdoutEmpty() Assertion {
225266
}
226267

227268
// GetResult offers an escape hatch from tcli
228-
// The passed pointer will be assigned to the commands *CommandResult
269+
// The pointer passed as "result" will be assigned to the command's *CommandResult
229270
func GetResult(result **CommandResult) Assertion {
230271
return simpleFuncAssert{
231272
valid: func(r *CommandResult) error {

0 commit comments

Comments
 (0)