def test_get_requirement_params(self): """ This @statmicmethod should return a dictionary. The function should work both with and without and application context. It retreives the relevant parameters from postgres, for consul. """ consul_mock = Mock(running_port=8500) postgres_mock = Mock(running_host='localhost', running_port=5437) redis_mock = Mock(running_host='localhost', running_port=6739) P = ConsulProvisioner(container=consul_mock, services=['adsws'], requirements={ 'postgres': postgres_mock, 'redis': redis_mock }) db_params = P.get_db_params() self.assertEqual(db_params['HOST'], '172.17.42.1') self.assertEqual(db_params['PORT'], 5437) cache_params = P.get_cache_params() self.assertEqual(cache_params['HOST'], '172.17.42.1') self.assertEqual(cache_params['PORT'], 6739)
def test_get_cli_params(self): """ This @staticmethod should return a string. The function should work both with and without an application context """ cli = ConsulProvisioner.get_cli_params() self.assertIsInstance(cli, basestring) with create_app().app_context(): self.assertEqual(cli, ConsulProvisioner.get_cli_params()) # Delete the requires config value to see if the method tries to # access it. Expect KeyError with self.assertRaises(KeyError): del current_app.config["DEPENDENCIES"]["CONSUL"] ConsulProvisioner.get_cli_params()
def test_discovers_services_from_templates(self): """ Provisioner should auto-discover which services it knows about. """ known_services = ["adsws"] discovered_services = ConsulProvisioner.known_services() self.assertEqual(known_services, discovered_services)
def test_get_database_params(self): """ This @statmicmethod should return a dictionary. The function should work both with and without and application context. It retreives the relevant parameters from postgres, for consul. """ db = ConsulProvisioner.get_db_params() self.assertIsInstance(db, dict) with create_app().app_context(): self.assertEqual(db, ConsulProvisioner.get_db_params()) # Delete the requires config value to see if the method tries to # access it. Expect KeyError with self.assertRaises(KeyError): del current_app.config["DEPENDENCIES"]["POSTGRES"] ConsulProvisioner.get_db_params()
def test_discovers_services_from_templates(self): """ Provisioner should auto-discover which services it knows about. """ known_services = ['adsws', 'biblib'] discovered_services = ConsulProvisioner.known_services() for item in known_services: self.assertIn(item, discovered_services)
def test_get_requirement_params(self): """ This @statmicmethod should return a dictionary. The function should work both with and without and application context. It retreives the relevant parameters from postgres, for consul. """ consul_mock = Mock(running_port=8500) postgres_mock = Mock(running_host='localhost', running_port=5437) redis_mock = Mock(running_host='localhost', running_port=6739) P = ConsulProvisioner(container=consul_mock, services=['adsws'], requirements={'postgres': postgres_mock, 'redis': redis_mock} ) db_params = P.get_db_params() self.assertEqual(db_params['HOST'], '172.17.42.1') self.assertEqual(db_params['PORT'], 5437) cache_params = P.get_cache_params() self.assertEqual(cache_params['HOST'], '172.17.42.1') self.assertEqual(cache_params['PORT'], 6739)
def test_templates(self): """ Test that the templates are rendered after init on a known service; The attribute self.services should be a dict with key,value = service, template the attribute directory should point to the base template directory """ services = ['adsws'] container = Mock(running_port=8500, running_host='localhost') P = ConsulProvisioner(services, container=container) self.assertIsInstance(P.services, dict) self.assertListEqual(services, P.services.keys()) for s in services: self.assertIsInstance(P.services[s], basestring) self.assertIn(s, P.services[s], msg="{} not in {}".format(s, P.services[s])) ends_with = 'templates/consul' self.assertTrue(P.directory.endswith(ends_with), msg='{} does not endwith {}'.format( P.directory, ends_with))
def _provision(self, service): """ Run the provision for a given service """ ConsulProvisioner(service, container=self.builder)()
def test_unknown_service(self): """ Consul should not provision config values for unknown services. """ with self.assertRaisesRegexp(UnknownServiceError, 'unknown-service'): ConsulProvisioner('unknown-service')