Ejemplo n.º 1
0
def test_executeRemoteCommand(monkeypatch):
    def mock_connect(self, hostname, username=None, key_filename=None):
        return True

    def mock_exec_command(self, command):
        stdin = io.StringIO('')
        stdout = io.StringIO('Mock STDOUT\n0')
        stderr = io.StringIO('')

        return stdin, stdout, stderr

    monkeypatch.setattr(paramiko.SSHClient, 'connect', mock_connect)
    monkeypatch.setattr(paramiko.SSHClient, 'exec_command', mock_exec_command)

    path = os.path.join(
        os.path.dirname(__file__),
        'etc/localhost.job',
    )

    j = job(path)

    cmd = command()

    (status, stdout, stderr) = cmd.executeRemoteCommand(j, 'uptime')

    assert status == 0
    assert 'Mock STDOUT' in stdout
Ejemplo n.º 2
0
    def __init__(self):
        logging.basicConfig(
            format='%(asctime)s %(levelname)s %(name)s: %(message)s',
            datefmt='%Y/%m/%d-%H:%M:%S',
            level=logging.DEBUG)
        # Catch signals and send to signal_handler
        signal.signal(signal.SIGHUP, self.signal_handler)
        signal.signal(signal.SIGINT, self.signal_handler)
        signal.signal(signal.SIGTERM, self.signal_handler)
        #FIXME add try fail on config load
        with open('etc/mcp.conf', 'r') as confile:
            global CONFIG
            CONFIG = yaml.load(confile)

        self.state = dict()
        self.state['agent'] = dict()
        #self.storage = lib.storage(CONFIG['storage'], self.state)
        self.eventqueue = Queue()
        self.subscription = dict()
        self.command = command(self.queueevent)  # TODO: config?
        #self.command = command(self.queueevent, CONFIG)
        self.scheduler = schedule(self.queueevent, CONFIG)
        self.loadagents()
        #TODO: return agent load result

        # Create queue worker threads
        for i in range(WORKERNUM):
            t = Thread(target=self.dispatch)
            t.daemon = True
            t.start()
Ejemplo n.º 3
0
 def executeJobs(self,  job,  commands):
     comm = command()
     for c in commands:
         if c['local']:
             logger().debug('Running local command %s' % c['script'])
             c['returncode'],  c['stdout'],  c['stderr'] = comm.executeLocalCommand(job,  c['script'])
             logger().debug('command %s' % ('succeeded' if c['returncode'] == 0 else 'failed'))
         else:
             logger().debug('Running remote command %s' % c['script'])
             c['returncode'],  c['stdout'],  c['stderr'] =  comm.executeRemoteCommand(job,  c['script'])
             logger().debug('command %s' % ('succeeded' if c['returncode'] == 0 else 'failed'))
         if c['returncode'] != 0 and c['continueonerror'] == False:
             logger().debug('command failed and continueonerror = false: exception')
             raise CommandException('Hook %s failed to execute' % c['script'])
Ejemplo n.º 4
0
def test_executeLocalCommand():
    path = os.path.join(
        os.path.dirname(__file__),
        'etc/localhost.job',
    )

    j = job(path)

    cmd = command()

    (status, stdout, stderr) = cmd.executeLocalCommand(j, 'uptime')

    assert status == 0
    assert 'load average' in stdout
    assert stderr == ''
Ejemplo n.º 5
0
def test_checkRemoteHostViaSshProtocol(monkeypatch):
    def mock_connect(self, hostname, username=None, key_filename=None):
        return True

    monkeypatch.setattr(paramiko.SSHClient, 'connect', mock_connect)

    path = os.path.join(
        os.path.dirname(__file__),
        'etc/localhost.job',
    )

    j = job(path)

    cmd = command()

    ret = cmd.checkRemoteHostViaSshProtocol(j)

    assert ret is True
Ejemplo n.º 6
0
 def executeJobs(self, job, commands):
     comm = command()
     for c in commands:
         if c['local']:
             logger().debug('Running local command %s' % c['script'])
             c['returncode'], c['stdout'], c[
                 'stderr'] = comm.executeLocalCommand(job, c['script'])
             logger().debug(
                 'command %s' %
                 ('succeeded' if c['returncode'] == 0 else 'failed'))
         else:
             logger().debug('Running remote command %s' % c['script'])
             c['returncode'], c['stdout'], c[
                 'stderr'] = comm.executeRemoteCommand(job, c['script'])
             logger().debug(
                 'command %s' %
                 ('succeeded' if c['returncode'] == 0 else 'failed'))
         if c['returncode'] != 0 and c['continueonerror'] is False:
             logger().debug(
                 'command failed and continueonerror = false: exception')
             raise CommandException('Hook %s failed to execute' %
                                    c['script'])
Ejemplo n.º 7
0
def test_checkRemoteHostViaSshProtocol_exception(monkeypatch, caplog):
    logger().debuglevel = 3

    def mock_connect(self, hostname, username=None, key_filename=None):
        raise IOError('Mock connection failed')

    monkeypatch.setattr(paramiko.SSHClient, 'connect', mock_connect)

    path = os.path.join(
        os.path.dirname(__file__),
        'etc/localhost.job',
    )

    j = job(path)

    cmd = command()

    ret = cmd.checkRemoteHostViaSshProtocol(j)

    assert 'Error while connecting to host' in caplog.text

    assert ret is False
Ejemplo n.º 8
0
def test_executeRemoteCommand_exception(monkeypatch, caplog):
    logger().debuglevel = 3

    def mock_connect(self, hostname, username=None, key_filename=None):
        raise IOError('Mock connection failed')

    monkeypatch.setattr(paramiko.SSHClient, 'connect', mock_connect)

    path = os.path.join(
        os.path.dirname(__file__),
        'etc/localhost.job',
    )

    j = job(path)

    cmd = command()

    (status, stdout, stderr) = cmd.executeRemoteCommand(j, 'uptime')

    assert 'Error while connecting to host' in caplog.text

    assert status == 1
    assert stdout == ''
    assert 'Mock connection failed' in stderr