def test_edit_target_form(): """ This test is imitating editing a new target and updating it back to Brain.Targets. """ target_key = "" for target_item in get_brain_targets(): target_key = target_item["id"] plugin_name = "Plugin1" location_num = location_generated_num("172.16.5.") port_num = "8005" optional_char = "optional here" r.db("Brain").table("Targets").get(target_key).update({ "PluginName": plugin_name, "id": target_key, "Location": location_num, "Port": port_num, "Optional": optional_char }).run(connect()) update_new_target2 = r.db("Brain").table("Targets").get( target_key).update({ "PluginName": plugin_name, "Location": location_num, "Port": port_num[:2] + "1", "Optional": optional_char }).run(connect()) assert update_new_target2['replaced'] == 1
def switch_to_error2(): connection_var = connect() query_obj = rtdb.db("Brain").table("Jobs").filter( (rtdb.row["Status"].ne("Error") & rtdb.row["Status"].ne("Done"))).changes().run(connection_var) for query_item in query_obj: if query_item and query_item.get( "new_val") and query_item['new_val'].get("id"): print(query_item['new_val']['id']) sleep(2) print( rtdb.db("Brain").table("Outputs").insert({ "OutputJob": query_item['new_val'], "Content": "data\noutput\n{}\n\n".format( query_item['new_val']["JobCommand"]["Inputs"][0] ["Value"]) }).run(connection_var)) print( rtdb.db("Brain").table("Jobs").get( query_item['new_val']['id']).update({ "Status": "Done" }).run(connection_var)) print( rtdb.db("Brain").table("Jobs").get( query_item['new_val']['id']).update({ "Status": "Error" }).run(connection_var))
def confirm_brain_db_info(): """ confirm_brain_db_info function checks to see if it's using a local rethinkdb connection or docker's brain instance connection. It also checks to see if Brain db exist and if any tables exist within the Brain db. If db and tables don't exist they will be created only locally. :return: nothing at the moment """ if not check_dev_env(): # Check for Development Environment return db_con_var = connect() if rtdb.db_list().contains("Brain").run(db_con_var) is not True: print("log: db Brain doesn't exist locally") rtdb.db_create("Brain").run(db_con_var) print("log: db Brain was created to locally since it didn't exist") # create local Brain tables tables_create("Brain", ["Targets", "Jobs", "Outputs"]) else: # if Brain does exist locally print("log: db Brain exist locally") non_existing_tables = tables_check("Brain", ["Targets", "Jobs", "Outputs"]) tables_create("Brain", non_existing_tables) rtdb.db("Brain").table("Targets").insert(_TEST_TARGETS).run(db_con_var) print("log: db Dummy data was inserted to Brain.Targets locally")
def test_update_error(proc, rethink, connection): rethinkdb.db("Brain").table("Jobs").delete().run(connection) rethinkdb.db("Brain").table("Outputs").delete().run(connection) sleep(5) rethinkdb.db("Brain").table("Jobs").insert(SAMPLE_JOB).run(connection) sleep(3) environ["TEST_SELECTION"] = "TEST5" environ["STAGE"] = "TESTING" try: proc.start() sleep(5) proc.terminate() sleep(2) except SystemExit as ex: assert str(ex) == "0" cursor = rethinkdb.db("Brain").table("Jobs").run(connection) job = cursor.next() assert job["id"] == SAMPLE_JOB["id"] assert job["Status"] == "Error" cursor = rethinkdb.db("Brain").table("Outputs").run(connection) output = cursor.next() assert output["OutputJob"]["id"] == SAMPLE_JOB["id"] assert output["Content"] == "Testing Error"
def test_job_status_update(proc, rethink, connection): """Test sending a job status update This test send a job status update from the plugin to the database. Arguments: sup {class instance} -- a SupervisorController class instance. rethink {None} -- indicates that this test will need the rethinkdb to be accessable. """ environ["TEST_SELECTION"] = "TEST3" environ["STAGE"] = "TESTING" try: proc.start() sleep(5) proc.terminate() sleep(2) except SystemExit as ex: assert str(ex) == "0" cursor = rethinkdb.db("Brain").table("Jobs").run(connection) job = cursor.next() assert job["id"] == SAMPLE_JOB["id"] assert job["Status"] == "Pending"
def test_send_output(proc, rethink, connection): """Test sending output from the plugin This test should send a mock output to the database from the plugin running in the supervisor. Arguments: sup {class instance} -- a SupervisorController class instance. rethink {None} -- indicates that this test will need the rethinkdb to be accessable. """ environ["TEST_SELECTION"] = "TEST2" environ["STAGE"] = "TESTING" try: proc.start() sleep(5) proc.terminate() sleep(2) except SystemExit as ex: assert str(ex) == "0" cursor = rethinkdb.db("Brain").table("Outputs").run(connection) output = cursor.next() assert output["OutputJob"]["id"] == SAMPLE_JOB["id"] assert output["Content"] == "test output"
def tables_check(database, tables): """Takes a list of tables and checks for them This function takes a list of table names and checks if they exist in the database. Arguments: database {string} -- a string denoting the name of the database. tables {list<str>} -- a list of table names to check for. Returns: {list} -- a list of tables that do not exist in the database. """ db_con_var = connect() if "Plugin2" in tables: print("\nlog: db {}.{} table exist locally".format( database, "Plugin2")) table_clear(database, "Plugin2") for i, table_name in enumerate(tables): # {database}.{table_name} does exist if rtdb.db(database).table_list().contains(table_name).run(db_con_var): print("\nlog: db {}.{} table exist locally".format( database, table_name)) table_clear(database, table_name) del tables[i] else: print("log: db {}.{} doesnt exist".format(database, table_name)) return tables
def table_clear(database, table): """Clears data from a table Clears all data from a given table in a database. Arguments: database {str} -- database name table {str} -- name of the table to clear. """ db_con_var = connect() try: rtdb.db(database).table(table).delete().run(db_con_var) print("log: db {}.{} table has been cleared.".format(database, table)) except rtdb.ReqlError as err: err = sys.exc_info()[0] print("EXCEPT == {}".format(err))
def dummy_output_data(): """ This test is used for other functions test in order to have data to test """ conn = connect() r.db("Brain").table("Jobs").insert(SAMPLE_JOB).run(conn) r.db("Brain").table("Outputs").insert(SAMPLE_OUTPUT).run(conn) yield r.db("Brain").table("Outputs").delete().run(conn) r.db("Brain").table("Jobs").delete().run(conn)
def tables_create(database, tables): """Create a list of tables in the database Creates tables in a database from provided list. Arguments: database {str} -- a string denoting the name of the database. tables {list<str>} -- a list of table names to check for. """ db_con_var = connect() for table_name in tables: try: rtdb.db(database).table_create(table_name).run(db_con_var) print("log: db {}.{} table was created to locally \ since it didn't exist".format(database, table_name)) except rtdb.ReqlError as err: err = sys.exc_info()[0] print("EXCEPT == {}".format(err))
def test_create_plugin(rethink, connection): """Test creating a plugin This test tests to see if the previous test created a table for the IntegrationTest plugin. Arguments: sup {class instance} -- a SupervisorController class instance. rethink {None} -- indicates that this test will need the rethinkdb to be accessable. """ tables = rethinkdb.db("Plugins").table_list().run(connection) assert "IntegrationTest" in tables
def test_pull_job(proc, rethink, connection): """Test pulling a job This test runs a supervisor, which runs a plugin that attempts to pull a job. Arguments: sup {class instance} -- a SupervisorController class instance. rethink {None} -- indicates that this test will need the rethinkdb to be accessable. """ environ["TEST_SELECTION"] = "TEST1" environ["STAGE"] = "TESTING" rethinkdb.db("Brain").table("Jobs").insert( SAMPLE_JOB ).run(connection) try: proc.start() sleep(5) proc.terminate() sleep(2) except SystemExit as ex: assert str(ex) == "0"
def confirm_plugin_db_info(): """ confirm_plugin_db_info function checks to see if the Plugins db exist and if any tables exist within the Plugins db. If db and tables don't exist they will be created only locally. """ db_con_var = connect() if check_prod_env(): # For Production Environment if rtdb.db_list().contains("Plugins").run(db_con_var): print("\nlog: db Plugins exist") if rtdb.db("Plugins").table_list().run(db_con_var): print( "log: db Plugins tables are listed down below:\n{}".format( rtdb.db("Plugins").table_list().run(db_con_var))) else: print("log: db Plugins tables don't exist\n") else: print("\nlog: db Plugins DOESN'T exist\n") else: # is check_dev_env()-- if Plugins does exit locally if rtdb.db_list().contains("Plugins").run(db_con_var) is not True: print("\nlog: db Plugins doesn't exist locally") rtdb.db_create("Plugins").run(db_con_var) print("log: db Plugins didn't exist, was created to locally") tables_create("Plugins", ["Plugin1", "Plugin2"]) else: # if Plugins does exit locally print("\nlog: db Plugins exist locally") non_existing_tables = tables_check("Plugins", ["Plugin1", "Plugin2"]) tables_create("Plugins", non_existing_tables) rtdb.db("Plugins").table("Plugin1").insert(_TEST_COMMANDS).run( db_con_var) rtdb.db("Plugins").table("Plugin2").insert(_TEST_COMMANDS2).run( db_con_var) rtdb.db("Controller").table("Plugins").delete().run(db_con_var) rtdb.db("Controller").table("Plugins")\ .insert(plugins).run(db_con_var) rtdb.db("Controller").table("Ports").delete().run(db_con_var) rtdb.db("Controller").table("Ports") \ .insert([TEST_PORT_DATA, TEST_PORT_DATA2]).run(db_con_var) # Brain.Logs rtdb.db("Brain").table("Logs").delete().run(db_con_var) rtdb.db("Brain").table("Logs").insert( gen_logs_data(50)).run(db_con_var) print("\nlog: db Dummy data was inserted to Brain.Logs locally\n") if rtdb.db("Brain").table_list().contains("UIW2").run(db_con_var): rtdb.db("Brain").table("UIW2").delete().run(db_con_var) else: rtdb.db("Brain").table_create("UIW2").run(db_con_var) rtdb.db("Brain").table("UIW2").insert(TEST_SAVED_COMMANDS).run( db_con_var) print("log: db Dummy data was inserted to Plugins.Plugin1 locally\n")
from os import mkdir try: from brain.binary.filesystem import start_filesystem, BrainStoreConfig from brain import connect, r except ImportError: from .brain.binary.filesystem import start_filesystem, BrainStoreConfig from .brain import connect, r # docker run -ti --rm --cap-add SYS_ADMIN --device /dev/fuse --security-opt apparmor:unconfined danfusetest if __name__ == "__main__": try: mkdir("/tmp/tst") except FileExistsError: pass c = connect() try: r.db("Brain").table_create("Files").run(c) except: pass bsc = BrainStoreConfig(allow_remove=True, allow_list=True) start_filesystem("/tmp/tst", bsc)