Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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))
Пример #4
0
 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)
Пример #5
0
 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())
Пример #6
0
 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)
Пример #7
0
    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
Пример #8
0
 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)
Пример #9
0
    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
Пример #10
0
    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))
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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())
Пример #14
0
 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)
Пример #15
0
 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)
Пример #16
0
 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)