Exemple #1
0
class Context(object):
    def __init__(self):
        self.hostname = None
        self.connection = Client()
        self.ml = None
        self.logger = logging.getLogger('cli')
        self.plugin_dirs = []
        self.task_callbacks = {}
        self.plugins = {}
        self.variables = VariableStore()
        self.root_ns = RootNamespace('')
        self.event_masks = ['*']
        self.event_divert = False
        self.event_queue = Queue.Queue()
        self.keepalive_timer = None
        config.instance = self

    @property
    def is_interactive(self):
        return os.isatty(sys.stdout.fileno())

    def start(self):
        self.discover_plugins()
        self.connect()

    def connect(self):
        self.connection.connect(self.hostname)

    def login(self, user, password):
        try:
            self.connection.login_user(user, password)
            self.connection.subscribe_events(*EVENT_MASKS)
            self.connection.on_event(self.handle_event)
            self.connection.on_error(self.connection_error)

        except RpcException, e:
            if e.code == errno.EACCES:
                self.connection.disconnect()
                output_msg(_("Wrong username od password"))
                sys.exit(1)

        self.login_plugins()
Exemple #2
0
class Context(object):
    def __init__(self):
        self.hostname = None
        self.connection = Client()
        self.ml = None
        self.logger = logging.getLogger('cli')
        self.plugin_dirs = []
        self.task_callbacks = {}
        self.plugins = {}
        self.variables = VariableStore()
        self.root_ns = RootNamespace('')
        self.event_masks = ['*']
        self.event_divert = False
        self.event_queue = Queue.Queue()
        self.keepalive_timer = None
        config.instance = self

    @property
    def is_interactive(self):
        return os.isatty(sys.stdout.fileno())

    def start(self):
        self.discover_plugins()
        self.connect()

    def connect(self):
        self.connection.connect(self.hostname)

    def login(self, user, password):
        try:
            self.connection.login_user(user, password)
            self.connection.subscribe_events(*EVENT_MASKS)
            self.connection.on_event(self.handle_event)
            self.connection.on_error(self.connection_error)

        except RpcException, e:
            if e.code == errno.EACCES:
                self.connection.disconnect()
                output_msg(_("Wrong username od password"))
                sys.exit(1)

        self.login_plugins()
Exemple #3
0
class BaseTestCase(unittest.TestCase):
    class TaskState(object):
        def __init__(self):
            self.tid = None
            self.state = None
            self.message = None
            self.result = None
            self.ended = Event()

    def __init__(self, methodName):
        super(BaseTestCase, self).__init__(methodName)
        self.tasks = {}
        self.tasks_lock = Lock()
        self.conn = None
        self.task_timeout = 20

    def setUp(self):
        try:
            self.conn = Client()
            self.conn.event_callback = self.on_event
            self.conn.connect(os.getenv('TESTHOST', '127.0.0.1'))
            self.conn.login_service('tests')
            self.conn.subscribe_events('*')
        except:
            raise

    def tearDown(self):
        self.conn.disconnect()

    def submitTask(self, name, *args):
        self.tasks_lock.acquire()
        try:
            tid = self.conn.call_sync('task.submit', name, args)
        except RpcException:
            self.tasks_lock.release()
            raise

        self.tasks[tid] = self.TaskState()
        self.tasks[tid].tid = tid
        self.tasks_lock.release()
        return tid

    def assertTaskCompletion(self, tid):
        t = self.tasks[tid]
        if not t.ended.wait(self.task_timeout):
            self.fail('Task {0} timed out'.format(tid))

        self.assertEqual(t.state, 'FINISHED', msg=t.message)

    def assertTaskFailure(self, tid):
        t = self.tasks[tid]
        if not t.ended.wait(self.task_timeout):
            self.fail('Task {0} timed out'.format(tid))

        self.assertNotEqual(t.state, 'FINISHED', msg=t.message)

    def assertSeenEvent(self, name, func=None):
        pass

    def getTaskResult(self, tid):
        t = self.tasks[tid]
        return t.result

    def on_event(self, name, args):
        self.tasks_lock.acquire()

        if name == 'task.updated':
            t = self.tasks[args['id']]
            t.state = args['state']
            if t.state in ('FINISHED', 'FAILED'):
                t.result = args['result'] if 'result' in args else None
                t.ended.set()

        elif name == 'task.progress':
            t = self.tasks[args['id']]
            t.message = args['message']

        self.tasks_lock.release()