Ejemplo n.º 1
0
    def test_03_node_emulation(self):
        def onOperationResultRoutine(operation_name, session_id, status, ret_params_map):
            print '[DEBUG] callback: ', session_id, status, ret_params_map
            self.assertEqual(session_id>0, True)
            self.assertEqual(operation_name, 'TEST_OPERATION')
            self.assertEqual(status, operationsEngine.ORS_COMPLETE)
            self.assertEqual(ret_params_map, {'127.0.0.1':{}})

        def onOperationResultRoutine2(operation_name, session_id, status, ret_params_map):
            print '[DEBUG] callback2: ', session_id, status, ret_params_map
            self.assertEqual(session_id>0, True)
            self.assertEqual(operation_name, 'TEST_OPERATION')
            self.assertEqual(status, operationsEngine.ORS_COMPLETE)
            self.assertEqual(ret_params_map, {'127.0.0.1':{'param1':23}})

        sessions = []

        #call on node
        session_id, code, message = ENGINE.callOperationOnNodes('fabregas',
                            ['127.0.0.1'], 'TEST_OPERATION', {}, onOperationResultRoutine)
        sessions.append(session_id)
        self.assertEqual(session_id>0, True)
        self.assertEqual(code, 0)
        self.assertEqual(len(message)>0, True)

        client = FRIClient('127.0.0.1', friBase.FRI_BIND_PORT)
        err_code, err_message = client.call({'id':session_id, 'node':'127.0.0.1', 'ret_code':0, 'ret_message':'ok', 'progress':'100'})
        self.assertEqual(err_code, 0)

        session_id, code, message = ENGINE.callOperationOnNodes('fabregas',
                            ['127.0.0.1'], 'TEST_OPERATION', {}, onOperationResultRoutine2)
        sessions.append(session_id)
        self.assertEqual(session_id>0, True)
        self.assertEqual(code, 0)
        self.assertEqual(len(message)>0, True)

        client = FRIClient('127.0.0.1', friBase.FRI_BIND_PORT)
        err_code, err_message = client.call({'id':session_id, 'node':'127.0.0.1', 'ret_code':0, 'ret_message':'ok', 'progress':'20','ret_parameters':{'param1':23}})
        self.assertEqual(err_code, 0)
        err_code, err_message = client.call({'id':session_id, 'node':'127.0.0.1', 'ret_code':0, 'ret_message':'ok', 'progress':'75','ret_parameters':{'param1':23}})
        self.assertEqual(err_code, 0)
        err_code, err_message = client.call({'id':session_id, 'node':'127.0.0.1', 'ret_code':0, 'ret_message':'ok', 'progress':'100','ret_parameters':{'param1':23}})
        self.assertEqual(err_code, 0)

        #waiting operations finishing
        for session_id in sessions:
            while True:
                status = ENGINE.getOperationStatus(session_id)
                if status == operationsEngine.ORS_INPROGRESS:
                    time.sleep(1)
                    continue
                break
Ejemplo n.º 2
0
    def test_01_test_dbus_iface(self):
        def process_func():
            #install test plugin
            os.mkdir(TEST_PLUGIN_DIR)
            open(TEST_PLUGIN_DIR+'/__init__.py','w').write(plugin_src)

            #init d-bus service
            gobject.threads_init()
            dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
            bus = dbus.SystemBus()
            name = dbus.service.BusName(NODES_MANAGER_INTERFACE, bus)
            nodes_manager = NodesManagerService(bus, '/nodes/manager_test')
            loop = gobject.MainLoop()
            loop.run()
            print 'process for D-BUS service started'

        p = multiprocessing.Process(target=process_func, args=())
        p.start()
        print 'process for D-BUS service starting...'

        time.sleep(1)

        try:
            def onSignal(session_id, status, ret_params):
                print 'SIGNAL: ', session_id, status, ret_params

                self.assertEqual(session_id > 0, True)
                self.assertEqual(status > 0, True)
                if status == 1:
                    self.assertEqual(ret_params.has_key('127.0.0.1'), True)

            #init client
            print 'init client'
            bus = dbus.SystemBus()
            proxy = bus.get_object(NODES_MANAGER_INTERFACE, '/nodes/manager_test')
            print proxy.Introspect()
            proxy.connect_to_signal('onOperationFinishEvent', onSignal, dbus_interface=NODES_MANAGER_INTERFACE)
            proxy.setLogLevel('DEBUG')

            #negative calls
            session_id, ret_code, ret_message = proxy.callOperationOnCluster('fabregas', 'TEST_CLUSTER', 'TEST_OPERATION', {'error':'yes'})
            self.assertEqual(ret_code, 33, ret_message)
            self.assertEqual(session_id>0, False)

            session_id, ret_code, ret_message = proxy.callOperationOnCluster('e', 'TEST_CLUSTER', 'TEST_OPERATION', Dictionary(signature='ss')) #empty dictionary
            self.assertEqual(ret_code>0, True, ret_message)
            self.assertEqual(session_id>0, False)

            session_id, ret_code, ret_message = proxy.callOperationOnCluster('fabregas', 'fake_cluster', 'TEST_OPERATION', {'e':'4444'})
            self.assertEqual(ret_code>0, True, ret_message)
            self.assertEqual(session_id>0, False)

            session_id, ret_code, ret_message = proxy.callOperationOnCluster('fabregas', 'TEST_CLUSTER', 'fake_operation', {'ddd':'s'})
            self.assertEqual(ret_code>0, True, ret_message)
            self.assertEqual(session_id>0, False)

            #test log level changes
            c_log_level = proxy.getLogLevel()
            self.assertEqual(c_log_level, 'DEBUG')

            proxy.setLogLevel('ERROR')
            c_log_level = proxy.getLogLevel()
            self.assertEqual(c_log_level, 'ERROR')

            try:
                proxy.setLogLevel('FAKE')
            except:
                pass
            else:
                raise Exception('Should be exception in this case!')
            #end test log level changes

            #positive tests
            session_id, ret_code, ret_message = proxy.callOperationOnCluster('fabregas', 'TEST_CLUSTER', 'TEST_OPERATION', {'param1':10})
            self.assertEqual(ret_code, 0, ret_message)
            self.assertEqual(session_id>0, True)

            fail_session_id, ret_code, ret_message = proxy.callOperationOnNodes('fabregas', ['127.0.0.1', '23.23.23.23'], 'TEST_OPERATION',{'a':2})
            self.assertNotEqual(ret_code, 0, ret_message)

            while True:
                status_code, status_name = proxy.getOperationStatus(session_id)
                if status_name == 'INPROGRESS':
                    time.sleep(1)
                    continue
                break

            session_id, ret_code, ret_message = proxy.callOperationOnNodes('fabregas', ['127.0.0.1', '23.23.23.23'], 'TEST_OPERATION', {'param1':10})
            self.assertEqual(ret_code, 0, ret_message)
            self.assertEqual(session_id>0, True)

            client = FRIClient('127.0.0.1', friBase.FRI_BIND_PORT)
            err_code, err_message = client.call({'id':session_id, 'node':'127.0.0.1', 'progress':'100','ret_code':0, 'ret_message':'ok'})
            self.assertEqual(err_code, 0)

            #waiting operations finishing
            while True:
                status_code, status_name = proxy.getOperationStatus(session_id)
                if status_name == 'INPROGRESS':
                    time.sleep(1)
                    continue
                break

            time.sleep(1)

        finally:
            print 'process for D-BUS service stoping...'
            p.terminate()
            print 'process for D-BUS service stoped'