Esempio n. 1
0
    def test_app_dns_configure_create(self):
        """Test cli.app-dns.configure: create"""
        restclient.get.side_effect = restclient.NotFoundError('')

        self.runner.invoke(
            self.app_dns,
            ['configure', 'proid.app_dns1', '--pattern', 'proid.xxx'])

        restclient.post.assert_called_with(mock.ANY, '/app-dns/proid.app_dns1',
                                           {'pattern': 'proid.xxx'})
Esempio n. 2
0
    def test_reevaluate_notfound(self):
        """Test state reevaluation."""

        state = {
            'scheduled': {
                'foo.bar': [],
            },
            'monitors': {
                'foo.bar': {
                    'count': 1,
                    'available': 0,
                    'rate': 1.0,
                    'last_update': 100,
                },
            },
            'delayed': {},
        }

        time.time.return_value = 101

        restclient.post.side_effect = restclient.NotFoundError('xxx')

        appmonitor.reevaluate('/cellapi.sock', state)
        self.assertTrue(restclient.post.called)
        self.assertIn('foo.bar', state['delayed'])
        self.assertEqual(state['delayed']['foo.bar'], float(101 + 300))

        restclient.post.reset_mock()
        time.time.return_value = 102
        appmonitor.reevaluate('/cellapi.sock', state)
        self.assertFalse(restclient.post.called)
        self.assertIn('foo.bar', state['delayed'])
        # Delay did not increase
        self.assertEqual(state['delayed']['foo.bar'], float(101 + 300))

        # After time reached, call will happen, fail, and delay will be
        # extended.
        restclient.post.reset_mock()
        time.time.return_value = 500
        appmonitor.reevaluate('/cellapi.sock', state)
        self.assertTrue(restclient.post.called)
        self.assertIn('foo.bar', state['delayed'])
        self.assertEqual(state['delayed']['foo.bar'], float(500 + 300))

        # More time pass, and this time call will succeed - application will
        # be removed from delay dict.
        restclient.post.reset_mock()
        restclient.post.return_value = ()
        restclient.post.side_effect = None
        time.time.return_value = 500 + 300 + 1
        appmonitor.reevaluate('/cellapi.sock', state)
        self.assertTrue(restclient.post.called)
        self.assertNotIn('foo.bar', state['delayed'])
Esempio n. 3
0
class LogsTest(unittest.TestCase):
    """Mock test for treadmill.cli.logs"""
    def setUp(self):
        """Setup common test variables"""
        self.runner = click.testing.CliRunner()
        self.log_mod = plugin_manager.load('treadmill.cli', 'logs')
        self.log_cli = self.log_mod.init()

    @mock.patch('treadmill.websocketutils.find_running_instance',
                mock.Mock(return_value={
                    'host': 'host',
                    'uniq': 'uniq'
                }))
    @mock.patch('treadmill.cli.logs._find_endpoints',
                mock.Mock(return_value=[{
                    'host': 'host',
                    'port': 1234
                }]))
    @mock.patch('treadmill.restclient.get',
                side_effect=restclient.NotFoundError('foobar'))
    def test_no_logfile_found(self, _):
        """Test the output if no log file can be found."""
        result = self.runner.invoke(
            self.log_cli,
            ['--cell', 'foo', 'proid.app#123/running/service/foo'])

        # let's check that NotFoundError is handled
        self.assertEqual(str(result.exception).find('foobar'), -1)

    @mock.patch('treadmill.websocketutils.find_uniq_instance',
                side_effect=wsclient.WSConnectionError('foo'))
    def test_ws_connection_error(self, _):
        """Test that the websocket.client.WSConnectionError is handled."""
        result = self.runner.invoke(
            self.log_cli,
            ['--cell', 'foo', 'proid.app#123/5FiY/service/zk2fs'])

        # let's check that WSConnectionError is handled ie. it cannot be found
        # in the result
        self.assertEqual(str(result.exception).find('foo'), -1)
        self.assertEqual(result.exit_code, 1)
        self.assertTrue(
            'Could not connect to the websocket API' in result.output)
Esempio n. 4
0
    def test_reevaluate_notfound(self):
        """Test state reevaluation."""
        zkclient = mock.Mock()
        alerter = mock.Mock()

        state = {
            'scheduled': {
                'foo.bar': [],
            },
            'monitors': {
                'foo.bar': {
                    'count': 1,
                    'available': 0,
                    'rate': 1.0,
                    'last_update': 100,
                },
            },
            'suspended': {},
        }

        time.time.return_value = 101

        restclient.post.side_effect = restclient.NotFoundError('xxx')

        appmonitor.reevaluate('/cellapi.sock', alerter, state, zkclient, {})
        self.assertTrue(restclient.post.called)
        self.assertIn('foo.bar', state['suspended'])
        self.assertEqual(state['suspended']['foo.bar'], float(101 + 300))
        zkutils.update.assert_called_with(zkclient, '/app-monitors',
                                          {'foo.bar': float(101 + 300)})
        alerter.assert_called_with('foo.bar',
                                   'Monitor suspended: App not configured')

        restclient.post.reset_mock()
        time.time.return_value = 102
        appmonitor.reevaluate('/cellapi.sock', alerter, state, zkclient, {})
        self.assertFalse(restclient.post.called)
        self.assertIn('foo.bar', state['suspended'])
        alerter.assert_called_with('foo.bar',
                                   'Monitor suspended: App not configured')
        # Delay did not increase
        self.assertEqual(state['suspended']['foo.bar'], float(101 + 300))

        # After time reached, call will happen, fail, and delay will be
        # extended.
        alerter.reset_mock()
        restclient.post.reset_mock()
        time.time.return_value = 500
        last_waited = appmonitor.reevaluate('/cellapi.sock', alerter, state,
                                            zkclient, {})
        self.assertTrue(restclient.post.called)
        alerter.assert_called_with('foo.bar',
                                   'Monitor suspended: App not configured')
        self.assertIn('foo.bar', state['suspended'])
        self.assertEqual(state['suspended']['foo.bar'], float(500 + 300))
        zkutils.update.assert_called_with(zkclient, '/app-monitors',
                                          {'foo.bar': float(500 + 300)})
        self.assertEqual(last_waited, {'foo.bar': 800.0})

        # More time pass, and this time call will succeed - application will
        # be removed from delay dict.
        alerter.reset_mock()
        restclient.post.reset_mock()
        restclient.post.return_value = ()
        restclient.post.side_effect = None
        time.time.return_value = 500 + 300 + 1
        last_waited = appmonitor.reevaluate('/cellapi.sock', alerter, state,
                                            zkclient, last_waited)
        self.assertTrue(restclient.post.called)
        self.assertNotIn('foo.bar', state['suspended'])
        alerter.assert_called_with('foo.bar',
                                   'Monitor active again',
                                   status='clear')
        zkutils.update.assert_called_with(zkclient, '/app-monitors', {})
        self.assertEqual(last_waited, {})