11import re
22from pathlib import Path
33from typing import Any , Dict , List , Optional , Set
4- from unittest .mock import MagicMock , patch
4+ from unittest .mock import MagicMock , patch , PropertyMock
55
66import pytest
77from operator_repo import Repo
@@ -291,9 +291,7 @@ def test_metadata_container_image_validation(
291291 assert expected_successes .intersection (collected_results .keys ()) == set ()
292292
293293
294- @patch ("operator_repo.core.Operator.config" )
295- def test_check_dangling_bundles (mock_config : MagicMock , tmp_path : Path ) -> None :
296- mock_config .get .return_value = "replaces-mode"
294+ def test_check_dangling_bundles (tmp_path : Path ) -> None :
297295 create_files (
298296 tmp_path ,
299297 bundle_files ("hello" , "0.0.1" ),
@@ -303,16 +301,23 @@ def test_check_dangling_bundles(mock_config: MagicMock, tmp_path: Path) -> None:
303301 repo = Repo (tmp_path )
304302 operator = repo .operator ("hello" )
305303 bundle3 = operator .bundle ("0.0.3" )
306- failures = list (check_dangling_bundles (bundle3 ))
307- assert failures == []
308304
309- mock_config .get .return_value = "unknown-mode"
310- is_loop = list (check_dangling_bundles (bundle3 ))
311- assert is_loop == [
312- Fail ("Operator(hello): unsupported updateGraph value: unknown-mode" )
313- ]
305+ with patch .object (
306+ type (operator ), "config" , new_callable = PropertyMock
307+ ) as mock_config :
308+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
309+ failures = list (check_dangling_bundles (bundle3 ))
310+ assert failures == []
311+
312+ with patch .object (
313+ type (operator ), "config" , new_callable = PropertyMock
314+ ) as mock_config :
315+ mock_config .return_value = {"updateGraph" : "unknown-mode" }
316+ is_loop = list (check_dangling_bundles (bundle3 ))
317+ assert is_loop == [
318+ Fail ("Operator(hello): unsupported updateGraph value: unknown-mode" )
319+ ]
314320
315- mock_config .get .return_value = "replaces-mode"
316321 # Bundle 0.0.2 is not referenced by any bundle and it is not a HEAD of channel
317322 create_files (
318323 tmp_path ,
@@ -323,11 +328,16 @@ def test_check_dangling_bundles(mock_config: MagicMock, tmp_path: Path) -> None:
323328 repo = Repo (tmp_path )
324329 operator = repo .operator ("hello" )
325330 bundle3 = operator .bundle ("0.0.3" )
326- failures = list (check_dangling_bundles (bundle3 ))
327- assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
328- assert (
329- failures [0 ].reason == "Channel beta has dangling bundles: {Bundle(hello/0.0.2)}"
330- )
331+ with patch .object (
332+ type (operator ), "config" , new_callable = PropertyMock
333+ ) as mock_config :
334+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
335+ failures = list (check_dangling_bundles (bundle3 ))
336+ assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
337+ assert (
338+ failures [0 ].reason
339+ == "Channel beta has dangling bundles: {Bundle(hello/0.0.2)}"
340+ )
331341
332342 # Malformed .spec.replaces
333343 create_files (
@@ -338,9 +348,16 @@ def test_check_dangling_bundles(mock_config: MagicMock, tmp_path: Path) -> None:
338348 repo = Repo (tmp_path )
339349 operator = repo .operator ("malformed" )
340350 bundle1 = operator .bundle ("0.0.1" )
341- failures = list (check_dangling_bundles (bundle1 ))
342- assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
343- assert "Bundle(malformed/0.0.1) has invalid 'replaces' field:" in failures [0 ].reason
351+ with patch .object (
352+ type (operator ), "config" , new_callable = PropertyMock
353+ ) as mock_config :
354+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
355+ failures = list (check_dangling_bundles (bundle1 ))
356+ assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
357+ assert (
358+ "Bundle(malformed/0.0.1) has invalid 'replaces' field:"
359+ in failures [0 ].reason
360+ )
344361
345362
346363@pytest .mark .parametrize (
@@ -444,9 +461,7 @@ def test_check_api_version_constraints(
444461 assert set (check_api_version_constraints (bundle )) == expected
445462
446463
447- @patch ("operator_repo.core.Operator.config" )
448- def test_check_upgrade_graph_loop (mock_config : MagicMock , tmp_path : Path ) -> None :
449- mock_config .get .return_value = "replaces-mode"
464+ def test_check_upgrade_graph_loop (tmp_path : Path ) -> None :
450465 create_files (
451466 tmp_path ,
452467 bundle_files ("hello" , "0.0.1" ),
@@ -456,16 +471,22 @@ def test_check_upgrade_graph_loop(mock_config: MagicMock, tmp_path: Path) -> Non
456471 repo = Repo (tmp_path )
457472 operator = repo .operator ("hello" )
458473 bundle = operator .bundle ("0.0.1" )
459- is_loop = list (check_upgrade_graph_loop (bundle ))
460- assert is_loop == []
461-
462- mock_config .get .return_value = "unknown-mode"
463- is_loop = list (check_upgrade_graph_loop (bundle ))
464- assert is_loop == [
465- Fail ("Operator(hello): unsupported updateGraph value: unknown-mode" )
466- ]
474+ with patch .object (
475+ type (operator ), "config" , new_callable = PropertyMock
476+ ) as mock_config :
477+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
478+ is_loop = list (check_upgrade_graph_loop (bundle ))
479+ assert is_loop == []
480+
481+ with patch .object (
482+ type (operator ), "config" , new_callable = PropertyMock
483+ ) as mock_config :
484+ mock_config .return_value = {"updateGraph" : "unknown-mode" }
485+ is_loop = list (check_upgrade_graph_loop (bundle ))
486+ assert is_loop == [
487+ Fail ("Operator(hello): unsupported updateGraph value: unknown-mode" )
488+ ]
467489
468- mock_config .get .return_value = "replaces-mode"
469490 # Both bundles replace each other
470491 create_files (
471492 tmp_path ,
@@ -476,13 +497,17 @@ def test_check_upgrade_graph_loop(mock_config: MagicMock, tmp_path: Path) -> Non
476497 repo = Repo (tmp_path )
477498 operator = repo .operator ("hello" )
478499 bundle = operator .bundle ("0.0.1" )
479- is_loop = list (check_upgrade_graph_loop (bundle ))
480- assert len (is_loop ) == 1 and isinstance (is_loop [0 ], Fail )
481- assert (
482- is_loop [0 ].reason
483- == "Upgrade graph loop detected for bundle: [Bundle(hello/0.0.1), "
484- "Bundle(hello/0.0.2), Bundle(hello/0.0.1)]"
485- )
500+ with patch .object (
501+ type (operator ), "config" , new_callable = PropertyMock
502+ ) as mock_config :
503+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
504+ is_loop = list (check_upgrade_graph_loop (bundle ))
505+ assert len (is_loop ) == 1 and isinstance (is_loop [0 ], Fail )
506+ assert (
507+ is_loop [0 ].reason
508+ == "Upgrade graph loop detected for bundle: [Bundle(hello/0.0.1), "
509+ "Bundle(hello/0.0.2), Bundle(hello/0.0.1)]"
510+ )
486511
487512 # Malformed .spec.replaces
488513 create_files (
@@ -493,9 +518,16 @@ def test_check_upgrade_graph_loop(mock_config: MagicMock, tmp_path: Path) -> Non
493518 repo = Repo (tmp_path )
494519 operator = repo .operator ("malformed" )
495520 bundle = operator .bundle ("0.0.1" )
496- failures = list (check_upgrade_graph_loop (bundle ))
497- assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
498- assert "Bundle(malformed/0.0.1) has invalid 'replaces' field:" in failures [0 ].reason
521+ with patch .object (
522+ type (operator ), "config" , new_callable = PropertyMock
523+ ) as mock_config :
524+ mock_config .return_value = {"updateGraph" : "replaces-mode" }
525+ failures = list (check_upgrade_graph_loop (bundle ))
526+ assert len (failures ) == 1 and isinstance (failures [0 ], Fail )
527+ assert (
528+ "Bundle(malformed/0.0.1) has invalid 'replaces' field:"
529+ in failures [0 ].reason
530+ )
499531
500532
501533def test_check_replaces_availability_no_replaces (
0 commit comments