def test_diff_add_remove(self):
        options = DummyOptions()
        supervisord = self._makeOne(options)

        pconfig = DummyPConfig(options, 'process1', 'process1')
        group1 = DummyPGroupConfig(options, 'group1', pconfigs=[pconfig])

        pconfig = DummyPConfig(options, 'process2', 'process2')
        group2 = DummyPGroupConfig(options, 'group2', pconfigs=[pconfig])

        new = [group1, group2]

        added, changed, removed = supervisord.diff_to_active()
        self.assertEqual(added, [])
        self.assertEqual(changed, [])
        self.assertEqual(removed, [])

        added, changed, removed = supervisord.diff_to_active(new)
        self.assertEqual(added, new)
        self.assertEqual(changed, [])
        self.assertEqual(removed, [])

        supervisord.options.process_group_configs = new
        added, changed, removed = supervisord.diff_to_active()
        self.assertEqual(added, new)

        supervisord.add_process_group(group1)
        supervisord.add_process_group(group2)

        pconfig = DummyPConfig(options, 'process3', 'process3')
        new_group1 = DummyPGroupConfig(options, pconfigs=[pconfig])

        pconfig = DummyPConfig(options, 'process4', 'process4')
        new_group2 = DummyPGroupConfig(options, pconfigs=[pconfig])

        new = [group2, new_group1, new_group2]

        added, changed, removed = supervisord.diff_to_active(new)
        self.assertEqual(added, [new_group1, new_group2])
        self.assertEqual(changed, [])
        self.assertEqual(removed, [group1])
 def test_runforever_select_dispatcher_exitnow(self):
     options = DummyOptions()
     supervisord = self._makeOne(options)
     pconfig = DummyPConfig(
         options,
         'foo',
         '/bin/foo',
     )
     gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
     pgroup = DummyProcessGroup(gconfig)
     from docker_supervisor.medusa import asyncore_25 as asyncore
     exitnow = DummyDispatcher(readable=True, error=asyncore.ExitNow)
     pgroup.dispatchers = {6: exitnow}
     supervisord.process_groups = {'foo': pgroup}
     options.select_result = [6], [], []
     options.test = True
     self.assertRaises(asyncore.ExitNow, supervisord.runforever)
    def test_add_process_group(self):
        options = DummyOptions()
        pconfig = DummyPConfig(options, 'foo', 'foo', '/bin/foo')
        gconfig = DummyPGroupConfig(options, 'foo', pconfigs=[pconfig])
        options.process_group_configs = [gconfig]
        supervisord = self._makeOne(options)

        self.assertEqual(supervisord.process_groups, {})

        result = supervisord.add_process_group(gconfig)
        self.assertEqual(supervisord.process_groups.keys(), ['foo'])
        self.assertTrue(result)

        group = supervisord.process_groups['foo']
        result = supervisord.add_process_group(gconfig)
        self.assertEqual(group, supervisord.process_groups['foo'])
        self.assertTrue(not result)
 def test_handle_sigusr2(self):
     options = DummyOptions()
     options._signal = signal.SIGUSR2
     pconfig1 = DummyPConfig(options, 'process1', 'process1',
                             '/bin/process1')
     from docker_supervisor.process import ProcessStates
     process1 = DummyProcess(pconfig1, state=ProcessStates.STOPPING)
     process1.delay = time.time() - 1
     supervisord = self._makeOne(options)
     pconfigs = [DummyPConfig(options, 'foo', 'foo', '/bin/foo')]
     options.process_group_configs = DummyPGroupConfig(options,
                                                       'foo',
                                                       pconfigs=pconfigs)
     supervisord.handle_signal()
     self.assertEqual(supervisord.options.mood, 1)
     self.assertEqual(options.logs_reopened, True)
     self.assertEqual(options.logger.data[0],
                      'received SIGUSR2 indicating log reopen request')
    def test_add_process_group_event(self):
        from docker_supervisor import events
        L = []

        def callback(event):
            L.append(1)

        events.subscribe(events.ProcessGroupAddedEvent, callback)
        options = DummyOptions()
        pconfig = DummyPConfig(options, 'foo', 'foo', '/bin/foo')
        gconfig = DummyPGroupConfig(options, 'foo', pconfigs=[pconfig])
        options.process_group_configs = [gconfig]
        supervisord = self._makeOne(options)

        supervisord.add_process_group(gconfig)

        options.test = True
        supervisord.runforever()
        self.assertEqual(L, [1])
    def test_remove_process_group(self):
        options = DummyOptions()
        pconfig = DummyPConfig(options, 'foo', 'foo', '/bin/foo')
        gconfig = DummyPGroupConfig(options, 'foo', pconfigs=[pconfig])
        supervisord = self._makeOne(options)

        self.assertRaises(KeyError, supervisord.remove_process_group, 'asdf')

        supervisord.add_process_group(gconfig)
        result = supervisord.remove_process_group('foo')
        self.assertEqual(supervisord.process_groups, {})
        self.assertTrue(result)

        supervisord.add_process_group(gconfig)
        supervisord.process_groups['foo'].unstopped_processes = [
            DummyProcess(None)
        ]
        result = supervisord.remove_process_group('foo')
        self.assertEqual(supervisord.process_groups.keys(), ['foo'])
        self.assertTrue(not result)
    def test_exit(self):
        options = DummyOptions()
        supervisord = self._makeOne(options)
        pconfig = DummyPConfig(
            options,
            'foo',
            '/bin/foo',
        )
        gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
        pgroup = DummyProcessGroup(gconfig)
        L = []

        def callback():
            L.append(1)

        supervisord.process_groups = {'foo': pgroup}
        supervisord.options.mood = 0
        supervisord.options.test = True
        from docker_supervisor.medusa import asyncore_25 as asyncore
        self.assertRaises(asyncore.ExitNow, supervisord.runforever)
        self.assertEqual(pgroup.all_stopped, True)
    def test_exit_delayed(self):
        options = DummyOptions()
        supervisord = self._makeOne(options)
        pconfig = DummyPConfig(
            options,
            'foo',
            '/bin/foo',
        )
        process = DummyProcess(pconfig)
        gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
        pgroup = DummyProcessGroup(gconfig)
        pgroup.unstopped_processes = [process]
        L = []

        def callback():
            L.append(1)

        supervisord.process_groups = {'foo': pgroup}
        supervisord.options.mood = 0
        supervisord.options.test = True
        supervisord.runforever()
        self.assertNotEqual(supervisord.lastshutdownreport, 0)
    def test_runforever_stopping_emits_events(self):
        options = DummyOptions()
        supervisord = self._makeOne(options)
        gconfig = DummyPGroupConfig(options)
        pgroup = DummyProcessGroup(gconfig)
        supervisord.process_groups = {'foo': pgroup}
        supervisord.options.mood = -1
        L = []

        def callback(event):
            L.append(event)

        from docker_supervisor import events
        events.subscribe(events.SupervisorStateChangeEvent, callback)
        from docker_supervisor.medusa import asyncore_25 as asyncore
        options.test = True
        self.assertRaises(asyncore.ExitNow, supervisord.runforever)
        self.assertTrue(pgroup.all_stopped)
        self.assertTrue(isinstance(L[0], events.SupervisorRunningEvent))
        self.assertTrue(isinstance(L[0], events.SupervisorStateChangeEvent))
        self.assertTrue(isinstance(L[1], events.SupervisorStoppingEvent))
        self.assertTrue(isinstance(L[1], events.SupervisorStateChangeEvent))
 def test_runforever_select_dispatchers(self):
     options = DummyOptions()
     supervisord = self._makeOne(options)
     pconfig = DummyPConfig(
         options,
         'foo',
         '/bin/foo',
     )
     gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
     pgroup = DummyProcessGroup(gconfig)
     readable = DummyDispatcher(readable=True)
     writable = DummyDispatcher(writable=True)
     error = DummyDispatcher(writable=True, error=OSError)
     pgroup.dispatchers = {6: readable, 7: writable, 8: error}
     supervisord.process_groups = {'foo': pgroup}
     options.select_result = [6], [7, 8], []
     options.test = True
     supervisord.runforever()
     self.assertEqual(pgroup.transitioned, True)
     self.assertEqual(readable.read_event_handled, True)
     self.assertEqual(writable.write_event_handled, True)
     self.assertEqual(error.error_handled, True)
 def test_main_first(self):
     options = DummyOptions()
     pconfig = DummyPConfig(options, 'foo', 'foo', '/bin/foo')
     gconfigs = [DummyPGroupConfig(options, 'foo', pconfigs=[pconfig])]
     options.process_group_configs = gconfigs
     options.test = True
     options.first = True
     supervisord = self._makeOne(options)
     supervisord.main()
     self.assertEqual(options.environment_processed, True)
     self.assertEqual(options.fds_cleaned_up, False)
     self.assertEqual(options.rlimits_set, True)
     self.assertEqual(options.make_logger_messages,
                      (['setuid_called'], [], ['rlimits_set']))
     self.assertEqual(options.autochildlogdir_cleared, True)
     self.assertEqual(len(supervisord.process_groups), 1)
     self.assertEqual(supervisord.process_groups['foo'].config.options,
                      options)
     self.assertEqual(options.environment_processed, True)
     self.assertEqual(options.httpservers_opened, True)
     self.assertEqual(options.signals_set, True)
     self.assertEqual(options.daemonized, True)
     self.assertEqual(options.pidfile_written, True)
     self.assertEqual(options.cleaned_up, True)