예제 #1
0
    def test_load(self):
        """Verifies that only valid classes are accepted as implementation.
        """
        # Access to a protected member _load_impl of a client class
        # pylint: disable=W0212

        self.assertRaises(
            AssertionError,
            _base_service.ResourceService(
                service_dir=self.root,
                impl=object,
            )._load_impl
        )
        self.assertRaises(
            AssertionError,
            _base_service.ResourceService(
                service_dir=self.root,
                impl='socket.socket',
            )._load_impl
        )

        self.assertTrue(
            _base_service.ResourceService(
                service_dir=self.root,
                impl=MyTestService,
            )._load_impl()
        )
예제 #2
0
    def test__run_events(self, mock_poll):
        """Test event dispatcher.
        """
        # Access to a protected member _run_events of a client class
        # pylint: disable=W0212

        instance = _base_service.ResourceService(
            service_dir=self.root,
            impl='a.sample.module',
        )
        mock_callbacks = {
            i: {
                'callback': mock.Mock(return_value=i)
            }
            for i in range(3)
        }
        loop_poll = mock_poll.return_value
        loop_poll.poll.return_value = ((i, select.POLLIN) for i in range(2))

        res = instance._run_events(loop_poll, 42, mock_callbacks)

        loop_poll.poll.assert_called_with(42 * 1000)
        self.assertTrue(mock_callbacks[0]['callback'].called)
        self.assertTrue(mock_callbacks[1]['callback'].called)
        self.assertFalse(mock_callbacks[2]['callback'].called)

        self.assertTrue(res)
예제 #3
0
    def test_init(self):
        """Validate simple instanciation.
        """
        instance = _base_service.ResourceService(
            service_dir=self.root,
            impl='a.sample.module',
        )

        self.assertEqual(instance.name, 'module')
예제 #4
0
 def test_name(self):
     """Check how the name is derived from the class name.
     """
     self.assertEqual(
         _base_service.ResourceService(
             service_dir=self.root,
             impl='treadmill.services.MyClass',
         ).name,
         'MyClass',
     )
     self.assertEqual(
         _base_service.ResourceService(
             service_dir=self.root,
             impl='treadmill.services.MyClass',
         ).name,
         'MyClass',
     )
     self.assertEqual(
         _base_service.ResourceService(
             service_dir=self.root,
             impl=MyTestService,
         ).name,
         'MyTestService',
     )
예제 #5
0
    def test_run(self, mock_watchdog, mock_load_impl, mock_dirwatcher,
                 mock_poll):
        """Test the run method setup before the main loop.
        """
        # Access to a protected member _is_dead of a client class
        # pylint: disable=W0212

        mock_impl_instance = mock_load_impl.return_value.return_value
        mock_impl_instance.configure_mock(WATCHDOG_HEARTBEAT_SEC=60)
        mock_impl_instance.report_status.return_value = {'hello': 'world'}
        mock_impl_instance.event_handlers.return_value = [
            ('filenoA', 'eventsA', 'callbackA'),
            ('filenoB', 'eventsB', 'callbackB'),
        ]
        mock_dirwatcher.return_value.configure_mock(inotify='mock_inotiy', )
        instance = _base_service.ResourceService(
            service_dir=self.root,
            impl='MyTestService',
        )

        instance._is_dead = True
        instance.run(
            os.path.join(self.root, 'watchdogs'),
            'foo',
            bar='baz',
        )

        mock_load_impl.assert_called_with()
        # Make sure the implementation was passed the correct parameters.
        mock_load_impl.return_value.assert_called_with(
            'foo',
            bar='baz',
        )

        # Watchdog should be set
        mock_watchdog.assert_called_with(os.path.join(self.root,
                                                      'watchdogs'), )
        mock_watchdog.return_value.create.assert_called_with(
            content=mock.ANY, name='svc-MyTestService', timeout='60s')
        mock_watchdog_lease = mock_watchdog.return_value.create.return_value

        # Implementation should be given the root as argument to `initialize`
        mock_impl_instance.initialize.assert_called_with(self.root)
        # First watcher should be setup
        mock_dirwatcher.assert_called_with(os.path.join(
            self.root, 'resources'))
        # Then we check/cleanup pre-existing requests
        _base_service.ResourceService._check_requests.assert_called_with()
        _base_service.ResourceService._on_created.assert_has_calls([
            mock.call(mock_impl_instance, 'foo-1'),
            mock.call(mock_impl_instance, 'foo-2'),
        ])
        # Status should be queried first
        mock_impl_instance.report_status.assert_called_with()

        # The poll registration should be properly initialized
        mock_impl_instance.event_handlers.assert_called_with()
        instance._update_poll_registration.assert_called_with(
            mock_poll.return_value,
            {},
            [
                ('eventfd', mock.ANY, mock.ANY),
                ('mock_inotiy', mock.ANY, mock.ANY),
                ('status_socket', mock.ANY, mock.ANY),
                ('filenoA', mock.ANY, mock.ANY),
                ('filenoB', mock.ANY, mock.ANY),
            ],
        )

        # Loop exits immediately

        # Watchdog lease should be cleared
        mock_watchdog_lease.remove.assert_called_with()