-
Notifications
You must be signed in to change notification settings - Fork 509
/
configtest.go
58 lines (51 loc) · 1.48 KB
/
configtest.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
package client
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/datawire/dtest"
"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
)
// SetDefaultConfig creates a config that has the registry set correctly.
// This ensures that the config on the machine of whatever is running the test,
// isn't used, which could cause conflict with the tests.
func SetDefaultConfig(ctx context.Context, configDir string) (context.Context, error) {
registry := dtest.DockerRegistry(ctx)
configYml := fmt.Sprintf(`
images:
registry: %[1]s
webhookRegistry: %[1]s
cloud:
systemaHost: 127.0.0.1
`, registry)
return SetConfig(ctx, configDir, configYml)
}
// SetConfig creates a config from the configYml provided and assigns it to a new context which
// is returned. Use this if you are testing components of the config.yml, otherwise you can use setDefaultConfig.
func SetConfig(ctx context.Context, configDir, configYml string) (context.Context, error) {
config, err := os.Create(filepath.Join(configDir, "config.yml"))
if err != nil {
return ctx, err
}
_, err = config.WriteString(configYml)
if err != nil {
return ctx, err
}
config.Close()
// Load env if it isn't loaded already
ctx = filelocation.WithAppUserConfigDir(ctx, configDir)
if env := GetEnv(ctx); env == nil {
env, err = LoadEnv(ctx)
if err != nil {
return ctx, err
}
ctx = WithEnv(ctx, env)
}
cfg, err := LoadConfig(ctx)
if err != nil {
return ctx, err
}
ctx = WithConfig(ctx, cfg)
return ctx, nil
}