示例#1
0
 def test_main_foreground(self, mock_unlink, mock_kill, mock_open):
     with mock.patch.object(control, 'application') as ml:
         mock_kill.side_effect = OSError()
         vbmcd.main(['--foreground'])
         mock_kill.assert_called_once()
         ml.assert_called_once()
         mock_unlink.assert_called_once()
示例#2
0
 def test_main_foreground(self, mock_unlink, mock_kill, mock_open):
     with mock.patch.object(control, 'application') as ml:
         mock_kill.side_effect = OSError()
         vbmcd.main(['--foreground'])
         mock_kill.assert_called_once()
         ml.assert_called_once()
         mock_unlink.assert_called_once()
示例#3
0
 def test_main_background(self, mock_unlink, mock_kill, mock_open):
     with mock.patch.object(utils, 'detach_process') as dp, \
             mock.patch.object(control, 'application') as ml:
         mock_kill.side_effect = OSError()
         vbmcd.main([])
         mock_kill.assert_called_once()
         dp.assert_called_once()
         ml.assert_called_once()
         mock_unlink.assert_called_once()
示例#4
0
 def test_main_background(self, mock_unlink, mock_kill, mock_open):
     with mock.patch.object(utils, 'detach_process') as dp, \
             mock.patch.object(control, 'application') as ml:
         mock_kill.side_effect = OSError()
         vbmcd.main([])
         mock_kill.assert_called_once()
         dp.assert_called_once()
         ml.assert_called_once()
         mock_unlink.assert_called_once()
示例#5
0
 def test_main_background(self, mock_unlink, mock_kill, mock_open):
     with mock.patch.object(utils, 'detach_process') as mock_dp:
         with mock.patch.object(control, 'application') as mock_ml:
             mock_kill.side_effect = OSError()
             mock_dp.return_value.__enter__.return_value = 0
             vbmcd.main([])
             mock_kill.assert_called_once()
             mock_dp.assert_called_once()
             mock_ml.assert_called_once()
             mock_unlink.assert_called_once()
示例#6
0
    def communicate(self, command, args, no_daemon=False):

        data_out = self.to_dict(args)

        data_out.update(command=command)

        data_out = json.dumps(data_out)

        server_port = CONF['default']['server_port']

        context = socket = None

        try:
            context = zmq.Context()
            socket = context.socket(zmq.REQ)
            socket.setsockopt(zmq.LINGER, 5)
            socket.connect("tcp://127.0.0.1:%s" % server_port)

            poller = zmq.Poller()
            poller.register(socket, zmq.POLLIN)

            while True:
                try:
                    if data_out:
                        socket.send(data_out.encode('utf-8'))

                    socks = dict(poller.poll(timeout=self.SERVER_TIMEOUT))
                    if socket in socks and socks[socket] == zmq.POLLIN:
                        data_in = socket.recv()
                        break

                    raise zmq.ZMQError(
                        zmq.RCVTIMEO, msg='Server response timed out')

                except zmq.ZMQError as ex:
                    LOG.debug('Server at %(port)s connection error: '
                              '%(error)s', {'port': server_port, 'error': ex})

                    if no_daemon:
                        msg = ('Server at %(port)s may be dead, will not '
                               'try to revive it' % {'port': server_port})
                        LOG.error(msg)
                        raise VirtualBMCError(msg)

                no_daemon = True

                LOG.debug("Attempting to start `vbmcd` behind the "
                          "scenes. Consider configuring your system to "
                          "manage `vbmcd` via systemd. Automatic "
                          "`vbmcd` start up will be removed in the "
                          "future releases!")

                # attempt to start and daemonize the server
                if os.fork() == 0:
                    # this will also fork and detach properly
                    vbmcd.main([])

                # TODO(etingof): perform some more retries
                time.sleep(CONF['default']['server_spawn_wait'] / 1000.)

                # MQ will deliver the original message to the daemon
                # we've started
                data_out = {}

        finally:
            if socket:
                socket.close()
                context.destroy()

        try:
            data_in = json.loads(data_in.decode('utf-8'))

        except ValueError as ex:
            msg = 'Server response parsing error %(error)s' % {'error': ex}
            LOG.error(msg)
            raise VirtualBMCError(msg)

        rc = data_in.pop('rc', None)
        if rc:
            msg = '(%(rc)s): %(msg)s' % {
                'rc': rc,
                'msg': '\n'.join(data_in.get('msg', ()))
            }
            LOG.error(msg)
            raise VirtualBMCError(msg)

        return data_in