def setUp(self): initialize_database(":memory:", reset=True) self.source = BaseBootstrapSource() def lookup_host(hostname): return { 'the.host': { 'hostname': 'the.host', 'uses_ssl': True, 'cdn': 'thecdn' }, 'missingcdn.host': { 'hostname': 'missingcdn.host', 'cdn': 'missingcdn' }, 'invalid.host': { 'hostname': 'invalid.host', 'uses_ssl': True, }, 'mismatch.host': { 'hostname': 'the.host', 'uses_ssl': True, 'cdn': 'thecdn' }, 'badcdn.host': { 'hostname': 'badcdn.host', 'cdn': 'invalidcdn' } }.get(hostname, None) def lookup_cdn(cdn_id): return { 'thecdn': { 'id': 'thecdn', 'name': 'The CDN', 'edge_server': '1.2.3.4' }, 'invalidcdn': { 'name': 'The CDN', 'edge_server': '1.2.3.4' }, }.get(cdn_id, None) self.source.lookup_host = lookup_host self.source.lookup_cdn = lookup_cdn self.bootstrapper = Bootstrapper() self.bootstrapper.add_source(self.source)
def cachebrowser(click_context, config, verbose, reset_db, dev, **kwargs): settings = DevelopmentSettings() if dev else ProductionSettings() if config is None and os.path.isfile(settings.data_path('config.yaml')): config = open(settings.data_path('config.yaml')) try: settings.update_with_settings_file(config) settings.update_with_args(kwargs) settings.validate() except SettingsValidationError as e: print("Error parsing settings") print(e.message) sys.exit(1) initialize_logging(verbose) if not dev: check_data_files(settings) logger.debug("Initializing database {}".format(settings.database)) initialize_database(settings.database, reset_db) logger.debug("Initializing bootstrapper") bootstrapper = Bootstrapper(settings) context = Context() context.bootstrapper = bootstrapper context.settings = settings context.click = click_context click_context.obj = context if click_context.invoked_subcommand is None: logger.debug("No command specified, starting CacheBrowser server") click_context.invoke(start_cachebrowser_server)
class BootstrapperTest(unittest.TestCase): def setUp(self): initialize_database(":memory:", reset=True) self.source = BaseBootstrapSource() def lookup_host(hostname): return { 'the.host': { 'hostname': 'the.host', 'uses_ssl': True, 'cdn': 'thecdn' }, 'missingcdn.host': { 'hostname': 'missingcdn.host', 'cdn': 'missingcdn' }, 'invalid.host': { 'hostname': 'invalid.host', 'uses_ssl': True, }, 'mismatch.host': { 'hostname': 'the.host', 'uses_ssl': True, 'cdn': 'thecdn' }, 'badcdn.host': { 'hostname': 'badcdn.host', 'cdn': 'invalidcdn' } }.get(hostname, None) def lookup_cdn(cdn_id): return { 'thecdn': { 'id': 'thecdn', 'name': 'The CDN', 'edge_server': '1.2.3.4' }, 'invalidcdn': { 'name': 'The CDN', 'edge_server': '1.2.3.4' }, }.get(cdn_id, None) self.source.lookup_host = lookup_host self.source.lookup_cdn = lookup_cdn self.bootstrapper = Bootstrapper() self.bootstrapper.add_source(self.source) def test_bootstrap(self): self.bootstrapper.bootstrap('the.host') self.assertTrue(Host.select().where(Host.hostname == 'the.host').exists()) self.assertTrue(CDN.select().where(CDN.id == 'thecdn').exists()) def test_unavailable_host__host_not_in_db(self): with self.assertRaises(HostNotAvailableError): self.bootstrapper.bootstrap('doesnt.exist') def test_unavailable_host__host_in_db__unavailable_cdn__cdn_in_db_with_no_edge(self): cdn = CDN.create(id="missingcdn") Host.create(hostname="host.db", cdn=cdn) with self.assertRaises(CDNNotAvailableError): self.bootstrapper.bootstrap('missingcdn.host') def test_unavailable_host__host_in_db__available_cdn__cdn_in_db_with_no_edge(self): cdn = CDN.create(id="thecdn") Host.create(hostname="host.db", cdn=cdn) self.bootstrapper.bootstrap('host.db') def test_invalid_hostname(self): with self.assertRaises(BootstrapValidationError): self.bootstrapper.bootstrap('http://someinvalid.host') def test_invalid_host_data(self): with self.assertRaises(BootstrapValidationError): self.bootstrapper.bootstrap('invalid.host') def test_hostname_mismatch(self): with self.assertRaises(BootstrapValidationError): self.bootstrapper.bootstrap('mismatch.host') def test_invalid_cdn_data(self): with self.assertRaises(BootstrapValidationError): self.bootstrapper.bootstrap('badcdn.host')