Skip to content

Commit 1aed5f4

Browse files
Merge pull request #1104 from ArangoGutierrez/bp/1102
[release-1.17] Edit discover.mounts to have a deterministic output
2 parents 7732638 + dd40dad commit 1aed5f4

2 files changed

Lines changed: 64 additions & 29 deletions

File tree

internal/discover/mounts.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func (d *mounts) Mounts() ([]Mount, error) {
6969
d.Lock()
7070
defer d.Unlock()
7171

72-
uniqueMounts := make(map[string]Mount)
73-
72+
var mounts []Mount
73+
seen := make(map[string]bool)
7474
for _, candidate := range d.required {
7575
d.logger.Debugf("Locating %v", candidate)
7676
located, err := d.lookup.Locate(candidate)
@@ -84,7 +84,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
8484
}
8585
d.logger.Debugf("Located %v as %v", candidate, located)
8686
for _, p := range located {
87-
if _, ok := uniqueMounts[p]; ok {
87+
if seen[p] {
8888
d.logger.Debugf("Skipping duplicate mount %v", p)
8989
continue
9090
}
@@ -95,7 +95,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
9595
}
9696

9797
d.logger.Infof("Selecting %v as %v", p, r)
98-
uniqueMounts[p] = Mount{
98+
mount := Mount{
9999
HostPath: p,
100100
Path: r,
101101
Options: []string{
@@ -105,14 +105,11 @@ func (d *mounts) Mounts() ([]Mount, error) {
105105
"bind",
106106
},
107107
}
108+
mounts = append(mounts, mount)
109+
seen[p] = true
108110
}
109111
}
110112

111-
var mounts []Mount
112-
for _, m := range uniqueMounts {
113-
mounts = append(mounts, m)
114-
}
115-
116113
d.cache = mounts
117114

118115
return d.cache, nil

internal/discover/mounts_test.go

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ func TestMounts(t *testing.T) {
4444
"bind",
4545
}
4646

47-
logger, logHook := testlog.NewNullLogger()
47+
logger, _ := testlog.NewNullLogger()
4848

4949
testCases := []struct {
5050
description string
5151
expectedError error
5252
expectedMounts []Mount
5353
input *mounts
54+
repeat int
5455
}{
5556
{
5657
description: "nill lookup returns error",
@@ -159,31 +160,68 @@ func TestMounts(t *testing.T) {
159160
{Path: "/located", HostPath: "/some/root/located", Options: mountOptions},
160161
},
161162
},
163+
{
164+
description: "multiple mounts ordering",
165+
input: &mounts{
166+
lookup: &lookup.LocatorMock{
167+
LocateFunc: func(s string) ([]string, error) {
168+
return []string{
169+
"first",
170+
"second",
171+
"third",
172+
"fourth",
173+
"second",
174+
"second",
175+
"second",
176+
"fifth",
177+
"sixth"}, nil
178+
},
179+
},
180+
required: []string{""},
181+
},
182+
expectedMounts: []Mount{
183+
{Path: "first", HostPath: "first", Options: mountOptions},
184+
{Path: "second", HostPath: "second", Options: mountOptions},
185+
{Path: "third", HostPath: "third", Options: mountOptions},
186+
{Path: "fourth", HostPath: "fourth", Options: mountOptions},
187+
{Path: "fifth", HostPath: "fifth", Options: mountOptions},
188+
{Path: "sixth", HostPath: "sixth", Options: mountOptions},
189+
},
190+
repeat: 10,
191+
},
162192
}
163193

164194
for _, tc := range testCases {
165-
logHook.Reset()
166-
t.Run(tc.description, func(t *testing.T) {
167-
tc.input.logger = logger
168-
mounts, err := tc.input.Mounts()
169-
170-
if tc.expectedError != nil {
171-
require.Error(t, err)
172-
} else {
173-
require.NoError(t, err)
195+
for i := 1; ; i++ {
196+
test_name := tc.description
197+
if tc.repeat > 1 {
198+
test_name += fmt.Sprintf("/%d", i)
174199
}
175-
require.ElementsMatch(t, tc.expectedMounts, mounts)
200+
success := t.Run(test_name, func(t *testing.T) {
201+
tc.input.logger = logger
202+
mounts, err := tc.input.Mounts()
203+
204+
if tc.expectedError != nil {
205+
require.Error(t, err)
206+
} else {
207+
require.NoError(t, err)
208+
}
209+
require.EqualValues(t, tc.expectedMounts, mounts)
176210

177-
// We check that the mock is called for each element of required
178-
if tc.input.lookup != nil {
179-
mock := tc.input.lookup.(*lookup.LocatorMock)
180-
require.Len(t, mock.LocateCalls(), len(tc.input.required))
181-
var args []string
182-
for _, c := range mock.LocateCalls() {
183-
args = append(args, c.S)
211+
// We check that the mock is called for each element of required
212+
if i == 1 && tc.input.lookup != nil {
213+
mock := tc.input.lookup.(*lookup.LocatorMock)
214+
require.Len(t, mock.LocateCalls(), len(tc.input.required))
215+
var args []string
216+
for _, c := range mock.LocateCalls() {
217+
args = append(args, c.S)
218+
}
219+
require.EqualValues(t, args, tc.input.required)
184220
}
185-
require.EqualValues(t, args, tc.input.required)
221+
})
222+
if !success || i >= tc.repeat {
223+
break
186224
}
187-
})
225+
}
188226
}
189227
}

0 commit comments

Comments
 (0)