def test_pod_to_service_no_match(self):
     jsons = self._load_json_array(['service_cache_services2.json'])
     with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
         mapper = PodServiceMapper(self.kube)
         mapper._fill_services_cache()
         no_match = self._build_pod_metadata(0, {'app': 'unknown'})
         self.assertEqual(0, len(mapper.match_services_for_pod(no_match)))
 def test_pod_to_service_no_match(self):
     jsons = self._load_json_array(['service_cache_services2.json'])
     with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
         mapper = PodServiceMapper(self.kube)
         mapper._fill_services_cache()
         no_match = self._build_pod_metadata(0, {'app': 'unknown'})
         self.assertEqual(0, len(mapper.match_services_for_pod(no_match)))
    def test_403_disable(self):
        exception403 = requests.exceptions.HTTPError()
        exception403.response = Mock()
        exception403.response.status_code = 403
        self.assertEquals(403, exception403.response.status_code)
        self.assertTrue(isinstance(exception403,
                                   requests.exceptions.HTTPError))

        with patch.object(self.kube,
                          'retrieve_json_auth',
                          side_effect=exception403) as request_mock:
            # Fill pod label cache
            mapper = PodServiceMapper(self.kube)
            self.assertEqual(0, mapper._403_errors)

            for i in range(0, MAX_403_RETRIES):
                self.assertFalse(mapper._403_disable)
                mapper._fill_services_cache()

            self.assertTrue(mapper._403_disable)

            # No new requests to the apiserver
            request_mock.assert_called()
            request_mock.reset_mock()
            mapper._fill_services_cache()
            request_mock.assert_not_called()
 def test_service_cache_invalidation_true(self):
     jsons = self._load_json_array(
         ['service_cache_events1.json', 'service_cache_services1.json', 'service_cache_events2.json'])
     with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
         mapper = PodServiceMapper(self.kube)
         mapper._fill_services_cache()
         mapper.check_services_cache_freshness()
         self.assertEqual(True, mapper._service_cache_invalidated)
Example #5
0
 def test_service_cache_invalidation_true(self):
     jsons = self._load_json_array([
         'service_cache_events1.json', 'service_cache_services1.json',
         'service_cache_events2.json'
     ])
     with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
         mapper = PodServiceMapper(self.kube)
         mapper._fill_services_cache()
         mapper.check_services_cache_freshness()
         self.assertEqual(True, mapper._service_cache_invalidated)
    def test_service_cache_fill(self):
        jsons = self._load_json_array(['service_cache_services2.json'])
        with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
            mapper = PodServiceMapper(self.kube)
            mapper._fill_services_cache()
        # Kubernetes service not imported because no selector
        self.assertEqual(3, len(mapper._service_cache_selectors))
        self.assertEqual(3, len(mapper._service_cache_names))

        self.assertEqual('redis-hello', mapper._service_cache_names['9474d98a-1aad-11e7-8b67-42010a840226'])
        redis = mapper._service_cache_selectors['9474d98a-1aad-11e7-8b67-42010a840226']
        self.assertEqual(2, len(redis))
        self.assertEqual('hello', redis['app'])
        self.assertEqual('db', redis['tier'])
    def test_service_cache_fill(self):
        jsons = self._load_json_array(['service_cache_services2.json'])
        with patch.object(self.kube, 'retrieve_json_auth', side_effect=jsons):
            mapper = PodServiceMapper(self.kube)
            mapper._fill_services_cache()
        # Kubernetes service not imported because no selector
        self.assertEqual(3, len(mapper._service_cache_selectors))
        self.assertEqual(3, len(mapper._service_cache_names))

        self.assertEqual('redis-hello', mapper._service_cache_names['9474d98a-1aad-11e7-8b67-42010a840226'])
        redis = mapper._service_cache_selectors['9474d98a-1aad-11e7-8b67-42010a840226']
        self.assertEqual(2, len(redis))
        self.assertEqual('hello', redis['app'])
        self.assertEqual('db', redis['tier'])
    def test_403_disable(self):
        exception403 = requests.exceptions.HTTPError()
        exception403.response = Mock()
        exception403.response.status_code = 403
        self.assertEquals(403, exception403.response.status_code)
        self.assertTrue(isinstance(exception403, requests.exceptions.HTTPError))

        with patch.object(self.kube, 'retrieve_json_auth', side_effect=exception403) as request_mock:
            # Fill pod label cache
            mapper = PodServiceMapper(self.kube)
            self.assertEqual(0, mapper._403_errors)

            for i in range(0, MAX_403_RETRIES):
                self.assertFalse(mapper._403_disable)
                mapper._fill_services_cache()

            self.assertTrue(mapper._403_disable)

            # No new requests to the apiserver
            request_mock.assert_called()
            request_mock.reset_mock()
            mapper._fill_services_cache()
            request_mock.assert_not_called()