@@ -23,13 +23,90 @@ import (
2323
2424 "github.com/opencontainers/cgroups/devices/config"
2525 "github.com/stretchr/testify/require"
26+ "tags.cncf.io/container-device-interface/pkg/cdi"
2627 "tags.cncf.io/container-device-interface/specs-go"
2728
2829 "github.com/NVIDIA/nvidia-container-toolkit/internal/devices"
2930 "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
3031 "github.com/NVIDIA/nvidia-container-toolkit/internal/test/to"
3132)
3233
34+ func TestDeviceToEdits (t * testing.T ) {
35+ testCases := []struct {
36+ description string
37+ device discover.Device
38+ deviceslib devices.Interface
39+ expected * cdi.ContainerEdits
40+ }{
41+ {
42+ device : discover.Device {
43+ Path : "/foo" ,
44+ },
45+ expected : & cdi.ContainerEdits {
46+ ContainerEdits : & specs.ContainerEdits {
47+ DeviceNodes : []* specs.DeviceNode {
48+ {Path : "/foo" },
49+ },
50+ },
51+ },
52+ },
53+ {
54+ description : "device with additional GIDs" ,
55+ device : discover.Device {
56+ Path : "/foo" ,
57+ },
58+ deviceslib : & devices.InterfaceMock {
59+ DeviceFromPathFunc : func (path , permissions string ) (* devices.Device , error ) {
60+ if path != "/foo" {
61+ return nil , fmt .Errorf ("not found %v" , path )
62+ }
63+ cd := & config.Device {
64+ Rule : config.Rule {
65+ Major : 100 ,
66+ Minor : 200 ,
67+ Permissions : config .Permissions ("w" ),
68+ },
69+ // The bits which indicate this is a character device in the filemode
70+ // have been masked. This mimics the behavior of the real DeviceFromPath
71+ // function.
72+ FileMode : 0660 & os .ModePerm ,
73+ Uid : 11 ,
74+ Gid : 44 ,
75+ }
76+
77+ return (* devices .Device )(cd ), nil
78+ },
79+ },
80+ expected : & cdi.ContainerEdits {
81+ ContainerEdits : & specs.ContainerEdits {
82+ DeviceNodes : []* specs.DeviceNode {
83+ {
84+ Path : "/foo" ,
85+ HostPath : "" ,
86+ Permissions : "w" ,
87+ Major : 100 ,
88+ Minor : 200 ,
89+ FileMode : to .Ptr (0660 & os .ModePerm ),
90+ GID : ptrIfNonZero [uint32 ](44 ),
91+ },
92+ },
93+ AdditionalGIDs : []uint32 {44 },
94+ },
95+ },
96+ },
97+ }
98+
99+ for _ , tc := range testCases {
100+ f := factory {}
101+ t .Run (tc .description , func (t * testing.T ) {
102+ defer devices .SetInterfaceForTests (tc .deviceslib )()
103+ edits , err := f .device (tc .device ).toEdits ()
104+ require .NoError (t , err )
105+ require .EqualValues (t , tc .expected , edits )
106+ })
107+ }
108+ }
109+
33110func TestDeviceToSpec (t * testing.T ) {
34111 testCases := []struct {
35112 description string
0 commit comments