1 package lifttest
2
3 import (
4 "fmt"
5 "io/fs"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
10
11 "github.com/bazelbuild/rules_go/go/runfiles"
12 "github.com/stretchr/testify/assert"
13 "gopkg.in/yaml.v2"
14
15 "edge-infra.dev/pkg/f8n/warehouse/lift"
16 "edge-infra.dev/pkg/f8n/warehouse/oci/layout"
17 "edge-infra.dev/pkg/k8s/kustomize"
18 "edge-infra.dev/pkg/lib/build/bazel"
19 "edge-infra.dev/pkg/lib/build/git"
20 "edge-infra.dev/pkg/lib/cli/sh"
21 "edge-infra.dev/test/fixtures"
22 )
23
24 var (
25 dummyInfraCfg = lift.CapabilityConfig{
26 Package: "fake/infra-package",
27 ResourceMatcher: lift.ResourceMatcher{
28 APIGroups: []string{"fake.apigroup.u"},
29 },
30 }
31 cfg lift.Config
32 path string
33 )
34
35 type Option = func(*options)
36 type options struct {
37 fixtures bool
38 }
39
40 func makeOptions(opts ...Option) options {
41 o := options{}
42 for _, opt := range opts {
43 opt(&o)
44 }
45 return o
46 }
47
48
49
50 func Setup(t *testing.T, dir string, opts ...Option) {
51 options := makeOptions(opts...)
52 if options.fixtures {
53 path = fixturesSetup(t, dir)
54 }
55 if !options.fixtures {
56 cfg = lift.Config{Infrastructure: dummyInfraCfg}
57 path = dir
58 }
59 gr := createTestGitRepo(t, path)
60 assert.NoError(t, os.Chdir(gr))
61 g := git.NewInDir(gr)
62 gPath, err := g.Path()
63 if err != nil {
64 t.Fatal(err)
65 }
66 WriteCfg(t, &cfg, gPath)
67 if os.Getenv("WAREHOUSE_REPO") == "" {
68 repo := "us-east1-docker.pkg.dev"
69 t.Setenv("WAREHOUSE_REPO", repo)
70 }
71
72 t.Setenv("WAREHOUSE_PATH", path)
73 cachepath := "test/cache"
74 t.Setenv("WAREHOUSE_CACHE", cachepath)
75 }
76
77
78 func WriteCfg(t *testing.T, cfg *lift.Config, path string) {
79 cfgPath := filepath.Join(path, "warehouse.yaml")
80 versionPath := filepath.Join(path, ".version")
81 t.Helper()
82 data, err := yaml.Marshal(cfg)
83 assert.NoError(t, err)
84 assert.NoError(t, os.WriteFile(cfgPath, data, 0644))
85 assert.NoError(t, os.WriteFile(versionPath, []byte("0.17.0"), 0644))
86 t.Setenv("WAREHOUSE_CONFIG", cfgPath)
87 }
88
89
90
91 func CreateLiftBinary() (string, error) {
92 var path string
93 var err error
94 if ws := os.Getenv(bazel.TestWorkspace); ws == "" {
95 path, err = exec.LookPath("lift")
96 if err != nil {
97 return "", fmt.Errorf("unable to locate path at %v", path)
98 }
99 } else {
100 path, err = runfiles.Rlocation(filepath.Join(ws, "cmd/f8n/warehouse/lift/lift_/lift"))
101 if err != nil {
102 return "", fmt.Errorf("unable to create absolute path at %v", path)
103 }
104 }
105 return path, nil
106 }
107
108 func createTestGitRepo(t *testing.T, dir string) string {
109 t.Helper()
110
111 repo, err := os.MkdirTemp(dir, "git_test-*")
112 assert.NoError(t, err)
113 t.Log("test repo dir", repo)
114
115 shell := sh.NewInDir(repo)
116 for _, c := range []string{
117 "git init -q",
118 "git config user.name \"testbot\"",
119 "git config user.email \"testmail@testworld.co\"",
120 "git checkout -b test",
121
122 "git remote add origin git@github.com:testworld",
123 "git commit --allow-empty -m \"\"",
124 } {
125 assert.NoError(t, shell.RunE(c))
126 }
127
128 return repo
129 }
130
131 func fixturesSetup(t *testing.T, dir string) string {
132 f, err := fs.Sub(fixtures.PalletFixtures, "warehouse/src")
133 if err != nil {
134 t.Fatal(err)
135 }
136 testFS := &kustomize.FS{FS: f}
137 p, err := layout.FromFS(testFS.FS, filepath.Join(dir, "warehouse/src"))
138 if err != nil {
139 t.Fatal()
140 }
141 path := string(p.Path)
142 testCfgBytes, err := testFS.ReadFile(".warehouse.yaml")
143 if err != nil {
144 t.Fatalf("failed to read .warehouse.yaml in test fs: %s", err)
145 }
146 cfg = lift.Config{}
147 err = yaml.Unmarshal(testCfgBytes, &cfg)
148 if err != nil {
149 t.Fatalf("failed to unmarshal test cfg: %s", err)
150 }
151 return path
152 }
153
154
155 func WithFixtures() Option {
156 return func(o *options) {
157 o.fixtures = true
158 }
159 }
160
View as plain text