Exemplo n.º 1
0
    async def _execute(self):
        if self._path.is_dir():
            return TaskResult(TaskResultStatus.SUCCESS, f"Directory [{self._path}] exists")

        if self._path.is_file():
            return TaskResult(TaskResultStatus.WARNING, f"Path [{self._path}] is a file, not a directory")

        return TaskResult(TaskResultStatus.FAILURE, f"Directory [{self._path}] does NOT exist")
Exemplo n.º 2
0
 def test_valid_json_nested_multi_result_test(self) -> None:
     self._do_test(
         MultiTaskResult(TaskResultStatus.FAILURE, "failure message", [
             TaskResult(TaskResultStatus.INFO, "test message"),
             MultiTaskResult(TaskResultStatus.SUCCESS, "success message", [
                 TaskResult(TaskResultStatus.WARNING, "warning message"),
                 MultiTaskResult(TaskResultStatus.UNKNOWN, "unknown message", [
                     TaskResult(TaskResultStatus.FAILURE, "failure 2 message")
                 ])
             ])
         ]))
Exemplo n.º 3
0
    def test_dumps_and_get_lines_produces_same_results(self):
        msg1 = 'message1'
        result1 = TaskResult(TaskResultStatus.SUCCESS, msg1)

        msg2 = 'message2'
        result2 = TaskResult(TaskResultStatus.INFO, msg2)

        msg_multi = 'multi message'
        status_multi = TaskResultStatus.FAILURE
        multi = MultiTaskResult(status_multi, msg_multi, [result1, result2])
        self.assertEqual(self.serializer.dumps(multi),
                         '\n'.join(self.serializer.get_lines(multi)))
    def test_returns_one_result_for_each_line(self) -> None:
        input_lines = [b'test1', b'test2']
        result = self._validator.validate(input_lines)

        self.assertEqual(result.status, TaskResultStatus.INFO)
        self.assertSequenceEqual(
            result.results,
            [
                TaskResult(TaskResultStatus.INFO, 'test1'),
                TaskResult(TaskResultStatus.INFO, 'test2')
            ]
        )
    def test_multi_result(self):
        msg1 = 'unknown_message'
        result1 = TaskResult(TaskResultStatus.SUCCESS, msg1)

        msg2 = 'unknown_message'
        result2 = TaskResult(TaskResultStatus.INFO, msg2)

        msg_multi = 'multi message'
        status_multi = TaskResultStatus.FAILURE
        multi = MultiTaskResult(status_multi, msg_multi, [result1, result2])
        self.writer.write(multi)

        self.serializer.get_lines.assert_valled_once_with(multi)
Exemplo n.º 6
0
    def test_multi_result(self):
        msg1 = 'message1'
        result1 = TaskResult(TaskResultStatus.SUCCESS, msg1)

        msg2 = 'message2'
        result2 = TaskResult(TaskResultStatus.INFO, msg2)

        msg_multi = 'multi message'
        status_multi = TaskResultStatus.FAILURE
        multi = MultiTaskResult(status_multi, msg_multi, [result1, result2])

        self.assertEqual(
            self.serializer.dumps(multi), f'{{"status": "FAILURE", "message": "{msg_multi}", "results": [' +
            f'{{"status": "SUCCESS", "message": "{msg1}"}}, {{"status": "INFO", "message": "{msg2}"}}]}}')
 def _validate_line(self, lineno: int,
                    line: str) -> Iterator[BaseTaskResult]:
     if 'not in the whitelist' in line:
         if lineno == 0:
             yield TaskResult(
                 TaskResultStatus.WARNING,
                 'Please add [mntr] to [4lw.commands.whitelist] property in zookeeper.conf file'
             )
     else:
         try:
             (key, *values) = filter(None, line.split())
             yield TaskResult(TaskResultStatus.INFO,
                              f'[{key}]=[{" ".join(values)}]')
         except ValueError:
             yield TaskResult(TaskResultStatus.WARNING,
                              f'Line [{line}] is not parseable')
Exemplo n.º 8
0
    def test_multi_result(self):
        msg1 = 'unknown_message'
        result1 = TaskResult(TaskResultStatus.SUCCESS, msg1)

        msg2 = 'unknown_message'
        result2 = TaskResult(TaskResultStatus.INFO, msg2)

        msg_multi = 'multi message'
        status_multi = TaskResultStatus.FAILURE
        multi = MultiTaskResult(status_multi, msg_multi, [result1, result2])

        self.assertCountEqual(list(self.serializer.get_lines(multi)), [
            f'[FAILURE ] {msg_multi}',
            f'    [SUCCESS ] {msg1}',
            f'    [INFO    ] {msg2}',
        ])
Exemplo n.º 9
0
    async def _execute(self) -> BaseTaskResult:
        if not self._path.exists():
            return TaskResult(TaskResultStatus.FAILURE,
                              f'File [{self._path}] does not exist')

        with open(self._path, 'r', 1) as stream:
            return self._validator.validate(stream)
Exemplo n.º 10
0
    def test_valid_task_result_with_non_iterable_results_returns_TaskResult(self) -> None:
        input_results = TaskResult(TaskResultStatus.FAILURE, 'test message')
        input_dict = dict(status=input_results.status, message=input_results.message, results=1)
        serialized = self.serializer.dumps(input_dict)
        deserialized = self.deserializer.loads(serialized)

        self.assertEqual(input_results, deserialized)
Exemplo n.º 11
0
    def validate(self, mem_total: int,
                 mem_used_percent: float) -> BaseTaskResult:
        gigs = round(mem_total / GIGABYTE, 2)

        msg = f'System memory is [{gigs}] GB is used in [{mem_used_percent}%] which is'
        if mem_used_percent > self._usage_percent_error_treshold:
            return TaskResult(
                TaskResultStatus.FAILURE,
                f'{msg} greater than error treshold [{self._usage_percent_error_treshold}%]'
            )
        if mem_used_percent > self._usage_percent_warn_treshold:
            return TaskResult(
                TaskResultStatus.WARNING,
                f'{msg} greater than warning treshold [{self._usage_percent_warn_treshold}%]'
            )
        return TaskResult(TaskResultStatus.SUCCESS,
                          f'{msg} at acceptable level')
    def test_unknown_result(self):
        msg = 'unknown_message'
        result = TaskResult(TaskResultStatus.UNKNOWN, msg)

        self.writer.write(result)

        self.serializer.get_lines.assert_valled_once_with(result)
        self.logger.debug.assert_called_once_with('serializer_result')
Exemplo n.º 13
0
    def test_build_and_of_results_creates_same_results(self):
        msg = 'test_message'

        for status in TaskResultStatus:
            result = TaskResult(status, msg)
            self.assertEqual(self.builder.build(status, msg),
                             self.builder.of_results(status, [result]),
                             f'Incorrect message for status=[{status}]')
    def test_repr_shortens_message(self):
        message = 'foobarbazbat'
        long_message = message * 5
        result = TaskResult(TaskResultStatus.INFO, long_message)
        r = repr(result)

        self.assertNotIn(long_message, r)
        self.assertIn(message, r)
Exemplo n.º 15
0
 def test_calls_onlt_build_on_inner_builder_not_of_results(self):
     msg = 'test_message'
     result = TaskResult(TaskResultStatus.UNKNOWN, msg)
     self.assertEqual(
         self.builder.of_results(TaskResultStatus.INFO, [result]),
         f'{self.prefix}mocked_message')
     self.inner_builder.build.assert_called_once_with(
         TaskResultStatus.INFO, [msg])
     self.assertEqual(self.inner_builder.of_results.call_count, 0)
Exemplo n.º 16
0
 def validate(self, logical_cpu_count: int, cpu_load_1_minute: float,
              cpu_load_5_minute: float,
              cpu_load_15_minute: float) -> BaseTaskResult:
     total_load_warn_treshold = logical_cpu_count * self._per_cpu_load_warn_treshold
     total_load_error_treshold = logical_cpu_count * self._per_cpu_load_error_treshold
     msg = f'System load 1m is [{cpu_load_1_minute}] which is'
     if cpu_load_1_minute > total_load_error_treshold:
         return TaskResult(
             TaskResultStatus.FAILURE,
             f'{msg} greater than error treshold [{total_load_error_treshold}/CPU]'
         )
     if cpu_load_1_minute > total_load_warn_treshold:
         return TaskResult(
             TaskResultStatus.WARNING,
             f'{msg} greater than warning treshold [{total_load_warn_treshold}/CPU]'
         )
     return TaskResult(TaskResultStatus.SUCCESS,
                       f'{msg} at acceptable level')
Exemplo n.º 17
0
 def validate(self, stdout: str, stderr: str,
              exit_code: int) -> BaseTaskResult:
     try:
         return self._validate(stdout, stderr, exit_code)
     except Exception as e:
         msg = f'Could not parse subprocess result. Details:\n\nstdout:\n{stdout}\n\nstderr:\n{stderr}' + \
               f'\n\nexit_code: {exit_code}\n\nException:\n{e}'
         _logger.exception(msg)
         return TaskResult(TaskResultStatus.UNKNOWN, msg)
Exemplo n.º 18
0
    def _validate_disk_info_struct(
            self, disk_info_struct: DiskInfoStruct) -> BaseTaskResult:
        total_gigs = round(disk_info_struct.total / GIGABYTE, 2)

        msg = f'Device [{disk_info_struct.device}] (mount: [{disk_info_struct.mountpoint}]; ' +\
            f'fstype: [{disk_info_struct.fstype}]) has [{total_gigs}] GB in total and is used in ' +\
            f'[{disk_info_struct.percent}%] which is'
        if disk_info_struct.percent > self._usage_percent_error_treshold:
            return TaskResult(
                TaskResultStatus.FAILURE,
                f'{msg} greater than error treshold [{self._usage_percent_error_treshold}%]'
            )
        if disk_info_struct.percent > self._usage_percent_warn_treshold:
            return TaskResult(
                TaskResultStatus.WARNING,
                f'{msg} greater than warning treshold [{self._usage_percent_warn_treshold}%]'
            )
        return TaskResult(TaskResultStatus.SUCCESS,
                          f'{msg} at acceptable level')
Exemplo n.º 19
0
def task_result_decoder(obj):
    if 'status' in obj and 'message' in obj:
        status = TaskResultStatus[obj['status']]
        message = obj['message']

        if 'results' in obj and isinstance(obj['results'], Iterable):
            return MultiTaskResult(status, message, obj['results'])
        else:
            return TaskResult(status, message)
    return obj
Exemplo n.º 20
0
 async def validate(self,
                    response: aiohttp.ClientResponse) -> BaseTaskResult:
     if response.status == 200:
         return self._result_builder.of_results(
             await response.json(loads=self._deserializer.loads))
     else:
         r = MultiTaskResult(
             TaskResultStatus.FAILURE,
             f'Call to external checklist [{response.real_url}] failed. See subtasks for details.',
             [TaskResult(TaskResultStatus.INFO, await response.text())])
         return r
 def _validate_line(self, lineno: int,
                    line: str) -> Iterator[BaseTaskResult]:
     if lineno == 1:
         yield TaskResult(
             TaskResultStatus.WARNING,
             'Received more than 1 line from Zookeeper for [ruok] command. This was unexpected.'
         )
     elif lineno > 1:
         return
     elif line == 'imok':
         yield TaskResult(
             TaskResultStatus.SUCCESS,
             'Received [imok] from Zookeeper for [ruok] command')
     elif 'not in the whitelist' in line:
         yield TaskResult(
             TaskResultStatus.WARNING,
             'Please add [ruok] to [4lw.commands.whitelist] property in zookeeper.conf file'
         )
     else:
         yield TaskResult(TaskResultStatus.WARNING,
                          f'Line [{line}] is not [imok]')
Exemplo n.º 22
0
 def _validate_cpu_usage_info(
         self, interval: float, cpu_usage_list: Iterator[CPUUsageStruct]
 ) -> Iterator[BaseTaskResult]:
     for (idx, cpu_usage_info) in enumerate(cpu_usage_list):
         yield TaskResult(
             TaskResultStatus.INFO,
             f'CPU times for CPU #{idx} for past [{interval}] seconds are: '
             +
             f'user=[{cpu_usage_info.user}]; system=[{cpu_usage_info.system}]; '
             +
             f'idle=[{cpu_usage_info.idle}]; iowait=[{cpu_usage_info.iowait}]'
         )
    def test_defaults(self):
        msg1 = 'message1'
        result1 = TaskResult(TaskResultStatus.SUCCESS, msg1)

        msg2 = 'message2'
        result2 = TaskResult(TaskResultStatus.INFO, msg2)

        msg_multi = 'multi message'
        status_multi = TaskResultStatus.FAILURE
        multi = MultiTaskResult(status_multi, msg_multi, [result1, result2])
        self.writer.write(multi)

        writer = LoggingOutputWriter()
        with self.assertLogs(
                level=logging.DEBUG,
                logger=LoggingOutputWriter.DEFAULT_LOGGER_NAME) as logger:
            writer.write(multi)
            self.assertListEqual(logger.output, [
                'DEBUG:checklist.result:[FAILURE ] multi message',
                'DEBUG:checklist.result:    [SUCCESS ] message1',
                'DEBUG:checklist.result:    [INFO    ] message2',
            ])
Exemplo n.º 24
0
    def test_validator_calls_provided_builder(self):
        builder_result = mock.Mock(BaseTaskResult)
        result_builder = mock.Mock(MultiTaskResultBuilder)
        result_builder.of_results.return_value = builder_result
        validator = DiskInfoValidator(self._warn, self._error, result_builder)

        result = validator.validate([self._disk_provider(0)])

        self.assertIs(result, builder_result)
        expected_results = [
            TaskResult(
                TaskResultStatus.SUCCESS,
                'Device [test0] (mount: [/test]; fstype: [testfs]) has [0.0] GB in total '
                + 'and is used in [0%] which is at acceptable level')
        ]
        result_builder.of_results.assert_called_once_with(
            CheckGenerator(expected_results))
Exemplo n.º 25
0
    def test_unknown_result(self):
        msg = 'unknown_message'
        result = TaskResult(TaskResultStatus.UNKNOWN, msg)

        self.assertEqual(self.serializer.dumps(result), f'{{"status": "UNKNOWN", "message": "{msg}"}}')
Exemplo n.º 26
0
    def test_info_result(self):
        msg = 'info_message'
        result = TaskResult(TaskResultStatus.INFO, msg)

        self.assertEqual(self.serializer.dumps(result), f'{{"status": "INFO", "message": "{msg}"}}')
Exemplo n.º 27
0
 def _validate_line(self, lineno: int,
                    line: str) -> Iterator[BaseTaskResult]:
     yield TaskResult(TaskResultStatus.INFO, line)
Exemplo n.º 28
0
    async def _execute(self) -> BaseTaskResult:
        if self._variable_name not in environ:
            return TaskResult(TaskResultStatus.FAILURE, f'{self._msg_prefix} not found.')

        return TaskResult(TaskResultStatus.SUCCESS, f'{self._msg_prefix} has value [{environ[self._variable_name]}]')
Exemplo n.º 29
0
 def test_valid_json_result_test(self) -> None:
     self._do_test(TaskResult(TaskResultStatus.INFO, "test message"))
Exemplo n.º 30
0
 def test_valid_json_simple_multi_result_test(self) -> None:
     self._do_test(
         MultiTaskResult(TaskResultStatus.FAILURE, "failure message", [
             TaskResult(TaskResultStatus.INFO, "test message"),
         ]))