Beispiel #1
0
def test_reconcile_kubernetes_resource():
    with mock.patch(
        "paasta_tools.setup_kubernetes_cr.format_custom_resource", autospec=True
    ) as mock_format_custom_resource, mock.patch(
        "paasta_tools.setup_kubernetes_cr.create_custom_resource", autospec=True
    ) as mock_create_custom_resource, mock.patch(
        "paasta_tools.setup_kubernetes_cr.update_custom_resource", autospec=True
    ) as mock_update_custom_resource:
        mock_kind = mock.Mock(singular="flink", plural="flinks")
        mock_custom_resources = [
            KubeCustomResource(
                service="kurupt",
                instance="fm",
                config_sha="conf123",
                kind="flink",
                name="foo",
                namespace="paasta-flinks",
            )
        ]
        mock_client = mock.Mock()
        # no instances, do nothing
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service="mc",
            instance_configs={},
            cluster="mycluster",
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version="v1",
            group="yelp.com",
        )
        assert not mock_create_custom_resource.called
        assert not mock_update_custom_resource.called

        # instance up to date, do nothing
        mock_format_custom_resource.return_value = {
            "metadata": {
                "labels": {
                    "yelp.com/paasta_config_sha": "conf123",
                    "paasta.yelp.com/config_sha": "conf123",
                },
                "name": "foo",
                "namespace": "paasta-flinks",
            }
        }
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service="kurupt",
            instance_configs={"fm": {"some": "config"}},
            cluster="cluster",
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version="v1",
            group="yelp.com",
        )
        assert not mock_create_custom_resource.called
        assert not mock_update_custom_resource.called

        # instance diff config, update
        mock_format_custom_resource.return_value = {
            "metadata": {
                "labels": {"yelp.com/paasta_config_sha": "conf456"},
                "name": "foo",
                "namespace": "paasta-flinks",
            }
        }
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service="kurupt",
            instance_configs={"fm": {"some": "config"}},
            cluster="mycluster",
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version="v1",
            group="yelp.com",
        )
        assert not mock_create_custom_resource.called
        mock_update_custom_resource.assert_called_with(
            kube_client=mock_client,
            name="kurupt-fm",
            version="v1",
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group="yelp.com",
        )

        # instance not exist, create
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service="mc",
            instance_configs={"grindah": {"some": "conf"}},
            cluster="mycluster",
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version="v1",
            group="yelp.com",
        )
        mock_create_custom_resource.assert_called_with(
            kube_client=mock_client,
            version="v1",
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group="yelp.com",
        )

        # instance not exist, create but error with k8s
        mock_create_custom_resource.side_effect = Exception
        assert not setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service="mc",
            instance_configs={"grindah": {"some": "conf"}},
            cluster="mycluster",
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version="v1",
            group="yelp.com",
        )
        mock_create_custom_resource.assert_called_with(
            kube_client=mock_client,
            version="v1",
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group="yelp.com",
        )
Beispiel #2
0
def test_reconcile_kubernetes_resource():
    with mock.patch(
            'paasta_tools.setup_kubernetes_cr.format_custom_resource',
            autospec=True,
    ) as mock_format_custom_resource, mock.patch(
            'paasta_tools.setup_kubernetes_cr.create_custom_resource',
            autospec=True,
    ) as mock_create_custom_resource, mock.patch(
            'paasta_tools.setup_kubernetes_cr.update_custom_resource',
            autospec=True,
    ) as mock_update_custom_resource:
        mock_kind = mock.Mock(singular='flinkcluster')
        mock_custom_resources = [
            KubeCustomResource(
                service='kurupt',
                instance='fm',
                config_sha='conf123',
                kind='flinkcluster',
            ),
        ]
        mock_client = mock.Mock()
        # no instances, do nothing
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service='mc',
            instance_configs={},
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version='v1',
            group='yelp.com',
        )
        assert not mock_create_custom_resource.called
        assert not mock_update_custom_resource.called

        # instance up to date, do nothing
        mock_format_custom_resource.return_value = {
            'metadata': {
                'labels': {
                    'yelp.com/paasta_config_sha': 'conf123',
                },
            },
        }
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service='kurupt',
            instance_configs={'fm': {
                'some': 'config'
            }},
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version='v1',
            group='yelp.com',
        )
        assert not mock_create_custom_resource.called
        assert not mock_update_custom_resource.called

        # instance diff config, update
        mock_format_custom_resource.return_value = {
            'metadata': {
                'labels': {
                    'yelp.com/paasta_config_sha': 'conf456',
                },
            },
        }
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service='kurupt',
            instance_configs={'fm': {
                'some': 'config'
            }},
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version='v1',
            group='yelp.com',
        )
        assert not mock_create_custom_resource.called
        mock_update_custom_resource.assert_called_with(
            kube_client=mock_client,
            name='kurupt-fm',
            version='v1',
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group='yelp.com',
        )

        # instance not exist, create
        assert setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service='mc',
            instance_configs={'grindah': {
                'some': 'conf'
            }},
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version='v1',
            group='yelp.com',
        )
        mock_create_custom_resource.assert_called_with(
            kube_client=mock_client,
            version='v1',
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group='yelp.com',
        )

        # instance not exist, create but error with k8s
        mock_create_custom_resource.side_effect = Exception
        assert not setup_kubernetes_cr.reconcile_kubernetes_resource(
            kube_client=mock_client,
            service='mc',
            instance_configs={'grindah': {
                'some': 'conf'
            }},
            custom_resources=mock_custom_resources,
            kind=mock_kind,
            version='v1',
            group='yelp.com',
        )
        mock_create_custom_resource.assert_called_with(
            kube_client=mock_client,
            version='v1',
            kind=mock_kind,
            formatted_resource=mock_format_custom_resource.return_value,
            group='yelp.com',
        )