def test_unlock_with_regex(self): def release(service: ConsulDockerisedService): set_consul_env(service) captured_result = TestCli._CAPTURE_WRAP_BUILDER.build(main)( [Action.UNLOCK.value, f"-{REGEX_KEY_ENABLED_SHORT_PARAMETER}", KEYS_1_REGEX]) self.assertEqual(SUCCESS_EXIT_CODE, captured_result.exception.code) self.assertCountEqual(KEYS_1, json.loads(captured_result.stdout)) acquire_locks(TestCli._build_executor(Action.LOCK), KEYS_1 + KEYS_2, release)
def test_unlock_with_regex(self): def release(service: ConsulDockerisedService): set_consul_env(service) captured_result = TestCli._CAPTURE_WRAP_BUILDER.build(main)([ Action.UNLOCK.value, f"-{REGEX_KEY_ENABLED_SHORT_PARAMETER}", KEYS_1_REGEX ]) self.assertEqual(SUCCESS_EXIT_CODE, captured_result.exception.code) self.assertCountEqual(KEYS_1, json.loads(captured_result.stdout)) acquire_locks(TestCli._build_executor(Action.LOCK), KEYS_1 + KEYS_2, release)
def test_lock_callbacks_when_not_locked(self): on_before_lock_listeners = [ self._create_magic_mock_file() for _ in range(3) ] on_lock_already_locked_listeners = [ self._create_magic_mock_file() for _ in range(3) ] action_args = [ *itertools.chain( *[[f"--{ON_BEFORE_LOCK_LONG_PARAMETER}", listener] for listener in on_before_lock_listeners]), *itertools.chain( *[[f"--{ON_LOCK_ALREADY_LOCKED_LONG_PARAMETER}", listener] for listener in on_lock_already_locked_listeners]), _END_OF_CLI_KEYWORD_OPTIONS ] locker = TestCli._build_executor(Action.LOCK, pre_key_action_args=action_args) lock_result = acquire_locks(locker)[0] assert lock_result.exception.code == SUCCESS_EXIT_CODE for listener in on_before_lock_listeners: self.assertEqual([KEY_1], self._get_magic_mock_results(listener)) for listener in on_lock_already_locked_listeners: self.assertEqual([], self._get_magic_mock_results(listener))
def test_lock_when_unlocked(self): lock_result = acquire_locks(TestCli._build_executor( Action.LOCK, pre_key_action_args=[f"--{METADATA_LONG_PARAMETER}", json.dumps(METADATA)]))[0] self.assertEqual(SUCCESS_EXIT_CODE, lock_result.exception.code) parsed_stdout = json.loads(lock_result.stdout, cls=ConsulLockInformationJSONDecoder) self.assertIsInstance(parsed_stdout, ConsulLockInformation) self.assertEqual(METADATA, parsed_stdout.metadata)
def test_execute_when_not_locked(self): lock_result = acquire_locks( TestCli._build_executor( Action.EXECUTE, post_key_action_args=[ f"echo {VALUE_1} && exit {ERROR_EXIT_CODE}" ]))[0] self.assertEqual(ERROR_EXIT_CODE, lock_result.exception.code) self.assertEqual(VALUE_1, lock_result.stdout.strip())
def test_lock_with_invalid_session_ttl(self): lock_result = acquire_locks( TestCli._build_executor(Action.LOCK, pre_key_action_args=[ f"--{SESSION_TTL_LONG_PARAMETER}", MIN_LOCK_TIMEOUT_IN_SECONDS - 1 ]))[0] self.assertEqual(INVALID_SESSION_TTL_EXIT_CODE, lock_result.exception.code)
def test_lock_callback_when_callback_fails(self): on_before_lock_listener = self._create_magic_mock_file() with open(on_before_lock_listener, "w") as file: file.write(f"{_BASH_SHEBANG}\nexit 1") locker = TestCli._build_executor( Action.LOCK, pre_key_action_args=[ f"--{ON_BEFORE_LOCK_LONG_PARAMETER}", on_before_lock_listener, _END_OF_CLI_KEYWORD_OPTIONS]) lock_result = acquire_locks(locker)[0] assert lock_result.exception.code == SUCCESS_EXIT_CODE
def test_lock_when_unlocked(self): lock_result = acquire_locks( TestCli._build_executor(Action.LOCK, pre_key_action_args=[ f"--{METADATA_LONG_PARAMETER}", json.dumps(METADATA) ]))[0] self.assertEqual(SUCCESS_EXIT_CODE, lock_result.exception.code) parsed_stdout = json.loads(lock_result.stdout, cls=ConsulLockInformationJSONDecoder) self.assertIsInstance(parsed_stdout, ConsulLockInformation) self.assertEqual(METADATA, parsed_stdout.metadata)
def test_lock_callback_when_callback_fails(self): on_before_lock_listener = self._create_magic_mock_file() with open(on_before_lock_listener, "w") as file: file.write(f"{_BASH_SHEBANG}\nexit 1") locker = TestCli._build_executor( Action.LOCK, pre_key_action_args=[ f"--{ON_BEFORE_LOCK_LONG_PARAMETER}", on_before_lock_listener, _END_OF_CLI_KEYWORD_OPTIONS ]) lock_result = acquire_locks(locker)[0] assert lock_result.exception.code == SUCCESS_EXIT_CODE
def test_lock_callbacks_when_not_locked(self): on_before_lock_listeners = [self._create_magic_mock_file() for _ in range(3)] on_lock_already_locked_listeners = [self._create_magic_mock_file() for _ in range(3)] action_args = [ *itertools.chain(*[[f"--{ON_BEFORE_LOCK_LONG_PARAMETER}", listener] for listener in on_before_lock_listeners]), *itertools.chain(*[[f"--{ON_LOCK_ALREADY_LOCKED_LONG_PARAMETER}", listener] for listener in on_lock_already_locked_listeners]), _END_OF_CLI_KEYWORD_OPTIONS] locker = TestCli._build_executor(Action.LOCK, pre_key_action_args=action_args) lock_result = acquire_locks(locker)[0] assert lock_result.exception.code == SUCCESS_EXIT_CODE for listener in on_before_lock_listeners: self.assertEqual([KEY_1], self._get_magic_mock_results(listener)) for listener in on_lock_already_locked_listeners: self.assertEqual([], self._get_magic_mock_results(listener))
def test_lock_with_non_normalised_path(self): lock_result = acquire_locks(TestCli._build_executor(Action.LOCK), [NON_NORMALISED_KEY])[0] self.assertEqual(INVALID_KEY_EXIT_CODE, lock_result.exception.code)
def test_lock_with_double_slash_path(self): lock_result = acquire_locks(TestCli._build_executor(Action.LOCK), [DOUBLE_SLASH_KEY])[0] self.assertEqual(INVALID_KEY_EXIT_CODE, lock_result.exception.code)
def test_execute_when_not_locked(self): lock_result = acquire_locks(TestCli._build_executor(Action.EXECUTE, post_key_action_args=[ f"echo {VALUE_1} && exit {ERROR_EXIT_CODE}"]))[0] self.assertEqual(ERROR_EXIT_CODE, lock_result.exception.code) self.assertEqual(VALUE_1, lock_result.stdout.strip())
def test_lock_with_invalid_session_ttl(self): lock_result = acquire_locks(TestCli._build_executor( Action.LOCK, pre_key_action_args=[f"--{SESSION_TTL_LONG_PARAMETER}", MIN_LOCK_TIMEOUT_IN_SECONDS - 1]))[0] self.assertEqual(INVALID_SESSION_TTL_EXIT_CODE, lock_result.exception.code)