18870: Need to declare NODES as array
[arvados.git] / sdk / python / tests / fed-migrate / check.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import arvados
6 import arvados.errors
7 import json
8 import sys
9
10 j = json.load(open(sys.argv[1]))
11
12 apiA = arvados.api(host=j["arvados_api_hosts"][0], token=j["superuser_tokens"][0], insecure=True)
13 tok = apiA.api_client_authorizations().current().execute()
14 v2_token = "v2/%s/%s" % (tok["uuid"], tok["api_token"])
15
16 apiB = arvados.api(host=j["arvados_api_hosts"][1], token=v2_token, insecure=True)
17 apiC = arvados.api(host=j["arvados_api_hosts"][2], token=v2_token, insecure=True)
18
19 ###
20 ### Check users on API server "A" (the LoginCluster) ###
21 ###
22 by_username = {}
23 def check_A(users):
24     assert len(users["items"]) == 11
25
26     for i in range(1, 10):
27         found = False
28         for u in users["items"]:
29             if u["username"] == ("case%d" % i) and u["email"] == ("case%d@test" % i) and u["first_name"] == ("Case%d" % i) and u["last_name"] == "Testuser":
30                 found = True
31                 by_username[u["username"]] = u["uuid"]
32         assert found
33
34     # Should be active
35     for i in (1, 2, 3, 4, 5, 6, 7, 8):
36         found = False
37         for u in users["items"]:
38             if u["username"] == ("case%d" % i) and u["email"] == ("case%d@test" % i) and u["is_active"] is True:
39                 found = True
40         assert found, "Not found case%i" % i
41
42     # case9 should not be active
43     found = False
44     for u in users["items"]:
45         if (u["username"] == "case9" and u["email"] == "case9@test" and
46             u["uuid"] == by_username[u["username"]] and u["is_active"] is False):
47             found = True
48     assert found
49
50 users = apiA.users().list().execute()
51 check_A(users)
52
53 users = apiA.users().list(bypass_federation=True).execute()
54 check_A(users)
55
56 ###
57 ### Check users on API server "B" (federation member) ###
58 ###
59
60 # check for expected migrations on B
61 users = apiB.users().list(bypass_federation=True).execute()
62 assert len(users["items"]) == 11
63
64 for i in range(2, 9):
65     found = False
66     for u in users["items"]:
67         if (u["username"] == ("case%d" % i) and u["email"] == ("case%d@test" % i) and
68             u["first_name"] == ("Case%d" % i) and u["last_name"] == "Testuser" and
69             u["uuid"] == by_username[u["username"]] and u["is_active"] is True):
70             found = True
71     assert found, "Not found case%i" % i
72
73 found = False
74 for u in users["items"]:
75     if (u["username"] == "case9" and u["email"] == "case9@test" and
76         u["first_name"] == "Case9" and u["last_name"] == "Testuser" and
77         u["uuid"] == by_username[u["username"]] and u["is_active"] is False):
78         found = True
79 assert found
80
81 # check that federated user listing works
82 users = apiB.users().list().execute()
83 check_A(users)
84
85 ###
86 ### Check users on API server "C" (federation member) ###
87 ###
88
89 # check for expected migrations on C
90 users = apiC.users().list(bypass_federation=True).execute()
91 assert len(users["items"]) == 8
92
93 for i in (2, 4, 6, 7, 8):
94     found = False
95     for u in users["items"]:
96         if (u["username"] == ("case%d" % i) and u["email"] == ("case%d@test" % i) and
97             u["first_name"] == ("Case%d" % i) and u["last_name"] == "Testuser" and
98             u["uuid"] == by_username[u["username"]] and u["is_active"] is True):
99             found = True
100     assert found
101
102 # cases 3, 5, 9 involve users that have never accessed cluster C so
103 # there's nothing to migrate.
104 for i in (3, 5, 9):
105     found = False
106     for u in users["items"]:
107         if (u["username"] == ("case%d" % i) and u["email"] == ("case%d@test" % i) and
108             u["first_name"] == ("Case%d" % i) and u["last_name"] == "Testuser" and
109             u["uuid"] == by_username[u["username"]] and u["is_active"] is True):
110             found = True
111     assert not found
112
113 # check that federated user listing works
114 users = apiC.users().list().execute()
115 check_A(users)
116
117
118 ####
119 # bug 16683 tests
120
121 # Check that this query returns empty, instead of returning a 500 or
122 # 502 error.
123 # Yes, we're asking for a group from the users endpoint.  This is not a
124 # mistake, this is something workbench does to populate the sharing
125 # dialog.
126 clusterID_B = apiB.configs().get().execute()["ClusterID"]
127 i = apiB.users().list(filters=[["uuid", "in", ["%s-j7d0g-fffffffffffffff" % clusterID_B]]], count="none").execute()
128 assert len(i["items"]) == 0
129
130 # Check that we can create a project and give a remote user access to it
131
132 tok3 = apiA.api_client_authorizations().create(body={"api_client_authorization": {"owner_uuid": by_username["case3"]}}).execute()
133 tok4 = apiA.api_client_authorizations().create(body={"api_client_authorization": {"owner_uuid": by_username["case4"]}}).execute()
134
135 v2_token3 = "v2/%s/%s" % (tok3["uuid"], tok3["api_token"])
136 v2_token4 = "v2/%s/%s" % (tok4["uuid"], tok4["api_token"])
137
138 apiB_3 = arvados.api(host=j["arvados_api_hosts"][1], token=v2_token3, insecure=True)
139 apiB_4 = arvados.api(host=j["arvados_api_hosts"][1], token=v2_token4, insecure=True)
140
141 assert apiB_3.users().current().execute()["uuid"] == by_username["case3"]
142 assert apiB_4.users().current().execute()["uuid"] == by_username["case4"]
143
144 newproject = apiB_3.groups().create(body={"group_class": "project",
145                                            "name":"fed test project"},
146                                     ensure_unique_name=True).execute()
147
148 try:
149     # Expect to fail
150     apiB_4.groups().get(uuid=newproject["uuid"]).execute()
151 except arvados.errors.ApiError as e:
152     if e.resp['status'] == '404':
153         pass
154     else:
155         raise
156
157 l = apiB_3.links().create(body={"link_class": "permission",
158                             "name":"can_read",
159                             "tail_uuid": by_username["case4"],
160                             "head_uuid": newproject["uuid"]}).execute()
161
162 # Expect to succeed
163 apiB_4.groups().get(uuid=newproject["uuid"]).execute()
164
165 # remove permission
166 apiB_3.links().delete(uuid=l["uuid"]).execute()
167
168 try:
169     # Expect to fail again
170     apiB_4.groups().get(uuid=newproject["uuid"]).execute()
171 except arvados.errors.ApiError as e:
172     if e.resp['status'] == '404':
173         pass
174     else:
175         raise
176
177 print("Passed checks")