forked from genuinetools/reg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
123 lines (106 loc) · 2.94 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
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"testing"
"github.com/docker/docker/client"
"github.com/genuinetools/reg/testutils"
)
const (
domain = "localhost:5000"
)
var (
exeSuffix string // ".exe" on Windows
registryConfigs = []struct {
config string
username string
password string
}{
{
config: "noauth.yml",
username: "blah",
password: "blah",
},
{
config: "basicauth.yml",
username: "admin",
password: "testing",
},
}
registryHelper *testutils.RegistryHelper
)
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)
}
// remove test binary
defer os.Remove("testreg" + exeSuffix)
// create the docker client
dcli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(fmt.Errorf("could not connect to docker: %v", err))
}
// start the clair containers.
dbID, clairID, err := testutils.StartClair(dcli)
if err != nil {
testutils.RemoveContainer(dcli, dbID, clairID)
panic(fmt.Errorf("starting clair containers failed: %v", err))
}
for _, regConfig := range registryConfigs {
// start each registry
regID, _, err := testutils.StartRegistry(dcli, regConfig.config, regConfig.username, regConfig.password)
if err != nil {
testutils.RemoveContainer(dcli, dbID, clairID, regID)
panic(fmt.Errorf("starting registry container %s failed: %v", regConfig.config, err))
}
registryHelper, err = testutils.NewRegistryHelper(dcli, regConfig.username, regConfig.password, domain)
if err != nil {
panic(fmt.Errorf("creating registry helper %s failed: %v", regConfig.config, err))
}
flag.Parse()
merr := m.Run()
// remove registry
if err := testutils.RemoveContainer(dcli, regID); err != nil {
log.Printf("couldn't remove registry container %s: %v", regConfig.config, err)
}
if merr != 0 {
testutils.RemoveContainer(dcli, dbID, clairID)
fmt.Printf("testing config %s failed\n", regConfig.config)
os.Exit(merr)
}
}
// remove clair containers.
if err := testutils.RemoveContainer(dcli, dbID, clairID); err != nil {
log.Printf("couldn't remove clair containers: %v", err)
}
os.Exit(0)
}
func run(args ...string) (string, error) {
prog := "./testreg" + exeSuffix
// always add trust insecure, and the registry
newargs := []string{args[0], "-d", "-k"}
if len(args) > 1 {
newargs = append(newargs, args[1:]...)
}
cmd := exec.Command(prog, newargs...)
out, err := cmd.CombinedOutput()
return string(out), err
}