19907: Log when caching negative result for OIDC token check.
[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/lib/config"
11         "git.arvados.org/arvados.git/lib/controller/rpc"
12         "git.arvados.org/arvados.git/sdk/go/arvados"
13         "git.arvados.org/arvados.git/sdk/go/arvadostest"
14         "git.arvados.org/arvados.git/sdk/go/auth"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         check "gopkg.in/check.v1"
17 )
18
19 var _ = check.Suite(&ContainerRequestSuite{})
20
21 type ContainerRequestSuite struct {
22         cluster  *arvados.Cluster
23         localdb  *Conn
24         railsSpy *arvadostest.Proxy
25 }
26
27 func (s *ContainerRequestSuite) TearDownSuite(c *check.C) {
28         // Undo any changes/additions to the user database so they
29         // don't affect subsequent tests.
30         arvadostest.ResetEnv()
31         c.Check(arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil), check.IsNil)
32 }
33
34 func (s *ContainerRequestSuite) SetUpTest(c *check.C) {
35         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
36         c.Assert(err, check.IsNil)
37         s.cluster, err = cfg.GetCluster("")
38         c.Assert(err, check.IsNil)
39         s.localdb = NewConn(s.cluster)
40         s.railsSpy = arvadostest.NewProxy(c, s.cluster.Services.RailsAPI)
41         *s.localdb.railsProxy = *rpc.NewConn(s.cluster.ClusterID, s.railsSpy.URL, true, rpc.PassthroughTokenProvider)
42 }
43
44 func (s *ContainerRequestSuite) TearDownTest(c *check.C) {
45         s.railsSpy.Close()
46 }
47
48 func (s *ContainerRequestSuite) setUpVocabulary(c *check.C, testVocabulary string) {
49         if testVocabulary == "" {
50                 testVocabulary = `{
51                         "strict_tags": false,
52                         "tags": {
53                                 "IDTAGIMPORTANCES": {
54                                         "strict": true,
55                                         "labels": [{"label": "Importance"}, {"label": "Priority"}],
56                                         "values": {
57                                                 "IDVALIMPORTANCES1": { "labels": [{"label": "Critical"}, {"label": "Urgent"}, {"label": "High"}] },
58                                                 "IDVALIMPORTANCES2": { "labels": [{"label": "Normal"}, {"label": "Moderate"}] },
59                                                 "IDVALIMPORTANCES3": { "labels": [{"label": "Low"}] }
60                                         }
61                                 }
62                         }
63                 }`
64         }
65         voc, err := arvados.NewVocabulary([]byte(testVocabulary), []string{})
66         c.Assert(err, check.IsNil)
67         s.localdb.vocabularyCache = voc
68         s.cluster.API.VocabularyPath = "foo"
69 }
70
71 func (s *ContainerRequestSuite) TestCRCreateWithProperties(c *check.C) {
72         s.setUpVocabulary(c, "")
73         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
74
75         tests := []struct {
76                 name    string
77                 props   map[string]interface{}
78                 success bool
79         }{
80                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
81                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
82                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
83                 {"Empty properties", map[string]interface{}{}, true},
84         }
85         for _, tt := range tests {
86                 c.Log(c.TestName()+" ", tt.name)
87
88                 cnt, err := s.localdb.ContainerRequestCreate(ctx, arvados.CreateOptions{
89                         Select: []string{"uuid", "properties"},
90                         Attrs: map[string]interface{}{
91                                 "command":         []string{"echo", "foo"},
92                                 "container_image": "arvados/apitestfixture:latest",
93                                 "cwd":             "/tmp",
94                                 "environment":     map[string]string{},
95                                 "mounts": map[string]interface{}{
96                                         "/out": map[string]interface{}{
97                                                 "kind":     "tmp",
98                                                 "capacity": 1000000,
99                                         },
100                                 },
101                                 "output_path": "/out",
102                                 "runtime_constraints": map[string]interface{}{
103                                         "vcpus": 1,
104                                         "ram":   2,
105                                 },
106                                 "properties": tt.props,
107                         }})
108                 if tt.success {
109                         c.Assert(err, check.IsNil)
110                         c.Assert(cnt.Properties, check.DeepEquals, tt.props)
111                 } else {
112                         c.Assert(err, check.NotNil)
113                 }
114         }
115 }
116
117 func (s *ContainerRequestSuite) TestCRUpdateWithProperties(c *check.C) {
118         s.setUpVocabulary(c, "")
119         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
120
121         tests := []struct {
122                 name    string
123                 props   map[string]interface{}
124                 success bool
125         }{
126                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
127                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
128                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
129                 {"Empty properties", map[string]interface{}{}, true},
130         }
131         for _, tt := range tests {
132                 c.Log(c.TestName()+" ", tt.name)
133                 cnt, err := s.localdb.ContainerRequestCreate(ctx, arvados.CreateOptions{
134                         Attrs: map[string]interface{}{
135                                 "command":         []string{"echo", "foo"},
136                                 "container_image": "arvados/apitestfixture:latest",
137                                 "cwd":             "/tmp",
138                                 "environment":     map[string]string{},
139                                 "mounts": map[string]interface{}{
140                                         "/out": map[string]interface{}{
141                                                 "kind":     "tmp",
142                                                 "capacity": 1000000,
143                                         },
144                                 },
145                                 "output_path": "/out",
146                                 "runtime_constraints": map[string]interface{}{
147                                         "vcpus": 1,
148                                         "ram":   2,
149                                 },
150                         },
151                 })
152                 c.Assert(err, check.IsNil)
153                 cnt, err = s.localdb.ContainerRequestUpdate(ctx, arvados.UpdateOptions{
154                         UUID:   cnt.UUID,
155                         Select: []string{"uuid", "properties"},
156                         Attrs: map[string]interface{}{
157                                 "properties": tt.props,
158                         }})
159                 if tt.success {
160                         c.Assert(err, check.IsNil)
161                         c.Assert(cnt.Properties, check.DeepEquals, tt.props)
162                 } else {
163                         c.Assert(err, check.NotNil)
164                 }
165         }
166 }