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
515import nipyapi
16+ import urllib3
617from 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
1424from 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
2327ENDPOINT = f"https://test-nifi-node-default-0.test-nifi-node-default-headless.{ os .environ ['NAMESPACE' ]} .svc.cluster.local:8443"
2428USERNAME = "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
114155if __name__ == "__main__" :
0 commit comments