@@ -6,11 +6,16 @@ import (
6
6
"errors"
7
7
"io/ioutil"
8
8
"os"
9
+ "path/filepath"
9
10
"testing"
10
11
12
+ "github.com/docker/docker/api/types/blkiodev"
11
13
containertypes "github.com/docker/docker/api/types/container"
12
14
"github.com/docker/docker/container"
13
15
"github.com/docker/docker/daemon/config"
16
+ "golang.org/x/sys/unix"
17
+ "gotest.tools/assert"
18
+ is "gotest.tools/assert/cmp"
14
19
)
15
20
16
21
type fakeContainerGetter struct {
@@ -266,3 +271,61 @@ func TestNetworkOptions(t *testing.T) {
266
271
t .Fatal ("Expected networkOptions error, got nil" )
267
272
}
268
273
}
274
+
275
+ const (
276
+ // prepare major 0x1FD(509 in decimal) and minor 0x130(304)
277
+ DEVNO = 0x11FD30
278
+ MAJOR = 509
279
+ MINOR = 304
280
+ WEIGHT = 1024
281
+ )
282
+
283
+ func deviceTypeMock (t * testing.T , testAndCheck func (string )) {
284
+ if os .Getuid () != 0 {
285
+ t .Skip ("root required" ) // for mknod
286
+ }
287
+
288
+ t .Parallel ()
289
+
290
+ tempDir , err := ioutil .TempDir ("" , "tempDevDir" + t .Name ())
291
+ assert .NilError (t , err , "create temp file" )
292
+ tempFile := filepath .Join (tempDir , "dev" )
293
+
294
+ defer os .RemoveAll (tempDir )
295
+
296
+ if err = unix .Mknod (tempFile , unix .S_IFCHR , DEVNO ); err != nil {
297
+ t .Fatalf ("mknod error %s(%x): %v" , tempFile , DEVNO , err )
298
+ }
299
+
300
+ testAndCheck (tempFile )
301
+ }
302
+
303
+ func TestGetBlkioWeightDevices (t * testing.T ) {
304
+ deviceTypeMock (t , func (tempFile string ) {
305
+ mockResource := containertypes.Resources {
306
+ BlkioWeightDevice : []* blkiodev.WeightDevice {{Path : tempFile , Weight : WEIGHT }},
307
+ }
308
+
309
+ weightDevs , err := getBlkioWeightDevices (mockResource )
310
+
311
+ assert .NilError (t , err , "getBlkioWeightDevices" )
312
+ assert .Check (t , is .Len (weightDevs , 1 ), "getBlkioWeightDevices" )
313
+ assert .Check (t , weightDevs [0 ].Major == MAJOR , "get major device type" )
314
+ assert .Check (t , weightDevs [0 ].Minor == MINOR , "get minor device type" )
315
+ assert .Check (t , * weightDevs [0 ].Weight == WEIGHT , "get device weight" )
316
+ })
317
+ }
318
+
319
+ func TestGetBlkioThrottleDevices (t * testing.T ) {
320
+ deviceTypeMock (t , func (tempFile string ) {
321
+ mockDevs := []* blkiodev.ThrottleDevice {{Path : tempFile , Rate : WEIGHT }}
322
+
323
+ retDevs , err := getBlkioThrottleDevices (mockDevs )
324
+
325
+ assert .NilError (t , err , "getBlkioThrottleDevices" )
326
+ assert .Check (t , is .Len (retDevs , 1 ), "getBlkioThrottleDevices" )
327
+ assert .Check (t , retDevs [0 ].Major == MAJOR , "get major device type" )
328
+ assert .Check (t , retDevs [0 ].Minor == MINOR , "get minor device type" )
329
+ assert .Check (t , retDevs [0 ].Rate == WEIGHT , "get device rate" )
330
+ })
331
+ }
0 commit comments