Edit discover.mounts to have a deterministic output

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Carlos Eduardo Arango Gutierrez 2025-05-22 11:17:21 +02:00 committed by Evan Lezar
parent f93d96a0de
commit aaaa3c6275
No known key found for this signature in database
2 changed files with 64 additions and 27 deletions

View File

@ -57,8 +57,9 @@ func (d *mounts) Mounts() ([]Mount, error) {
if d.lookup == nil { if d.lookup == nil {
return nil, fmt.Errorf("no lookup defined") return nil, fmt.Errorf("no lookup defined")
} }
uniqueMounts := make(map[string]Mount)
var mounts []Mount
seen := make(map[string]bool)
for _, candidate := range d.required { for _, candidate := range d.required {
d.logger.Debugf("Locating %v", candidate) d.logger.Debugf("Locating %v", candidate)
located, err := d.lookup.Locate(candidate) located, err := d.lookup.Locate(candidate)
@ -72,7 +73,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
} }
d.logger.Debugf("Located %v as %v", candidate, located) d.logger.Debugf("Located %v as %v", candidate, located)
for _, p := range located { for _, p := range located {
if _, ok := uniqueMounts[p]; ok { if seen[p] {
d.logger.Debugf("Skipping duplicate mount %v", p) d.logger.Debugf("Skipping duplicate mount %v", p)
continue continue
} }
@ -83,7 +84,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
} }
d.logger.Infof("Selecting %v as %v", p, r) d.logger.Infof("Selecting %v as %v", p, r)
uniqueMounts[p] = Mount{ mount := Mount{
HostPath: p, HostPath: p,
Path: r, Path: r,
Options: []string{ Options: []string{
@ -94,13 +95,11 @@ func (d *mounts) Mounts() ([]Mount, error) {
"rprivate", "rprivate",
}, },
} }
mounts = append(mounts, mount)
seen[p] = true
} }
} }
var mounts []Mount
for _, m := range uniqueMounts {
mounts = append(mounts, m)
}
return mounts, nil return mounts, nil
} }

View File

@ -45,13 +45,14 @@ func TestMounts(t *testing.T) {
"rprivate", "rprivate",
} }
logger, logHook := testlog.NewNullLogger() logger, _ := testlog.NewNullLogger()
testCases := []struct { testCases := []struct {
description string description string
expectedError error expectedError error
expectedMounts []Mount expectedMounts []Mount
input *mounts input *mounts
repeat int
}{ }{
{ {
description: "nill lookup returns error", description: "nill lookup returns error",
@ -160,31 +161,68 @@ func TestMounts(t *testing.T) {
{Path: "/located", HostPath: "/some/root/located", Options: mountOptions}, {Path: "/located", HostPath: "/some/root/located", Options: mountOptions},
}, },
}, },
{
description: "multiple mounts ordering",
input: &mounts{
lookup: &lookup.LocatorMock{
LocateFunc: func(s string) ([]string, error) {
return []string{
"first",
"second",
"third",
"fourth",
"second",
"second",
"second",
"fifth",
"sixth"}, nil
},
},
required: []string{""},
},
expectedMounts: []Mount{
{Path: "first", HostPath: "first", Options: mountOptions},
{Path: "second", HostPath: "second", Options: mountOptions},
{Path: "third", HostPath: "third", Options: mountOptions},
{Path: "fourth", HostPath: "fourth", Options: mountOptions},
{Path: "fifth", HostPath: "fifth", Options: mountOptions},
{Path: "sixth", HostPath: "sixth", Options: mountOptions},
},
repeat: 10,
},
} }
for _, tc := range testCases { for _, tc := range testCases {
logHook.Reset() for i := 1; ; i++ {
t.Run(tc.description, func(t *testing.T) { test_name := tc.description
tc.input.logger = logger if tc.repeat > 1 {
mounts, err := tc.input.Mounts() test_name += fmt.Sprintf("/%d", i)
if tc.expectedError != nil {
require.Error(t, err)
} else {
require.NoError(t, err)
} }
require.ElementsMatch(t, tc.expectedMounts, mounts) success := t.Run(test_name, func(t *testing.T) {
tc.input.logger = logger
mounts, err := tc.input.Mounts()
// We check that the mock is called for each element of required if tc.expectedError != nil {
if tc.input.lookup != nil { require.Error(t, err)
mock := tc.input.lookup.(*lookup.LocatorMock) } else {
require.Len(t, mock.LocateCalls(), len(tc.input.required)) require.NoError(t, err)
var args []string
for _, c := range mock.LocateCalls() {
args = append(args, c.S)
} }
require.EqualValues(t, args, tc.input.required) require.EqualValues(t, tc.expectedMounts, mounts)
// We check that the mock is called for each element of required
if i == 1 && tc.input.lookup != nil {
mock := tc.input.lookup.(*lookup.LocatorMock)
require.Len(t, mock.LocateCalls(), len(tc.input.required))
var args []string
for _, c := range mock.LocateCalls() {
args = append(args, c.S)
}
require.EqualValues(t, args, tc.input.required)
}
})
if !success || i >= tc.repeat {
break
} }
}) }
} }
} }