def setUp(self): curr_dir = os.path.dirname(__file__) file_path = 'assets/config.yaml' # get the yaml file with open(os.path.join(curr_dir, file_path), 'r') as stream: try: self.normal_content = yaml.safe_load(stream) except yaml.YAMLError as err: Logger.error('error load yaml file ' + str(err)) self.vault_data_key = {'db': {'password': '******'}} self.kv_sample = { 'appmode': 'development', 'db': { 'username': '******', 'host': 'localhost', 'port': 1234 }, 'elasticsearch': { 'host': 'localhost', 'port': 9200 } } Outfit.setup(path='tests/assets/config.yaml')
def setUp(self): if not os.path.exists('tests/test_logs'): os.mkdir('tests/test_logs') curr_dir = os.path.dirname(__file__) file_path = 'assets/logging.yaml' # get the yaml file with open(os.path.join(curr_dir, file_path), 'r') as stream: try: self.normal_content = yaml.safe_load(stream) except yaml.YAMLError as err: Logger.error('error load yaml file ' + str(err))
def test_setup_log_py(self): self.delete_all_log_files() Outfit.setup('./tests/assets/config-log-py.yaml') Logger.info('test_info') with open('tests/logs/info.log', 'r') as finfo: temp_info = finfo.readlines() last_line = temp_info[len(temp_info) - 1] self.assertTrue('test_info' in last_line) Logger.debug('test_debug') with open('tests/logs/debug.log', 'r') as fdebug: temp_debug = fdebug.readlines() last_line = temp_debug[len(temp_debug) - 1] self.assertTrue('test_debug' in last_line) Logger.error('test_error') with open('tests/logs/errors.log', 'r') as ferrors: temp_errors = ferrors.readlines() last_line = temp_errors[len(temp_errors) - 1] self.assertTrue('test_error' in last_line) Logger.critical('test_critical') with open('tests/logs/errors.log', 'r') as fcritical: temp_critical = fcritical.readlines() last_line = temp_critical[len(temp_critical) - 1] self.assertTrue('test_critical' in last_line)
def test_setup_log_consulkv_failed(self, mock_kv): self.delete_all_log_files() from outfit.utils.io import load_yaml mock_kv.return_value = logging Outfit.setup('./tests/assets/config-log-kv.yaml') Logger.info('test_info') with open('tests/test_logs/info.log', 'r') as finfo: temp_info = finfo.readlines() print(temp_info) print(len(temp_info)) last_line = temp_info[len(temp_info) - 1] self.assertTrue('test_info' in last_line) Logger.debug('test_debug') with open('tests/test_logs/debug.log', 'r') as fdebug: temp_debug = fdebug.readlines() last_line = temp_debug[len(temp_debug) - 1] self.assertTrue('test_debug' in last_line) Logger.error('test_error') with open('tests/test_logs/errors.log', 'r') as ferrors: temp_errors = ferrors.readlines() last_line = temp_errors[len(temp_errors) - 1] self.assertTrue('test_error' in last_line) Logger.critical('test_critical') with open('tests/test_logs/errors.log', 'r') as fcritical: temp_critical = fcritical.readlines() last_line = temp_critical[len(temp_critical) - 1] self.assertTrue('test_critical' in last_line)
def setUp(self): curr_dir = os.path.dirname(__file__) file_path = 'assets/config.yaml' # get the yaml file with open(os.path.join(curr_dir, file_path), 'r') as stream: try: self.normal_content = yaml.safe_load(stream) except yaml.YAMLError as err: Logger.error('error load yaml file ' + str(err)) self.vault_data_key = {'db': {'password': '******'}} self.kv_sample = { 'appmode': 'development', 'db': { 'username': '******', 'host': 'localhost', 'port': 1234 }, 'elasticsearch': { 'host': 'localhost', 'port': 9200 } } Outfit.setup(path='tests/assets/config.yaml') os.environ['host_vault'] = 'localhost' os.environ['port_vault'] = '12345' os.environ['scheme_vault'] = 'http' os.environ['token_vault'] = 'qwerty123' os.environ['path_vault'] = 'application' os.environ['host_consul'] = 'localhost' os.environ['port_consul'] = '12345' os.environ['scheme_consul'] = 'http' os.environ['token_consul'] = 'qwerty123' os.environ['path_consul'] = 'application' os.environ['LOG_MODE'] = 'development' os.environ['LOG_LOCATION'] = './tests/assets/logging.yaml' os.environ['LOG_TYPE'] = 'yaml_file' os.environ['DEFAULT_TYPE'] = 'yaml_file' os.environ['DEFAULT_LOCATION'] = './tests/assets/logging.yaml'
def test_setup_log_consulkv(self, mock_kv): self.delete_all_log_files() from outfit.utils.io import load_yaml mock_kv.return_value = logging #result = convert_yaml(self.normal_content) Outfit.setup('./tests/assets/config-log-kv.yaml') m = minfo = mock_open( read_data='INFO:test_utilslogger.py(50)> test_info') mdebug = mock_open( read_data='ERROR:test_utilslogger.py(62)> test_debug') merror = mock_open( read_data='ERROR:test_utilslogger.py(62)> test_error') mcritical = mock_open( read_data='ERROR:test_utilslogger.py(62)> test_critical') m.side_effect = [ minfo.return_value, mdebug.return_value, merror.return_value, mcritical.return_value ] with patch('builtins.open', m): Logger.info('test_info') with open('tests/test_logs/info.log', 'r') as finfo: temp_info = finfo.read() last_line = temp_info[len(temp_info) - 1] self.assertTrue('test_info' in temp_info) Logger.debug('test_debug') with open('tests/test_logs/debug.log', 'r') as fdebug: temp_debug = fdebug.readlines() last_line = temp_debug[len(temp_debug) - 1] self.assertTrue('test_debug' in last_line) Logger.error('test_error') with open('tests/test_logs/errors.log', 'r') as ferrors: temp_errors = ferrors.readlines() last_line = temp_errors[len(temp_errors) - 1] self.assertTrue('test_error' in last_line) Logger.critical('test_critical') with open('tests/test_logs/errors.log', 'r') as fcritical: temp_critical = fcritical.readlines() last_line = temp_critical[len(temp_critical) - 1] self.assertTrue('test_critical' in last_line)
def test_env_var(self, mock_consul_kv, mock_vault_kv): mock_consul_kv.return_value = [ None, { 'Value': json.dumps(self.kv_sample).encode() } ] mock_vault_kv.return_value = {'data': secret_kv} Outfit.setup('tests/assets/config-env.yaml') vault = VaultCon() consul = ConsulCon() result_vault = vault.get_secret_kv() result_consul = consul.get_kv() Logger.debug(result_consul) result = merge_dict(result_vault, result_consul) Logger.debug(result) self.assertEqual(result['big_query']['client_id'], '1234567', ' client id from vault not match') self.assertEqual(result['appmode'], 'development', ' appmode from consul not match')