|
1 | 1 | package dalec |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
| 6 | + "slices" |
| 7 | + "strings" |
5 | 8 | "testing" |
6 | 9 |
|
7 | 10 | "github.com/goccy/go-yaml" |
| 11 | + "github.com/moby/buildkit/client/llb" |
8 | 12 | ) |
9 | 13 |
|
10 | 14 | func TestGomodReplaceUnmarshal(t *testing.T) { |
@@ -210,3 +214,145 @@ func TestGomodReplaceGoModEditArg(t *testing.T) { |
210 | 214 | }) |
211 | 215 | } |
212 | 216 | } |
| 217 | + |
| 218 | +func TestGomodDepsUsesGomodProxy(t *testing.T) { |
| 219 | + t.Parallel() |
| 220 | + |
| 221 | + const proxy = "http://proxy.example:5000,direct" |
| 222 | + spec := testGomodProxySpec() |
| 223 | + st := spec.GomodDeps(testGomodProxySourceOpts(proxy), llb.Scratch()) |
| 224 | + if st == nil { |
| 225 | + t.Fatal("gomod generator succeeded but returned nil state") |
| 226 | + } |
| 227 | + |
| 228 | + env := gomodDownloadExecEnv(context.Background(), t, *st) |
| 229 | + if !slices.Contains(env, "GOPROXY="+proxy) { |
| 230 | + t.Fatalf("expected gomod exec env to include GOPROXY=%q, got %v", proxy, env) |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +func TestGomodDepsSkipsEmptyGomodProxy(t *testing.T) { |
| 235 | + t.Parallel() |
| 236 | + |
| 237 | + spec := testGomodProxySpec() |
| 238 | + st := spec.GomodDeps(testGomodProxySourceOpts(""), llb.Scratch()) |
| 239 | + if st == nil { |
| 240 | + t.Fatal("gomod generator succeeded but returned nil state") |
| 241 | + } |
| 242 | + |
| 243 | + env := gomodDownloadExecEnv(context.Background(), t, *st) |
| 244 | + for _, item := range env { |
| 245 | + if strings.HasPrefix(item, "GOPROXY=") { |
| 246 | + t.Fatalf("expected empty gomod proxy to omit GOPROXY, got %v", env) |
| 247 | + } |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +func TestGomodProxyBuildArgIsKnown(t *testing.T) { |
| 252 | + t.Parallel() |
| 253 | + |
| 254 | + spec := &Spec{} |
| 255 | + err := spec.SubstituteArgs(map[string]string{ |
| 256 | + BuildArgDalecGomodProxy: "http://proxy.example:5000", |
| 257 | + }) |
| 258 | + if err != nil { |
| 259 | + t.Fatal(err) |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +func TestGomodPatchUsesGomodProxy(t *testing.T) { |
| 264 | + t.Parallel() |
| 265 | + |
| 266 | + const proxy = "http://proxy.example:5000,direct" |
| 267 | + gen := &SourceGenerator{ |
| 268 | + Gomod: &GeneratorGomod{ |
| 269 | + Edits: &GomodEdits{ |
| 270 | + Replace: []GomodReplace{ |
| 271 | + {Original: "example.com/old", Update: "example.com/new v1.2.3"}, |
| 272 | + }, |
| 273 | + }, |
| 274 | + }, |
| 275 | + } |
| 276 | + |
| 277 | + st, err := (&Spec{}).generateGomodPatchStateForSource("src", gen, llb.Scratch(), llb.Scratch(), nil, proxy) |
| 278 | + if err != nil { |
| 279 | + t.Fatal(err) |
| 280 | + } |
| 281 | + if st == nil { |
| 282 | + t.Fatal("gomod patch generation succeeded but returned nil state") |
| 283 | + } |
| 284 | + |
| 285 | + env := gomodPatchExecEnv(context.Background(), t, *st) |
| 286 | + if !slices.Contains(env, "GOPROXY="+proxy) { |
| 287 | + t.Fatalf("expected gomod patch exec env to include GOPROXY=%q, got %v", proxy, env) |
| 288 | + } |
| 289 | +} |
| 290 | + |
| 291 | +func testGomodProxySpec() *Spec { |
| 292 | + return &Spec{ |
| 293 | + Sources: map[string]Source{ |
| 294 | + "src": { |
| 295 | + Git: &SourceGit{ |
| 296 | + URL: "https://example.com/repo.git", |
| 297 | + Commit: "0123456789abcdef", |
| 298 | + }, |
| 299 | + Generate: []*SourceGenerator{ |
| 300 | + {Gomod: &GeneratorGomod{}}, |
| 301 | + }, |
| 302 | + }, |
| 303 | + }, |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +func testGomodProxySourceOpts(proxy string) SourceOpts { |
| 308 | + return SourceOpts{ |
| 309 | + GomodProxy: proxy, |
| 310 | + GetContext: func(name string, opts ...llb.LocalOption) (*llb.State, error) { |
| 311 | + st := llb.Local(name, opts...) |
| 312 | + return &st, nil |
| 313 | + }, |
| 314 | + GitCredHelperOpt: func() (llb.RunOption, error) { |
| 315 | + st := llb.Scratch().File(llb.Mkfile("/frontend", 0o755, []byte("#!/usr/bin/env bash\nexit 0\n"))) |
| 316 | + return RunOptFunc(func(ei *llb.ExecInfo) { |
| 317 | + llb.AddMount("/usr/local/bin/frontend", st, llb.SourcePath("/frontend")).SetRunOption(ei) |
| 318 | + }), nil |
| 319 | + }, |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +func gomodDownloadExecEnv(ctx context.Context, t *testing.T, st llb.State) []string { |
| 324 | + t.Helper() |
| 325 | + |
| 326 | + for _, op := range sourceOpsFromState(ctx, t, st) { |
| 327 | + exec := op.GetExec() |
| 328 | + if exec == nil { |
| 329 | + continue |
| 330 | + } |
| 331 | + |
| 332 | + env := exec.Meta.Env |
| 333 | + if slices.Contains(env, "GOPATH=/go") && slices.Contains(env, "TMP_GOMODCACHE=/tmp/dalec/gomod-proxy-cache") { |
| 334 | + return env |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + t.Fatal("expected gomod download exec") |
| 339 | + return nil |
| 340 | +} |
| 341 | + |
| 342 | +func gomodPatchExecEnv(ctx context.Context, t *testing.T, st llb.State) []string { |
| 343 | + t.Helper() |
| 344 | + |
| 345 | + for _, op := range sourceOpsFromState(ctx, t, st) { |
| 346 | + exec := op.GetExec() |
| 347 | + if exec == nil { |
| 348 | + continue |
| 349 | + } |
| 350 | + |
| 351 | + if slices.Contains(exec.Meta.Args, "/gomod-patch.sh") { |
| 352 | + return exec.Meta.Env |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + t.Fatal("expected gomod patch exec") |
| 357 | + return nil |
| 358 | +} |
0 commit comments