Skip to content

Commit

Permalink
Fix Tests bug with process context
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Honaker <[email protected]>
  • Loading branch information
HonakerM committed Sep 6, 2024
1 parent 24a0916 commit 83f81c7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 46 deletions.
2 changes: 1 addition & 1 deletion oper8/watch_manager/python_watch_manager/threads/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__( # pylint: disable=too-many-arguments
# Variables for tracking retries
self.attempts_left = config.python_watch_manager.watch_retry_count
self.retry_delay = parse_time_delta(
config.python_watch_manager.watch_retry_delay
config.python_watch_manager.watch_retry_delay or ""
)

def run(self):
Expand Down
107 changes: 62 additions & 45 deletions tests/watch_manager/python_watch_manager/threads/test_watch_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
@pytest.mark.timeout(5)
def test_watch_thread_happy_path():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})
watched_object_id = ResourceId.from_resource(watched_object)

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -67,11 +69,13 @@ def test_watch_thread_happy_path():
@pytest.mark.timeout(5)
def test_watch_thread_filtered():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})
watched_object_id = ResourceId.from_resource(watched_object)

with library_config(python_watch_manager={"filter": DisableFilter}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": DisableFilter}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -100,11 +104,13 @@ def test_watch_thread_filtered():
@pytest.mark.timeout(5)
def test_watch_thread_deleted():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})
watched_object_id = ResourceId.from_resource(watched_object)

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -133,7 +139,6 @@ def test_watch_thread_deleted():
@pytest.mark.timeout(5)
def test_watch_thread_owner_watch():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
owner_object = make_resource(
kind="DifferentKind", name="owner", spec={"test": "value"}
)
Expand All @@ -146,7 +151,10 @@ def test_watch_thread_owner_watch():
# Deploy owner before watch has started
dm.deploy([owner_object])

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -179,7 +187,6 @@ def test_watch_thread_owner_watch():
@pytest.mark.timeout(5)
def test_watch_thread_global_watch():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
owner_object = make_resource(
kind="DifferentKind", name="owner", spec={"test": "value"}
)
Expand All @@ -190,7 +197,10 @@ def test_watch_thread_global_watch():

dm.deploy([owner_object])

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -227,13 +237,15 @@ def test_watch_thread_global_watch():
@pytest.mark.timeout(5)
def test_watch_thread_all_events():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})
request_resource_id = ResourceId(
api_version=watched_object.get("apiVersion"), kind=watched_object.get("kind")
)

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -267,7 +279,6 @@ def test_watch_thread_all_events():
@pytest.mark.timeout(5)
def test_watch_thread_global_watch_two_owners():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
owner_object = make_resource(kind="OwnerKind", name="owner", spec={"test": "value"})
owner_2_object = make_resource(
kind="OwnerKind", name="owner2", spec={"test": "value"}
Expand All @@ -281,7 +292,10 @@ def test_watch_thread_global_watch_two_owners():
dm.deploy([owner_object])
dm.deploy([owner_2_object])

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand Down Expand Up @@ -330,10 +344,12 @@ def test_watch_thread_global_watch_two_owners():
@pytest.mark.timeout(5)
def test_watch_thread_no_watch():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})

with library_config(python_watch_manager={"filter": DisableFilter}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": DisableFilter}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
Expand All @@ -355,11 +371,13 @@ def test_watch_thread_no_watch():
@pytest.mark.timeout(5)
def test_watch_thread_not_leader():
dm = DryRunDeployManager()
mocked_reconcile_thread = MockedReconcileThread()
watched_object = make_resource(spec={"test": "value"})
watched_object_id = ResourceId.from_resource(watched_object)

with library_config(python_watch_manager={"filter": None}):
with library_config(
python_watch_manager={"process_context": "fork", "filter": None}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
leadership_manager=DisabledLeadershipManager(),
reconcile_thread=mocked_reconcile_thread,
Expand Down Expand Up @@ -390,30 +408,29 @@ def test_watch_thread_invalid_rbac():
with patch(
"oper8.watch_manager.python_watch_manager.threads.watch.sys.exit",
side_effect=Exception("EndTest"),
) as exit_mock:
with library_config(
python_watch_manager={
"filter": None,
"watch_retry_count": 3,
"watch_retry_delay": "0.1s",
}
):
watch_thread = WatchThread(
reconcile_thread=None,
kind="Foo",
api_version="foo.bar.com/v1",
namespace="test",
deploy_manager=dm,
)
request = WatchRequest(
watched=watched_object_id, requester=watched_object_id
)
watch_thread.request_watch(request)
watch_thread.start_thread()

# Wait for the retries
time.sleep(1)

# Assert we tried to watch 4 times (3 retries plus the initial)
assert dm.watch_objects.call_count == 4
assert exit_mock.called
) as exit_mock, library_config(
python_watch_manager={
"process_context": "fork",
"filter": None,
"watch_retry_count": 3,
"watch_retry_delay": "0.1s",
}
):
mocked_reconcile_thread = MockedReconcileThread()
watch_thread = WatchThread(
reconcile_thread=mocked_reconcile_thread,
kind="Foo",
api_version="foo.bar.com/v1",
namespace="test",
deploy_manager=dm,
)
request = WatchRequest(watched=watched_object_id, requester=watched_object_id)
watch_thread.request_watch(request)
watch_thread.start_thread()

# Wait for the retries
time.sleep(1)

# Assert we tried to watch 4 times (3 retries plus the initial)
assert dm.watch_objects.call_count == 4
assert exit_mock.called

0 comments on commit 83f81c7

Please sign in to comment.