Exemple #1
0
    def test__create_supervision_tree(self):
        """Test creation of the supervision tree."""
        # Access protected module _create_supervision_tree
        # pylint: disable=W0212
        app = utils.to_obj(
            {
                'type': 'native',
                'proid': 'myproid',
                'name': 'myproid.test#0',
                'uniqueid': 'ID1234',
                'environment': 'prod',
                'services': [
                    {
                        'name': 'command1',
                        'proid': 'test',
                        'command': '/path/to/command',
                        'restart': {
                            'limit': 3,
                            'interval': 60,
                        },
                        'environ': {},
                        'config': None,
                        'downed': False,
                        'trace': True,
                    },
                    {
                        'name': 'command2',
                        'proid': 'test',
                        'command': '/path/to/other/command',
                        'restart': {
                            'limit': 3,
                            'interval': 60,
                        },
                        'environ': {},
                        'config': None,
                        'downed': False,
                        'trace': True,
                    }
                ],
                'system_services': [
                    {
                        'name': 'command3',
                        'proid': 'root',
                        'command': '/path/to/sbin/command',
                        'restart': {
                            'limit': 5,
                            'interval': 60,
                        },
                        'environ': {},
                        'config': None,
                        'downed': True,
                        'trace': False,
                    },
                    {
                        'name': 'command4',
                        'proid': 'root',
                        'command': '/path/to/other/sbin/command',
                        'restart': None,  # not monitored
                        'environ': {},
                        'config': None,
                        'downed': False,
                        'trace': False,
                    }
                ],
                'vring': {
                    'cells': ['a', 'b']
                },
            }
        )
        base_dir = '/some/dir'

        mock_service_dir = mock.create_autospec(supervisor.ScanDir)
        treadmill.supervisor.create_scan_dir.return_value =\
            mock_service_dir

        native.create_supervision_tree(
            self.tm_env,
            base_dir,
            os.path.join(base_dir, 'root'),
            app,
            '/test/cgroup/path'
        )

        treadmill.supervisor.create_scan_dir.assert_has_calls([
            mock.call(
                os.path.join(base_dir, 'sys'),
                finish_timeout=6000,
                wait_cgroups='/test/cgroup/path'
            ),
            mock.call().write(),
            mock.call(
                os.path.join(base_dir, 'services'),
                finish_timeout=5000,
            ),
            mock.call().write(),
        ])

        treadmill.supervisor.create_service.assert_has_calls([
            # system services
            mock.call(mock_service_dir,
                      name='command3',
                      app_run_script='/path/to/sbin/command',
                      userid='root',
                      environ_dir='/some/dir/env',
                      environ={},
                      environment='prod',
                      downed=True,
                      monitor_policy={
                          'limit': 5,
                          'interval': 60,
                          'tombstone': {
                              'uds': False,
                              'path': self.services_tombstone_dir,
                              'id': 'myproid.test-0-0000000ID1234,command3'
                          }
                      },
                      trace=None),
            mock.call(mock_service_dir,
                      name='command4',
                      app_run_script='/path/to/other/sbin/command',
                      userid='root',
                      environ_dir='/some/dir/env',
                      environ={},
                      environment='prod',
                      downed=False,
                      monitor_policy=None,
                      trace=None),
            # user services
            mock.call(mock_service_dir,
                      name='command1',
                      app_run_script='/path/to/command',
                      userid='test',
                      environ_dir='/env',
                      environ={},
                      environment='prod',
                      downed=False,
                      monitor_policy={
                          'limit': 3,
                          'interval': 60,
                          'tombstone': {
                              'uds': True,
                              'path': '/run/tm_ctl/tombstone',
                              'id': 'myproid.test-0-0000000ID1234,command1'
                          }
                      },
                      log_run_script='s6.app-logger.run',
                      trace={
                          'instanceid': 'myproid.test#0',
                          'uniqueid': 'ID1234',
                          'service': 'command1',
                          'path': '/run/tm_ctl/appevents'
                      }),
            mock.call(mock_service_dir,
                      name='command2',
                      app_run_script='/path/to/other/command',
                      userid='test',
                      environ_dir='/env',
                      environ={},
                      environment='prod',
                      downed=False,
                      monitor_policy={
                          'limit': 3,
                          'interval': 60,
                          'tombstone': {
                              'uds': True,
                              'path': '/run/tm_ctl/tombstone',
                              'id': 'myproid.test-0-0000000ID1234,command2'
                          }
                      },
                      log_run_script='s6.app-logger.run',
                      trace={
                          'instanceid': 'myproid.test#0',
                          'uniqueid': 'ID1234',
                          'service': 'command2',
                          'path': '/run/tm_ctl/appevents'
                      })
        ])

        self.assertEqual(2, mock_service_dir.write.call_count)

        treadmill.fs.linux.mount_bind.assert_has_calls([
            mock.call('/some/dir/root', '/services',
                      source='/some/dir/services',
                      read_only=False, recursive=False),
            mock.call('/some/dir/root', '/run/tm_ctl',
                      source=os.path.join(self.root, 'ctl'),
                      read_only=False, recursive=False),
        ])

        treadmill.fs.rmtree_safe.assert_called_once_with(
            '/some/dir/sys/command0'
        )
Exemple #2
0
    def test__create_supervision_tree(self):
        """Test creation of the supervision tree."""
        # Access protected module _create_supervision_tree
        # pylint: disable=W0212
        app = utils.to_obj({
            'type':
            'native',
            'proid':
            'myproid',
            'name':
            'myproid.test#0',
            'uniqueid':
            'ID1234',
            'environment':
            'prod',
            'services': [{
                'name': 'command1',
                'command': '/path/to/command',
                'restart': {
                    'limit': 3,
                    'interval': 60,
                },
            }, {
                'name': 'command2',
                'command': '/path/to/other/command',
                'restart': {
                    'limit': 3,
                    'interval': 60,
                },
            }],
            'system_services': [{
                'name': 'command3',
                'command': '/path/to/sbin/command',
                'restart': {
                    'limit': 5,
                    'interval': 60,
                },
            }, {
                'name': 'command4',
                'command': '/path/to/other/sbin/command',
                'restart': {
                    'limit': 5,
                    'interval': 60,
                },
            }],
            'vring': {
                'cells': ['a', 'b']
            },
        })
        base_dir = '/some/dir'

        native.create_supervision_tree(base_dir, app)

        treadmill.fs.mkdir_safe.assert_has_calls([
            mock.call('/some/dir/root/services'),
            mock.call('/some/dir/services'),
            mock.call('/some/dir/sys/.s6-svscan'),
            mock.call('/some/dir/services/.s6-svscan'),
            mock.call('/some/dir/services/command1/log'),
            mock.call('/some/dir/services/command2/log'),
            mock.call('/some/dir/services/command3/log'),
            mock.call('/some/dir/services/command4/log'),
            mock.call('/some/dir/sys/vring.a'),
            mock.call('/some/dir/sys/vring.a/log'),
            mock.call('/some/dir/sys/vring.b'),
            mock.call('/some/dir/sys/vring.b/log'),
            mock.call('/some/dir/sys/monitor'),
            mock.call('/some/dir/sys/monitor/log'),
            mock.call('/some/dir/sys/register'),
            mock.call('/some/dir/sys/register/log'),
            mock.call('/some/dir/sys/hostaliases'),
            mock.call('/some/dir/sys/hostaliases/log'),
            mock.call('/some/dir/sys/start_container'),
            mock.call('/some/dir/sys/start_container/log'),
        ])
        treadmill.fs.mount_bind.assert_called_with(
            '/some/dir/root',
            '/services',
            '/some/dir/services',
        )

        pwd.getpwnam.assert_has_calls(
            [mock.call('myproid'), mock.call('root')], any_order=True)

        treadmill.supervisor.create_service.assert_has_calls([
            # user services
            mock.call('/some/dir/services',
                      'myproid',
                      mock.ANY,
                      mock.ANY,
                      'command1',
                      '/path/to/command',
                      as_root=True,
                      down=True,
                      envdirs=['/environ/app', '/environ/sys'],
                      env='prod'),
            mock.call('/some/dir/services',
                      'myproid',
                      mock.ANY,
                      mock.ANY,
                      'command2',
                      '/path/to/other/command',
                      as_root=True,
                      down=True,
                      envdirs=['/environ/app', '/environ/sys'],
                      env='prod'),
            # system services
            mock.call('/some/dir/services',
                      'root',
                      mock.ANY,
                      mock.ANY,
                      'command3',
                      '/path/to/sbin/command',
                      as_root=True,
                      down=False,
                      envdirs=['/environ/sys'],
                      env='prod'),
            mock.call('/some/dir/services',
                      'root',
                      mock.ANY,
                      mock.ANY,
                      'command4',
                      '/path/to/other/sbin/command',
                      as_root=True,
                      down=False,
                      envdirs=['/environ/sys'],
                      env='prod')
        ])

        treadmill.utils.create_script.assert_has_calls([
            mock.call('/some/dir/sys/.s6-svscan/finish',
                      'svscan.finish',
                      timeout=mock.ANY),
            mock.call('/some/dir/services/.s6-svscan/finish',
                      'svscan.finish',
                      timeout=mock.ANY),
            mock.call('/some/dir/services/command1/log/run', 'logger.run'),
            mock.call('/some/dir/services/command2/log/run', 'logger.run'),
            mock.call('/some/dir/services/command3/log/run', 'logger.run'),
            mock.call('/some/dir/services/command4/log/run', 'logger.run'),
            mock.call('/some/dir/sys/vring.a/run',
                      'supervisor.run_sys',
                      cmd=mock.ANY),
            mock.call('/some/dir/sys/vring.a/log/run', 'logger.run'),
            mock.call('/some/dir/sys/vring.b/run',
                      'supervisor.run_sys',
                      cmd=mock.ANY),
            mock.call('/some/dir/sys/vring.b/log/run', 'logger.run'),
            mock.call('/some/dir/sys/monitor/run',
                      'supervisor.run_sys',
                      cmd=mock.ANY),
            mock.call('/some/dir/sys/monitor/log/run', 'logger.run'),
            mock.call('/some/dir/sys/register/run',
                      'supervisor.run_sys',
                      cmd=mock.ANY),
            mock.call('/some/dir/sys/register/log/run', 'logger.run'),
            mock.call('/some/dir/sys/hostaliases/run',
                      'supervisor.run_sys',
                      cmd=mock.ANY),
            mock.call('/some/dir/sys/hostaliases/log/run', 'logger.run'),
            mock.call('/some/dir/sys/start_container/run',
                      'supervisor.run_sys',
                      cmd=('/bin/chroot /some/dir/root /path/to/pid1 '
                           '-m -p -i /path/to/s6-svscan /services')),
            mock.call('/some/dir/sys/start_container/log/run', 'logger.run'),
        ])
        treadmill.utils.touch.assert_has_calls([
            mock.call('/some/dir/sys/start_container/down'),
        ])