def create_app(environment, port): flask = Flask(__name__) flask.config.from_pyfile('config.py') config_loader = ConfigLoader(verify=False) info = config_loader.load_application_info("./") config = config_loader.load_config("resources/", environment) view_util.navigation_bar = navigation_bar view_util.app_name = info['name'] status.blueprint.navigation_bar = navigation_bar status.blueprint.info = info status.blueprint.environment = environment status.blueprint.port = port status.blueprint.start_time = Delorean.now() mongo = MongoConnect(config) state.mongo = mongo views.blueprint.mongo = mongo views.blueprint.config = config['app'] api.blueprint.mongo = mongo api.blueprint.config = config['app'] flask.register_blueprint(status.blueprint) flask.register_blueprint(views.blueprint) flask.register_blueprint(api.blueprint) return flask
def create_app(port, environment, working_dir, greedy_mode): flask = Flask(__name__) flask.config.from_pyfile('config.py') flask.jinja_env.filters['ceil'] = view_util.ceil config_loader = ConfigLoader(verify=False) config.info = config_loader.load_application_info("./") config.config = config_loader.load_config("resources/", environment, fill_with_defaults=True) config.rdb = redislite.Redis(working_dir + 'redis.db') config.rdb.flushall() config.rdb.flushdb() start_tasks(config.config, greedy_mode) register_status_page(flask, config.rdb, config.info, environment, port) flask.register_blueprint(views.blueprint) flask.register_blueprint(styleguide.blueprint) return flask
class TestConfig(unittest.TestCase): @classmethod def setUpClass(cls): cls.maxDiff = None cls.env_HOME = os.environ.get("HOME", "Test Error. Could not read env variable 'HOME'") def setUp(self): if 'test_env_variable' in os.environ: del os.environ['test_env_variable'] def test_load_config(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='develop') self.assertEqual({'filename': 'develop'}, self.config) def test_load_multiple_configs(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments=['develop', 'live']) self.assertEqual({'filename': 'live'}, self.config) def test_load_multiple_configs_with_empty_files(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments=['develop', 'empty', 'live']) self.assertEqual({'filename': 'live'}, self.config) def test_load_config_with_env_variable(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='env') self.assertEqual({'filename': 'env', 'test_env': self.env_HOME, 'empty_test_env': ''}, self.config) def test_load_config_with_vault(self, mock): mock.register_uri('GET', "http://www.some-url.de/v1/secret/path", text=json.dumps({'data': {'value': 'some secret'}})) self.config_loader = ConfigLoader(use_vault=True, vault_addr="http://www.some-url.de", vault_token="AAAA") self.config = self.config_loader.load_config(path='resources/', environments='vault') self.assertEqual({'filename': 'vault', 'secret': 'some secret'}, self.config) def test_load_config_with_vault_disabled(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='vault') self.assertEqual({'filename': 'vault', 'secret': ''}, self.config) def test_load_config_fill_with_defaults(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='with_defaults', fill_with_defaults=True) expected = { 'defaults': { 'config': { 'protocol': 'http', 'api': '/v2/apps', 'username': '******', 'password': '******'}}, 'config': [{'api': '/v2/apps', 'password': '******', 'protocol': 'ftp', 'username': '******'}, {'api': '/v2/apps', 'host': 'marathon.tesla.develop.lhotse.ov.otto.de', 'password': '******', 'protocol': 'http', 'username': '******'}, {'api': '/v2/apps', 'password': '******', 'protocol': 'http', 'username': '******'}] } self.assertDictEqual(expected, self.config)
class TestConfig(unittest.TestCase): @classmethod def setUpClass(cls): cls.maxDiff = None cls.env_HOME = os.environ.get( "HOME", "Test Error. Could not read env variable 'HOME'") def setUp(self): if 'test_env_variable' in os.environ: del os.environ['test_env_variable'] def test_load_config(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='develop') self.assertEqual({'filename': 'develop'}, self.config) def test_load_multiple_configs(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config( path='resources/', environments=['develop', 'live']) self.assertEqual({'filename': 'live'}, self.config) def test_load_multiple_configs_with_empty_files(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config( path='resources/', environments=['develop', 'empty', 'live']) self.assertEqual({'filename': 'live'}, self.config) def test_load_config_with_env_variable(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='env') self.assertEqual( { 'filename': 'env', 'test_env': self.env_HOME, 'empty_test_env': '' }, self.config) def test_load_config_with_vault(self, mock): mock.register_uri('GET', "http://www.some-url.de/v1/secret/path", text=json.dumps({'data': { 'value': 'some secret' }})) self.config_loader = ConfigLoader(use_vault=True, vault_addr="http://www.some-url.de", vault_token="AAAA") self.config = self.config_loader.load_config(path='resources/', environments='vault') self.assertEqual({ 'filename': 'vault', 'secret': 'some secret' }, self.config) def test_load_config_with_vault_disabled(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config(path='resources/', environments='vault') self.assertEqual({'filename': 'vault', 'secret': ''}, self.config) def test_load_config_fill_with_defaults(self, _): self.config_loader = ConfigLoader(use_vault=False) self.config = self.config_loader.load_config( path='resources/', environments='with_defaults', fill_with_defaults=True) expected = { 'defaults': { 'config': { 'protocol': 'http', 'api': '/v2/apps', 'username': '******', 'password': '******' } }, 'config': [{ 'api': '/v2/apps', 'password': '******', 'protocol': 'ftp', 'username': '******' }, { 'api': '/v2/apps', 'host': 'marathon.tesla.develop.lhotse.ov.otto.de', 'password': '******', 'protocol': 'http', 'username': '******' }, { 'api': '/v2/apps', 'password': '******', 'protocol': 'http', 'username': '******' }] } self.assertDictEqual(expected, self.config)