| 113 | } |
| 114 | |
| 115 | func kubernetes3(t TestingT) (*envtest.Environment, client.Client) { |
| 116 | t.Helper() |
| 117 | |
| 118 | if !envtestVarsSet { |
| 119 | t.SkipNow() |
| 120 | } |
| 121 | |
| 122 | kubernetes.Lock() |
| 123 | defer kubernetes.Unlock() |
| 124 | |
| 125 | // Skip any remaining tests after the environment fails to start once. |
| 126 | // The test that tried to start the environment has reported the error. |
| 127 | if kubernetes.err != nil { |
| 128 | t.SkipNow() |
| 129 | } |
| 130 | |
| 131 | if kubernetes.env == nil { |
| 132 | // Get the current call stack, minus the closure below. |
| 133 | frames := func() *goruntime.Frames { |
| 134 | var pcs [5]uintptr |
| 135 | n := goruntime.Callers(2, pcs[:]) |
| 136 | return goruntime.CallersFrames(pcs[0:n]) |
| 137 | }() |
| 138 | |
| 139 | // Calculate the project directory as reported by [goruntime.CallersFrames]. |
| 140 | frame, ok := frames.Next() |
| 141 | self := frame.File |
| 142 | root := Value(filepath.EvalSymlinks(strings.TrimSuffix(self, |
| 143 | filepath.Join("internal", "testing", "require", "kubernetes.go")))) |
| 144 | |
| 145 | // Find the first caller that is not in this file. |
| 146 | for ok && frame.File == self { |
| 147 | frame, ok = frames.Next() |
| 148 | } |
| 149 | caller := Value(filepath.EvalSymlinks(frame.File)) |
| 150 | |
| 151 | // Calculate the project directory path relative to the caller. |
| 152 | base := Value(filepath.Rel(filepath.Dir(caller), root)) |
| 153 | |
| 154 | // Calculate the snapshotter module directory path relative to the project directory. |
| 155 | // Ignore any "vendor" directory by explicitly passing "-mod=readonly" https://go.dev/ref/mod#build-commands |
| 156 | var snapshotter string |
| 157 | if pkgs, err := packages.Load( |
| 158 | &packages.Config{BuildFlags: []string{"-mod=readonly"}, Mode: packages.NeedModule}, |
| 159 | "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1", |
| 160 | ); assert.Check(t, |
| 161 | err == nil && len(pkgs) > 0 && pkgs[0].Module != nil, "unable to load package: %v\n%#v", err, pkgs, |
| 162 | ) { |
| 163 | mod := pkgs[0].Module |
| 164 | assert.Assert(t, mod.Dir != "" && mod.Error == nil, "expected module in cache\n%#v", mod) |
| 165 | |
| 166 | snapshotter, err = filepath.Rel(root, mod.Dir) |
| 167 | assert.NilError(t, err, "module directory: %q", mod.Dir) |
| 168 | } |
| 169 | |
| 170 | env := EnvTest(t, envtest.CRDInstallOptions{ |
| 171 | ErrorIfPathMissing: true, |
| 172 | Paths: []string{ |