Skip to content

Commit

Permalink
chore: add report tests for hard links
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijie-yang committed Nov 28, 2024
1 parent 9be1e54 commit 0a922ec
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/manifest/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func (r *Report) getHardLinkId(fsEntry *fsutil.Entry) uint64 {
r.Entries[relLinkPath] = entry
}
hardLinkId = entry.HardLinkId
fsEntry.SHA256 = entry.SHA256
fsEntry.Size = entry.Size
if fsEntry.Mode.IsRegular() {
// The hard link links to a regular file
fsEntry.SHA256 = entry.SHA256
fsEntry.Size = entry.Size
fsEntry.Link = ""
} else {
// The hard link links to a symlink
Expand Down
124 changes: 124 additions & 0 deletions internal/manifest/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ var sampleLink = fsutil.Entry{
Link: "/base/example-file",
}

var sampleHardLinkReg = fsutil.Entry{
Path: "/base/example-hard-link-reg",
Mode: sampleFile.Mode,
Link: "/base/example-file",
LinkType: fsutil.TypeHardLink,
}

var sampleHardLinkSym = fsutil.Entry{
Path: "/base/example-hard-link-sym",
Mode: fs.ModeSymlink | sampleFile.Mode,
Link: "/base/example-link",
LinkType: fsutil.TypeHardLink,
}

var sampleFileMutated = fsutil.Entry{
Path: sampleFile.Path,
SHA256: sampleFile.SHA256 + "_changed",
Expand Down Expand Up @@ -267,6 +281,116 @@ var reportTests = []struct {
add: []sliceAndEntry{{entry: sampleDir, slice: oneSlice}},
mutate: []*fsutil.Entry{&sampleDir},
err: `cannot mutate path in report: /example-dir/ is a directory`,
}, {
summary: "Regular file hard link",
add: []sliceAndEntry{
{entry: sampleFile, slice: oneSlice},
{entry: sampleHardLinkReg, slice: oneSlice}},
expected: map[string]manifest.ReportEntry{
"/example-file": {
Path: "/example-file",
Mode: 0777,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 1,
},
"/example-hard-link-reg": {
Path: "/example-hard-link-reg",
Mode: sampleFile.Mode,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 1,
}},
}, {
summary: "Symlink hard link",
add: []sliceAndEntry{
{entry: sampleLink, slice: oneSlice},
{entry: sampleHardLinkSym, slice: oneSlice}},
expected: map[string]manifest.ReportEntry{
"/example-link": {
Path: "/example-link",
Mode: fs.ModeSymlink | 0777,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "/base/example-file",
HardLinkId: 1,
},
"/example-hard-link-sym": {
Path: "/example-hard-link-sym",
Mode: fs.ModeSymlink | 0777,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "/base/example-file",
HardLinkId: 1,
}},
}, {
summary: "Multiple hard links",
add: []sliceAndEntry{
{entry: sampleFile, slice: oneSlice},
{entry: sampleHardLinkReg, slice: oneSlice},
{
entry: fsutil.Entry{
Path: "/base/another-example-file",
Mode: 0777,
SHA256: "another-example-file_hash",
Size: 5678,
Link: "",
},
slice: oneSlice,
},
{
entry: fsutil.Entry{
Path: "/base/another-example-hard-link-reg",
Mode: sampleFile.Mode,
Link: "/base/another-example-file",
LinkType: fsutil.TypeHardLink,
},
slice: oneSlice,
},
},
expected: map[string]manifest.ReportEntry{
"/example-file": {
Path: "/example-file",
Mode: 0777,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 1,
},
"/example-hard-link-reg": {
Path: "/example-hard-link-reg",
Mode: sampleFile.Mode,
SHA256: "example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 1,
},
"/another-example-file": {
Path: "/another-example-file",
Mode: 0777,
SHA256: "another-example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 2,
},
"/another-example-hard-link-reg": {
Path: "/another-example-hard-link-reg",
Mode: sampleFile.Mode,
SHA256: "another-example-file_hash",
Size: 5678,
Slices: map[*setup.Slice]bool{oneSlice: true},
Link: "",
HardLinkId: 2,
}},
}}

func (s *S) TestReport(c *C) {
Expand Down

0 comments on commit 0a922ec

Please sign in to comment.