Exemplo n.º 1
0
def kopf_runner(kube):
    logger = logging.getLogger()
    old_handlers = logger.handlers[:]
    args = [
        "--verbose",
        "--standalone",
        "--namespace",
        kube._ns,
        "-m",
        "secret_sync.handlers",
    ]
    # Set the kopf watcher stream timeout to something small so we don't have
    # to wait too long at the end of the tests for all the background watcher
    # threads to end.
    settings = kopf.OperatorSettings()
    settings.watching.server_timeout = 1
    try:
        with KopfRunner(["run", *args], settings=settings) as runner:
            # Remove any extra log handlers that starting kopf may have added.
            # The built-in pytest log capture does what we need already.
            for handler in logger.handlers[:]:
                if handler not in old_handlers:
                    logger.removeHandler(handler)
            yield runner
    finally:
        # The runner captures all output, so print it for pytest to capture.
        print(runner.stdout)
Exemplo n.º 2
0
async def test_declared_public_interface_and_promised_defaults():
    settings = kopf.OperatorSettings()
    assert settings.posting.level == logging.INFO
    assert settings.peering.name == "default"
    assert settings.peering.stealth == False
    assert settings.peering.priority == 0
    assert settings.peering.lifetime == 60
    assert settings.peering.mandatory == False
    assert settings.peering.standalone == False
    assert settings.peering.namespaced == True
    assert settings.peering.clusterwide == False
    assert settings.watching.reconnect_backoff == 0.1
    assert settings.watching.connect_timeout is None
    assert settings.watching.server_timeout is None
    assert settings.watching.client_timeout is None
    assert settings.batching.worker_limit is None
    assert settings.batching.idle_timeout == 5.0
    assert settings.batching.exit_timeout == 2.0
    assert settings.batching.batch_window == 0.1
    assert settings.batching.error_delays == (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610)
    assert settings.scanning.disabled == False
    assert settings.admission.server is None
    assert settings.admission.managed is None
    assert settings.execution.executor is not None
    assert settings.execution.max_workers is None
Exemplo n.º 3
0
async def test_synchronous_calls_use_replaced_executor():
    settings = kopf.OperatorSettings()
    executor = CatchyExecutor()
    settings.execution.executor = executor

    mock = MagicMock()
    await invoke(fn=mock, settings=settings)

    assert mock.called
    assert len(executor.calls) == 1
Exemplo n.º 4
0
async def test_synchronous_executor_limit_is_applied():
    settings = kopf.OperatorSettings()
    assert hasattr(settings.execution.executor, '_max_workers')  # prerequisite

    assert settings.execution.max_workers is None  # as in "unset by us, assume defaults"
    assert settings.execution.executor._max_workers is not None  # usually CPU count * N.

    settings.execution.max_workers = 123456

    assert settings.execution.max_workers == 123456
    assert settings.execution.executor._max_workers == 123456
def test_startup(mocker):
    settings = kopf.OperatorSettings()
    mocker.patch("aws_auth.kopf.login_via_client")
    mocker.patch("aws_auth.get_protected_mapping")
    mocker.patch("aws_auth.get_config_map")
    mocker.patch("aws_auth.write_protected_mapping")
    aws_auth.get_protected_mapping.return_value = None
    aws_auth.startup(logger, settings=settings)
    aws_auth.get_protected_mapping.assert_called_once()
    aws_auth.get_config_map.assert_called_once()
    aws_auth.write_protected_mapping.assert_called_once()
Exemplo n.º 6
0
async def test_declared_public_interface_and_promised_defaults():
    settings = kopf.OperatorSettings()
    assert settings.posting.level == logging.INFO
    assert settings.watching.reconnect_backoff == 0.1
    assert settings.watching.connect_timeout is None
    assert settings.watching.server_timeout is None
    assert settings.watching.client_timeout is None
    assert settings.batching.worker_limit is None
    assert settings.batching.idle_timeout == 5.0
    assert settings.batching.exit_timeout == 2.0
    assert settings.batching.batch_window == 0.1
    assert settings.execution.executor is not None
    assert settings.execution.max_workers is None
Exemplo n.º 7
0
async def test_synchronous_calls_are_threaded():
    settings = kopf.OperatorSettings()
    thread = None

    def fn():
        nonlocal thread
        thread = threading.current_thread()

    mock = MagicMock(wraps=fn)
    await invoke(fn=mock, settings=settings)

    assert mock.called
    assert thread is not None  # remembered from inside fn()
    assert thread is not threading.current_thread()  # not in the main thread
Exemplo n.º 8
0
def create_stop_timer(logger, resource_claim, **kwargs):
    stop_timestamp = resource_claim.stop_timestamp
    if not stop_timestamp:
        return
    stop_datetime = isoparse(stop_timestamp)
    notification_timedelta = stop_datetime - datetime.now(
        timezone.utc) - timedelta(minutes=30, seconds=30)
    notification_interval = notification_timedelta.total_seconds()
    if notification_interval > 0:
        logger.info("scheduled stop notification in " +
                    naturaldelta(notification_timedelta))
        timer = Timer(notification_interval,
                      notify_scheduled_stop,
                      kwargs={
                          "logger":
                          kopf.LocalObjectLogger(
                              body=resource_claim.definition,
                              settings=kopf.OperatorSettings()),
                          "resource_claim":
                          resource_claim,
                          **kwargs
                      })
        stop_timers[resource_claim.uid] = timer
        timer.start()
Exemplo n.º 9
0
async def test_peering_clusterwide_is_modified_by_namespaced():
    settings = kopf.OperatorSettings()
    assert settings.peering.clusterwide == False
    settings.peering.namespaced = not settings.peering.namespaced
    assert settings.peering.clusterwide == True