| 142 | } |
| 143 | |
| 144 | func TestMountAt(t *testing.T) { |
| 145 | testutil.RequiresRoot(t) |
| 146 | |
| 147 | dir1 := t.TempDir() |
| 148 | dir2 := t.TempDir() |
| 149 | |
| 150 | defer unix.Unmount(filepath.Join(dir2, "bar"), unix.MNT_DETACH) |
| 151 | |
| 152 | if err := os.WriteFile(filepath.Join(dir1, "foo"), []byte("foo"), 0644); err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | |
| 156 | if err := os.WriteFile(filepath.Join(dir2, "bar"), []byte{}, 0644); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | |
| 160 | wd, err := os.Getwd() |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | |
| 165 | // mount ${dir1}/foo at ${dir2}/bar |
| 166 | // But since we are using `mountAt` we only need to specify the relative path to dir2 as the target mountAt will chdir to there. |
| 167 | if err := mountAt(dir2, filepath.Join(dir1, "foo"), "bar", "none", unix.MS_BIND, ""); err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | |
| 171 | b, err := os.ReadFile(filepath.Join(dir2, "bar")) |
| 172 | if err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | |
| 176 | if string(b) != "foo" { |
| 177 | t.Fatalf("unexpected file content: %s", b) |
| 178 | } |
| 179 | |
| 180 | newWD, err := os.Getwd() |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if wd != newWD { |
| 185 | t.Fatalf("unexpected working directory: %s", newWD) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestUnmountMounts(t *testing.T) { |
| 190 | testutil.RequiresRoot(t) |