Skip to content

Commit be6a36c

Browse files
Merge pull request #1102 from ArangoGutierrez/i/1094
Edit discover.mounts to have a deterministic output
2 parents f93d96a + aaaa3c6 commit be6a36c

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

internal/discover/mounts.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func (d *mounts) Mounts() ([]Mount, error) {
5757
if d.lookup == nil {
5858
return nil, fmt.Errorf("no lookup defined")
5959
}
60-
uniqueMounts := make(map[string]Mount)
6160

61+
var mounts []Mount
62+
seen := make(map[string]bool)
6263
for _, candidate := range d.required {
6364
d.logger.Debugf("Locating %v", candidate)
6465
located, err := d.lookup.Locate(candidate)
@@ -72,7 +73,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
7273
}
7374
d.logger.Debugf("Located %v as %v", candidate, located)
7475
for _, p := range located {
75-
if _, ok := uniqueMounts[p]; ok {
76+
if seen[p] {
7677
d.logger.Debugf("Skipping duplicate mount %v", p)
7778
continue
7879
}
@@ -83,7 +84,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
8384
}
8485

8586
d.logger.Infof("Selecting %v as %v", p, r)
86-
uniqueMounts[p] = Mount{
87+
mount := Mount{
8788
HostPath: p,
8889
Path: r,
8990
Options: []string{
@@ -94,13 +95,11 @@ func (d *mounts) Mounts() ([]Mount, error) {
9495
"rprivate",
9596
},
9697
}
98+
mounts = append(mounts, mount)
99+
seen[p] = true
97100
}
98101
}
99102

100-
var mounts []Mount
101-
for _, m := range uniqueMounts {
102-
mounts = append(mounts, m)
103-
}
104103
return mounts, nil
105104
}
106105

internal/discover/mounts_test.go

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

48-
logger, logHook := testlog.NewNullLogger()
48+
logger, _ := testlog.NewNullLogger()
4949

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

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

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

0 commit comments

Comments
 (0)