45b6de453b160aacc441a0f978e69e549c0f478a
[arvados.git] / lib / controller / localdb / container_request_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package localdb
6
7 import (
8         "context"
9
10         "git.arvados.org/arvados.git/sdk/go/arvados"
11         "git.arvados.org/arvados.git/sdk/go/arvadostest"
12         "git.arvados.org/arvados.git/sdk/go/auth"
13         check "gopkg.in/check.v1"
14 )
15
16 var _ = check.Suite(&ContainerRequestSuite{})
17
18 type ContainerRequestSuite struct {
19         localdbSuite
20 }
21
22 func (s *ContainerRequestSuite) TestCRCreateWithProperties(c *check.C) {
23         s.setUpVocabulary(c, "")
24         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
25
26         tests := []struct {
27                 name    string
28                 props   map[string]interface{}
29                 success bool
30         }{
31                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
32                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
33                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
34                 {"Empty properties", map[string]interface{}{}, true},
35         }
36         for _, tt := range tests {
37                 c.Log(c.TestName()+" ", tt.name)
38
39                 cnt, err := s.localdb.ContainerRequestCreate(ctx, arvados.CreateOptions{
40                         Select: []string{"uuid", "properties"},
41                         Attrs: map[string]interface{}{
42                                 "command":         []string{"echo", "foo"},
43                                 "container_image": "arvados/apitestfixture:latest",
44                                 "cwd":             "/tmp",
45                                 "environment":     map[string]string{},
46                                 "mounts": map[string]interface{}{
47                                         "/out": map[string]interface{}{
48                                                 "kind":     "tmp",
49                                                 "capacity": 1000000,
50                                         },
51                                 },
52                                 "output_path": "/out",
53                                 "runtime_constraints": map[string]interface{}{
54                                         "vcpus": 1,
55                                         "ram":   2,
56                                 },
57                                 "properties": tt.props,
58                         }})
59                 if tt.success {
60                         c.Assert(err, check.IsNil)
61                         c.Assert(cnt.Properties, check.DeepEquals, tt.props)
62                 } else {
63                         c.Assert(err, check.NotNil)
64                 }
65         }
66 }
67
68 func (s *ContainerRequestSuite) TestCRUpdateWithProperties(c *check.C) {
69         s.setUpVocabulary(c, "")
70         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
71
72         tests := []struct {
73                 name    string
74                 props   map[string]interface{}
75                 success bool
76         }{
77                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
78                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
79                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
80                 {"Empty properties", map[string]interface{}{}, true},
81         }
82         for _, tt := range tests {
83                 c.Log(c.TestName()+" ", tt.name)
84                 cnt, err := s.localdb.ContainerRequestCreate(ctx, arvados.CreateOptions{
85                         Attrs: map[string]interface{}{
86                                 "command":         []string{"echo", "foo"},
87                                 "container_image": "arvados/apitestfixture:latest",
88                                 "cwd":             "/tmp",
89                                 "environment":     map[string]string{},
90                                 "mounts": map[string]interface{}{
91                                         "/out": map[string]interface{}{
92                                                 "kind":     "tmp",
93                                                 "capacity": 1000000,
94                                         },
95                                 },
96                                 "output_path": "/out",
97                                 "runtime_constraints": map[string]interface{}{
98                                         "vcpus": 1,
99                                         "ram":   2,
100                                 },
101                         },
102                 })
103                 c.Assert(err, check.IsNil)
104                 cnt, err = s.localdb.ContainerRequestUpdate(ctx, arvados.UpdateOptions{
105                         UUID:   cnt.UUID,
106                         Select: []string{"uuid", "properties"},
107                         Attrs: map[string]interface{}{
108                                 "properties": tt.props,
109                         }})
110                 if tt.success {
111                         c.Assert(err, check.IsNil)
112                         c.Assert(cnt.Properties, check.DeepEquals, tt.props)
113                 } else {
114                         c.Assert(err, check.NotNil)
115                 }
116         }
117 }