@@ -18,9 +18,14 @@ package oci
1818
1919import (
2020 "context"
21+ "io/ioutil"
22+ "os"
23+ "path/filepath"
2124 "testing"
2225
26+ "github.com/containerd/containerd/pkg/testutil"
2327 specs "github.com/opencontainers/runtime-spec/specs-go"
28+ "golang.org/x/sys/unix"
2429)
2530
2631func TestAddCaps (t * testing.T ) {
@@ -106,3 +111,113 @@ func TestDropCaps(t *testing.T) {
106111 }
107112 }
108113}
114+
115+ func TestGetDevices (t * testing.T ) {
116+ testutil .RequiresRoot (t )
117+
118+ dir , err := ioutil .TempDir ("/dev" , t .Name ())
119+ if err != nil {
120+ t .Fatal (err )
121+ }
122+ defer os .RemoveAll (dir )
123+
124+ zero := filepath .Join (dir , "zero" )
125+ if err := ioutil .WriteFile (zero , nil , 0600 ); err != nil {
126+ t .Fatal (err )
127+ }
128+
129+ if err := unix .Mount ("/dev/zero" , zero , "" , unix .MS_BIND , "" ); err != nil {
130+ t .Fatal (err )
131+ }
132+ defer unix .Unmount (filepath .Join (dir , "zero" ), unix .MNT_DETACH )
133+
134+ t .Run ("single device" , func (t * testing.T ) {
135+ t .Run ("no container path" , func (t * testing.T ) {
136+ devices , err := getDevices (dir , "" )
137+ if err != nil {
138+ t .Fatal (err )
139+ }
140+
141+ if len (devices ) != 1 {
142+ t .Fatalf ("expected one device %v" , devices )
143+ }
144+ if devices [0 ].Path != zero {
145+ t .Fatalf ("got unexpected device path %s" , devices [0 ].Path )
146+ }
147+ })
148+ t .Run ("with container path" , func (t * testing.T ) {
149+ newPath := "/dev/testNew"
150+ devices , err := getDevices (dir , newPath )
151+ if err != nil {
152+ t .Fatal (err )
153+ }
154+
155+ if len (devices ) != 1 {
156+ t .Fatalf ("expected one device %v" , devices )
157+ }
158+ if devices [0 ].Path != filepath .Join (newPath , "zero" ) {
159+ t .Fatalf ("got unexpected device path %s" , devices [0 ].Path )
160+ }
161+ })
162+ })
163+ t .Run ("With symlink in dir" , func (t * testing.T ) {
164+ if err := os .Symlink ("/dev/zero" , filepath .Join (dir , "zerosym" )); err != nil {
165+ t .Fatal (err )
166+ }
167+ devices , err := getDevices (dir , "" )
168+ if err != nil {
169+ t .Fatal (err )
170+ }
171+ if len (devices ) != 1 {
172+ t .Fatalf ("expected one device %v" , devices )
173+ }
174+ if devices [0 ].Path != filepath .Join (dir , "zero" ) {
175+ t .Fatalf ("got unexpected device path, expected %q, got %q" , filepath .Join (dir , "zero" ), devices [0 ].Path )
176+ }
177+ })
178+ t .Run ("No devices" , func (t * testing.T ) {
179+ dir := dir + "2"
180+ if err := os .MkdirAll (dir , 0755 ); err != nil {
181+ t .Fatal (err )
182+ }
183+ defer os .RemoveAll (dir )
184+
185+ t .Run ("empty dir" , func (T * testing.T ) {
186+ devices , err := getDevices (dir , "" )
187+ if err != nil {
188+ t .Fatal (err )
189+ }
190+ if len (devices ) != 0 {
191+ t .Fatalf ("expected no devices, got %+v" , devices )
192+ }
193+ })
194+ t .Run ("symlink to device in dir" , func (t * testing.T ) {
195+ if err := os .Symlink ("/dev/zero" , filepath .Join (dir , "zerosym" )); err != nil {
196+ t .Fatal (err )
197+ }
198+ defer os .Remove (filepath .Join (dir , "zerosym" ))
199+
200+ devices , err := getDevices (dir , "" )
201+ if err != nil {
202+ t .Fatal (err )
203+ }
204+ if len (devices ) != 0 {
205+ t .Fatalf ("expected no devices, got %+v" , devices )
206+ }
207+ })
208+ t .Run ("regular file in dir" , func (t * testing.T ) {
209+ if err := ioutil .WriteFile (filepath .Join (dir , "somefile" ), []byte ("hello" ), 0600 ); err != nil {
210+ t .Fatal (err )
211+ }
212+ defer os .Remove (filepath .Join (dir , "somefile" ))
213+
214+ devices , err := getDevices (dir , "" )
215+ if err != nil {
216+ t .Fatal (err )
217+ }
218+ if len (devices ) != 0 {
219+ t .Fatalf ("expected no devices, got %+v" , devices )
220+ }
221+ })
222+ })
223+ }
0 commit comments