Merge branch '21504-arv-mount-reference'
[arvados.git] / lib / crunchrun / docker_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         "os/exec"
9         "time"
10
11         dockercontainer "github.com/docker/docker/api/types/container"
12         . "gopkg.in/check.v1"
13 )
14
15 var _ = Suite(&dockerSuite{})
16
17 type dockerSuite struct {
18         executorSuite
19 }
20
21 func (s *dockerSuite) SetUpSuite(c *C) {
22         _, err := exec.LookPath("docker")
23         if err != nil {
24                 c.Skip("looks like docker is not installed")
25         }
26         s.newExecutor = func(c *C) {
27                 exec.Command("docker", "rm", "zzzzz-zzzzz-zzzzzzzzzzzzzzz").Run()
28                 var err error
29                 s.executor, err = newDockerExecutor("zzzzz-zzzzz-zzzzzzzzzzzzzzz", c.Logf, time.Second/2)
30                 c.Assert(err, IsNil)
31         }
32 }
33
34 var _ = Suite(&dockerStubSuite{})
35
36 // dockerStubSuite tests don't really connect to the docker service,
37 // so we can run them even if docker is not installed.
38 type dockerStubSuite struct{}
39
40 func (s *dockerStubSuite) TestDockerContainerConfig(c *C) {
41         e, err := newDockerExecutor("zzzzz-zzzzz-zzzzzzzzzzzzzzz", c.Logf, time.Second/2)
42         c.Assert(err, IsNil)
43         cfg, hostCfg := e.config(containerSpec{
44                 VCPUs:           4,
45                 RAM:             123123123,
46                 WorkingDir:      "/WorkingDir",
47                 Env:             map[string]string{"FOO": "bar"},
48                 BindMounts:      map[string]bindmount{"/mnt": {HostPath: "/hostpath", ReadOnly: true}},
49                 EnableNetwork:   false,
50                 CUDADeviceCount: 3,
51         })
52         c.Check(cfg.WorkingDir, Equals, "/WorkingDir")
53         c.Check(cfg.Env, DeepEquals, []string{"FOO=bar"})
54         c.Check(hostCfg.NetworkMode, Equals, dockercontainer.NetworkMode("none"))
55         c.Check(hostCfg.Resources.NanoCPUs, Equals, int64(4000000000))
56         c.Check(hostCfg.Resources.Memory, Equals, int64(123123123))
57         c.Check(hostCfg.Resources.MemorySwap, Equals, int64(123123123))
58         c.Check(hostCfg.Resources.KernelMemory, Equals, int64(123123123))
59         c.Check(hostCfg.Resources.DeviceRequests, DeepEquals, []dockercontainer.DeviceRequest{{
60                 Driver:       "nvidia",
61                 Count:        3,
62                 Capabilities: [][]string{{"gpu", "nvidia", "compute", "utility"}},
63         }})
64 }