class Hooks: def __init__(self): self.utils = Utils() self.whobj = WebHook() self.config_file = "" def run_hook(self, hookname, appdata, path, env, user): try: exit_code = 0 execution_result = 'SUCCESS' self.whobj.invoke_webhook(appdata, self.config_file, hookname, env, user) abs_path = os.path.abspath(path) if "hooks" in appdata and hookname in appdata["hooks"]: command = appdata["hooks"][hookname] with chdir(abs_path): print("About to run {} hook [{}] at path {}".format( hookname, command, abs_path)) exit_code = os.system(command) except (Exception) as e: printException(e) execution_result = 'FAILURE' raise finally: # todo: maybe send a datadog event ? pass return exit_code
class Hooks: def __init__(self): self.utils = Utils() self.whobj = WebHook() self.statsd_message_list = [] self.config_file = "" def run_hook(self, hookname, appdata, path, hook_input_metric): try: exit_code = 0 function_execution_start_time = datetime.now() execution_result = 'SUCCESS' self.whobj.invoke_webhook(appdata, hook_input_metric, self.config_file) abs_path = os.path.abspath(path) if "hooks" in appdata and hookname in appdata["hooks"]: command = appdata["hooks"][hookname] with chdir(abs_path): print("About to run {} hook [{}] at path {}".format( hookname, command, abs_path)) exit_code = os.system(command) except (Exception) as e: print("The following error occurred: %s" % e, file=sys.stderr) execution_result = 'FAILURE' raise finally: try: if 'execution_result' not in globals( ) and 'execution_result' not in locals(): execution_result = 'FAILURE' if 'function_execution_start_time' not in globals( ) and 'function_execution_start_time' not in locals(): function_execution_start_time = datetime.now() sc = self.utils.getStatsClient() time_take_milliseonds = ( (datetime.now() - function_execution_start_time).total_seconds() * 1000) hook_input_metric = hook_input_metric + ",outcome=" + str( execution_result) tup = (hook_input_metric, time_take_milliseonds) self.statsd_message_list.append(tup) except (Exception) as e: print("The following error occurred: %s" % e, file=sys.stderr) raise return exit_code
class TestWebhook(unittest.TestCase): def setUp(self): self.webhook = WebHook() self.settingObj = Settings() self.base_dir = self.settingObj.getCliDir() self.configs_dir = self.base_dir + "/tests/configs" self.components_dir = self.base_dir + '/tests/components/dev' with open(self.configs_dir + '/roger-mesos-tools.config') as roger: roger_env = yaml.load(roger) self.roger_env = roger_env # check for value error / exception assert # check for one positive case - May be introduce exit code or something @pytest.mark.skip def test_invoke_webhook_when_hook_input_metrics_is_invalid(self): settings = mock(Settings) appConfig = mock(AppConfig) appdata = 'valid-app-data' conf_file = 'roger-mesos-tools.config' when(settings).getConfigDir().thenReturn(self.configs_dir) when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env) when(appConfig).getConfig(any(), any()).thenReturn(conf_file) with self.assertRaises(ValueError): self.webhook.invoke_webhook(appdata, conf_file, any(), any(), any()) @pytest.mark.skip def test_invoke_webhook_when_appdata_is_invalid(self): settings = mock(Settings) appConfig = mock(AppConfig) appdata = 'invalid-app-data' conf_file = 'roger-mesos-tools.config' when(settings).getConfigDir().thenReturn(self.configs_dir) when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env) when(appConfig).getConfig(any(), any()).thenReturn(conf_file) with self.assertRaises(ValueError): self.webhook.invoke_webhook(appdata, conf_file, any(), any(), any()) def tearDown(self): pass
class TestWebhook(unittest.TestCase): def setUp(self): self.webhook = WebHook() self.settingObj = Settings() self.base_dir = self.settingObj.getCliDir() self.configs_dir = self.base_dir + "/tests/configs" self.components_dir = self.base_dir + '/tests/components/dev' with open(self.configs_dir + '/roger-mesos-tools.config') as roger: roger_env = yaml.load(roger) self.roger_env = roger_env # check for value error / exception assert # check for one positive case - May be introduce exit code or something @pytest.mark.skip def test_invoke_webhook_when_hook_input_metrics_is_invalid(self): settings = mock(Settings) appConfig = mock(AppConfig) appdata = 'valid-app-data' hook_input_metrics = 'invalid-hook-input-metrics' conf_file = 'roger-mesos-tools.config' when(settings).getConfigDir().thenReturn(self.configs_dir) when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env) when(appConfig).getConfig(any(), any()).thenReturn(conf_file) with self.assertRaises(ValueError): self.webhook.invoke_webhook(appdata, hook_input_metrics, conf_file) @pytest.mark.skip def test_invoke_webhook_when_appdata_is_invalid(self): settings = mock(Settings) appConfig = mock(AppConfig) appdata = 'invalid-app-data' hook_input_metrics = 'hook-input-metrics' conf_file = 'roger-mesos-tools.config' when(settings).getConfigDir().thenReturn(self.configs_dir) when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env) when(appConfig).getConfig(any(), any()).thenReturn(conf_file) with self.assertRaises(ValueError): self.webhook.invoke_webhook(appdata, hook_input_metrics, conf_file) def tearDown(self): pass
class Hooks: def __init__(self): self.utils = Utils() self.whobj = WebHook() self.statsd_message_list = [] self.config_file = "" def run_hook(self, hookname, appdata, path, hook_input_metric): try: exit_code = 0 function_execution_start_time = datetime.now() execution_result = 'SUCCESS' self.whobj.invoke_webhook(appdata, hook_input_metric, self.config_file) abs_path = os.path.abspath(path) if "hooks" in appdata and hookname in appdata["hooks"]: command = appdata["hooks"][hookname] with chdir(abs_path): print("About to run {} hook [{}] at path {}".format( hookname, command, abs_path)) exit_code = os.system(command) except (Exception) as e: printException(e) execution_result = 'FAILURE' raise finally: try: if 'execution_result' not in globals() and 'execution_result' not in locals(): execution_result = 'FAILURE' if 'function_execution_start_time' not in globals() and 'function_execution_start_time' not in locals(): function_execution_start_time = datetime.now() sc = self.utils.getStatsClient() time_take_milliseonds = ((datetime.now() - function_execution_start_time).total_seconds() * 1000) hook_input_metric = hook_input_metric + ",outcome=" + str(execution_result) tup = (hook_input_metric, time_take_milliseonds) self.statsd_message_list.append(tup) except (Exception) as e: printException(e) raise return exit_code