def test_running_running(self):
        def get_container_info():
            started_at = self.base_date
            running = True
            return started_at, running

        def load_state():
            return {
                'status': 'running',
                'started_at': self.base_date
            }

        def save_state(state):
            self.assertEqual(state['status'], 'running')
            self.assertEqual(
                state['started_at'], self.base_date)

        def _send(msg):
            raise AssertionError("This routine should not be running")

        with mock.patch('watchdog.get_container_info', get_container_info), \
                mock.patch('watchdog.load_state', load_state), \
                mock.patch('watchdog.save_state', save_state), \
                mock.patch('watchdog.get_config', get_config), \
                mock.patch('watchdog._send', _send):
            watchdog.check_docker()
    def test_crashed_crashed_still_crashing(self):
        def get_container_info():
            started_at = self.base_date + datetime.timedelta(days=1)
            running = False
            return started_at, running

        def load_state():
            return {
                'status': 'crashed',
                'started_at': self.base_date
            }

        def get_utcnow():
            return self.base_date + datetime.timedelta(seconds=10)

        def save_state(state):
            self.assertEqual(state['status'], 'crashed')
            self.assertEqual(state['started_at'], self.base_date)

        def _send(msg):
            raise AssertionError("This routine should not be running")

        with mock.patch('watchdog.get_container_info', get_container_info), \
                mock.patch('watchdog.load_state', load_state), \
                mock.patch('watchdog.save_state', save_state), \
                mock.patch('watchdog.get_config', get_config), \
                mock.patch('watchdog.get_utcnow', get_utcnow), \
                mock.patch('watchdog._send', _send):
            watchdog.check_docker()
    def test_crashed_running(self):
        def gethostname():
            return "Foo"

        def get_container_info():
            started_at = self.base_date + datetime.timedelta(minutes=2)
            running = True
            return started_at, running

        def load_state():
            return {
                'status': 'crashed',
                'started_at': self.base_date
            }

        def get_utcnow():
            return self.base_date + datetime.timedelta(seconds=130)

        def save_state(state):
            self.assertEqual(state['status'], 'running')
            self.assertEqual(
                state['started_at'], self.base_date + datetime.timedelta(minutes=2))

        def _send(msg):
            self.assertEqual(msg['Subject'], "RECOVERED message")
            self.assertEqual(msg.get_payload(), "Server Foo recovered")

        with mock.patch('watchdog.get_container_info', get_container_info), \
                mock.patch('watchdog.load_state', load_state), \
                mock.patch('watchdog.save_state', save_state), \
                mock.patch('watchdog.get_config', get_config), \
                mock.patch('watchdog.get_utcnow', get_utcnow), \
                mock.patch('watchdog._send', _send), \
                mock.patch('socket.gethostname', gethostname):
            watchdog.check_docker()
    def test_running_crashed(self):
        def gethostname():
            return "Foo"

        def get_container_info():
            started_at = self.base_date + datetime.timedelta(days=1)
            started_at = started_at.replace(tzinfo = pytz.utc)
            running = True
            return started_at, running

        def load_state():
            return {
                'status': 'running',
                'started_at': self.base_date
            }

        def save_state(state):
            self.assertEqual(state['status'], 'crashed')
            self.assertEqual(
                state['started_at'], self.base_date + datetime.timedelta(days=1))

        def _send(msg):
            self.assertEqual(msg['Subject'], "CRASH message")
            self.assertEqual(msg.get_payload(), "Server Foo crashed")

        with mock.patch('watchdog.get_container_info', get_container_info), \
                mock.patch('watchdog.load_state', load_state), \
                mock.patch('watchdog.save_state', save_state), \
                mock.patch('watchdog.get_config', get_config), \
                mock.patch('watchdog._send', _send), \
                mock.patch('socket.gethostname', gethostname):
            watchdog.check_docker()