Skip to content

Commit 7013b70

Browse files
test: Fix the NiFi upgrade kuttl test (#1032)
1 parent 153e5e8 commit 7013b70

5 files changed

Lines changed: 94 additions & 43 deletions

File tree

‎tests/templates/kuttl/upgrade/04-assert.yaml.j2‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ kind: TestAssert
44
timeout: 300
55
commands:
66
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- python /tmp/test_nifi.py -u admin -p supersecretpassword -n $NAMESPACE -c 3
7-
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "python /tmp/flow.py > /tmp/old_input"
8-
# This tests if the output contains an Error or zero flow files are queued, which also indicates that something went wrong
7+
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "python /tmp/flow.py run > /tmp/old_input"
8+
# flow.py itself fails on errors. This additionally catches zero (or no) queued flow files, which also indicates that something went wrong
99
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "cat /tmp/old_input | grep -Eov 'Error|\b0\b'"

‎tests/templates/kuttl/upgrade/06-assert.yaml.j2‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ metadata:
1010
status:
1111
readyReplicas: 3
1212
replicas: 3
13+
# `readyReplicas` alone is not sufficient: During a rolling update it returns to 3 after every
14+
# single pod has been replaced, so the assertion would already succeed when just one of the three
15+
# pods runs the new version. `updatedReplicas` counts the pods which were created from the
16+
# StatefulSet's current `updateRevision`. It drops below 3 as soon as the operator patches the
17+
# StatefulSet and only reaches 3 again once every pod runs the new revision.
18+
#
19+
# Both fields are also 3 *before* the rotation starts, so this assertion on its own would still
20+
# succeed against the old version. The sleep in 06-sleep.yaml gives the operator time to patch
21+
# the StatefulSet first and is therefore required.
22+
updatedReplicas: 3
1323
---
1424
apiVersion: nifi.stackable.tech/v1alpha1
1525
kind: NifiCluster

‎tests/templates/kuttl/upgrade/06-sleep.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ kind: TestStep
44
commands:
55
# Sleep for a little while to ensure the pod rotation of the NiFi cluster has started. Otherwise,
66
# the assertion in 06 will immediately succeed because it is run _before_ the pod rotation has
7-
# started.
7+
# started. See the comment on `updatedReplicas` in 06-assert.yaml.j2.
88
- script: sleep 15

‎tests/templates/kuttl/upgrade/07-assert.yaml.j2‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ metadata:
66
timeout: 300
77
commands:
88
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- python /tmp/test_nifi.py -u admin -p supersecretpassword -n $NAMESPACE -c 3
9-
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "python /tmp/flow.py -e https://test-nifi-node-default-0.test-nifi-node-default-headless.$NAMESPACE.svc.cluster.local:8443 query > /tmp/new_input"
10-
# This tests if the output contains an Error or zero flow files are queued, which also indicates that something went wrong
9+
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "python /tmp/flow.py query > /tmp/new_input"
10+
# flow.py itself fails on errors. This additionally catches zero (or no) queued flow files, which also indicates that something went wrong
1111
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- sh -c "cat /tmp/new_input | grep -Eov 'Error|\b0\b'"
1212
# This tests that the number of input records stays the same after the upgrade.
1313
- script: kubectl exec -n $NAMESPACE test-nifi-0 -- diff /tmp/old_input /tmp/new_input

‎tests/templates/kuttl/upgrade/flow.py‎

100644100755
Lines changed: 79 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#!/usr/bin/env python3
22
# This script is used to create a process group, run it for a couple of seconds, stop it and then
3-
# query the number of queued flow files.
3+
# query the number of queued flow files ("run"), or to just query the number of queued flow files
4+
# of an already existing process group ("query").
5+
#
6+
# The informational output is written to stderr so that stdout only contains the number of queued
7+
# flow files. This way the output of two invocations can be compared with diff.
8+
9+
import argparse
10+
import os
11+
import sys
12+
import time
13+
from dataclasses import dataclass
414

515
import nipyapi
16+
import urllib3
617
from nipyapi.canvas import (
718
get_root_pg_id,
8-
schedule_process_group,
919
list_all_controllers,
10-
schedule_controller,
1120
recurse_flow,
21+
schedule_controller,
22+
schedule_process_group,
1223
)
13-
from nipyapi.security import service_login
1424
from nipyapi.nifi.api_client import ApiClient
15-
16-
from typing import Union
17-
from dataclasses import dataclass
18-
19-
import os
20-
import time
21-
import urllib3
25+
from nipyapi.security import service_login
2226

2327
ENDPOINT = f"https://test-nifi-node-default-0.test-nifi-node-default-headless.{os.environ['NAMESPACE']}.svc.cluster.local:8443"
2428
USERNAME = "admin"
@@ -42,36 +46,56 @@ def schedule(pg_id: str, toggle: bool) -> bool:
4246
return schedule_process_group(pg_id, scheduled=toggle)
4347

4448

45-
def flow_files_queued(connection_name: str) -> Union[str, Error]:
46-
"""Returns the current input record number for the given component"""
47-
flow = recurse_flow()
48-
49-
if flow.process_group_flow.flow.process_groups:
50-
inner_flow = recurse_flow(flow.process_group_flow.flow.process_groups[0].id)
51-
conn = [
52-
conn
53-
for conn in inner_flow.process_group_flow.flow.connections
54-
if conn.status.name == connection_name
55-
]
56-
if conn:
57-
return conn[0].status.aggregate_snapshot.flow_files_queued
58-
else:
59-
return Error(f"No connection named '{connection_name}' found.")
60-
else:
49+
def flow_files_queued(connection_name: str) -> int | Error:
50+
"""Returns the number of queued flow files for the given connection"""
51+
process_groups = recurse_flow().process_group_flow.flow.process_groups
52+
53+
# All flow files must be queued in a single process group, otherwise comparing the counts
54+
# before and after the upgrade is meaningless. More than one process group means that the
55+
# upload ran twice, which happens when kuttl retries the assertion in step 4 after the upload
56+
# already succeeded. Such a run cannot recover, so report it instead of returning the count of
57+
# an arbitrary copy.
58+
if len(process_groups) != 1:
59+
return Error(
60+
f"Expected exactly one process group, found {len(process_groups)}."
61+
)
62+
63+
flow = recurse_flow(process_groups[0].id)
64+
connections = [
65+
conn
66+
for conn in flow.process_group_flow.flow.connections
67+
if conn.status.name == connection_name
68+
]
69+
if not connections:
6170
return Error(f"No connection named '{connection_name}' found.")
6271

72+
return connections[0].status.aggregate_snapshot.flow_files_queued
6373

64-
def main():
74+
75+
def parse_args(args: list[str]) -> argparse.Namespace:
76+
parser = argparse.ArgumentParser(description="Set up a flow and query its status.")
77+
subparsers = parser.add_subparsers(dest="subcommand", required=True)
78+
subparsers.add_parser(
79+
"run",
80+
help="Upload the process group, run it for a couple of seconds, stop it and query the number of queued flow files.",
81+
)
82+
subparsers.add_parser(
83+
"query",
84+
help="Query the number of queued flow files of the already uploaded process group.",
85+
)
86+
return parser.parse_args(args)
87+
88+
89+
def login() -> None:
6590
nipyapi.config.nifi_config.host = f"{ENDPOINT}/nifi-api"
6691
nipyapi.config.nifi_config.verify_ssl = False
6792

68-
print(f"Logging in as {USERNAME}")
93+
print(f"Logging in as {USERNAME}", file=sys.stderr)
6994
service_login(username=USERNAME, password=PASSWORD)
70-
print("Logged in")
95+
print("Logged in", file=sys.stderr)
7196

72-
pg_id = get_root_pg_id()
73-
print(f"Got root process group id: {pg_id}")
7497

98+
def upload_process_group(pg_id: str) -> None:
7599
if not nipyapi.config.nifi_config.api_client:
76100
nipyapi.config.nifi_config.api_client = ApiClient()
77101

@@ -85,7 +109,7 @@ def main():
85109
)
86110
)
87111

88-
print("Uploading process group")
112+
print("Uploading process group", file=sys.stderr)
89113
nipyapi.config.nifi_config.api_client.call_api(
90114
"/process-groups/{pg_id}/process-groups/upload",
91115
"POST",
@@ -102,13 +126,30 @@ def main():
102126
files={"file": FLOWFILE},
103127
auth_settings=["tokenAuth"],
104128
)
105-
print("Process group uploaded")
129+
print("Process group uploaded", file=sys.stderr)
130+
131+
132+
def main():
133+
args = parse_args(sys.argv[1:])
134+
135+
login()
136+
137+
if args.subcommand == "run":
138+
pg_id = get_root_pg_id()
139+
print(f"Got root process group id: {pg_id}", file=sys.stderr)
140+
141+
upload_process_group(pg_id)
142+
143+
schedule(pg_id, True) # Start the flow
144+
time.sleep(5) # Let the flow run for 5 seconds
145+
schedule(pg_id, False) # Stop the flow
106146

107-
schedule(pg_id, True) # Start the flow
108-
time.sleep(5) # Let the flow run for 5 seconds
109-
schedule(pg_id, False) # Stop the flow
147+
queued = flow_files_queued(CONNECTION_NAME)
148+
if isinstance(queued, Error):
149+
print(queued.message, file=sys.stderr)
150+
sys.exit(1)
110151

111-
print(flow_files_queued(CONNECTION_NAME))
152+
print(queued)
112153

113154

114155
if __name__ == "__main__":

0 commit comments

Comments
 (0)