def testBasicSessionManagement(self): service = PluginService("Test") ''' Basic info''' result = service.get_info() if (result["name"] is not "Test"): self.fail("Unexpected name returned %s" % result) if (result["version"] is not 1): self.fail("Unexpected version returned %s" % result) ''' Should get a set of empty sessions''' result = service.get_sessions() if (result is None or "sessions" not in result): self.fail("No sessions returned %s" % result) ''' start a valid session ''' result = service.create_session("TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] ''' start an invalid session (no such plugin)''' try: service.create_session("BadPlugin") self.fail("Successfully started a bad session %s" % result) except PluginServiceError: pass ''' Should get a set with one session in''' result = service.get_sessions() if (result is None or "sessions" not in result): self.fail("No sessions returned %s" % result) ''' TODO: fix this test - guess other tests are interfering ''' if (len(result["sessions"]) != 1): self.fail("Unexpected number of sessions returned %s" % result) if (session not in result["sessions"]): self.fail("Session %s not returned" % session) pass
def newscan(request, template=None): #Page has been POSTED to if request.method == 'POST': url_entered = request.POST["new_scan_url_input"] data = {"url_entered": url_entered} te = TaskEngine() te.add_plugin_service(PluginService("TestService1")) result = te.get_all_plugins() log.debug("result " + str(result)) result = te.get_plugin_template("TemplatePlugin", 1) log.debug("result " + str(result)) result = te.create_plugin_session("TemplatePlugin", 1) log.debug("result " + str(result)) '''te.set_plugin_service_session_config(service_name, session, {"target" : "http://localhost"}) result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_START) log.debug("result " + str(result)) result = te.get_plugin_service_session_status(service_name, session) log.debug("result " + str(result)) result = te.get_plugin_service_session_results(service_name, session) log.debug("result " + str(result))''' return render(request, template, data) #Page has not been posted to else: data = {} # You'd add data here that you're sending to the template. return render(request, template, data)
def testSessionStateManagement(self): te = TaskEngine() service_name = "TestService1" ''' Add a services to start with ''' te.add_plugin_service(PluginService(service_name)) ''' start a valid session ''' result = te.create_plugin_service_session(service_name, "TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] ''' check the status ''' result = te.get_plugin_service_session_status(service_name, session) if (result["status"] is not MinionPlugin.STATUS_PENDING): self.fail("Unexpected session state %s" % result) ''' Should fail - invalid state ''' result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_RESUME) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = te.set_plugin_service_session_state( service_name, session, MinionPlugin.STATE_SUSPEND) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = te.set_plugin_service_session_state( service_name, session, MinionPlugin.STATE_TERMINATE) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' te.set_plugin_service_session_config(service_name, session, {"target": "http://localhost"}) result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_START) if (result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) ''' Should fail - invalid state ''' result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_START) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_RESUME) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' result = te.set_plugin_service_session_state( service_name, session, MinionPlugin.STATE_SUSPEND) if (result["status"] is not MinionPlugin.STATUS_WAITING): self.fail("Unexpected result - should have worked %s" % result) #print "Result from set_session_states:"# #print result ''' start the plugin '''
def testBasicApi(self): te = TaskEngine() result = te.get_all_plugins() if (len(result["plugins"]) is not 0): self.fail("Unexpected number of plugin services returned %s" % result) ''' Add a service ''' te.add_plugin_service(PluginService("TestService1")) ''' Should now be one plugion ''' result = te.get_all_plugins() if (len(result["plugins"]) is not 1): self.fail("Unexpected number of plugin services returned %s" % result) ''' Add another service ''' te.add_plugin_service(PluginService("TestService2")) ''' Should now be one plugion ''' result = te.get_all_plugins() if (len(result["plugins"]) is not 1): self.fail("Unexpected number of plugin services returned %s" % result) ''' Add another service, with 2 plugins, one of which is new ''' ps = PluginService("TestService3") ps.set_plugins({ "TemplatePlugin": TemplatePlugin, "MinionPlugin": MinionPlugin }) te.add_plugin_service(ps) result = te.get_all_plugins() if (len(result["plugins"]) is not 2): self.fail("Unexpected number of plugin services returned %s" % result) # TODO: Check the results - just doing manually nowe ;) ''' Get the interface for a plugin ''' result = te.get_plugin_template("TemplatePlugin", 1) print result result = te.create_plugin_session("TemplatePlugin", 1) print result session = result["session"] service_name = result["plugin_service"]["name"] result = te.get_plugin_service_session_status(service_name, session) te.set_plugin_service_session_config(service_name, session, {"target": "http://localhost"}) result = te.set_plugin_service_session_state(service_name, session, MinionPlugin.STATE_START) if (result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) result = te.get_plugin_service_session_status(service_name, session) print result # TODO: Why isnt the progress coming through?? result = te.get_plugin_service_session_results(service_name, session) print result
def testPluginServiceManagement(self): te = TaskEngine() ''' Check no services to start with ''' result = te.get_plugin_services() if (len(result["plugin_services"]) is not 0): self.fail("Unexpected number of plugin services returned %s" % result) service_name = "TestService1" ''' Add a service to start with ''' te.add_plugin_service(PluginService(service_name)) result = te.get_plugin_services() if (len(result) is not 1): self.fail("Unexpected number of plugin services returned %s" % result) if (result["plugin_services"][0]["name"] is not service_name): self.fail("Unexpected name returned %s" % result) if (result["plugin_services"][0]["version"] is not 1): self.fail("Unexpected version returned %s" % result) ''' Should get a set of empty sessions''' result = te.get_plugin_service_sessions(service_name) if (result is None or "sessions" not in result): self.fail("No sessions returned %s" % result) if (len(result["sessions"]) != 0): self.fail("Unexpected number of sessions returned %s" % result) ''' start a valid session ''' result = te.create_plugin_service_session(service_name, "TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] ''' start an invalid session (no such plugin)''' try: te.create_plugin_service_session(service_name, "BadPlugin") self.fail("Successfully started a bad session %s" % result) except PluginServiceError: pass ''' Should get a set with one session in''' result = te.get_plugin_service_sessions(service_name) if (result is None or "sessions" not in result): self.fail("No sessions returned %s" % result) if (len(result["sessions"]) != 1): self.fail("Unexpected number of sessions returned %s" % result) if (session not in result["sessions"]): self.fail("Session %s not returned" % session) pass
def testBasicPluginManagement(self): service = PluginService("Test3") result = service.get_plugins() if (len(result["plugins"]) != 1): self.fail("Unexpected number of plugins returned %s" % result) found = False for plugin in result["plugins"]: if plugin["plugin"] is "TemplatePlugin": found = True if (not found): self.fail("Unexpected plugin returned %s" % result) result = service.get_plugin_config("TemplatePlugin") ''' start a valid session ''' result = service.create_session("TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] result = service.set_plugin_config(session, {"target": "http://localhost:8080"}) result = service.get_plugin_config("TemplatePlugin")
def testBasicPluginManagement(self): service = PluginService("Test3") result = service.get_plugins() if (len(result["plugins"]) != 1): self.fail("Unexpected number of plugins returned %s" % result) found = False for plugin in result["plugins"]: if plugin["plugin"] is "TemplatePlugin": found = True if (not found): self.fail("Unexpected plugin returned %s" % result) result = service.get_plugin_config("TemplatePlugin") ''' start a valid session ''' result = service.create_session("TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] result = service.set_plugin_config(session, {"target" : "http://localhost:8080" }) result = service.get_plugin_config("TemplatePlugin")
def testSessionStateManagement(self): service = PluginService("Test2") ''' start a valid session ''' result = service.create_session("TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] ''' check the status ''' result = service.get_session_status(session) if (result["status"] is not MinionPlugin.STATUS_PENDING): self.fail("Unexpected session state %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_RESUME) if ( result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_SUSPEND) if ( result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_TERMINATE) if ( result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' service.set_plugin_config(session, {"target" : "http://localhost"}) result = service.set_session_state(session, MinionPlugin.STATE_START) if ( result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) result = service.get_session_status(session) if ( result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) if ( result["progress"] is not 20): self.fail("Unexpected progress - expected 10 %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_START) if ( result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_RESUME) if ( result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' result = service.set_session_state(session, MinionPlugin.STATE_SUSPEND) if ( result["status"] is not MinionPlugin.STATUS_WAITING): self.fail("Unexpected result - should have worked %s" % result) #print "Result from set_session_state:" #print result ''' start the plugin '''
''' Created on 25 Sep 2012 @author: test Simple wrapper around the PluginService providing, yes, a REST API ''' from PluginService import PluginService, PluginServiceError #from bottle import abort, get, put, post, delete, request, run from bottle import Bottle, run keys = {"64c4c469ab0743a368d00466e1eb8608", "ca8601b9a687c34703e46328e3dc69eb"} app = Bottle() service = PluginService("Test") def is_authorized(req): key = req.headers.get('Authorization') # XX - timing independent check of key strings is required. return (key in keys) @app.get("/info") def get_info(): if (not is_authorized(app.request)): app.abort(401, "Unauthorized request.") return service.get_info()
def testSessionStateManagement(self): service = PluginService("Test2") ''' start a valid session ''' result = service.create_session("TemplatePlugin") if (result is None or "session" not in result): self.fail("Failed to start session %s" % result) session = result["session"] ''' check the status ''' result = service.get_session_status(session) if (result["status"] is not MinionPlugin.STATUS_PENDING): self.fail("Unexpected session state %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_RESUME) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_SUSPEND) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_TERMINATE) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' service.set_plugin_config(session, {"target": "http://localhost"}) result = service.set_session_state(session, MinionPlugin.STATE_START) if (result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) result = service.get_session_status(session) if (result["status"] is not MinionPlugin.STATUS_RUNNING): self.fail("Unexpected result - should have worked %s" % result) if (result["progress"] is not 20): self.fail("Unexpected progress - expected 10 %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_START) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should fail - invalid state ''' result = service.set_session_state(session, MinionPlugin.STATE_RESUME) if (result["status"] is not MinionPlugin.STATUS_FAILED): self.fail("Unexpected result - should have failed %s" % result) ''' Should work ok ''' result = service.set_session_state(session, MinionPlugin.STATE_SUSPEND) if (result["status"] is not MinionPlugin.STATUS_WAITING): self.fail("Unexpected result - should have worked %s" % result) #print "Result from set_session_state:" #print result ''' start the plugin '''