|
8 | 8 | "github.com/docker-library/bashbrew/pkg/gitfs"
|
9 | 9 |
|
10 | 10 | "github.com/go-git/go-git/v5"
|
| 11 | + goGitConfig "github.com/go-git/go-git/v5/config" |
11 | 12 | "github.com/go-git/go-git/v5/storage/memory"
|
12 | 13 | )
|
13 | 14 |
|
@@ -52,7 +53,7 @@ func TestCommitFS(t *testing.T) {
|
52 | 53 | })
|
53 | 54 | }
|
54 | 55 |
|
55 |
| -func TestSymlinkFS(t *testing.T) { |
| 56 | +func TestRootSymlinkFS(t *testing.T) { |
56 | 57 | // TODO instead of cloning a remote repository, synthesize a very simple Git repository right in the test here (benefit of the remote repository is that it's much larger, so fstest.TestFS has a lot more data to test against)
|
57 | 58 | repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
58 | 59 | URL: "https://github.com/tianon/gosu.git", // just a repository with a known symlink (`.dockerignore` -> `.gitignore`)
|
@@ -93,3 +94,80 @@ func TestSymlinkFS(t *testing.T) {
|
93 | 94 | }
|
94 | 95 | })
|
95 | 96 | }
|
| 97 | + |
| 98 | +func TestSubdirSymlinkFS(t *testing.T) { |
| 99 | + // TODO instead of cloning a remote repository, synthesize a very simple Git repository right in the test here (benefit of the remote repository is that it's much larger, so fstest.TestFS has a lot more data to test against) |
| 100 | + // Init + CreateRemoteAnonymous + Fetch because Clone doesn't support fetch-by-commit |
| 101 | + repo, err := git.Init(memory.NewStorage(), nil) |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + remote, err := repo.CreateRemoteAnonymous(&goGitConfig.RemoteConfig{ |
| 106 | + Name: "anonymous", |
| 107 | + URLs: []string{"https://github.com/docker-library/busybox.git"}, // just a repository with a known symlink at a non-root level (`latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318` -> `../../image-config.json`) |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + commit := "668d52e6f0596e0fd0b1be1d8267c4b9240dc2b3" |
| 113 | + err = remote.Fetch(&git.FetchOptions{ |
| 114 | + RefSpecs: []goGitConfig.RefSpec{goGitConfig.RefSpec(commit + ":FETCH_HEAD")}, |
| 115 | + Tags: git.NoTags, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + f, err := gitfs.CommitHash(repo, commit) |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + t.Run("Open+ReadAll", func(t *testing.T) { |
| 126 | + r, err := f.Open("latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318") |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + defer func() { |
| 131 | + if err := r.Close(); err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + }() |
| 135 | + b, err := io.ReadAll(r) |
| 136 | + if err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | + expected := `{ |
| 140 | + "config": { |
| 141 | + "Cmd": [ |
| 142 | + "sh" |
| 143 | + ] |
| 144 | + }, |
| 145 | + "created": "2023-05-18T22:34:17Z", |
| 146 | + "history": [ |
| 147 | + { |
| 148 | + "created": "2023-05-18T22:34:17Z", |
| 149 | + "created_by": "BusyBox 1.36.1 (musl), Alpine 3.19.1" |
| 150 | + } |
| 151 | + ], |
| 152 | + "rootfs": { |
| 153 | + "type": "layers", |
| 154 | + "diff_ids": [ |
| 155 | + "sha256:994bf8f4adc78c5c1e4a6b5e3b59ad57902b301e0e79255a3e95ea4b213a76bd" |
| 156 | + ] |
| 157 | + }, |
| 158 | + "architecture": "amd64", |
| 159 | + "os": "linux" |
| 160 | +} |
| 161 | +` |
| 162 | + if string(b) != expected { |
| 163 | + t.Fatalf("expected %q, got %q", expected, string(b)) |
| 164 | + } |
| 165 | + }) |
| 166 | + |
| 167 | + // might as well run fstest again, now that we have a new filesystem tree 😅 |
| 168 | + t.Run("fstest.TestFS", func(t *testing.T) { |
| 169 | + if err := fstest.TestFS(f, "latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318", "latest/musl/amd64/index.json"); err != nil { |
| 170 | + t.Fatal(err) |
| 171 | + } |
| 172 | + }) |
| 173 | +} |
0 commit comments