def test_ensure_pod_disruption_budget_noop_when_min_available_is_set(): with mock.patch( "paasta_tools.kubernetes.application.controller_wrappers.pod_disruption_budget_for_service_instance", autospec=True, ) as mock_pdr_for_service_instance: mock_req_pdr = mock.Mock() mock_req_pdr.spec.max_unavailable = 10 mock_pdr_for_service_instance.return_value = mock_req_pdr mock_client = mock.MagicMock() mock_pdr = mock.Mock() mock_pdr.spec.max_unavailable = 5 mock_pdr.spec.min_available = 5 mock_client.policy.read_namespaced_pod_disruption_budget.return_value = mock_pdr app = mock.MagicMock() app.soa_config.get_bounce_margin_factor.return_value = 0.1 app.kube_deployment.service.return_value = "fake_service" app.kube_deployment.instance.return_value = "fake_instance" Application.ensure_pod_disruption_budget(self=app, kube_client=mock_client) mock_client.policy.patch_namespaced_pod_disruption_budget.assert_not_called( )
def test_ensure_pod_disruption_budget_replaces_outdated( mock_pdr_for_service_instance, mock_load_system_paasta_config): mock_req_pdr = mock.Mock() mock_req_pdr.spec.max_unavailable = 10 mock_pdr_for_service_instance.return_value = mock_req_pdr mock_client = mock.MagicMock() mock_pdr = mock.Mock() mock_pdr.spec.max_unavailable = 5 mock_pdr.spec.min_available = None mock_client.policy.read_namespaced_pod_disruption_budget.return_value = mock_pdr app = mock.MagicMock() app.soa_config.get_bounce_margin_factor.return_value = 0.1 app.kube_deployment.service.return_value = "fake_service" app.kube_deployment.instance.return_value = "fake_instance" Application.ensure_pod_disruption_budget(self=app, kube_client=mock_client) mock_client.policy.patch_namespaced_pod_disruption_budget.assert_called_once_with( name=mock_req_pdr.metadata.name, namespace=mock_req_pdr.metadata.namespace, body=mock_req_pdr, )
def test_ensure_pod_disruption_budget_create( bounce_margin_factor_set, mock_pdr_for_service_instance, mock_load_system_paasta_config, ): mock_load_system_paasta_config.return_value.get_pdb_max_unavailable.return_value = 3 mock_req_pdr = mock.Mock() mock_req_pdr.spec.max_unavailable = 10 if bounce_margin_factor_set else 3 mock_pdr_for_service_instance.return_value = mock_req_pdr mock_client = mock.MagicMock() mock_client.policy.read_namespaced_pod_disruption_budget.side_effect = ApiException( status=404) app = mock.MagicMock() if bounce_margin_factor_set: app.soa_config.config_dict = {"bounce_margin_factor": 0.1} app.soa_config.get_bounce_margin_factor.return_value = 0.1 app.kube_deployment.service.return_value = "fake_service" app.kube_deployment.instance.return_value = "fake_instance" Application.ensure_pod_disruption_budget(self=app, kube_client=mock_client) mock_client.policy.create_namespaced_pod_disruption_budget.assert_called_once_with( namespace="paasta", body=mock_req_pdr)
def test_ensure_pod_disruption_budget_create(): with mock.patch( "paasta_tools.kubernetes.application.controller_wrappers.pod_disruption_budget_for_service_instance", autospec=True, ) as mock_pdr_for_service_instance: mock_req_pdr = mock.Mock() mock_req_pdr.spec.max_unavailable = 10 mock_pdr_for_service_instance.return_value = mock_req_pdr mock_client = mock.MagicMock() mock_client.policy.read_namespaced_pod_disruption_budget.side_effect = ApiException( status=404) app = mock.MagicMock() app.soa_config.get_bounce_margin_factor.return_value = 0.1 app.kube_deployment.service.return_value = "fake_service" app.kube_deployment.instance.return_value = "fake_instance" Application.ensure_pod_disruption_budget(self=app, kube_client=mock_client) mock_client.policy.create_namespaced_pod_disruption_budget.assert_called_once_with( namespace="paasta", body=mock_req_pdr)