Ejemplo n.º 1
0
    def test_method_report_channel_nsca(self):
        watch = cwatcher.Watch(config_file=CONF, service_name='my_service')
        with mock.patch('jflib.command_watcher.icinga.send_passive_check') as \
                send_passive_check, \
                mock.patch('jflib.command_watcher.send_email'):
            watch.report(
                status=0,
                custom_message='My message',
                performance_data={
                    'perf_1': 1,
                    'perf_2': 'test'
                },
                prefix='',
            )
        send_passive_check.assert_called_with(
            url='1.2.3.4',
            user='******',
            password=1234,
            status=0,
            host_name=cwatcher.HOSTNAME,
            service_name='my_service',
            text_output='MY_SERVICE OK - My message',
            performance_data='perf_1=1 perf_2=test')

        records = watch._log_handler.all_records
        self.assertIn('DEBUG [Message]', records)
        self.assertIn("custom_message: 'My message',", records)
        self.assertIn("message: 'MY_SERVICE OK - My message',", records)
        self.assertIn(
            "message_monitoring: 'MY_SERVICE OK - My message | "
            "perf_1=1 perf_2=test',", records)
        self.assertIn("performance_data: 'perf_1=1 perf_2=test'", records)
        self.assertIn("service_name: 'my_service',", records)
        self.assertIn("status_text: 'OK',", records)
        self.assertIn("user: '******'".format(cwatcher.USERNAME), records)
Ejemplo n.º 2
0
    def test_method_report_channel_email(self):
        watch = cwatcher.Watch(config_file=CONF, service_name='my_service')
        watch.log.info('info')
        watch.run('ls')

        with mock.patch('jflib.command_watcher.icinga.send_passive_check'), \
                mock.patch('smtplib.SMTP') as SMTP:
            watch.report(
                status=0,
                custom_message='My message',
                performance_data={
                    'perf_1': 1,
                    'perf_2': 'test'
                },
                prefix='',
            )

        SMTP.assert_called_with('smtp.example.com:587')
        server = SMTP.return_value
        server.login.assert_called_with('Login', 'Password')
        call_args = server.sendmail.call_args[0]
        self.assertEqual(call_args[0], '*****@*****.**')
        self.assertEqual(call_args[1], ['*****@*****.**'])
        self.assertIn('From: [email protected]\nTo: [email protected]\n',
                      call_args[2])
Ejemplo n.º 3
0
 def test_method_run_log_false(self):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            raise_exceptions=False)
     process = watch.run(self.cmd_stdout, log=False)
     self.assertEqual(watch.stdout, '')
     self.assertEqual(process.stdout, 'One line to stdout!')
Ejemplo n.º 4
0
 def test_propertyprocesses(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     self.assertEqual(watch.processes, [])
     watch.run(['ls'])
     watch.run(['ls', '-l'])
     watch.run(['ls', '-la'])
     self.assertEqual(len(watch.processes), 3)
Ejemplo n.º 5
0
 def test_ignore_exceptions_raise(self):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            report_channels=[])
     with self.assertRaises(cwatcher.CommandWatcherError):
         watch.run(os.path.join(DIR_FILES, 'exit-2.sh'),
                   ignore_exceptions=[1])
Ejemplo n.º 6
0
 def final_report(self, **data):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            report_channels=[])
     watch._timer.result = mock.Mock()
     watch._timer.result.return_value = '11.123s'
     return watch.final_report(**data)
Ejemplo n.º 7
0
 def test_method_run_multiple(self):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            raise_exceptions=False)
     watch.run(self.cmd_stdout)
     watch.run(self.cmd_stderr)
     self.assertEqual(len(watch._log_handler.buffer), 9)
     self.assertIn('Hostname: ', watch._log_handler.all_records)
Ejemplo n.º 8
0
 def test_method_run_output_stdout(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     with Capturing() as output:
         process = watch.run(self.cmd_stdout)
     self.assertEqual(process.subprocess.returncode, 0)
     self.assertEqual(len(output), 3)
     self.assertIn('STDOUT', output[1])
     self.assertIn('One line to stdout!', output[1])
     self.assertIn('Execution time: ', output[2])
Ejemplo n.º 9
0
 def test_method_run_kwargs(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     with mock.patch('subprocess.Popen') as Popen:
         process = Popen.return_value
         process.stdout = b''
         process.stderr = b''
         process.returncode = 0
         watch.run('ls', cwd='/')
     Popen.assert_called_with(['ls'], cwd='/', stderr=-1, stdout=-1)
Ejemplo n.º 10
0
 def test_method_report_channel_email_critical(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='my_service')
     with mock.patch('jflib.command_watcher.icinga.send_passive_check'), \
             mock.patch('smtplib.SMTP') as SMTP:
         watch.report(status=2)
     server = SMTP.return_value
     call_args = server.sendmail.call_args[0]
     self.assertEqual(call_args[1], ['*****@*****.**'])
     self.assertIn('From: [email protected]\nTo: [email protected]\n',
                   call_args[2])
Ejemplo n.º 11
0
 def test_method_run_output_stderr(self):
     watch = cwatcher.Watch(
         config_file=CONF,
         service_name='test',
         raise_exceptions=False,
     )
     with Capturing(stream='stderr') as output:
         process = watch.run(self.cmd_stderr)
     self.assertEqual(process.subprocess.returncode, 1)
     # On travis
     # ['Exception in thread Thread-59:', 'Trace[993 chars][0m'] != ''
     # self.assertEqual(len(output), 1)
     self.assertIn('STDERR', output.tostring())
     self.assertIn('One line to stderr!', output.tostring())
Ejemplo n.º 12
0
 def test_ignore_exceptions(self):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            report_channels=[])
     process = watch.run(self.cmd_stderr, ignore_exceptions=[1])
     self.assertEqual(process.subprocess.returncode, 1)
Ejemplo n.º 13
0
 def test_exception(self):
     watch = cwatcher.Watch(config_file=CONF,
                            service_name='test',
                            report_channels=[])
     with self.assertRaises(cwatcher.CommandWatcherError):
         watch.run(self.cmd_stderr)
Ejemplo n.º 14
0
 def test_property_hostname(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     self.assertEqual(watch._hostname, cwatcher.HOSTNAME)
Ejemplo n.º 15
0
 def test_property_service_name(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='Service')
     self.assertEqual(watch._service_name, 'Service')
Ejemplo n.º 16
0
 def test_method_run_kwargs_exception(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     with self.assertRaises(TypeError):
         watch.run('ls', xxx=False)
Ejemplo n.º 17
0
 def test_property_stderr(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     watch.log.stderr('stderr')
     self.assertEqual(watch.stderr, 'stderr')
Ejemplo n.º 18
0
 def test_argument_config_file(self):
     watch = cwatcher.Watch(config_file=CONF, service_name='test')
     self.assertEqual(watch._conf.email.to_addr, '*****@*****.**')