17944: Vocabulary loading, monitoring and checking on several object types.
[arvados.git] / lib / controller / localdb / group_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(&GroupSuite{})
20
21 type GroupSuite struct {
22         cluster  *arvados.Cluster
23         localdb  *Conn
24         railsSpy *arvadostest.Proxy
25 }
26
27 func (s *GroupSuite) 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 *GroupSuite) 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 *GroupSuite) TearDownTest(c *check.C) {
45         s.railsSpy.Close()
46 }
47
48 func (s *GroupSuite) 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         c.Assert(voc.Validate(), check.IsNil)
68         s.localdb.vocabularyCache = voc
69         s.cluster.API.VocabularyPath = "foo"
70 }
71
72 func (s *GroupSuite) TestGroupCreateWithProperties(c *check.C) {
73         s.setUpVocabulary(c, "")
74         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
75
76         tests := []struct {
77                 name    string
78                 props   map[string]interface{}
79                 success bool
80         }{
81                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
82                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
83                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
84                 {"Empty properties", map[string]interface{}{}, true},
85         }
86         for _, tt := range tests {
87                 c.Log(c.TestName()+" ", tt.name)
88
89                 grp, err := s.localdb.GroupCreate(ctx, arvados.CreateOptions{
90                         Select: []string{"uuid", "properties"},
91                         Attrs: map[string]interface{}{
92                                 "group_class": "project",
93                                 "properties":  tt.props,
94                         }})
95                 if tt.success {
96                         c.Assert(err, check.IsNil)
97                         c.Assert(grp.Properties, check.DeepEquals, tt.props)
98                 } else {
99                         c.Assert(err, check.NotNil)
100                 }
101         }
102 }
103
104 func (s *GroupSuite) TestGroupUpdateWithProperties(c *check.C) {
105         s.setUpVocabulary(c, "")
106         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
107
108         tests := []struct {
109                 name    string
110                 props   map[string]interface{}
111                 success bool
112         }{
113                 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
114                 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
115                 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
116                 {"Empty properties", map[string]interface{}{}, true},
117         }
118         for _, tt := range tests {
119                 c.Log(c.TestName()+" ", tt.name)
120                 grp, err := s.localdb.GroupCreate(ctx, arvados.CreateOptions{
121                         Attrs: map[string]interface{}{
122                                 "group_class": "project",
123                         },
124                 })
125                 c.Assert(err, check.IsNil)
126                 grp, err = s.localdb.GroupUpdate(ctx, arvados.UpdateOptions{
127                         UUID:   grp.UUID,
128                         Select: []string{"uuid", "properties"},
129                         Attrs: map[string]interface{}{
130                                 "properties": tt.props,
131                         }})
132                 if tt.success {
133                         c.Assert(err, check.IsNil)
134                         c.Assert(grp.Properties, check.DeepEquals, tt.props)
135                 } else {
136                         c.Assert(err, check.NotNil)
137                 }
138         }
139 }