| // 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 integrationtests | |
| import ( | |
| "os" | |
| "path/filepath" | |
| "strconv" | |
| "strings" | |
| "syscall" | |
| "testing" | |
| "go.fuchsia.dev/jiri" | |
| "go.fuchsia.dev/jiri/osutil" | |
| "go.fuchsia.dev/jiri/project" | |
| ) | |
| const ( | |
| gnPkgName = "gn/gn/${platform}" | |
| gnPkgPath = "prebuilt/third_party/gn" | |
| gnV1 = "git_revision:f9cea148d88f2a3570fc2f81a5931f96d20194f1" | |
| gnV2 = "git_revision:925dd782d25df9a60d6402a98a1c22a5c54149a2" | |
| gnV3 = "git_revision:1c73f62240db1517dfd60ff193f53f2e4a52f3a5" | |
| ) | |
| func setupPackageCacheTest(t *testing.T, pkgs ...project.Package) (string, string, func(args ...string) string) { | |
| t.Helper() | |
| remoteDir := t.TempDir() | |
| setupGitRepo(t, remoteDir, map[string]any{ | |
| "manifest": project.Manifest{ | |
| Packages: pkgs, | |
| }, | |
| }) | |
| root := t.TempDir() | |
| jiriRun := jiriInit(t, root, "-enable-lockfile=false") | |
| jiriRun("import", "manifest", remoteDir) | |
| return root, remoteDir, jiriRun | |
| } | |
| func getInode(t *testing.T, path string) uint64 { | |
| t.Helper() | |
| fi, err := os.Stat(path) | |
| if err != nil { | |
| t.Fatalf("failed to stat %s: %v", path, err) | |
| } | |
| stat, ok := fi.Sys().(*syscall.Stat_t) | |
| if !ok { | |
| t.Fatalf("failed to get syscall.Stat_t for %s", path) | |
| } | |
| return stat.Ino | |
| } | |
| func updateRemoteManifestPackageVersion(t *testing.T, remoteDir, pkgName, newVersion string) { | |
| t.Helper() | |
| manifestPath := filepath.Join(remoteDir, "manifest") | |
| manifest, err := project.ManifestFromFile(t.Context(), manifestPath) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| found := false | |
| for i := range manifest.Packages { | |
| if manifest.Packages[i].Name == pkgName { | |
| manifest.Packages[i].Version = newVersion | |
| found = true | |
| break | |
| } | |
| } | |
| if !found { | |
| t.Fatalf("Package %s not found in remote manifest", pkgName) | |
| } | |
| updateGitRepo(t, remoteDir, map[string]any{ | |
| "manifest": *manifest, | |
| }) | |
| } | |
| // TestPackageCache_LegacySymlinkResetsCacheAndRedownloads verifies that if a legacy | |
| // symlink destination is detected, Jiri resets the entire package cache, removes the | |
| // symlink, redownloads clean instances from CIPD, and links a hardlinked directory. | |
| func TestPackageCache_LegacySymlinkResetsCacheAndRedownloads(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkg := project.Package{ | |
| Name: gnPkgName, | |
| Path: gnPkgPath, | |
| Version: gnV1, | |
| } | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| // Sync to create initial cache and hard links. | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| hashFile := filepath.Join(destDir, ".jiri_cache_hash") | |
| // Simulate a legacy symlink worktree: plant a stale file in the cached instance | |
| // and replace destDir with a symlink to the cache directory. | |
| if err := os.RemoveAll(destDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| instancesDir := filepath.Join(root, jiri.RootMetaDir, "packages", "instances") | |
| entries, err := os.ReadDir(instancesDir) | |
| if err != nil || len(entries) == 0 { | |
| t.Fatalf("failed to read instances dir: %v", err) | |
| } | |
| cacheDir := filepath.Join(instancesDir, entries[0].Name()) | |
| staleCacheMarker := filepath.Join(cacheDir, "stale_cache_marker") | |
| if err := os.WriteFile(staleCacheMarker, []byte("contaminated"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.Symlink(cacheDir, destDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| fi, err := os.Lstat(destDir) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| if fi.Mode()&os.ModeSymlink == 0 { | |
| t.Fatalf("Expected symlink, got %s at %s", fi.Mode(), destDir) | |
| } | |
| // Sync again: should nuke the entire cache, remove the symlink, and redownload cleanly. | |
| jiriRun("update") | |
| if _, err := os.Stat(staleCacheMarker); !os.IsNotExist(err) { | |
| t.Fatalf("Expected cache to be nuked and redownloaded, but stale_cache_marker still exists (err=%v)", err) | |
| } | |
| fi, err = os.Lstat(destDir) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| if fi.Mode()&os.ModeSymlink != 0 || !fi.IsDir() { | |
| t.Fatalf("Expected real directory after re-init, got %s at %s", fi.Mode(), destDir) | |
| } | |
| if _, err := os.Stat(hashFile); err != nil { | |
| t.Fatalf("Expected .jiri_cache_hash file to exist after re-init: %v", err) | |
| } | |
| if getInode(t, filepath.Join(destDir, "gn")) != getInode(t, filepath.Join(cacheDir, "gn")) { | |
| t.Errorf("Expected %s/gn to be hardlinked to freshly redownloaded %s/gn", destDir, cacheDir) | |
| } | |
| } | |
| // Case S1: Cache Missing, Dest Missing | |
| // Fresh fetch with package cache. | |
| // Expected: Package is downloaded from CIPD into cache, hardlinked to destDir, and .jiri_cache_hash is written. | |
| func TestPackageCacheState_FreshDownload(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkg := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| hashFile := filepath.Join(destDir, ".jiri_cache_hash") | |
| if _, err := os.Stat(hashFile); err != nil { | |
| t.Errorf("Expected .jiri_cache_hash to exist: %v", err) | |
| } | |
| instancesDir := filepath.Join(root, jiri.RootMetaDir, "packages", "instances") | |
| entries, _ := os.ReadDir(instancesDir) | |
| linked := false | |
| destInode := getInode(t, filepath.Join(destDir, "gn")) | |
| for _, e := range entries { | |
| cacheInode := getInode(t, filepath.Join(instancesDir, e.Name(), "gn")) | |
| if cacheInode == destInode { | |
| linked = true | |
| break | |
| } | |
| } | |
| if !linked { | |
| t.Errorf("Expected destDir/gn to be hardlinked to cacheDir/gn") | |
| } | |
| } | |
| // Case S2: Cache Valid, Dest Valid Hardlink (matching hash) | |
| // Steady-state no-op update when everything is already up to date. | |
| // Expected: Fast path succeeds without modifications. | |
| func TestPackageCacheState_NoOpSteadyState(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkg := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| jiriRun("update") | |
| // Run update again. | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| if _, err := os.Stat(filepath.Join(destDir, ".jiri_cache_hash")); err != nil { | |
| t.Errorf("Expected .jiri_cache_hash to exist: %v", err) | |
| } | |
| } | |
| // Case S3: Cache Valid, Dest Missing | |
| // Worktree creation or deleted destDir when package is already in cache. | |
| // Expected: Instant hardlink from cache without network download. | |
| func TestPackageCacheState_WorktreeCacheHit(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkg := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| hashFile := filepath.Join(destDir, ".jiri_cache_hash") | |
| _ = readFile(t, hashFile) | |
| // Delete destDir to simulate a missing destination. | |
| if err := os.RemoveAll(destDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Re-run update. | |
| jiriRun("update") | |
| if _, err := os.Stat(hashFile); err != nil { | |
| t.Errorf("Expected .jiri_cache_hash to be recreated: %v", err) | |
| } | |
| instancesDir := filepath.Join(root, jiri.RootMetaDir, "packages", "instances") | |
| entries, _ := os.ReadDir(instancesDir) | |
| linked := false | |
| destInode := getInode(t, filepath.Join(destDir, "gn")) | |
| for _, e := range entries { | |
| cacheInode := getInode(t, filepath.Join(instancesDir, e.Name(), "gn")) | |
| if cacheInode == destInode { | |
| linked = true | |
| break | |
| } | |
| } | |
| if !linked { | |
| t.Errorf("Expected destDir/gn to be hardlinked to cacheDir/gn") | |
| } | |
| } | |
| // Case S4: Cache Valid, Dest Stale Hardlink (pointing to older version) | |
| // Version roll where the target version was already cached (e.g. by another worktree). | |
| // Expected: destDir is replaced with hardlinks to the target version's cache. | |
| func TestPackageCacheState_RollWithPrecachedTarget(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| // Fetch v2 first into cache. | |
| pkgV2 := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV2} | |
| root, remoteDir, jiriRun := setupPackageCacheTest(t, pkgV2) | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| v2Hash := strings.TrimSpace(readFile(t, filepath.Join(destDir, ".jiri_cache_hash"))) | |
| // Switch to v1 so destDir is on v1 hardlinks. | |
| updateRemoteManifestPackageVersion(t, remoteDir, gnPkgName, gnV1) | |
| jiriRun("update") | |
| v1Hash := strings.TrimSpace(readFile(t, filepath.Join(destDir, ".jiri_cache_hash"))) | |
| if v1Hash == v2Hash { | |
| t.Fatalf("Expected different hashes for v1 and v2") | |
| } | |
| // Roll back to v2 (which is already valid in cache). | |
| updateRemoteManifestPackageVersion(t, remoteDir, gnPkgName, gnV2) | |
| jiriRun("update") | |
| currentHash := strings.TrimSpace(readFile(t, filepath.Join(destDir, ".jiri_cache_hash"))) | |
| if currentHash != v2Hash { | |
| t.Errorf("Expected destDir to be updated to v2 hash %s, got %s", v2Hash, currentHash) | |
| } | |
| } | |
| // Case S6: Cache Missing, Dest Stale Hardlink (contains .jiri_cache_hash for v1) | |
| // Version roll. | |
| // Expected: Stale destDir is wiped, v2 is downloaded from CIPD into cache, and hardlinked to dest. | |
| func TestPackageCacheState_VersionRoll(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkgV1 := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, remoteDir, jiriRun := setupPackageCacheTest(t, pkgV1) | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| markerFile := filepath.Join(destDir, "v1_marker") | |
| if err := os.WriteFile(markerFile, []byte("v1"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| v1Version := readFile(t, filepath.Join(destDir, ".versions", "gn.cipd_version")) | |
| // Roll to v2. | |
| updateRemoteManifestPackageVersion(t, remoteDir, gnPkgName, gnV2) | |
| jiriRun("update") | |
| // Failure condition: if stale destDir was not wiped before linking v2, v1_marker still exists. | |
| if _, err := os.Stat(markerFile); !os.IsNotExist(err) { | |
| t.Errorf("Stale destDir was not wiped on version roll! Marker from v1 still exists in v2 destDir.") | |
| } | |
| v2Version := readFile(t, filepath.Join(destDir, ".versions", "gn.cipd_version")) | |
| if v1Version == v2Version { | |
| t.Errorf("Package version was not updated! Still on v1: %s", v1Version) | |
| } | |
| } | |
| // Case S9: Cache Missing, Dest Dirty Standalone (untracked / build artifact files) | |
| // Pre-existing dirty directory without .jiri_cache_hash. | |
| // Expected: Dirty destDir is wiped and replaced with clean hardlinks from CIPD cache. | |
| func TestPackageCacheState_PreexistingDirtyDestDir(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkgV1 := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkgV1) | |
| // Simulate dirty pre-existing destDir. | |
| destDir := filepath.Join(root, gnPkgPath) | |
| if err := os.MkdirAll(destDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| dirtyFile := filepath.Join(destDir, "dirty_build_artifact") | |
| if err := os.WriteFile(dirtyFile, []byte("dirty"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| jiriRun("update") | |
| // Failure condition: dirty file remained in destDir instead of being wiped. | |
| if _, err := os.Stat(dirtyFile); !os.IsNotExist(err) { | |
| t.Errorf("Dirty file was not wiped from destDir when installing clean package.") | |
| } | |
| if _, err := os.Stat(filepath.Join(destDir, ".versions", "gn.cipd_version")); err != nil { | |
| t.Errorf("Clean CIPD package was not installed: %v", err) | |
| } | |
| } | |
| // Case S10: Cache Invalid (corrupted as regular file), Dest Missing | |
| // Cache directory is corrupted into a non-directory. | |
| // Expected: Detects invalid cache, removes it, downloads clean package into cache. | |
| func TestPackageCacheState_CorruptCacheEntry(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkgV1 := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkgV1) | |
| destDir := filepath.Join(root, gnPkgPath) | |
| // Run update once to create normal setup, then corrupt cache. | |
| jiriRun("update") | |
| _ = strings.TrimSpace(readFile(t, filepath.Join(destDir, ".jiri_cache_hash"))) | |
| instancesDir := filepath.Join(root, jiri.RootMetaDir, "packages", "instances") | |
| entries, _ := os.ReadDir(instancesDir) | |
| cacheDir := filepath.Join(instancesDir, entries[0].Name()) | |
| if err := os.RemoveAll(cacheDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.RemoveAll(destDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Write a file instead of directory. | |
| if err := os.WriteFile(cacheDir, []byte("corrupted_cache_not_a_dir"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Re-run update: should remove corrupt file and recreate valid cache directory. | |
| jiriRun("update") | |
| fi, err := os.Lstat(cacheDir) | |
| if err != nil || !fi.IsDir() { | |
| t.Errorf("Expected cacheDir to be restored as a valid directory, got err=%v", err) | |
| } | |
| } | |
| // TestPackageCache_WorktreeClangRoll_GCPreservationAndPrebuiltCleanup tests the scenario where: | |
| // - Worktrees were created using Clang 23. | |
| // - Main tree updates to Clang 24 (and removes obsolete packages). | |
| // - GC runs. | |
| func TestPackageCache_WorktreeClangRoll_GCPreservationAndPrebuiltCleanup(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| clangPkgPath := "prebuilt/third_party/clang" | |
| oldToolPkgPath := "prebuilt/third_party/old_tool" | |
| clangPkgV1 := project.Package{Name: gnPkgName, Path: clangPkgPath, Version: gnV1} | |
| oldToolPkg := project.Package{Name: gnPkgName, Path: oldToolPkgPath, Version: gnV3} | |
| root, remoteDir, jiriRun := setupPackageCacheTest(t, clangPkgV1, oldToolPkg) | |
| // Initial fetch. | |
| jiriRun("update", "-gc=true") | |
| mainClangDir := filepath.Join(root, clangPkgPath) | |
| mainOldToolDir := filepath.Join(root, oldToolPkgPath) | |
| v1Version := readFile(t, filepath.Join(mainClangDir, ".versions", "gn.cipd_version")) | |
| wtRoot := filepath.Join(root, ".jiri_root", "worktrees", "wt1") | |
| wtClangDir := filepath.Join(wtRoot, clangPkgPath) | |
| os.MkdirAll(filepath.Dir(wtClangDir), 0755) | |
| if err := osutil.LinkPath(mainClangDir, wtClangDir); err != nil { | |
| t.Fatal(err) | |
| } | |
| // In main tree, update to Clang 24 and remove old_tool from the manifest. | |
| updateGitRepo(t, remoteDir, map[string]any{ | |
| "manifest": project.Manifest{ | |
| Packages: []project.Package{ | |
| {Name: gnPkgName, Path: clangPkgPath, Version: gnV2}, | |
| }, | |
| }, | |
| }) | |
| // Run update in the main tree. | |
| jiriRun("update", "-gc=true") | |
| // Main tree /prebuilt: MUST reflect ONLY the packages currently needed by main (Clang 24). | |
| v2Version := readFile(t, filepath.Join(mainClangDir, ".versions", "gn.cipd_version")) | |
| if v2Version == v1Version { | |
| t.Log("main tree clang not updated to v2 version json, but CIPD handled files") | |
| } | |
| if _, err := os.Stat(filepath.Join(mainClangDir, ".jiri_cache_hash")); err != nil { | |
| t.Errorf("main tree clang lost .jiri_cache_hash: %v", err) | |
| } | |
| // Obsolete package prebuilt/third_party/old_tool MUST be removed from main /prebuilt! | |
| if _, err := os.Stat(mainOldToolDir); !os.IsNotExist(err) { | |
| t.Errorf("obsolete package %s was NOT garbage collected from main prebuilt!", mainOldToolDir) | |
| } | |
| // Worktree /prebuilt: MUST reflect the packages currently needed by wt1 (Clang 23). | |
| if readFile(t, filepath.Join(wtClangDir, ".versions", "gn.cipd_version")) != v1Version { | |
| t.Log("worktree clang version changed unexpectedly!") | |
| } | |
| if _, err := os.Stat(filepath.Join(wtClangDir, ".jiri_cache_hash")); err != nil { | |
| t.Errorf("worktree clang lost .jiri_cache_hash: %v", err) | |
| } | |
| } | |
| // TestPackageCache_GCRespectsLocalManifestProjects checks that the package GC | |
| // at the end of `jiri update -local-manifest-project=<p>` loads <p>'s manifest | |
| // from the working tree, just like the package fetch does, so it doesn't | |
| // delete packages that only exist in local manifest changes. <p> is imported | |
| // indirectly so it's not one of the default local manifest projects. | |
| func TestPackageCache_GCRespectsLocalManifestProjects(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| fooPkg := project.Package{Name: gnPkgName, Path: "prebuilt/foo", Version: gnV1} | |
| barPkg := project.Package{Name: gnPkgName, Path: "prebuilt/bar", Version: gnV2} | |
| bRemoteDir := t.TempDir() | |
| setupGitRepo(t, bRemoteDir, map[string]any{ | |
| "b_manifest": project.Manifest{ | |
| Projects: []project.Project{{Name: "b", Path: "b", Remote: bRemoteDir}}, | |
| Packages: []project.Package{fooPkg}, | |
| }, | |
| }) | |
| aRemoteDir := t.TempDir() | |
| setupGitRepo(t, aRemoteDir, map[string]any{ | |
| "a_manifest": project.Manifest{ | |
| Imports: []project.Import{{ | |
| Name: "b", | |
| Manifest: "b_manifest", | |
| Remote: bRemoteDir, | |
| Revision: currentRevision(t, bRemoteDir), | |
| }}, | |
| Projects: []project.Project{{Name: "a", Path: "a", Remote: aRemoteDir}}, | |
| }, | |
| }) | |
| root := t.TempDir() | |
| jiri := jiriInit(t, root, "-enable-lockfile=false") | |
| jiri("import", "-name", "a", "a_manifest", aRemoteDir) | |
| jiri("update") | |
| localManifest := filepath.Join(root, "b", "b_manifest") | |
| m, err := project.ManifestFromFile(t.Context(), localManifest) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| m.Packages = append(m.Packages, barPkg) | |
| b, err := m.ToBytes() | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| writeFile(t, localManifest, string(b)) | |
| jiri("update", "-gc=true", "-local-manifest-project", "b") | |
| for _, pkg := range []project.Package{fooPkg, barPkg} { | |
| if _, err := os.Stat(filepath.Join(root, pkg.Path, ".jiri_cache_hash")); err != nil { | |
| t.Errorf("Expected %s to be installed: %s", pkg.Path, err) | |
| } | |
| } | |
| } | |
| // TestWorktreePackageIndex_RawInstanceIDVersion verifies that packages pinned directly | |
| // to a raw CIPD instance ID in their manifest Version attribute (which therefore have | |
| // len(pkg.Instances) == 0 because they are not in jiri.lock) are populated directly | |
| // in WorktreePackageIndex without invoking cipd ensure-file-resolve, and are properly | |
| // linked into the workspace. | |
| func TestWorktreePackageIndex_RawInstanceIDVersion(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| rawIID := "GvbSt6gklhdVYRp9r7bot-rllr0smxfv9rAElX3K798C" | |
| pkg := project.Package{ | |
| Name: gnPkgName, | |
| Path: gnPkgPath, | |
| Version: rawIID, | |
| } | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| jiriRun("update") | |
| destDir := filepath.Join(root, gnPkgPath) | |
| stamp := strings.TrimSpace(readFile(t, filepath.Join(destDir, ".jiri_cache_hash"))) | |
| if !strings.Contains(stamp, rawIID) { | |
| t.Errorf("expected .jiri_cache_hash to contain %q, got %q", rawIID, stamp) | |
| } | |
| cacheGn := filepath.Join(root, jiri.RootMetaDir, "packages", "instances", rawIID, "gn") | |
| if getInode(t, filepath.Join(destDir, "gn")) != getInode(t, cacheGn) { | |
| t.Errorf("expected %s/gn to be hardlinked to %s", destDir, cacheGn) | |
| } | |
| } | |
| // TestPackageCache_LegacyCheckoutCleanReinit verifies that a legacy checkout with | |
| // standalone <root>/.cipd, legacy .jiri_root/packages/<hex64> group cache directories, | |
| // a v1 .jiri_cache_format_version, and a legacy plain-text stamped prebuilt destination | |
| // resets the entire package cache and recovers via clean re-initialization during jiri update. | |
| func TestPackageCache_LegacyCheckoutCleanReinit(t *testing.T) { | |
| t.Parallel() | |
| if testing.Short() { | |
| t.Skip("Skipping test in short mode.") | |
| } | |
| pkg := project.Package{Name: gnPkgName, Path: gnPkgPath, Version: gnV1} | |
| root, _, jiriRun := setupPackageCacheTest(t, pkg) | |
| // Populate legacy standalone .cipd directory. | |
| standaloneCipd := filepath.Join(root, ".cipd", "pkgs", "0") | |
| if err := os.MkdirAll(standaloneCipd, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(filepath.Join(standaloneCipd, "description.json"), []byte("{}"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Populate legacy group cache directory, stale instance, and v1 format version in .jiri_root/packages/. | |
| cacheRoot := filepath.Join(root, jiri.RootMetaDir, "packages") | |
| legacyGroupDir := filepath.Join(cacheRoot, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") | |
| if err := os.MkdirAll(legacyGroupDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| staleInstanceDir := filepath.Join(cacheRoot, "instances", "stale-contaminated-iid") | |
| if err := os.MkdirAll(staleInstanceDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| versionFile := filepath.Join(cacheRoot, project.PackageCacheFormatVersionFileName) | |
| if err := os.WriteFile(versionFile, []byte("1\n"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| // Populate legacy destination with a stale file and plain-text .jiri_cache_hash stamp. | |
| destDir := filepath.Join(root, gnPkgPath) | |
| if err := os.MkdirAll(destDir, 0755); err != nil { | |
| t.Fatal(err) | |
| } | |
| legacyMarker := filepath.Join(destDir, "legacy_marker") | |
| if err := os.WriteFile(legacyMarker, []byte("legacy"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| stamp := project.NewCacheStamp(destDir) | |
| if err := os.WriteFile(stamp.Path(), []byte("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| jiriRun("update") | |
| if _, err := os.Stat(filepath.Join(root, ".cipd")); !os.IsNotExist(err) { | |
| t.Errorf("expected standalone <root>/.cipd to be removed, got err=%v", err) | |
| } | |
| if _, err := os.Stat(legacyGroupDir); !os.IsNotExist(err) { | |
| t.Errorf("expected legacy group cache dir to be removed, got err=%v", err) | |
| } | |
| if _, err := os.Stat(staleInstanceDir); !os.IsNotExist(err) { | |
| t.Errorf("expected stale instance dir to be removed on cache reset, got err=%v", err) | |
| } | |
| if got := strings.TrimSpace(readFile(t, versionFile)); got != strconv.Itoa(project.CurrentPackageCacheFormatVersion) { | |
| t.Errorf("expected %s to be %d, got %q", project.PackageCacheFormatVersionFileName, project.CurrentPackageCacheFormatVersion, got) | |
| } | |
| if _, err := os.Stat(legacyMarker); !os.IsNotExist(err) { | |
| t.Errorf("expected legacy marker in destination to be wiped, got err=%v", err) | |
| } | |
| data, err := stamp.ReadData() | |
| if err != nil { | |
| t.Fatalf("expected valid JSON stamp after update, got err=%v", err) | |
| } | |
| if data.FormatVersion != project.CurrentCacheStampFormatVersion { | |
| t.Errorf("expected stamp format_version %d, got %d", project.CurrentCacheStampFormatVersion, data.FormatVersion) | |
| } | |
| // Verify that an invalid stamp file alone (even when .jiri_cache_format_version is current) | |
| // triggers a full cache reset and clean redownload. | |
| instancesDir := filepath.Join(cacheRoot, "instances") | |
| entries, err := os.ReadDir(instancesDir) | |
| if err != nil || len(entries) == 0 { | |
| t.Fatalf("failed to read instances dir: %v", err) | |
| } | |
| cacheDir := filepath.Join(instancesDir, entries[0].Name()) | |
| staleCacheMarker := filepath.Join(cacheDir, "stale_cache_marker") | |
| if err := os.WriteFile(staleCacheMarker, []byte("contaminated"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.WriteFile(stamp.Path(), []byte("invalid-old-style-stamp\n"), 0644); err != nil { | |
| t.Fatal(err) | |
| } | |
| jiriRun("update") | |
| if _, err := os.Stat(staleCacheMarker); !os.IsNotExist(err) { | |
| t.Errorf("expected invalid stamp file to reset cache and redownload, but stale_cache_marker still exists (err=%v)", err) | |
| } | |
| if _, err := stamp.ReadData(); err != nil { | |
| t.Fatalf("expected valid JSON stamp after invalid-stamp reset, got err=%v", err) | |
| } | |
| } |