| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file. | |
| package project_test | |
| import ( | |
| "context" | |
| "fmt" | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| "sync/atomic" | |
| "testing" | |
| "go.fuchsia.dev/jiri" | |
| "go.fuchsia.dev/jiri/cipd" | |
| "go.fuchsia.dev/jiri/jiritest" | |
| "go.fuchsia.dev/jiri/jiritest/xtest" | |
| "go.fuchsia.dev/jiri/project" | |
| ) | |
| func TestFetchPackagePaths(t *testing.T) { | |
| t.Parallel() | |
| fuchsiaPlat := cipd.FuchsiaPlatform(cipd.CurrentPlatform) | |
| platSubdir := fmt.Sprintf("%s-%s", fuchsiaPlat.OS, fuchsiaPlat.Arch) | |
| cipdPlatStr := cipd.CurrentPlatform.String() | |
| const ( | |
| edk2IID = "TfGjbhGrxzU0x2fYk8elEgwMTrvwe-3DSPTQe4gb0tMC" | |
| qemuIID = "TDWhWdNilJMXsvMLvdrLeqjo8KHaGUEQUB82NHMFPXEC" | |
| parentIID = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | |
| childIID = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" | |
| ) | |
| snapshotXML := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/third_party/edk2" version="git_revision:edk2" path="prebuilt/third_party/edk2" platforms="%s"> | |
| <instance name="fuchsia/third_party/edk2" id="%s"></instance> | |
| </package> | |
| <package name="fuchsia/third_party/qemu/${platform}" version="git_revision:qemu" path="prebuilt/third_party/qemu/{{.OS}}-{{.Arch}}" platforms="%s"> | |
| <instance name="fuchsia/third_party/qemu/%s" id="%s"></instance> | |
| </package> | |
| <package name="fuchsia/third_party/parent" version="git_revision:parent" path="prebuilt/third_party/parent" platforms="%s"> | |
| <instance name="fuchsia/third_party/parent" id="%s"></instance> | |
| </package> | |
| <package name="fuchsia/third_party/parent/child" version="git_revision:child" path="prebuilt/third_party/parent/child" platforms="%s"> | |
| <instance name="fuchsia/third_party/parent/child" id="%s"></instance> | |
| </package> | |
| <package name="fuchsia/third_party/opt_pkg" version="git_revision:opt" path="prebuilt/third_party/opt_pkg" attributes="opt" platforms="%s"> | |
| <instance name="fuchsia/third_party/opt_pkg" id="%s"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, edk2IID, cipdPlatStr, cipdPlatStr, qemuIID, cipdPlatStr, parentIID, cipdPlatStr, childIID, cipdPlatStr, edk2IID) | |
| var fetchCalls atomic.Int32 | |
| mockFetcher := func(_ context.Context, inst project.PackageCacheInstance, destDir string, _ uint) error { | |
| fetchCalls.Add(1) | |
| switch inst.InstanceID { | |
| case edk2IID: | |
| sub := filepath.Join(destDir, "qemu-x64") | |
| if err := os.MkdirAll(sub, 0755); err != nil { | |
| return err | |
| } | |
| return os.WriteFile(filepath.Join(sub, "OVMF_CODE.fd"), []byte("ovmf-code"), 0644) | |
| case qemuIID: | |
| binDir := filepath.Join(destDir, "bin") | |
| if err := os.MkdirAll(binDir, 0755); err != nil { | |
| return err | |
| } | |
| return os.WriteFile(filepath.Join(binDir, "qemu-system-x86_64"), []byte("#!/bin/sh\necho qemu\n"), 0755) | |
| case parentIID: | |
| if err := os.MkdirAll(destDir, 0755); err != nil { | |
| return err | |
| } | |
| return os.WriteFile(filepath.Join(destDir, "parent.txt"), []byte("parent-data"), 0644) | |
| case childIID: | |
| if err := os.MkdirAll(destDir, 0755); err != nil { | |
| return err | |
| } | |
| return os.WriteFile(filepath.Join(destDir, "child.txt"), []byte("child-data"), 0644) | |
| default: | |
| return fmt.Errorf("unexpected instance %+v", inst) | |
| } | |
| } | |
| opts := project.PrebuiltProviderOptions{ | |
| InstanceFetcher: mockFetcher, | |
| } | |
| writeSnapshot := func(t *testing.T, root string) { | |
| t.Helper() | |
| updateHistoryDir := filepath.Join(root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(snapshotXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| } | |
| t.Run("materializes enclosing package and re-links deleted file without re-downloading", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| writeSnapshot(t, checkout.Root) | |
| fetchCalls.Store(0) | |
| reqFile := "prebuilt/third_party/edk2/qemu-x64/OVMF_CODE.fd" | |
| if err := project.FetchPackagePaths(ctx, []string{reqFile}, 5, opts); err != nil { | |
| t.Fatalf("FetchPackagePaths(%s) failed: %v", reqFile, err) | |
| } | |
| if got := fetchCalls.Load(); got != 1 { | |
| t.Fatalf("expected 1 fetch call on cold miss, got %d", got) | |
| } | |
| // Warm hit does not invoke fetcher. | |
| if err := project.FetchPackagePaths(ctx, []string{reqFile}, 5, opts); err != nil { | |
| t.Fatalf("warm FetchPackagePaths(%s) failed: %v", reqFile, err) | |
| } | |
| if got := fetchCalls.Load(); got != 1 { | |
| t.Errorf("expected warm hit not to call fetcher, got %d", got) | |
| } | |
| // Deleting single file inside stamped destination re-links from cache without re-fetching. | |
| if err := os.Remove(filepath.Join(checkout.Root, reqFile)); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := project.FetchPackagePaths(ctx, []string{reqFile}, 5, opts); err != nil { | |
| t.Fatalf("re-link FetchPackagePaths(%s) failed: %v", reqFile, err) | |
| } | |
| if got := fetchCalls.Load(); got != 1 { | |
| t.Errorf("expected re-link from cache not to call fetcher, got %d", got) | |
| } | |
| if _, err := os.Stat(filepath.Join(checkout.Root, reqFile)); err != nil { | |
| t.Errorf("expected %s to be restored on disk: %v", reqFile, err) | |
| } | |
| }) | |
| t.Run("resolves umbrella directory, optional attributes, and .jiri_manifest fallback", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| checkout.LockfileEnabled = false | |
| manifestXML := fmt.Sprintf(`<manifest> | |
| <packages> | |
| <package name="fuchsia/third_party/qemu/${platform}" version="%s" path="prebuilt/third_party/qemu/{{.OS}}-{{.Arch}}" platforms="%s"/> | |
| <package name="fuchsia/third_party/opt_pkg" version="%s" path="prebuilt/third_party/opt_pkg" attributes="opt" platforms="%s"/> | |
| </packages> | |
| </manifest> | |
| `, qemuIID, cipdPlatStr, edk2IID, cipdPlatStr) | |
| if err := os.WriteFile(checkout.JiriManifestFile(), []byte(manifestXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Umbrella directory matches descendant platform destination via .jiri_manifest. | |
| if err := project.FetchPackagePaths(ctx, []string{"prebuilt/third_party/qemu"}, 5, opts); err != nil { | |
| t.Fatalf("umbrella FetchPackagePaths failed: %v", err) | |
| } | |
| qemuBin := filepath.Join(checkout.Root, "prebuilt", "third_party", "qemu", platSubdir, "bin", "qemu-system-x86_64") | |
| if _, err := os.Stat(qemuBin); err != nil { | |
| t.Errorf("expected %s to exist: %v", qemuBin, err) | |
| } | |
| // Optional package is skipped until FetchingAttrs includes "opt", even if an older | |
| // update_history/latest snapshot without optional packages exists on disk. | |
| snapshotWithoutOpt := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/third_party/qemu/${platform}" version="git_revision:qemu" path="prebuilt/third_party/qemu/{{.OS}}-{{.Arch}}" platforms="%s"> | |
| <instance name="fuchsia/third_party/qemu/%s" id="%s"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, cipdPlatStr, qemuIID) | |
| updateHistoryDir := filepath.Join(checkout.Root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(snapshotWithoutOpt), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| optFile := "prebuilt/third_party/opt_pkg/qemu-x64/OVMF_CODE.fd" | |
| if err := project.FetchPackagePaths(ctx, []string{optFile}, 5, opts); err == nil || !strings.Contains(err.Error(), "no prebuilt package found in manifest") { | |
| t.Fatalf("expected missing optional package error, got %v", err) | |
| } | |
| checkout.FetchingAttrs = "opt" | |
| if err := project.FetchPackagePaths(ctx, []string{optFile}, 5, opts); err != nil { | |
| t.Fatalf("FetchPackagePaths(%s) with FetchingAttrs=opt failed: %v", optFile, err) | |
| } | |
| }) | |
| t.Run("preserves nested child destination and ignores unstamped child directory", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| writeSnapshot(t, checkout.Root) | |
| parentFile := "prebuilt/third_party/parent/parent.txt" | |
| childFile := "prebuilt/third_party/parent/child/child.txt" | |
| if err := os.MkdirAll(filepath.Join(checkout.Root, "prebuilt", "third_party", "parent", "child"), 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| fetchCalls.Store(0) | |
| if err := project.FetchPackagePaths(ctx, []string{"prebuilt/third_party/parent"}, 5, opts); err != nil { | |
| t.Fatalf("fetch parent directory failed: %v", err) | |
| } | |
| if got := fetchCalls.Load(); got != 1 { | |
| t.Fatalf("expected unstamped child directory not to trigger child fetch when requesting parent directory, got %d calls", got) | |
| } | |
| if err := project.FetchPackagePaths(ctx, []string{childFile}, 5, opts); err != nil { | |
| t.Fatalf("fetch child failed: %v", err) | |
| } | |
| if err := os.Remove(filepath.Join(checkout.Root, parentFile)); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := project.FetchPackagePaths(ctx, []string{parentFile}, 5, opts); err != nil { | |
| t.Fatalf("re-fetch parent failed: %v", err) | |
| } | |
| if _, err := os.Stat(filepath.Join(checkout.Root, childFile)); err != nil { | |
| t.Errorf("expected child file to survive parent re-materialization: %v", err) | |
| } | |
| }) | |
| t.Run("worktree falls back to WorktreeParentRoot prebuilt.json for internal package access", func(t *testing.T) { | |
| parentCtx := xtest.NewContext(t) | |
| parentCheckout := jiri.CheckoutFromContext(parentCtx) | |
| parentCheckout.PrebuiltJSON = "prebuilt.json" | |
| internalXML := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/internal/edk2" version="git_revision:edk2" path="prebuilt/third_party/edk2" internal="true" platforms="%s"> | |
| <instance name="fuchsia/internal/edk2" id="%s"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, edk2IID) | |
| updateHistoryDir := filepath.Join(parentCheckout.Root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(internalXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(parentCheckout.RootMetaDir(), parentCheckout.PrebuiltJSON), []byte(`{"internal_access": true}`), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| wtCtx := xtest.NewContext(t) | |
| wtCheckout := jiri.CheckoutFromContext(wtCtx) | |
| wtCheckout.PrebuiltJSON = "prebuilt.json" | |
| wtCheckout.WorktreeParentRoot = parentCheckout.Root | |
| reqFile := "prebuilt/third_party/edk2/qemu-x64/OVMF_CODE.fd" | |
| if err := project.FetchPackagePaths(wtCtx, []string{reqFile}, 5, opts); err != nil { | |
| t.Fatalf("worktree FetchPackagePaths for internal package failed: %v", err) | |
| } | |
| // Once materialized on disk, even if prebuilt.json is removed, the pinned up-to-date fast path returns without calling FilterACL. | |
| if err := os.Remove(filepath.Join(parentCheckout.RootMetaDir(), parentCheckout.PrebuiltJSON)); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := project.FetchPackagePaths(wtCtx, []string{reqFile}, 5, opts); err != nil { | |
| t.Fatalf("warm FetchPackagePaths for internal package without prebuilt.json failed: %v", err) | |
| } | |
| }) | |
| t.Run("skips packages with constrained CIPD template names excluding current platform", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| // Include current platform in platforms="..." so GetPlatforms() passes on all | |
| // host architectures (including arm64), while the inline CIPD template | |
| // constrains the package to a non-host OS. | |
| otherOS := "windows" | |
| if cipd.CurrentPlatform.OS == "windows" { | |
| otherOS = "linux" | |
| } | |
| constrainedXML := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/third_party/edk2" version="git_revision:edk2" path="prebuilt/third_party/edk2" platforms="%s"> | |
| <instance name="fuchsia/third_party/edk2" id="%s"></instance> | |
| </package> | |
| <package name="fuchsia/third_party/other/${os=%s}-${arch}" version="git_revision:other" path="prebuilt/third_party/other" platforms="%s"> | |
| <instance name="fuchsia/third_party/other/%s-amd64" id="CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, edk2IID, otherOS, cipdPlatStr, otherOS) | |
| updateHistoryDir := filepath.Join(checkout.Root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(constrainedXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Direct request for the excluded platform package must report not found. | |
| if err := project.FetchPackagePaths(ctx, []string{"prebuilt/third_party/other"}, 5, opts); err == nil || !strings.Contains(err.Error(), "no prebuilt package found in manifest") { | |
| t.Fatalf("expected missing package error for platform-excluded constrained template, got %v", err) | |
| } | |
| // Umbrella request must materialize edk2 without attempting to fetch the excluded package. | |
| fetchCalls.Store(0) | |
| if err := project.FetchPackagePaths(ctx, []string{"prebuilt/third_party"}, 5, opts); err != nil { | |
| t.Fatalf("umbrella FetchPackagePaths with platform-excluded sibling failed: %v", err) | |
| } | |
| if got := fetchCalls.Load(); got != 1 { | |
| t.Errorf("expected 1 fetch call (edk2 only), got %d", got) | |
| } | |
| }) | |
| t.Run("falls back to .jiri_manifest on snapshot miss when attributes match", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| checkout.LockfileEnabled = false | |
| // Snapshot has matching empty attributes but only contains edk2. | |
| staleSnapshotXML := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/third_party/edk2" version="git_revision:edk2" path="prebuilt/third_party/edk2" platforms="%s"> | |
| <instance name="fuchsia/third_party/edk2" id="%s"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, edk2IID) | |
| updateHistoryDir := filepath.Join(checkout.Root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(staleSnapshotXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Local .jiri_manifest adds qemu. | |
| localManifestXML := fmt.Sprintf(`<manifest> | |
| <packages> | |
| <package name="fuchsia/third_party/edk2" version="%s" path="prebuilt/third_party/edk2" platforms="%s"/> | |
| <package name="fuchsia/third_party/qemu/${platform}" version="%s" path="prebuilt/third_party/qemu/{{.OS}}-{{.Arch}}" platforms="%s"/> | |
| </packages> | |
| </manifest> | |
| `, edk2IID, cipdPlatStr, qemuIID, cipdPlatStr) | |
| if err := os.WriteFile(checkout.JiriManifestFile(), []byte(localManifestXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| qemuBinRel := filepath.Join("prebuilt", "third_party", "qemu", platSubdir, "bin", "qemu-system-x86_64") | |
| if err := project.FetchPackagePaths(ctx, []string{qemuBinRel}, 5, opts); err != nil { | |
| t.Fatalf("FetchPackagePaths(%s) with local .jiri_manifest addition failed: %v", qemuBinRel, err) | |
| } | |
| if _, err := os.Stat(filepath.Join(checkout.Root, qemuBinRel)); err != nil { | |
| t.Errorf("expected %s to be materialized via .jiri_manifest fallback: %v", qemuBinRel, err) | |
| } | |
| }) | |
| t.Run("preserves cipd auth-login hint when up-to-date public package is batched with ACL-filtered internal package", func(t *testing.T) { | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| jiritest.InstallFakeCIPDACL(t, checkout.CIPDPath()) | |
| mixedXML := fmt.Sprintf(`<manifest version="1.1"> | |
| <packages> | |
| <package name="fuchsia/third_party/edk2" version="git_revision:edk2" path="prebuilt/third_party/edk2" platforms="%s"> | |
| <instance name="fuchsia/third_party/edk2" id="%s"></instance> | |
| </package> | |
| <package name="denied/internal_tool" version="git_revision:internal" path="prebuilt/internal/tool" internal="true" platforms="%s"> | |
| <instance name="denied/internal_tool" id="%s"></instance> | |
| </package> | |
| </packages> | |
| </manifest> | |
| `, cipdPlatStr, edk2IID, cipdPlatStr, childIID) | |
| updateHistoryDir := filepath.Join(checkout.Root, jiri.RootMetaDir, "update_history") | |
| if err := os.MkdirAll(updateHistoryDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(updateHistoryDir, "latest"), []byte(mixedXML), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Pre-materialize edk2 so the remaining public destination is already up to date. | |
| edk2File := "prebuilt/third_party/edk2/qemu-x64/OVMF_CODE.fd" | |
| if err := project.FetchPackagePaths(ctx, []string{edk2File}, 5, opts); err != nil { | |
| t.Fatalf("pre-fetch edk2 failed: %v", err) | |
| } | |
| err := project.FetchPackagePaths(ctx, []string{edk2File, "prebuilt/internal/tool/bin"}, 5, opts) | |
| if err == nil { | |
| t.Fatalf("expected error for ACL-filtered internal package, got nil") | |
| } | |
| if !strings.Contains(err.Error(), "cipd auth-login") { | |
| t.Errorf("expected 'cipd auth-login' hint in error, got: %v", err) | |
| } | |
| }) | |
| } | |
| func TestPackageMaterializeInSourceManifestRoundTrip(t *testing.T) { | |
| t.Parallel() | |
| rawXML := `<manifest> | |
| <packages> | |
| <package name="fuchsia/pkg/default" version="v1" path="prebuilt/default"/> | |
| <package name="fuchsia/pkg/explicit_true" version="v1" path="prebuilt/true" materialize-in-source="true"/> | |
| <package name="fuchsia/pkg/build_only" version="v1" path="prebuilt/build_only" materialize-in-source="false"/> | |
| </packages> | |
| </manifest> | |
| ` | |
| m, err := project.ManifestFromBytes([]byte(rawXML)) | |
| if err != nil { | |
| t.Fatalf("ManifestFromBytes failed: %v", err) | |
| } | |
| if len(m.Packages) != 3 { | |
| t.Fatalf("got %d packages, want 3", len(m.Packages)) | |
| } | |
| byName := make(map[string]project.Package, len(m.Packages)) | |
| for _, p := range m.Packages { | |
| byName[p.Name] = p | |
| } | |
| if !byName["fuchsia/pkg/default"].ShouldMaterializeInSource() { | |
| t.Errorf("expected default package ShouldMaterializeInSource() = true") | |
| } | |
| if !byName["fuchsia/pkg/explicit_true"].ShouldMaterializeInSource() { | |
| t.Errorf("expected explicit_true package ShouldMaterializeInSource() = true") | |
| } | |
| if byName["fuchsia/pkg/build_only"].ShouldMaterializeInSource() { | |
| t.Errorf("expected build_only package ShouldMaterializeInSource() = false") | |
| } | |
| serialized, err := m.ToBytes() | |
| if err != nil { | |
| t.Fatalf("ToBytes failed: %v", err) | |
| } | |
| serializedStr := string(serialized) | |
| if !strings.Contains(serializedStr, `materialize-in-source="false"`) { | |
| t.Errorf("expected ToBytes output to preserve materialize-in-source=\"false\", got:\n%s", serializedStr) | |
| } | |
| if strings.Contains(serializedStr, `materialize-in-source="true"`) { | |
| t.Errorf("expected ToBytes output to omit default materialize-in-source=\"true\", got:\n%s", serializedStr) | |
| } | |
| } | |
| func TestLoadWorktreePackageIndexMaterializeInSourceFalse(t *testing.T) { | |
| t.Parallel() | |
| cipdPlatStr := cipd.CurrentPlatform.String() | |
| const ( | |
| inSourceIID = "1111111111111111111111111111111111111111111" | |
| buildOnlyIID = "2222222222222222222222222222222222222222222" | |
| ) | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| falseVal := false | |
| inSourcePkg := project.Package{ | |
| Name: "fuchsia/third_party/in_source", | |
| Version: "git_revision:1", | |
| Path: "prebuilt/third_party/in_source", | |
| Platforms: cipdPlatStr, | |
| Instances: []project.PackageInstance{{Name: "fuchsia/third_party/in_source", ID: inSourceIID}}, | |
| } | |
| buildOnlyPkg := project.Package{ | |
| Name: "fuchsia/third_party/build_only", | |
| Version: "git_revision:2", | |
| Path: "prebuilt/third_party/build_only/nested", | |
| Platforms: cipdPlatStr, | |
| MaterializeInSource: &falseVal, | |
| Instances: []project.PackageInstance{{Name: "fuchsia/third_party/build_only", ID: buildOnlyIID}}, | |
| } | |
| pkgs := project.Packages{ | |
| inSourcePkg.Key(): inSourcePkg, | |
| buildOnlyPkg.Key(): buildOnlyPkg, | |
| } | |
| // Before buildOnlyPkg is cached on disk, LoadWorktreePackageIndex excludes it from both | |
| // DestinationsByWorkspace and Instances. | |
| idx, err := project.LoadWorktreePackageIndex(ctx, pkgs) | |
| if err != nil { | |
| t.Fatalf("LoadWorktreePackageIndex failed: %v", err) | |
| } | |
| dests := idx.DestinationsFor(checkout.Root) | |
| if len(dests) != 1 || dests[0].RelPath != "prebuilt/third_party/in_source" { | |
| t.Fatalf("expected only in_source destination, got %+v", dests) | |
| } | |
| if len(idx.Instances) != 1 || idx.Instances[0].InstanceID != inSourceIID { | |
| t.Fatalf("expected only inSourceIID in Instances when buildOnlyIID is not cached, got %+v", idx.Instances) | |
| } | |
| // Once buildOnlyIID exists in .jiri_root/packages/instances/, LoadWorktreePackageIndex | |
| // retains buildOnlyIID in Instances so jiri update will not prune it, while still | |
| // excluding it from DestinationsByWorkspace. | |
| cache := project.NewPackageCache(checkout.PackageCacheDir()) | |
| buildOnlyInst := project.PackageCacheInstance{ | |
| Name: "fuchsia/third_party/build_only", | |
| Version: "git_revision:2", | |
| InstanceID: buildOnlyIID, | |
| } | |
| if err := os.MkdirAll(cache.InstanceDir(buildOnlyInst), 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| idxWithCached, err := project.LoadWorktreePackageIndex(ctx, pkgs) | |
| if err != nil { | |
| t.Fatalf("LoadWorktreePackageIndex with cached instance failed: %v", err) | |
| } | |
| if len(idxWithCached.DestinationsFor(checkout.Root)) != 1 { | |
| t.Fatalf("expected DestinationsFor to still exclude buildOnlyPkg, got %+v", idxWithCached.DestinationsFor(checkout.Root)) | |
| } | |
| if len(idxWithCached.Instances) != 2 { | |
| t.Fatalf("expected both inSourceIID and cached buildOnlyIID in Instances, got %+v", idxWithCached.Instances) | |
| } | |
| // An unpinned materialize-in-source="false" package that is not installed in .cipd/pkgs | |
| // must be skipped without invoking cipd ensure-file-resolve (even when another instance | |
| // exists in instances/). | |
| // Remove any hardlinked shared test CIPD binary before writing the fake script | |
| // so os.TempDir()'s cached binary is not overwritten. | |
| if err := os.Remove(checkout.CIPDPath()); err != nil && !os.IsNotExist(err) { | |
| t.Fatal(err) | |
| } | |
| if err := os.MkdirAll(filepath.Dir(checkout.CIPDPath()), 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(checkout.CIPDPath(), []byte("#!/bin/sh\necho 'cipd should not be invoked' >&2\nexit 1\n"), 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| unpinnedBuildOnlyPkg := project.Package{ | |
| Name: "fuchsia/third_party/unpinned_build_only", | |
| Version: "latest", | |
| Path: "prebuilt/third_party/unpinned_build_only", | |
| Platforms: cipdPlatStr, | |
| MaterializeInSource: &falseVal, | |
| } | |
| pkgs[unpinnedBuildOnlyPkg.Key()] = unpinnedBuildOnlyPkg | |
| idxWithUnpinned, err := project.LoadWorktreePackageIndex(ctx, pkgs) | |
| if err != nil { | |
| t.Fatalf("LoadWorktreePackageIndex with uncached unpinned build-only pkg failed: %v", err) | |
| } | |
| if len(idxWithUnpinned.Instances) != 2 { | |
| t.Fatalf("expected uncached unpinned build-only pkg not to alter Instances, got %+v", idxWithUnpinned.Instances) | |
| } | |
| } | |
| func TestCIPDPrebuiltProviderPrunesRolledUnmaterializedCacheWhenIndexEmpty(t *testing.T) { | |
| t.Parallel() | |
| cipdPlatStr := cipd.CurrentPlatform.String() | |
| const ( | |
| oldBuildOnlyIID = "3333333333333333333333333333333333333333333" | |
| newBuildOnlyIID = "4444444444444444444444444444444444444444444" | |
| ) | |
| ctx := xtest.NewContext(t) | |
| checkout := jiri.CheckoutFromContext(ctx) | |
| cache := project.NewPackageCache(checkout.PackageCacheDir()) | |
| // Seed cache with a previously fetched build-only instance (including a read-only | |
| // directory and .cipd/pkgs metadata) and a current format version stamp. | |
| oldInst := project.PackageCacheInstance{ | |
| Name: "fuchsia/third_party/build_only", | |
| Version: "git_revision:old", | |
| InstanceID: oldBuildOnlyIID, | |
| } | |
| roDir := filepath.Join(cache.InstanceDir(oldInst), "ro") | |
| if err := os.MkdirAll(roDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(roDir, "tool"), []byte("old-tool"), 0444); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.Chmod(roDir, 0555); err != nil { | |
| t.Fatal(err) | |
| } | |
| t.Cleanup(func() { _ = os.Chmod(roDir, 0755) }) | |
| cipdSlotDir := filepath.Join(cache.Root(), ".cipd", "pkgs", "0") | |
| if err := os.MkdirAll(cipdSlotDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| desc := fmt.Sprintf(`{"subdir":"instances/%s","package_name":"fuchsia/third_party/build_only"}`, oldBuildOnlyIID) | |
| if err := os.WriteFile(filepath.Join(cipdSlotDir, "description.json"), []byte(desc), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(cache.Root(), project.PackageCacheFormatVersionFileName), []byte(fmt.Sprintf("%d\n", project.CurrentPackageCacheFormatVersion)), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Active manifest only contains the rolled unfetched materialize-in-source="false" package. | |
| falseVal := false | |
| rolledPkg := project.Package{ | |
| Name: "fuchsia/third_party/build_only", | |
| Version: "git_revision:new", | |
| Path: "prebuilt/third_party/build_only", | |
| Platforms: cipdPlatStr, | |
| MaterializeInSource: &falseVal, | |
| Instances: []project.PackageInstance{{Name: "fuchsia/third_party/build_only", ID: newBuildOnlyIID}}, | |
| } | |
| pkgs := project.Packages{rolledPkg.Key(): rolledPkg} | |
| if err := project.RunUnifiedPackageCachePipeline(ctx, pkgs, 5); err != nil { | |
| t.Fatalf("RunUnifiedPackageCachePipeline failed: %v", err) | |
| } | |
| if _, err := os.Stat(cache.InstanceDir(oldInst)); !os.IsNotExist(err) { | |
| t.Errorf("expected stale rolled instance %s to be pruned, got err=%v", oldBuildOnlyIID, err) | |
| } | |
| if installed := cache.InstalledInstances(); len(installed) != 0 { | |
| t.Errorf("expected no installed instances after pruning empty index, got %+v", installed) | |
| } | |
| if _, err := os.Stat(filepath.Join(cache.Root(), project.PackageCacheFormatVersionFileName)); err != nil { | |
| t.Errorf("expected format version file to remain after pruning, got err=%v", err) | |
| } | |
| } |