forked from genuinetools/reg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
90 lines (77 loc) · 1.96 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"testing"
"github.com/docker/docker/client"
"github.com/jessfraz/reg/testutils"
)
var (
exeSuffix string // ".exe" on Windows
registryAddr string
)
func init() {
switch runtime.GOOS {
case "windows":
exeSuffix = ".exe"
}
}
// The TestMain function creates a reg command for testing purposes and
// deletes it after the tests have been run.
// It also spins up a local registry prefilled with an alpine image and
// removes that after the tests have been run.
func TestMain(m *testing.M) {
// build the test binary
args := []string{"build", "-o", "testreg" + exeSuffix}
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "building testreg failed: %v\n%s", err, out)
os.Exit(2)
}
// create the docker client
dcli, err := client.NewEnvClient()
if err != nil {
panic(fmt.Errorf("could not connect to docker: %v", err))
}
// start registry
regID, addr, err := testutils.StartRegistry(dcli)
if err != nil {
testutils.RemoveContainer(dcli, regID)
panic(fmt.Errorf("starting registry container failed: %v", err))
}
registryAddr = addr
flag.Parse()
merr := m.Run()
// remove registry
if err := testutils.RemoveContainer(dcli, regID); err != nil {
log.Printf("couldn't remove registry container: %v", err)
}
// remove test binary
os.Remove("testreg" + exeSuffix)
os.Exit(merr)
}
func run(args ...string) (string, error) {
prog := "./testreg" + exeSuffix
// always add trust insecure, and the registry
newargs := append([]string{"-k", "-r", "localhost:5000"}, args...)
cmd := exec.Command(prog, newargs...)
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestList(t *testing.T) {
out, err := run("ls")
if err != nil {
t.Fatal(err)
}
expected := `Repositories for localhost:5000
REPO TAGS
alpine latest
`
if out != expected {
t.Fatalf("expected: %s\ngot: %s", expected, out)
}
}