Skip to content

Commit

Permalink
Merge pull request #241 from Epsilon314/sort-mounts-stable
Browse files Browse the repository at this point in the history
sortMounts stable and remove alphabetical order
  • Loading branch information
bart0sh authored Nov 26, 2024
2 parents 43e3378 + 6b16d3c commit 4149a8a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
11 changes: 2 additions & 9 deletions pkg/cdi/container-edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func ensureOCIHooks(spec *oci.Spec) {
func sortMounts(specgen *ocigen.Generator) {
mounts := specgen.Mounts()
specgen.ClearMounts()
sort.Sort(orderedMounts(mounts))
sort.Stable(orderedMounts(mounts))
specgen.Config.Mounts = mounts
}

Expand All @@ -375,14 +375,7 @@ func (m orderedMounts) Len() int {
// mount indexed by parameter 1 is less than that of the mount indexed by
// parameter 2. Used in sorting.
func (m orderedMounts) Less(i, j int) bool {
ip, jp := m.parts(i), m.parts(j)
if ip < jp {
return true
}
if jp < ip {
return false
}
return m[i].Destination < m[j].Destination
return m.parts(i) < m.parts(j)
}

// Swap swaps two items in an array of mounts. Used in sorting
Expand Down
78 changes: 78 additions & 0 deletions pkg/cdi/container-edits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,84 @@ func TestApplyContainerEdits(t *testing.T) {
},
result: &oci.Spec{},
},
{
name: "apply mount edits do not change the order of original mounts",
spec: &oci.Spec{
Mounts: []oci.Mount{
{
Source: "/some/host/path1",
Destination: "/dest/path/b",
},
{
Source: "/some/host/path2",
Destination: "/dest/path/a",
},
},
},
edits: &cdi.ContainerEdits{
Mounts: []*cdi.Mount{
{
HostPath: "/some/host/path3",
ContainerPath: "/dest/edit",
},
},
},
result: &oci.Spec{
Mounts: []oci.Mount{
{
Source: "/some/host/path3",
Destination: "/dest/edit",
},
{
Source: "/some/host/path1",
Destination: "/dest/path/b",
},
{
Source: "/some/host/path2",
Destination: "/dest/path/a",
},
},
},
},
{
name: "mount added by edit comes after existing ones of same number of path parts",
spec: &oci.Spec{
Mounts: []oci.Mount{
{
Source: "/some/host/path1",
Destination: "/dest/path/c",
},
{
Source: "/some/host/path2",
Destination: "/dest/path/b",
},
},
},
edits: &cdi.ContainerEdits{
Mounts: []*cdi.Mount{
{
HostPath: "/some/host/path3",
ContainerPath: "/dest/path/a",
},
},
},
result: &oci.Spec{
Mounts: []oci.Mount{
{
Source: "/some/host/path1",
Destination: "/dest/path/c",
},
{
Source: "/some/host/path2",
Destination: "/dest/path/b",
},
{
Source: "/some/host/path3",
Destination: "/dest/path/a",
},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
edits := ContainerEdits{tc.edits}
Expand Down

0 comments on commit 4149a8a

Please sign in to comment.