1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.arvados.org/arvados.git/lib/config"
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/arvadostest"
21 "git.arvados.org/arvados.git/sdk/go/ctxlog"
22 "github.com/prometheus/client_golang/prometheus"
26 func Test(t *testing.T) {
30 var _ = check.Suite(&suite{})
34 crTooBig arvados.ContainerRequest
35 crPending arvados.ContainerRequest
36 crCUDARequest arvados.ContainerRequest
39 func (s *suite) TearDownTest(c *check.C) {
40 arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
43 func (s *suite) SetUpTest(c *check.C) {
44 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
45 c.Assert(err, check.IsNil)
46 cluster, err := cfg.GetCluster("")
47 c.Assert(err, check.IsNil)
48 cluster.Containers.ReserveExtraRAM = 256 << 20
49 cluster.Containers.CloudVMs.PollInterval = arvados.Duration(time.Second / 4)
50 cluster.Containers.MinRetryPeriod = arvados.Duration(time.Second / 4)
51 cluster.InstanceTypes = arvados.InstanceTypeMap{
52 "biggest_available_node": arvados.InstanceType{
53 RAM: 100 << 30, // 100 GiB
55 IncludedScratch: 100 << 30,
58 s.disp = newHandler(context.Background(), cluster, arvadostest.Dispatch1Token, prometheus.NewRegistry()).(*dispatcher)
59 s.disp.lsfcli.stubCommand = func(string, ...string) *exec.Cmd {
60 return exec.Command("bash", "-c", "echo >&2 unimplemented stub; false")
62 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crTooBig, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
63 "container_request": map[string]interface{}{
64 "runtime_constraints": arvados.RuntimeConstraints{
68 "container_image": arvadostest.DockerImage112PDH,
69 "command": []string{"sleep", "1"},
70 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
71 "output_path": "/mnt/out",
72 "state": arvados.ContainerRequestStateCommitted,
74 "container_count_max": 1,
77 c.Assert(err, check.IsNil)
79 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crPending, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
80 "container_request": map[string]interface{}{
81 "runtime_constraints": arvados.RuntimeConstraints{
84 KeepCacheDisk: 8 << 30,
86 "container_image": arvadostest.DockerImage112PDH,
87 "command": []string{"sleep", "1"},
88 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
89 "output_path": "/mnt/out",
90 "state": arvados.ContainerRequestStateCommitted,
92 "container_count_max": 1,
95 c.Assert(err, check.IsNil)
97 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crCUDARequest, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
98 "container_request": map[string]interface{}{
99 "runtime_constraints": arvados.RuntimeConstraints{
102 CUDA: arvados.CUDARuntimeConstraints{
104 DriverVersion: "11.0",
105 HardwareCapability: "8.0",
108 "container_image": arvadostest.DockerImage112PDH,
109 "command": []string{"sleep", "1"},
110 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
111 "output_path": "/mnt/out",
112 "state": arvados.ContainerRequestStateCommitted,
114 "container_count_max": 1,
117 c.Assert(err, check.IsNil)
121 type lsfstub struct {
126 func (stub lsfstub) stubCommand(s *suite, c *check.C) func(prog string, args ...string) *exec.Cmd {
129 fakejobq := map[int]string{}
130 return func(prog string, args ...string) *exec.Cmd {
131 c.Logf("stubCommand: %q %q", prog, args)
132 if rand.Float64() < stub.errorRate {
133 return exec.Command("bash", "-c", "echo >&2 'stub random failure' && false")
135 if stub.sudoUser != "" && len(args) > 3 &&
139 args[2] == stub.sudoUser {
140 prog, args = args[3], args[4:]
144 defaultArgs := s.disp.Cluster.Containers.LSF.BsubArgumentsList
145 if args[5] == s.crCUDARequest.ContainerUUID {
146 c.Assert(len(args), check.Equals, len(defaultArgs)+len(s.disp.Cluster.Containers.LSF.BsubCUDAArguments))
148 c.Assert(len(args), check.Equals, len(defaultArgs))
150 // %%J must have been rewritten to %J
151 c.Check(args[1], check.Equals, "/tmp/crunch-run.%J.out")
154 case arvadostest.LockedContainerUUID:
155 c.Check(args, check.DeepEquals, []string{
156 "-J", arvadostest.LockedContainerUUID,
159 "-R", "rusage[mem=11701MB:tmp=0MB] span[hosts=1]",
160 "-R", "select[mem>=11701MB]",
161 "-R", "select[tmp>=0MB]",
162 "-R", "select[ncpus>=4]"})
164 fakejobq[nextjobid] = args[1]
167 case arvadostest.QueuedContainerUUID:
168 c.Check(args, check.DeepEquals, []string{
169 "-J", arvadostest.QueuedContainerUUID,
172 "-R", "rusage[mem=11701MB:tmp=45777MB] span[hosts=1]",
173 "-R", "select[mem>=11701MB]",
174 "-R", "select[tmp>=45777MB]",
175 "-R", "select[ncpus>=4]"})
177 fakejobq[nextjobid] = args[1]
180 case s.crPending.ContainerUUID:
181 c.Check(args, check.DeepEquals, []string{
182 "-J", s.crPending.ContainerUUID,
185 "-R", "rusage[mem=352MB:tmp=8448MB] span[hosts=1]",
186 "-R", "select[mem>=352MB]",
187 "-R", "select[tmp>=8448MB]",
188 "-R", "select[ncpus>=2]"})
190 fakejobq[nextjobid] = args[1]
193 case s.crCUDARequest.ContainerUUID:
194 c.Check(args, check.DeepEquals, []string{
195 "-J", s.crCUDARequest.ContainerUUID,
198 "-R", "rusage[mem=528MB:tmp=256MB] span[hosts=1]",
199 "-R", "select[mem>=528MB]",
200 "-R", "select[tmp>=256MB]",
201 "-R", "select[ncpus>=1]",
204 fakejobq[nextjobid] = args[1]
208 c.Errorf("unexpected uuid passed to bsub: args %q", args)
209 return exec.Command("false")
211 return exec.Command("echo", "submitted job")
213 c.Check(args, check.DeepEquals, []string{"-u", "all", "-o", "jobid stat job_name pend_reason", "-json"})
214 var records []map[string]interface{}
215 for jobid, uuid := range fakejobq {
216 stat, reason := "RUN", ""
217 if uuid == s.crPending.ContainerUUID {
218 // The real bjobs output includes a trailing ';' here:
219 stat, reason = "PEND", "There are no suitable hosts for the job;"
221 records = append(records, map[string]interface{}{
222 "JOBID": fmt.Sprintf("%d", jobid),
225 "PEND_REASON": reason,
228 out, err := json.Marshal(map[string]interface{}{
230 "JOBS": len(fakejobq),
236 c.Logf("bjobs out: %s", out)
237 return exec.Command("printf", string(out))
239 killid, _ := strconv.Atoi(args[0])
240 if uuid, ok := fakejobq[killid]; !ok {
241 return exec.Command("bash", "-c", fmt.Sprintf("printf >&2 'Job <%d>: No matching job found\n'", killid))
242 } else if uuid == "" {
243 return exec.Command("bash", "-c", fmt.Sprintf("printf >&2 'Job <%d>: Job has already finished\n'", killid))
246 time.Sleep(time.Millisecond)
248 delete(fakejobq, killid)
251 return exec.Command("bash", "-c", fmt.Sprintf("printf 'Job <%d> is being terminated\n'", killid))
254 return exec.Command("bash", "-c", fmt.Sprintf("echo >&2 'stub: command not found: %+q'", prog))
259 func (s *suite) TestSubmit(c *check.C) {
260 s.disp.lsfcli.stubCommand = lsfstub{
262 sudoUser: s.disp.Cluster.Containers.LSF.BsubSudoUser,
266 deadline := time.Now().Add(20 * time.Second)
267 for range time.NewTicker(time.Second).C {
268 if time.Now().After(deadline) {
272 // "crTooBig" should never be submitted to lsf because
273 // it is bigger than any configured instance type
274 if ent, ok := s.disp.lsfqueue.Lookup(s.crTooBig.ContainerUUID); ok {
275 c.Errorf("Lookup(crTooBig) == true, ent = %#v", ent)
278 // "queuedcontainer" should be running
279 if _, ok := s.disp.lsfqueue.Lookup(arvadostest.QueuedContainerUUID); !ok {
280 c.Log("Lookup(queuedcontainer) == false")
283 // "crPending" should be pending
284 if ent, ok := s.disp.lsfqueue.Lookup(s.crPending.ContainerUUID); !ok {
285 c.Logf("Lookup(crPending) == false", ent)
288 // "lockedcontainer" should be cancelled because it
289 // has priority 0 (no matching container requests)
290 if ent, ok := s.disp.lsfqueue.Lookup(arvadostest.LockedContainerUUID); ok {
291 c.Logf("Lookup(lockedcontainer) == true, ent = %#v", ent)
294 var ctr arvados.Container
295 if err := s.disp.arvDispatcher.Arv.Get("containers", arvadostest.LockedContainerUUID, nil, &ctr); err != nil {
296 c.Logf("error getting container state for %s: %s", arvadostest.LockedContainerUUID, err)
298 } else if ctr.State != arvados.ContainerStateQueued {
299 c.Logf("LockedContainer is not in the LSF queue but its arvados record has not been updated to state==Queued (state is %q)", ctr.State)
303 if err := s.disp.arvDispatcher.Arv.Get("containers", s.crTooBig.ContainerUUID, nil, &ctr); err != nil {
304 c.Logf("error getting container state for %s: %s", s.crTooBig.ContainerUUID, err)
306 } else if ctr.State != arvados.ContainerStateCancelled {
307 c.Logf("container %s is not in the LSF queue but its arvados record has not been updated to state==Cancelled (state is %q)", s.crTooBig.ContainerUUID, ctr.State)
310 c.Check(ctr.RuntimeStatus["error"], check.Equals, "constraints not satisfiable by any configured instance type")
312 c.Log("reached desired state")