def test_metrics(monkeypatch): client = client_mock(monkeypatch) resp = MagicMock() resp.text = 'metrics' client.return_value.session.get.return_value = resp parsed = MagicMock() parsed.samples = [ ('metric-1', {}, 20.17), ('metric-2', {'verb': 'GET'}, 20.16), ('metric-1', {'verb': 'POST'}, 20.18) ] parser = MagicMock() parser.return_value = [parsed] monkeypatch.setattr('zmon_worker_monitor.builtins.plugins.kubernetes.text_string_to_metric_families', parser) k = KubernetesWrapper() metrics = k.metrics() expected = { 'metric-1': [({}, 20.17), ({'verb': 'POST'}, 20.18)], 'metric-2': [({'verb': 'GET'}, 20.16)], } assert metrics == expected parser.assert_called_with(resp.text) client.return_value.session.get.assert_called_with(CLUSTER_URL + '/metrics')
def test_namespaces(monkeypatch): client_mock(monkeypatch) res = [resource_mock({'metadata': {}})] ns = MagicMock() ns.objects.return_value.all.return_value = res monkeypatch.setattr('pykube.Namespace', ns) k = KubernetesWrapper() namespaces = k.namespaces() assert [r.obj for r in res] == namespaces
def test_namespaces(monkeypatch): client_mock(monkeypatch) res = [resource_mock({'metadata': {}})] ns = MagicMock() ns.objects.return_value.all.return_value = res monkeypatch.setattr('pykube.Namespace', ns) k = KubernetesWrapper() namespaces = k.namespaces() assert [r.obj for r in res] == namespaces
def test_nodes(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) node = MagicMock() node.objects.return_value.filter.return_value = res monkeypatch.setattr('pykube.Node', node) k = KubernetesWrapper() nodes = k.nodes(**kwargs) assert [r.obj for r in res] == nodes node.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_nodes(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) node = MagicMock() node.objects.return_value.filter.return_value = res monkeypatch.setattr('pykube.Node', node) k = KubernetesWrapper() nodes = k.nodes(**kwargs) assert [r.obj for r in res] == nodes node.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_persistentvolumes(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) persistentvolume = MagicMock() query = persistentvolume.objects.return_value.filter.return_value query.all.return_value = res monkeypatch.setattr('pykube.PersistentVolume', persistentvolume) k = KubernetesWrapper() persistentvolumes = k.persistentvolumes(**kwargs) assert [r.obj for r in res] == persistentvolumes persistentvolume.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_persistentvolumes(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) persistentvolume = MagicMock() query = persistentvolume.objects.return_value.filter.return_value query.all.return_value = res monkeypatch.setattr('pykube.PersistentVolume', persistentvolume) k = KubernetesWrapper() persistentvolumes = k.persistentvolumes(**kwargs) assert [r.obj for r in res] == persistentvolumes persistentvolume.objects.return_value.filter.assert_called_with( **filter_kwargs)
def test_metrics(monkeypatch): client = client_mock(monkeypatch) resp = MagicMock() resp.text = "metrics" client.session.get.return_value = resp parsed = MagicMock() parsed.samples = [ ("metric-1", {}, 20.17), ("metric-2", { "verb": "GET" }, 20.16), ("metric-1", { "verb": "POST" }, 20.18), ] parser = MagicMock() parser.return_value = [parsed] monkeypatch.setattr( "zmon_worker_monitor.builtins.plugins.kubernetes.text_string_to_metric_families", parser, ) k = KubernetesWrapper(check_id='<test>') metrics = k.metrics() expected = { "metric-1": [({}, 20.17), ({ "verb": "POST" }, 20.18)], "metric-2": [({ "verb": "GET" }, 20.16)], } assert metrics == expected parser.assert_called_with(resp.text) client.session.get.assert_called_with(CLUSTER_URL + "/metrics")
def test_deployments(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) deployment = MagicMock() query = deployment.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.Deployment', deployment) k = KubernetesWrapper() deployments = k.deployments(**kwargs) assert [r.obj for r in res] == deployments get_resources.assert_called_with(query) deployment.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_pods(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) pod = MagicMock() query = pod.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.Pod', pod) k = KubernetesWrapper() pods = k.pods(**kwargs) assert [r.obj for r in res] == pods get_resources.assert_called_with(query) pod.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_configmaps(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) configmap = MagicMock() query = configmap.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.ConfigMap', configmap) k = KubernetesWrapper() configmaps = k.configmaps(**kwargs) assert [r.obj for r in res] == configmaps get_resources.assert_called_with(query) configmap.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_cronjobs(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) cronjob = MagicMock() query = cronjob.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.CronJob', cronjob) k = KubernetesWrapper() pods = k.cronjobs(**kwargs) assert [r.obj for r in res] == pods get_resources.assert_called_with(query) cronjob.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_resourcequotas(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) resourcequota = MagicMock() query = resourcequota.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.ResourceQuota', resourcequota) query.all.return_value = res k = KubernetesWrapper() res_quotas = k.resourcequotas(**kwargs) assert [r.obj for r in res] == res_quotas resourcequota.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_configmaps(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) configmap = MagicMock() query = configmap.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.ConfigMap', configmap) k = KubernetesWrapper() configmaps = k.configmaps(**kwargs) assert [r.obj for r in res] == configmaps get_resources.assert_called_with(query) configmap.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_deployments(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) deployment = MagicMock() query = deployment.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.Deployment', deployment) k = KubernetesWrapper() deployments = k.deployments(**kwargs) assert [r.obj for r in res] == deployments get_resources.assert_called_with(query) deployment.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_ingresses(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) ingress = MagicMock() query = ingress.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.Ingress', ingress) k = KubernetesWrapper() ingresses = k.ingresses(**kwargs) assert [r.obj for r in res] == ingresses get_resources.assert_called_with(query) ingress.objects.return_value.filter.assert_called_with(**filter_kwargs)
def test_persistentvolumeclaims(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) persistentvolumeclaim = MagicMock() query = persistentvolumeclaim.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.PersistentVolumeClaim', persistentvolumeclaim) k = KubernetesWrapper() persistentvolumeclaims = k.persistentvolumeclaims(**kwargs) assert [r.obj for r in res] == persistentvolumeclaims get_resources.assert_called_with(query) persistentvolumeclaim.objects.return_value.filter.assert_called_with( **filter_kwargs)
def test_get_resources(monkeypatch, kwargs, ns, q, res): client_mock(monkeypatch) query = MagicMock() query.filter.return_value = q namespaces = MagicMock() namespaces.return_value = ns monkeypatch.setattr('zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper.namespaces', namespaces) k = KubernetesWrapper(**kwargs) result = k._get_resources(query) assert res == result if 'namespace' in kwargs and kwargs['namespace'] is None: calls = [call(namespace=n['metadata']['name']) for n in ns] query.filter.assert_has_calls(calls, any_order=True) else: query.filter.assert_called_with(namespace='default')
def test_metrics(monkeypatch): client = client_mock(monkeypatch) resp = MagicMock() resp.text = 'metrics' client.return_value.session.get.return_value = resp parsed = MagicMock() parsed.samples = [('metric-1', {}, 20.17), ('metric-2', { 'verb': 'GET' }, 20.16), ('metric-1', { 'verb': 'POST' }, 20.18)] parser = MagicMock() parser.return_value = [parsed] monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.text_string_to_metric_families', parser) k = KubernetesWrapper() metrics = k.metrics() expected = { 'metric-1': [({}, 20.17), ({ 'verb': 'POST' }, 20.18)], 'metric-2': [({ 'verb': 'GET' }, 20.16)], } assert metrics == expected parser.assert_called_with(resp.text) client.return_value.session.get.assert_called_with(CLUSTER_URL + '/metrics')
def test_resourcequotas(monkeypatch, kwargs, filter_kwargs, res): client_mock(monkeypatch) get_resources = get_resources_mock(res) resourcequota = MagicMock() query = resourcequota.objects.return_value.filter.return_value monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper._get_resources', get_resources) monkeypatch.setattr('pykube.ResourceQuota', resourcequota) query.all.return_value = res k = KubernetesWrapper() res_quotas = k.resourcequotas(**kwargs) assert [r.obj for r in res] == res_quotas resourcequota.objects.return_value.filter.assert_called_with( **filter_kwargs)
def test_get_resources(monkeypatch, kwargs, ns, q, res): client_mock(monkeypatch) query = MagicMock() query.filter.return_value = q namespaces = MagicMock() namespaces.return_value = ns monkeypatch.setattr( 'zmon_worker_monitor.builtins.plugins.kubernetes.KubernetesWrapper.namespaces', namespaces) k = KubernetesWrapper(**kwargs) result = k._get_resources(query) assert res == result if 'namespace' in kwargs and kwargs['namespace'] is None: calls = [call(namespace=n['metadata']['name']) for n in ns] query.filter.assert_has_calls(calls, any_order=True) else: query.filter.assert_called_with(namespace='default')
def __init__(self, monkeypatch, kind, namespace, resources, owning_module="pykube"): self._query = MagicMock(name="query") self._object_manager = MagicMock(name="object_manager") self._object_manager.objects.return_value = self._query self._get_resources = MagicMock("get_resources") self._get_resources.return_value = resources monkeypatch.setattr("{}.{}".format(owning_module, kind), self._object_manager) monkeypatch.setattr( "zmon_worker_monitor.builtins.plugins.kubernetes._get_resources", self._get_resources, ) self._client = client_mock(monkeypatch) self.wrapper = KubernetesWrapper(namespace=namespace, check_id='<test>')
def test_deployments_error(monkeypatch, kwargs): k = KubernetesWrapper() with pytest.raises(CheckError): k.deployments(**kwargs)
def test_pods_error(kwargs): k = KubernetesWrapper(check_id='<test>') with pytest.raises(CheckError): k.pods(**kwargs)
def test_pods_error(monkeypatch, kwargs): k = KubernetesWrapper() with pytest.raises(CheckError): k.pods(**kwargs)
def test_deployments_error(kwargs): k = KubernetesWrapper(check_id='<test>') with pytest.raises(CheckError): k.deployments(**kwargs)
def test_pods_error(monkeypatch, kwargs): k = KubernetesWrapper() with pytest.raises(CheckError): k.pods(**kwargs)
def test_deployments_error(monkeypatch, kwargs): k = KubernetesWrapper() with pytest.raises(CheckError): k.deployments(**kwargs)