Skip to content

Commit 4149a8a

Browse files
authored
Merge pull request #241 from Epsilon314/sort-mounts-stable
sortMounts stable and remove alphabetical order
2 parents 43e3378 + 6b16d3c commit 4149a8a

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

pkg/cdi/container-edits.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func ensureOCIHooks(spec *oci.Spec) {
355355
func sortMounts(specgen *ocigen.Generator) {
356356
mounts := specgen.Mounts()
357357
specgen.ClearMounts()
358-
sort.Sort(orderedMounts(mounts))
358+
sort.Stable(orderedMounts(mounts))
359359
specgen.Config.Mounts = mounts
360360
}
361361

@@ -375,14 +375,7 @@ func (m orderedMounts) Len() int {
375375
// mount indexed by parameter 1 is less than that of the mount indexed by
376376
// parameter 2. Used in sorting.
377377
func (m orderedMounts) Less(i, j int) bool {
378-
ip, jp := m.parts(i), m.parts(j)
379-
if ip < jp {
380-
return true
381-
}
382-
if jp < ip {
383-
return false
384-
}
385-
return m[i].Destination < m[j].Destination
378+
return m.parts(i) < m.parts(j)
386379
}
387380

388381
// Swap swaps two items in an array of mounts. Used in sorting

pkg/cdi/container-edits_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,84 @@ func TestApplyContainerEdits(t *testing.T) {
599599
},
600600
result: &oci.Spec{},
601601
},
602+
{
603+
name: "apply mount edits do not change the order of original mounts",
604+
spec: &oci.Spec{
605+
Mounts: []oci.Mount{
606+
{
607+
Source: "/some/host/path1",
608+
Destination: "/dest/path/b",
609+
},
610+
{
611+
Source: "/some/host/path2",
612+
Destination: "/dest/path/a",
613+
},
614+
},
615+
},
616+
edits: &cdi.ContainerEdits{
617+
Mounts: []*cdi.Mount{
618+
{
619+
HostPath: "/some/host/path3",
620+
ContainerPath: "/dest/edit",
621+
},
622+
},
623+
},
624+
result: &oci.Spec{
625+
Mounts: []oci.Mount{
626+
{
627+
Source: "/some/host/path3",
628+
Destination: "/dest/edit",
629+
},
630+
{
631+
Source: "/some/host/path1",
632+
Destination: "/dest/path/b",
633+
},
634+
{
635+
Source: "/some/host/path2",
636+
Destination: "/dest/path/a",
637+
},
638+
},
639+
},
640+
},
641+
{
642+
name: "mount added by edit comes after existing ones of same number of path parts",
643+
spec: &oci.Spec{
644+
Mounts: []oci.Mount{
645+
{
646+
Source: "/some/host/path1",
647+
Destination: "/dest/path/c",
648+
},
649+
{
650+
Source: "/some/host/path2",
651+
Destination: "/dest/path/b",
652+
},
653+
},
654+
},
655+
edits: &cdi.ContainerEdits{
656+
Mounts: []*cdi.Mount{
657+
{
658+
HostPath: "/some/host/path3",
659+
ContainerPath: "/dest/path/a",
660+
},
661+
},
662+
},
663+
result: &oci.Spec{
664+
Mounts: []oci.Mount{
665+
{
666+
Source: "/some/host/path1",
667+
Destination: "/dest/path/c",
668+
},
669+
{
670+
Source: "/some/host/path2",
671+
Destination: "/dest/path/b",
672+
},
673+
{
674+
Source: "/some/host/path3",
675+
Destination: "/dest/path/a",
676+
},
677+
},
678+
},
679+
},
602680
} {
603681
t.Run(tc.name, func(t *testing.T) {
604682
edits := ContainerEdits{tc.edits}

0 commit comments

Comments
 (0)