Skip to content

Commit

Permalink
Handle watch streams better in operator tests so that potential race …
Browse files Browse the repository at this point in the history
…conditions can be avoided
  • Loading branch information
meffmadd committed Jul 29, 2024
1 parent 283e66f commit b0b1de3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
22 changes: 14 additions & 8 deletions tests/operator_tests/test_config_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ def test_config_server_custom_resource(random_namespace, operator_file):

create_crd(client)

with KopfRunner(['run', '-A', '--verbose', operator_file]) as runner:
with KopfRunner(['run', '-n', random_namespace, '--verbose', operator_file]) as runner:
config_server_watch = watch.Watch()
pod_watch = watch.Watch()
config_map_watch = watch.Watch()
config_server_stream = config_server_watch.stream(custom_objects_api.list_namespaced_custom_object,
"datalab.tuwien.ac.at", "v1", random_namespace,
"configservers")
pod_stream = pod_watch.stream(core_api.list_namespaced_pod, random_namespace, timeout_seconds=10)
config_map_stream = config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace,
timeout_seconds=10)

create_config_server(client, random_namespace)

config_server_watch = watch.Watch()
entered = False
for event in config_server_watch.stream(custom_objects_api.list_namespaced_custom_object, "datalab.tuwien.ac.at", "v1", random_namespace, "configservers"):
for event in config_server_stream:
assert event['type'] == "ADDED"
obj = event['object'] # object is one of type return_type

Expand All @@ -48,10 +57,8 @@ def test_config_server_custom_resource(random_namespace, operator_file):
config_server_watch.stop()
assert entered

pod_watch = watch.Watch()
entered = False

for event in pod_watch.stream(core_api.list_namespaced_pod, random_namespace, timeout_seconds=10):
for event in pod_stream:
assert event['type'] == "ADDED"

obj = event['object'] # object is one of type return_type
Expand All @@ -73,9 +80,8 @@ def test_config_server_custom_resource(random_namespace, operator_file):
pod_watch.stop()
assert entered

config_map_watch = watch.Watch()
entered = False
for event in config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace, timeout_seconds=10):
for event in config_map_stream:
assert event['type'] == "ADDED"

obj = event['object'] # object is one of type return_type
Expand Down
47 changes: 32 additions & 15 deletions tests/operator_tests/test_key_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,23 @@ def test_key_value_custom_resource_before_server(random_namespace, operator_file

create_crd(client)

with KopfRunner(['run', '-A', '--verbose', operator_file]) as runner:
with KopfRunner(['run', '-n', random_namespace, '--verbose', operator_file]) as runner:
key_value_watch = watch.Watch()
config_server_watch = watch.Watch()
config_map_watch = watch.Watch()

key_value_stream = key_value_watch.stream(custom_objects_api.list_namespaced_custom_object,
"datalab.tuwien.ac.at", "v1", random_namespace, "keyvaluepairs")
config_server_stream = config_server_watch.stream(custom_objects_api.list_namespaced_custom_object,
"datalab.tuwien.ac.at", "v1", random_namespace,
"configservers")
config_map_stream = config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace,
timeout_seconds=10)

create_key_value_pair(client, random_namespace)

key_value_watch = watch.Watch()
entered = False
for event in key_value_watch.stream(custom_objects_api.list_namespaced_custom_object, "datalab.tuwien.ac.at", "v1", random_namespace, "keyvaluepairs"):
for event in key_value_stream:
assert event['type'] == "ADDED"
obj = event['object'] # object is one of type return_type

Expand All @@ -55,9 +66,8 @@ def test_key_value_custom_resource_before_server(random_namespace, operator_file

create_config_server(client, random_namespace)

config_server_watch = watch.Watch()
entered = False
for event in config_server_watch.stream(custom_objects_api.list_namespaced_custom_object, "datalab.tuwien.ac.at", "v1", random_namespace, "configservers"):
for event in config_server_stream:
assert event['type'] == "ADDED"
obj = event['object'] # object is one of type return_type

Expand All @@ -66,9 +76,8 @@ def test_key_value_custom_resource_before_server(random_namespace, operator_file
config_server_watch.stop()
assert entered

config_map_watch = watch.Watch()
entered = False
for event in config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace, timeout_seconds=10):
for event in config_map_stream:
assert event['type'] == "ADDED"

obj = event['object'] # object is one of type return_type
Expand Down Expand Up @@ -98,13 +107,24 @@ def test_key_value_custom_resource_after_server(random_namespace, operator_file)

create_crd(client)

with KopfRunner(['run', '-A', '--verbose', operator_file]) as runner:
with KopfRunner(['run', '-n', random_namespace, '--verbose', operator_file]) as runner:
key_value_watch = watch.Watch()
config_server_watch = watch.Watch()
config_map_watch = watch.Watch()

key_value_stream = key_value_watch.stream(custom_objects_api.list_namespaced_custom_object,
"datalab.tuwien.ac.at", "v1", random_namespace, "keyvaluepairs")
config_server_stream = config_server_watch.stream(custom_objects_api.list_namespaced_custom_object,
"datalab.tuwien.ac.at", "v1", random_namespace,
"configservers")
config_map_stream = config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace,
timeout_seconds=10)

# config-server
create_config_server(client, random_namespace)

config_server_watch = watch.Watch()
entered = False
for event in config_server_watch.stream(custom_objects_api.list_namespaced_custom_object, "datalab.tuwien.ac.at", "v1", random_namespace, "configservers"):
for event in config_server_stream:
assert event['type'] == "ADDED"
obj = event['object'] # object is one of type return_type

Expand All @@ -116,9 +136,8 @@ def test_key_value_custom_resource_after_server(random_namespace, operator_file)
# key-value
create_key_value_pair(client, random_namespace)

key_value_watch = watch.Watch()
entered = False
for event in key_value_watch.stream(custom_objects_api.list_namespaced_custom_object, "datalab.tuwien.ac.at", "v1", random_namespace, "keyvaluepairs"):
for event in key_value_stream:
assert event['type'] == "ADDED"
obj = event['object'] # object is one of type return_type

Expand All @@ -128,9 +147,8 @@ def test_key_value_custom_resource_after_server(random_namespace, operator_file)
assert entered

# config map
config_map_watch = watch.Watch()
entered = False
for event in config_map_watch.stream(core_api.list_namespaced_config_map, random_namespace, timeout_seconds=10):
for event in config_map_stream:
assert event['type'] == "ADDED"

obj = event['object'] # object is one of type return_type
Expand All @@ -151,4 +169,3 @@ def test_key_value_custom_resource_after_server(random_namespace, operator_file)

assert runner.exit_code == 0
assert runner.exception is None

0 comments on commit b0b1de3

Please sign in to comment.