Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / lib / dispatchcloud / node_size_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package dispatchcloud
6
7 import (
8         "git.arvados.org/arvados.git/sdk/go/arvados"
9         check "gopkg.in/check.v1"
10 )
11
12 var _ = check.Suite(&NodeSizeSuite{})
13
14 const GiB = arvados.ByteSize(1 << 30)
15
16 type NodeSizeSuite struct{}
17
18 func (*NodeSizeSuite) TestChooseNotConfigured(c *check.C) {
19         _, err := ChooseInstanceType(&arvados.Cluster{}, &arvados.Container{
20                 RuntimeConstraints: arvados.RuntimeConstraints{
21                         RAM:   1234567890,
22                         VCPUs: 2,
23                 },
24         })
25         c.Check(err, check.Equals, ErrInstanceTypesNotConfigured)
26 }
27
28 func (*NodeSizeSuite) TestChooseUnsatisfiable(c *check.C) {
29         checkUnsatisfiable := func(ctr *arvados.Container) {
30                 _, err := ChooseInstanceType(&arvados.Cluster{InstanceTypes: map[string]arvados.InstanceType{
31                         "small1": {Price: 1.1, RAM: 1000000000, VCPUs: 2, Name: "small1"},
32                         "small2": {Price: 2.2, RAM: 2000000000, VCPUs: 4, Name: "small2"},
33                         "small4": {Price: 4.4, RAM: 4000000000, VCPUs: 8, Name: "small4", Scratch: GiB},
34                 }}, ctr)
35                 c.Check(err, check.FitsTypeOf, ConstraintsNotSatisfiableError{})
36         }
37
38         for _, rc := range []arvados.RuntimeConstraints{
39                 {RAM: 9876543210, VCPUs: 2},
40                 {RAM: 1234567890, VCPUs: 20},
41                 {RAM: 1234567890, VCPUs: 2, KeepCacheRAM: 9876543210},
42         } {
43                 checkUnsatisfiable(&arvados.Container{RuntimeConstraints: rc})
44         }
45         checkUnsatisfiable(&arvados.Container{
46                 Mounts:             map[string]arvados.Mount{"/tmp": {Kind: "tmp", Capacity: int64(2 * GiB)}},
47                 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 12345, VCPUs: 1},
48         })
49 }
50
51 func (*NodeSizeSuite) TestChoose(c *check.C) {
52         for _, menu := range []map[string]arvados.InstanceType{
53                 {
54                         "costly": {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly"},
55                         "best":   {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best"},
56                         "small":  {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: 2 * GiB, Name: "small"},
57                 },
58                 {
59                         "costly":     {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly"},
60                         "goodenough": {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "goodenough"},
61                         "best":       {Price: 2.2, RAM: 4000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best"},
62                         "small":      {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: 2 * GiB, Name: "small"},
63                 },
64                 {
65                         "small":      {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: 2 * GiB, Name: "small"},
66                         "goodenough": {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "goodenough"},
67                         "best":       {Price: 2.2, RAM: 4000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best"},
68                         "costly":     {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly"},
69                 },
70                 {
71                         "small":  {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: GiB, Name: "small"},
72                         "nearly": {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: GiB, Name: "nearly"},
73                         "best":   {Price: 3.3, RAM: 4000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best"},
74                         "costly": {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly"},
75                 },
76                 {
77                         "small":  {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: GiB, Name: "small"},
78                         "nearly": {Price: 2.2, RAM: 1200000000, VCPUs: 4, Scratch: 2 * GiB, Name: "nearly"},
79                         "best":   {Price: 3.3, RAM: 4000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best"},
80                         "costly": {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly"},
81                 },
82         } {
83                 best, err := ChooseInstanceType(&arvados.Cluster{InstanceTypes: menu, Containers: arvados.ContainersConfig{ReserveExtraRAM: 268435456}}, &arvados.Container{
84                         Mounts: map[string]arvados.Mount{
85                                 "/tmp": {Kind: "tmp", Capacity: 2 * int64(GiB)},
86                         },
87                         RuntimeConstraints: arvados.RuntimeConstraints{
88                                 VCPUs:        2,
89                                 RAM:          987654321,
90                                 KeepCacheRAM: 123456789,
91                         },
92                 })
93                 c.Check(err, check.IsNil)
94                 c.Check(best.Name, check.Equals, "best")
95                 c.Check(best.RAM >= 1234567890, check.Equals, true)
96                 c.Check(best.VCPUs >= 2, check.Equals, true)
97                 c.Check(best.Scratch >= 2*GiB, check.Equals, true)
98         }
99 }
100
101 func (*NodeSizeSuite) TestChoosePreemptable(c *check.C) {
102         menu := map[string]arvados.InstanceType{
103                 "costly":      {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Preemptible: true, Name: "costly"},
104                 "almost best": {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "almost best"},
105                 "best":        {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Preemptible: true, Name: "best"},
106                 "small":       {Price: 1.1, RAM: 1000000000, VCPUs: 2, Scratch: 2 * GiB, Preemptible: true, Name: "small"},
107         }
108         best, err := ChooseInstanceType(&arvados.Cluster{InstanceTypes: menu}, &arvados.Container{
109                 Mounts: map[string]arvados.Mount{
110                         "/tmp": {Kind: "tmp", Capacity: 2 * int64(GiB)},
111                 },
112                 RuntimeConstraints: arvados.RuntimeConstraints{
113                         VCPUs:        2,
114                         RAM:          987654321,
115                         KeepCacheRAM: 123456789,
116                 },
117                 SchedulingParameters: arvados.SchedulingParameters{
118                         Preemptible: true,
119                 },
120         })
121         c.Check(err, check.IsNil)
122         c.Check(best.Name, check.Equals, "best")
123         c.Check(best.RAM >= 1234567890, check.Equals, true)
124         c.Check(best.VCPUs >= 2, check.Equals, true)
125         c.Check(best.Scratch >= 2*GiB, check.Equals, true)
126         c.Check(best.Preemptible, check.Equals, true)
127 }
128
129 func (*NodeSizeSuite) TestScratchForDockerImage(c *check.C) {
130         n := EstimateScratchSpace(&arvados.Container{
131                 ContainerImage: "d5025c0f29f6eef304a7358afa82a822+342",
132         })
133         // Actual image is 371.1 MiB (according to workbench)
134         // Estimated size is 384 MiB (402653184 bytes)
135         // Want to reserve 2x the estimated size, so 805306368 bytes
136         c.Check(n, check.Equals, int64(805306368))
137
138         n = EstimateScratchSpace(&arvados.Container{
139                 ContainerImage: "d5025c0f29f6eef304a7358afa82a822+-342",
140         })
141         // Parse error will return 0
142         c.Check(n, check.Equals, int64(0))
143
144         n = EstimateScratchSpace(&arvados.Container{
145                 ContainerImage: "d5025c0f29f6eef304a7358afa82a822+34",
146         })
147         // Short manifest will return 0
148         c.Check(n, check.Equals, int64(0))
149 }
150
151 func (*NodeSizeSuite) TestChooseGPU(c *check.C) {
152         menu := map[string]arvados.InstanceType{
153                 "costly":         {Price: 4.4, RAM: 4000000000, VCPUs: 8, Scratch: 2 * GiB, Name: "costly", CUDA: arvados.CUDAFeatures{DeviceCount: 2, HardwareCapability: "9.0", DriverVersion: "11.0"}},
154                 "low_capability": {Price: 2.1, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "low_capability", CUDA: arvados.CUDAFeatures{DeviceCount: 1, HardwareCapability: "8.0", DriverVersion: "11.0"}},
155                 "best":           {Price: 2.2, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "best", CUDA: arvados.CUDAFeatures{DeviceCount: 1, HardwareCapability: "9.0", DriverVersion: "11.0"}},
156                 "low_driver":     {Price: 2.1, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "low_driver", CUDA: arvados.CUDAFeatures{DeviceCount: 1, HardwareCapability: "9.0", DriverVersion: "10.0"}},
157                 "cheap_gpu":      {Price: 2.0, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "cheap_gpu", CUDA: arvados.CUDAFeatures{DeviceCount: 1, HardwareCapability: "8.0", DriverVersion: "10.0"}},
158                 "invalid_gpu":    {Price: 1.9, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "invalid_gpu", CUDA: arvados.CUDAFeatures{DeviceCount: 1, HardwareCapability: "12.0.12", DriverVersion: "12.0.12"}},
159                 "non_gpu":        {Price: 1.1, RAM: 2000000000, VCPUs: 4, Scratch: 2 * GiB, Name: "non_gpu"},
160         }
161
162         type GPUTestCase struct {
163                 CUDA             arvados.CUDARuntimeConstraints
164                 SelectedInstance string
165         }
166         cases := []GPUTestCase{
167                 GPUTestCase{
168                         CUDA: arvados.CUDARuntimeConstraints{
169                                 DeviceCount:        1,
170                                 HardwareCapability: "9.0",
171                                 DriverVersion:      "11.0",
172                         },
173                         SelectedInstance: "best",
174                 },
175                 GPUTestCase{
176                         CUDA: arvados.CUDARuntimeConstraints{
177                                 DeviceCount:        2,
178                                 HardwareCapability: "9.0",
179                                 DriverVersion:      "11.0",
180                         },
181                         SelectedInstance: "costly",
182                 },
183                 GPUTestCase{
184                         CUDA: arvados.CUDARuntimeConstraints{
185                                 DeviceCount:        1,
186                                 HardwareCapability: "8.0",
187                                 DriverVersion:      "11.0",
188                         },
189                         SelectedInstance: "low_capability",
190                 },
191                 GPUTestCase{
192                         CUDA: arvados.CUDARuntimeConstraints{
193                                 DeviceCount:        1,
194                                 HardwareCapability: "9.0",
195                                 DriverVersion:      "10.0",
196                         },
197                         SelectedInstance: "low_driver",
198                 },
199                 GPUTestCase{
200                         CUDA: arvados.CUDARuntimeConstraints{
201                                 DeviceCount:        1,
202                                 HardwareCapability: "",
203                                 DriverVersion:      "10.0",
204                         },
205                         SelectedInstance: "",
206                 },
207                 GPUTestCase{
208                         CUDA: arvados.CUDARuntimeConstraints{
209                                 DeviceCount:        0,
210                                 HardwareCapability: "9.0",
211                                 DriverVersion:      "11.0",
212                         },
213                         SelectedInstance: "non_gpu",
214                 },
215         }
216
217         for _, tc := range cases {
218                 best, err := ChooseInstanceType(&arvados.Cluster{InstanceTypes: menu}, &arvados.Container{
219                         Mounts: map[string]arvados.Mount{
220                                 "/tmp": {Kind: "tmp", Capacity: 2 * int64(GiB)},
221                         },
222                         RuntimeConstraints: arvados.RuntimeConstraints{
223                                 VCPUs:        2,
224                                 RAM:          987654321,
225                                 KeepCacheRAM: 123456789,
226                                 CUDA:         tc.CUDA,
227                         },
228                 })
229                 if best.Name != "" {
230                         c.Check(err, check.IsNil)
231                         c.Check(best.Name, check.Equals, tc.SelectedInstance)
232                 } else {
233                         c.Check(err, check.Not(check.IsNil))
234                 }
235         }
236 }