-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathfixtures.go
More file actions
52 lines (45 loc) · 1.3 KB
/
fixtures.go
File metadata and controls
52 lines (45 loc) · 1.3 KB
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
package fixtures
import (
"os"
"testing"
"github.com/docker/cli/cli/config"
"gotest.tools/v3/fs"
"gotest.tools/v3/icmd"
)
const (
// AlpineImage is an image in the test registry
AlpineImage = "registry:5000/alpine:frozen"
// BusyboxImage is an image in the test registry
BusyboxImage = "registry:5000/busybox:frozen"
)
// SetupConfigFile creates a config.json file for testing
func SetupConfigFile(t *testing.T) fs.Dir {
t.Helper()
dir := fs.NewDir(t, "trust_test", fs.WithMode(0o700), fs.WithFile("config.json", `{
"auths": {
"registry:5000": {
"auth": "ZWlhaXM6cGFzc3dvcmQK"
}
}}`), fs.WithDir("trust", fs.WithDir("private")))
return *dir
}
// WithConfig sets an environment variable for the docker config location
func WithConfig(dir string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) {
addEnvs(cmd, config.EnvOverrideConfigDir+"="+dir)
}
}
// WithHome sets the HOME environment variable
func WithHome(path string) func(*icmd.Cmd) {
return func(cmd *icmd.Cmd) {
addEnvs(cmd, "HOME="+path)
}
}
// addEnvs adds environment variables to cmd, making sure to preserve the
// current os.Environ(), which would otherwise be omitted (for non-empty .Env).
func addEnvs(cmd *icmd.Cmd, envs ...string) {
if len(cmd.Env) == 0 {
cmd.Env = os.Environ()
}
cmd.Env = append(cmd.Env, envs...)
}