def test_update_get_kind_call(mocked_get_kind: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_kind
    WHEN update is called with the body
    THEN get_kind is called with the body.
    """
    mock_body = mock.MagicMock()

    operations.update(body=mock_body, physical_name="physical name 1")

    mocked_get_kind.assert_called_once_with(body=mock_body)
def test_update_client_function_call(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function and False
        for namespaced and physical name
    WHEN update is called with the body and physical name
    THEN the client function is called with the body and name as the physical name.
    """
    mock_body = mock.MagicMock()
    mock_client_function = mock.MagicMock()
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )
    physical_name = "name 1"

    operations.update(body=mock_body, physical_name=physical_name)

    mock_client_function.assert_called_once_with(body=mock_body, name=physical_name)
def test_update_get_function_call(
    mocked_get_api_version: mock.MagicMock,
    mocked_get_kind: mock.MagicMock,
    mocked_get_function: mock.MagicMock,
):
    """
    GIVEN mocked get_api_version, get_kind and get_function
    WHEN update is called with the body
    THEN get_function is called with get_api_version and get_kind return value and the
        update operation.
    """
    operations.update(body=mock.MagicMock(), physical_name="physical name 1")

    mocked_get_function.assert_called_once_with(
        api_version=mocked_get_api_version.return_value,
        kind=mocked_get_kind.return_value,
        operation="update",
    )
def test_update_client_function_namespace_call(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function and True
        for namespaced and physical name with namespace and name
    WHEN update is called with the body and physical name
    THEN the client function is called with the body, name and physical name.
    """
    mock_body = mock.MagicMock()
    mock_client_function = mock.MagicMock()
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, True
    )
    name = "name 1"
    namespace = "namespace 1"

    operations.update(body=mock_body, physical_name=f"{namespace}/{name}")

    mock_client_function.assert_called_once_with(
        namespace=namespace, name=name, body=mock_body
    )
def test_update_get_kind_raises(mocked_get_kind: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_kind that raises KindMissingError
    WHEN update is called with the body
    THEN failure response is returned.
    """
    mocked_get_kind.side_effect = exceptions.KindMissingError

    return_value = operations.update(
        body=mock.MagicMock(), physical_name="physical name 1"
    )

    assert return_value == operations.ExistsReturn("FAILURE", "kind is required.")
def test_update_get_api_version_raises(mocked_get_api_version: mock.MagicMock):
    """
    GIVEN mocked get_api_version that raises ApiVersionMissingError
    WHEN update is called
    THEN failure response is returned.
    """
    mocked_get_api_version.side_effect = exceptions.ApiVersionMissingError

    return_value = operations.update(
        body=mock.MagicMock(), physical_name="physical name 1"
    )

    assert return_value == operations.ExistsReturn("FAILURE", "apiVersion is required.")
def test_update_client_function_return(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function that
        returns the metadata with a name and False for namespaced
    WHEN update is called
    THEN success response is returned.
    """
    mock_client_function = mock.MagicMock()
    mock_return = mock.MagicMock()
    mock_return.metadata.name = "name 1"
    mock_client_function.return_value = mock_return
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )

    return_value = operations.update(body=mock.MagicMock(), physical_name="name 1")

    assert return_value == operations.ExistsReturn("SUCCESS", None)
def test_update_client_function_raises(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function that
        raises ApiException and False for namespaced
    WHEN update is called
    THEN failure response is returned.
    """
    mock_client_function = mock.MagicMock()
    mock_client_function.side_effect = kubernetes.client.rest.ApiException(
        "400", "reason 1"
    )
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )

    return_value = operations.update(body=mock.MagicMock(), physical_name="name 1")

    assert return_value == operations.ExistsReturn(
        "FAILURE", "(400)\nReason: reason 1\n"
    )
def test_cluster_role_update(cluster_role_info, _cluster_role):
    """
    GIVEN cluster role as dictionary that has been created
    WHEN a label is added to the cluster role and update is called with the cluster role
        dictionary as a body and physical name
    THEN the cluster role is updated.
    """
    cluster_role_dict, name = cluster_role_info
    cluster_role_dict["metadata"]["labels"] = {"key": "value"}

    result = operations.update(body=cluster_role_dict, physical_name=name)

    # Check result
    assert result.status == "SUCCESS"
    # Check that the cluster_role is created
    rbac_v1_api = kubernetes.client.RbacAuthorizationV1Api()
    response = rbac_v1_api.list_cluster_role()
    assert response.items
    metadata_dict = {item.metadata.name: item.metadata for item in response.items}
    assert name in metadata_dict
    assert metadata_dict[name].labels["key"] == "value"
def test_deployment_update(nginx_deployment_info, _nginx_deployment):
    """
    GIVEN deployment as dictionary that has been created
    WHEN a label is added to the deployment and update is called with the deployment
        dictionary as a body and physical name
    THEN the deployment is updated.
    """
    deployment_dict, namespace, name = nginx_deployment_info
    deployment_dict["metadata"]["labels"] = {"key": "value"}

    result = operations.update(
        body=deployment_dict, physical_name=f"{namespace}/{name}"
    )

    # Check result
    assert result.status == "SUCCESS"
    # Check that the deployment is updated
    apps_v1_api = kubernetes.client.AppsV1Api()
    response = apps_v1_api.list_namespaced_deployment(namespace=namespace)
    assert response.items
    assert response.items[0].metadata.name == name
    assert response.items[0].metadata.labels["key"] == "value"