Skip to content

Commit d68aa49

Browse files
committed
Fix failing tests on Darwin
Signed-off-by: Evan Lezar <[email protected]>
1 parent f1abe95 commit d68aa49

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

pkg/cdi/cache.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/fs"
2323
"os"
2424
"path/filepath"
25+
"runtime"
2526
"sort"
2627
"strings"
2728
"sync"
@@ -529,17 +530,24 @@ func (w *watch) watch(fsw *fsnotify.Watcher, m *sync.Mutex, refresh func() error
529530
if watch == nil {
530531
return
531532
}
533+
534+
eventMask := fsnotify.Rename | fsnotify.Remove | fsnotify.Write
535+
// On macOS, we also need to watch for Create events.
536+
if runtime.GOOS == "darwin" {
537+
eventMask |= fsnotify.Create
538+
}
539+
532540
for {
533541
select {
534542
case event, ok := <-watch.Events:
535543
if !ok {
536544
return
537545
}
538546

539-
if (event.Op & (fsnotify.Rename | fsnotify.Remove | fsnotify.Write)) == 0 {
547+
if (event.Op & eventMask) == 0 {
540548
continue
541549
}
542-
if event.Op == fsnotify.Write {
550+
if event.Op == fsnotify.Write || event.Op == fsnotify.Create {
543551
if ext := filepath.Ext(event.Name); ext != ".json" && ext != ".yaml" {
544552
continue
545553
}

pkg/cdi/cache_test_darwin.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build darwin
2+
// +build darwin
3+
4+
/*
5+
Copyright © 2021 The CDI Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package cdi
21+
22+
import "syscall"
23+
24+
func osSync() {
25+
_ = syscall.Sync()
26+
}

pkg/cdi/cache_test_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !windows
2-
// +build !windows
1+
//go:build !windows && !darwin
2+
// +build !windows,!darwin
33

44
/*
55
Copyright © 2021 The CDI Authors

pkg/cdi/container-edits_test.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package cdi
1818

1919
import (
20+
"runtime"
2021
"testing"
2122

2223
oci "github.com/opencontainers/runtime-spec/specs-go"
@@ -303,6 +304,13 @@ func TestValidateContainerEdits(t *testing.T) {
303304
}
304305

305306
func TestApplyContainerEdits(t *testing.T) {
307+
nullDeviceMajor := int64(1)
308+
nullDeviceMinor := int64(3)
309+
if runtime.GOOS == "darwin" {
310+
nullDeviceMajor = 3
311+
nullDeviceMinor = 2
312+
}
313+
306314
type testCase struct {
307315
name string
308316
spec *oci.Spec
@@ -346,17 +354,17 @@ func TestApplyContainerEdits(t *testing.T) {
346354
{
347355
Path: "/dev/null",
348356
Type: "c",
349-
Major: 1,
350-
Minor: 3,
357+
Major: nullDeviceMajor,
358+
Minor: nullDeviceMinor,
351359
},
352360
},
353361
Resources: &oci.LinuxResources{
354362
Devices: []oci.LinuxDeviceCgroup{
355363
{
356364
Allow: true,
357365
Type: "c",
358-
Major: int64ptr(1),
359-
Minor: int64ptr(3),
366+
Major: &nullDeviceMajor,
367+
Minor: &nullDeviceMinor,
360368
Access: "rwm",
361369
},
362370
},
@@ -389,17 +397,17 @@ func TestApplyContainerEdits(t *testing.T) {
389397
{
390398
Path: "/dev/null",
391399
Type: "c",
392-
Major: 1,
393-
Minor: 3,
400+
Major: nullDeviceMajor,
401+
Minor: nullDeviceMinor,
394402
},
395403
},
396404
Resources: &oci.LinuxResources{
397405
Devices: []oci.LinuxDeviceCgroup{
398406
{
399407
Allow: true,
400408
Type: "c",
401-
Major: int64ptr(1),
402-
Minor: int64ptr(3),
409+
Major: &nullDeviceMajor,
410+
Minor: &nullDeviceMinor,
403411
Access: "rwm",
404412
},
405413
},

0 commit comments

Comments
 (0)