forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
113 lines (94 loc) · 2.4 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
)
var (
// exeSuffix is the suffix of executable files; ".exe" on Windows.
exeSuffix string
// stateDir is the temporary state directory used for the tests.
testStateDir string
mu sync.Mutex
)
func init() {
switch runtime.GOOS {
case "windows":
exeSuffix = ".exe"
}
}
// The TestMain function creates a img command for testing purposes and
// deletes it after the tests have been run.
func TestMain(m *testing.M) {
os.Unsetenv("IMG_RUNNING_TESTS")
args := []string{"build", "-o", "testimg" + exeSuffix}
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "building testimg failed: %v\n%s\n", err, out)
os.Exit(2)
}
// Create the temporary state directory.
testStateDir, err = ioutil.TempDir("", "img-test")
if err != nil {
fmt.Fprintf(os.Stderr, "create temporary directory failed: %v\n", err)
os.Exit(2)
}
defer os.RemoveAll(testStateDir)
r := m.Run()
os.Remove("testimg" + exeSuffix)
os.Exit(r)
}
// doRun runs the test command, recording stdout and stderr and
// returning exit status.
func doRun(args []string, stdin io.Reader) (string, error) {
prog := "./testimg" + exeSuffix
newargs := []string{args[0], "--state", testStateDir}
newargs = append(newargs, args[1:]...)
// TODO(genuinetools): the sudo here is horrible, I know.
cmd := exec.Command(prog, newargs...)
if stdin != nil {
cmd.Stdin = stdin
}
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), fmt.Errorf("Error running %s: %s\n%v", strings.Join(newargs, " "), string(out), err)
}
return string(out), nil
}
// run runs the test command, and expects it to succeed.
func run(t *testing.T, args ...string) string {
if runtime.GOOS == "windows" {
mu.Lock()
defer mu.Unlock()
}
out, err := doRun(args, nil)
if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
return out
}
func runBuild(t *testing.T, name string, stdin io.Reader) {
if runtime.GOOS == "windows" {
mu.Lock()
defer mu.Unlock()
}
buildCtx := "."
if stdin != nil {
buildCtx = "-"
}
args := []string{"build", "-t", name, buildCtx}
if _, err := doRun(args, stdin); err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
func withDockerfile(dockerfile string) io.Reader {
return strings.NewReader(dockerfile)
}