| 1 | //go:build linux |
| 2 | |
| 3 | package hostfiles |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | // SyncDirAt publishes the directory it was handed, whatever is renamed |
| 15 | // into its path afterwards, and leaves the caller's descriptor open. |
| 16 | func TestSyncDirAtPublishesTheDirectoryItWasHanded(t *testing.T) { |
| 17 | ctx := context.Background() |
| 18 | s := release.NewDirStore(t.TempDir()) |
| 19 | p := open(t, s) |
| 20 | base := t.TempDir() |
| 21 | dir, other := filepath.Join(base, "files"), t.TempDir() |
| 22 | if err := os.Mkdir(dir, 0o700); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if err := os.WriteFile(filepath.Join(dir, "mine.txt"), []byte("mine"), 0o600); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if err := os.WriteFile(filepath.Join(other, "secret"), []byte("not the tenant's"), 0o600); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | f, err := os.Open(dir) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | defer f.Close() |
| 36 | if err := os.Rename(dir, dir+".moved"); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if err := os.Symlink(other, dir); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if _, err := SyncDirAt(ctx, p, f, nil); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | commit(t, p) |
| 46 | m, ok, err := Load(ctx, s, pfx) |
| 47 | if err != nil || !ok { |
| 48 | t.Fatalf("manifest: %v %v", ok, err) |
| 49 | } |
| 50 | if _, ok := m.Entries["secret"]; ok { |
| 51 | t.Fatal("SyncDirAt published the directory the path names now") |
| 52 | } |
| 53 | if _, ok := m.Entries["mine.txt"]; !ok { |
| 54 | t.Fatalf("SyncDirAt did not publish the directory it was handed: %v", m.Entries) |
| 55 | } |
| 56 | if _, err := f.Stat(); err != nil { |
| 57 | t.Fatalf("SyncDirAt closed the caller's directory: %v", err) |
| 58 | } |
| 59 | reg, err := os.Open(filepath.Join(dir+".moved", "mine.txt")) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | defer reg.Close() |
| 64 | if _, err := SyncDirAt(ctx, open(t, release.NewDirStore(t.TempDir())), reg, nil); err == nil { |
| 65 | t.Fatal("SyncDirAt took a regular file for a directory") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // RestoreRoot restores into the root it was handed, whatever is renamed |
| 70 | // into its path afterwards, and Restore is RestoreRoot on the path. |
| 71 | func TestRestoreRootWritesIntoTheRootItWasHanded(t *testing.T) { |
| 72 | ctx := context.Background() |
| 73 | s := release.NewDirStore(t.TempDir()) |
| 74 | p := open(t, s) |
| 75 | stage(t, p, "a/b.txt", "restored") |
| 76 | commit(t, p) |
| 77 | base := t.TempDir() |
| 78 | dir, other := filepath.Join(base, "files"), t.TempDir() |
| 79 | if err := os.Mkdir(dir, 0o700); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | root, err := os.OpenRoot(dir) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | defer root.Close() |
| 87 | if err := os.Rename(dir, dir+".moved"); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | if err := os.Symlink(other, dir); err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | if _, st, err := RestoreRoot(ctx, s, pfx, root, nil); err != nil || st.Written != 1 { |
| 94 | t.Fatalf("RestoreRoot: %+v %v", st, err) |
| 95 | } |
| 96 | if names, _ := os.ReadDir(other); len(names) != 0 { |
| 97 | t.Fatalf("RestoreRoot wrote into the directory the path names now: %v", names) |
| 98 | } |
| 99 | if b, err := os.ReadFile(filepath.Join(dir+".moved", "a", "b.txt")); err != nil || string(b) != "restored" { |
| 100 | t.Fatalf("RestoreRoot did not write into its root: %q %v", b, err) |
| 101 | } |
| 102 | if _, err := root.Stat("."); err != nil { |
| 103 | t.Fatalf("RestoreRoot closed the caller's root: %v", err) |
| 104 | } |
| 105 | if _, _, err := RestoreRoot(ctx, s, pfx, nil, nil); err == nil { |
| 106 | t.Fatal("RestoreRoot took a nil root") |
| 107 | } |
| 108 | } |
| 109 | |