| 58 | } |
| 59 | |
| 60 | func testCopy(t *testing.T, copyFunc func(src, dst string) error) { |
| 61 | srcRoot := t.TempDir() |
| 62 | t.Logf("srcRoot location: %s\n", srcRoot) |
| 63 | |
| 64 | // Source Files |
| 65 | srcFile := filepath.Join(srcRoot, "srcFile") |
| 66 | if err := os.WriteFile(srcFile, []byte("test"), 0o644); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | // Source Dirs |
| 70 | srcDir := filepath.Join(srcRoot, "srcDir") |
| 71 | if err := os.Mkdir(srcDir, 0o755); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | // Source Symlink |
| 75 | srcLink := filepath.Join(srcRoot, "srcLink") |
| 76 | if err := os.Symlink("srcFile", srcLink); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | dstRoot := t.TempDir() |
| 81 | t.Logf("dstRoot location: %s\n", dstRoot) |
| 82 | |
| 83 | // Perform the actual copy to a subdir of our dst tempdir. |
| 84 | // This ensures CopyWithTar has to create the dest directory, which is |
| 85 | // where the non-wrapped call would fail for unprivileged users. |
| 86 | err := copyFunc(srcRoot, path.Join(dstRoot, "dst")) |
| 87 | if err != nil { |
| 88 | t.Fatalf("Error during CopyWithTar: %v", err) |
| 89 | } |
| 90 | |
| 91 | tests := []struct { |
| 92 | name string |
| 93 | expectPath string |
| 94 | expectFile bool |
| 95 | expectDir bool |
| 96 | expectLink bool |
| 97 | }{ |
| 98 | { |
| 99 | name: "file", |
| 100 | expectPath: "dst/srcFile", |
| 101 | expectFile: true, |
| 102 | }, |
| 103 | { |
| 104 | name: "dir", |
| 105 | expectPath: "dst/srcDir", |
| 106 | expectDir: true, |
| 107 | }, |
| 108 | { |
| 109 | name: "symlink", |
| 110 | expectPath: "dst/srcLink", |
| 111 | expectFile: true, |
| 112 | expectLink: true, |
| 113 | }, |
| 114 | } |
| 115 | |
| 116 | for _, tt := range tests { |
| 117 | t.Run(tt.name, func(t *testing.T) { |