Skip to content

Commit eb88fcf

Browse files
authored
fix(kuttl tests): Use bundled userinfo rego utility (#619)
* test(kuttl): improve error output in python scripts * chore(kuttl): ruff formatting * chore(kuttl): improve error output in python scripts * fix(kuttl): revert change from #580 * fix(kuttl): use bundled userinfo code
1 parent 984604d commit eb88fcf

File tree

6 files changed

+90
-30
lines changed

6 files changed

+90
-30
lines changed

tests/templates/kuttl/aas-user-info/10-install-opa.yaml.j2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ commands:
55
- script: |
66
kubectl apply -n $NAMESPACE -f - <<EOF
77
---
8+
apiVersion: v1
9+
kind: ConfigMap
10+
metadata:
11+
name: test
12+
labels:
13+
opa.stackable.tech/bundle: "true"
14+
data:
15+
test.rego: |
16+
package test
17+
18+
import data.stackable.opa.userinfo.v1 as userinfo
19+
20+
currentUserInfoByUsername := userinfo.userInfoByUsername(input.username)
21+
currentUserInfoById := userinfo.userInfoById(input.id)
22+
---
823
apiVersion: opa.stackable.tech/v1alpha1
924
kind: OpaCluster
1025
metadata:

tests/templates/kuttl/aas-user-info/30-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ kind: TestAssert
44
metadata:
55
name: test-regorule
66
commands:
7-
- script: kubectl exec -n $NAMESPACE test-regorule-0 -- python /tmp/test-regorule.py -u 'http://test-opa-server-default:8081/v1/data/stackable/opa/userinfo/v1'
7+
- script: kubectl exec -n $NAMESPACE test-regorule-0 -- python /tmp/test-regorule.py -u 'http://test-opa-server-default:8081/v1/data/test'

tests/templates/kuttl/aas-user-info/test-regorule.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,65 @@
44
import json
55

66

7-
def assertions(username, response, opa_attribute, expected_groups, expected_attributes={}):
7+
def assertions(
8+
username, response, opa_attribute, expected_groups, expected_attributes={}
9+
):
810
assert "result" in response
9-
assert opa_attribute in response["result"]
11+
result = response["result"]
12+
assert opa_attribute in result, f"expected {opa_attribute} in {result}"
1013

1114
# repeated the right hand side for better output on error
12-
assert "customAttributes" in response["result"][opa_attribute]
13-
assert "groups" in response["result"][opa_attribute]
14-
assert "id" in response["result"][opa_attribute]
15-
assert "username" in response["result"][opa_attribute]
15+
assert "customAttributes" in result[opa_attribute]
16+
assert "groups" in result[opa_attribute]
17+
assert "id" in result[opa_attribute]
18+
assert "username" in result[opa_attribute]
1619

1720
# todo: split out group assertions
1821
print(f"Testing for {username} in groups {expected_groups}")
19-
groups = sorted(response["result"][opa_attribute]["groups"])
22+
groups = sorted(result[opa_attribute]["groups"])
2023
expected_groups = sorted(expected_groups)
2124
assert groups == expected_groups, f"got {groups}, expected: {expected_groups}"
2225

2326
# todo: split out customAttribute assertions
2427
print(f"Testing for {username} with customAttributes {expected_attributes}")
25-
custom_attributes = response["result"][opa_attribute]["customAttributes"]
26-
assert custom_attributes == expected_attributes, f"got {custom_attributes}, expected: {expected_attributes}"
28+
custom_attributes = result[opa_attribute]["customAttributes"]
29+
assert (
30+
custom_attributes == expected_attributes
31+
), f"got {custom_attributes}, expected: {expected_attributes}"
2732

2833

2934
if __name__ == "__main__":
3035
all_args = argparse.ArgumentParser()
3136
all_args.add_argument("-u", "--url", required=True, help="OPA service url")
3237
args = vars(all_args.parse_args())
33-
params = {'strict-builtin-errors': 'true'}
38+
params = {"strict-builtin-errors": "true"}
3439

3540
def make_request(payload):
36-
return requests.post(args['url'], data=json.dumps(payload), params=params).json()
41+
response = requests.post(args["url"], data=json.dumps(payload), params=params)
42+
expected_status_code = 200
43+
assert (
44+
response.status_code == expected_status_code
45+
), f"got {response.status_code}, expected: {expected_status_code}"
46+
return response.json()
3747

3848
for subject_id in ["alice", "bob"]:
3949
try:
4050
# todo: try this out locally until it works
4151
# url = 'http://test-opa-svc:8081/v1/data'
42-
payload = {'input': {'id': subject_id}}
52+
payload = {"input": {"id": subject_id}}
4353
response = make_request(payload)
44-
assertions(subject_id, response, "currentUserInfoById", [], {"e-mail": f"{subject_id}@example.com", "company": "openid"})
54+
assertions(
55+
subject_id,
56+
response,
57+
"currentUserInfoById",
58+
[],
59+
{"e-mail": f"{subject_id}@example.com", "company": "openid"},
60+
)
4561
except Exception as e:
62+
print(f"exception: {e}")
4663
if response is not None:
47-
print(f"something went wrong. last response: {response}")
64+
print(f"request body: {payload}")
65+
print(f"response body: {response}")
4866
raise e
4967

5068
print("Test successful!")

tests/templates/kuttl/keycloak-user-info/10-install-opa.yaml.j2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ commands:
55
- script: |
66
kubectl apply -n $NAMESPACE -f - <<EOF
77
---
8+
apiVersion: v1
9+
kind: ConfigMap
10+
metadata:
11+
name: test
12+
labels:
13+
opa.stackable.tech/bundle: "true"
14+
data:
15+
test.rego: |
16+
package test
17+
18+
import data.stackable.opa.userinfo.v1 as userinfo
19+
20+
currentUserInfoByUsername := userinfo.userInfoByUsername(input.username)
21+
currentUserInfoById := userinfo.userInfoById(input.id)
22+
---
823
apiVersion: opa.stackable.tech/v1alpha1
924
kind: OpaCluster
1025
metadata:

tests/templates/kuttl/keycloak-user-info/30-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ kind: TestAssert
44
metadata:
55
name: test-regorule
66
commands:
7-
- script: kubectl exec -n $NAMESPACE test-regorule-0 -- python /tmp/test-regorule.py -u 'http://test-opa-server-default:8081/v1/data/stackable/opa/userinfo/v1'
7+
- script: kubectl exec -n $NAMESPACE test-regorule-0 -- python /tmp/test-regorule.py -u 'http://test-opa-server-default:8081/v1/data/test'

tests/templates/kuttl/keycloak-user-info/test-regorule.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,65 @@
1010
}
1111

1212

13-
def assertions(username, response, opa_attribute, expected_groups, expected_attributes={}):
13+
def assertions(
14+
username, response, opa_attribute, expected_groups, expected_attributes={}
15+
):
1416
assert "result" in response
15-
assert opa_attribute in response["result"]
17+
result = response["result"]
18+
assert opa_attribute in result, f"expected {opa_attribute} in {result}"
1619

1720
# repeated the right hand side for better output on error
18-
assert "customAttributes" in response["result"][opa_attribute]
19-
assert "groups" in response["result"][opa_attribute]
20-
assert "id" in response["result"][opa_attribute]
21-
assert "username" in response["result"][opa_attribute]
21+
assert "customAttributes" in result[opa_attribute]
22+
assert "groups" in result[opa_attribute]
23+
assert "id" in result[opa_attribute]
24+
assert "username" in result[opa_attribute]
2225

2326
# todo: split out group assertions
2427
print(f"Testing for {username} in groups {expected_groups}")
25-
groups = sorted(response["result"][opa_attribute]["groups"])
28+
groups = sorted(result[opa_attribute]["groups"])
2629
expected_groups = sorted(expected_groups)
2730
assert groups == expected_groups, f"got {groups}, expected: {expected_groups}"
2831

2932
# todo: split out customAttribute assertions
3033
print(f"Testing for {username} with customAttributes {expected_attributes}")
31-
custom_attributes = response["result"][opa_attribute]["customAttributes"]
32-
assert custom_attributes == expected_attributes, f"got {custom_attributes}, expected: {expected_attributes}"
34+
custom_attributes = result[opa_attribute]["customAttributes"]
35+
assert (
36+
custom_attributes == expected_attributes
37+
), f"got {custom_attributes}, expected: {expected_attributes}"
3338

3439

3540
if __name__ == "__main__":
3641
all_args = argparse.ArgumentParser()
3742
all_args.add_argument("-u", "--url", required=True, help="OPA service url")
3843
args = vars(all_args.parse_args())
39-
params = {'strict-builtin-errors': 'true'}
44+
params = {"strict-builtin-errors": "true"}
4045

4146
def make_request(payload):
42-
return requests.post(args['url'], data=json.dumps(payload), params=params).json()
47+
response = requests.post(args["url"], data=json.dumps(payload), params=params)
48+
expected_status_code = 200
49+
assert (
50+
response.status_code == expected_status_code
51+
), f"got {response.status_code}, expected: {expected_status_code}"
52+
return response.json()
4353

4454
for username, groups in users_and_groups.items():
4555
try:
4656
# todo: try this out locally until it works
4757
# url = 'http://test-opa-svc:8081/v1/data'
48-
payload = {'input': {'username': username}}
58+
payload = {"input": {"username": username}}
4959
response = make_request(payload)
5060
assertions(username, response, "currentUserInfoByUsername", groups, {})
5161

5262
# do the reverse lookup
5363
user_id = response["result"]["currentUserInfoByUsername"]["id"]
54-
payload = {'input': {'id': user_id}}
64+
payload = {"input": {"id": user_id}}
5565
response = make_request(payload)
5666
assertions(username, response, "currentUserInfoById", groups, {})
5767
except Exception as e:
68+
print(f"exception: {e}")
5869
if response is not None:
59-
print(f"something went wrong. last response: {response}")
70+
print(f"request body: {payload}")
71+
print(f"response body: {response}")
6072
raise e
6173

6274
print("Test successful!")

0 commit comments

Comments
 (0)