Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 3b747e9

Browse files
committed
add tests and example hat for on_start
1 parent fa6c4b3 commit 3b747e9

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

hat-examples/on_start/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM codercom/ubuntu-dev
2+
3+
# The command in the on_start label will be run immediately after the
4+
# project starts. You could use this to reinstall dependencies or
5+
# perform any other bootstrapping tasks.
6+
LABEL on_start="touch did_on_start"

internal/dockutil/exec.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ func Exec(cntName, cmd string, args ...string) *exec.Cmd {
1111
return exec.Command("docker", args...)
1212
}
1313

14+
func ExecDir(cntName, dir, cmd string, args ...string) *exec.Cmd {
15+
args = append([]string{"exec", "-w", dir, "-i", cntName, cmd}, args...)
16+
return exec.Command("docker", args...)
17+
}
18+
1419
func ExecTTY(cntName, dir, cmd string, args ...string) *exec.Cmd {
1520
args = append([]string{"exec", "-w", dir, "-it", cntName, cmd}, args...)
1621
return exec.Command("docker", args...)

runner_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package main
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
9+
10+
"go.coder.com/sail/internal/dockutil"
811
)
912

1013
func Test_runner(t *testing.T) {
1114
// Ensure that the testing environment won't conflict with any running sail projects.
12-
requireProjectsNotRunning(t, "cdr/nbin", "cdr/flog", "cdr/bigdur", "cdr/sshcode")
15+
requireProjectsNotRunning(t, "cdr/nbin", "cdr/flog", "cdr/bigdur", "cdr/sshcode", "cdr/cli")
1316
requireUbuntuDevImage(t)
1417

1518
// labelChecker asserts that all of the correct labels
@@ -73,6 +76,23 @@ func Test_runner(t *testing.T) {
7376
})
7477
}
7578

79+
// containsFile ensures that a container contains a file.
80+
// This is used for testing the on_start label.
81+
containsFile := func(name, path string) func(*testing.T, *params) {
82+
return func(t *testing.T, p *params) {
83+
t.Run(name, func(t *testing.T) {
84+
cntDir, err := p.proj.containerDir()
85+
require.NoError(t, err)
86+
cntDir = resolvePath(containerHome, cntDir)
87+
88+
// Run the file existence check using /bin/sh.
89+
cmdStr := fmt.Sprintf(`[ -f "%s" ]`, path)
90+
err = dockutil.ExecDir(p.proj.cntName(), cntDir, "/bin/sh", "-c", cmdStr).Run()
91+
require.NoError(t, err)
92+
})
93+
}
94+
}
95+
7696
run(t, "BaseImageNoHat", "https://github.com/cdr/nbin", "",
7797
labelChecker,
7898
codeServerStarts,
@@ -96,4 +116,13 @@ func Test_runner(t *testing.T) {
96116
codeServerStarts,
97117
loadFromContainer,
98118
)
119+
120+
run(t, "ProjImageOnStartHat", "https://github.com/cdr/cli", "./hat-examples/on_start",
121+
labelChecker,
122+
codeServerStarts,
123+
loadFromContainer,
124+
125+
// ./hat-examples/on_start should create `did_on_start` in the project directory.
126+
containsFile("ContainsOnStartFile", "did_on_start"),
127+
)
99128
}

0 commit comments

Comments
 (0)