def test_delete_dump(self): """Function: test_delete_dump Description: Test delete dump call. Arguments: """ global SKIP_PRINT global PRT_TEMPLATE global ERROR_PRINT cmdline = gen_libs.get_inst(sys) self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) status, msg = self.els.create_repo(self.repo_name, self.repo_dir) if status: print(ERROR_PRINT) print(PRT_TEMPLATE % (msg)) self.skipTest(SKIP_PRINT) els = elastic_class.ElasticSearchDump(self.cfg.host, repo=self.repo_name) els.dump_name = self.dump_name status, msg = els.dump_db() if status: print("Error detected for dump in repository: %s" % (self.repo_name)) print(PRT_TEMPLATE % (msg)) self.skipTest("Dump failed") self.argv_list.append("-S") self.argv_list.append(self.dump_name) self.argv_list.append("-r") self.argv_list.append(self.repo_name) cmdline.argv = self.argv_list elastic_db_repo.main() self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port, repo=self.repo_name) if self.dump_name not in elastic_class.get_dump_list( self.els.els, self.repo_name): status = True else: status = False self.assertTrue(status)
def setUp(self): """Function: setUp Description: Initialization for unit testing. Arguments: """ self.base_dir = "test/integration/elastic_db_dump" self.test_path = os.path.join(os.getcwd(), self.base_dir) self.config_path = os.path.join(self.test_path, "config") self.cfg = gen_libs.load_module("elastic", self.config_path) self.phy_repo_dir = os.path.join(self.cfg.phy_repo_dir, self.cfg.repo_name) self.func_dict = { "-C": elastic_db_dump.create_repo, "-D": elastic_db_dump.initate_dump, "-L": elastic_db_dump.list_dumps, "-R": elastic_db_dump.list_repos } self.args = {"-c": "elastic", "-d": self.config_path} elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) if elr.repo_dict: print("ERROR: Test environment not clean - repositories exist.") self.skipTest("Pre-conditions not met.") else: self.elr = None
def test_initate_dump_i_option(self): """Function: test_initate_dump_i_option Description: Test initiate dump call -i option. Arguments: """ els = elastic_class.ElasticSearchDump(self.cfg.host, self.cfg.port) # Capture the first database/indice name in Elasticsearch. dbs = [ str([x.split() for x in els.els.cat.indices().splitlines()][0][2]) ] self.args["-D"] = self.cfg.repo_name self.args["-i"] = dbs self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) elastic_db_dump.run_program(self.args, self.func_dict) dir_path = os.path.join(self.cfg.phy_repo_dir, self.cfg.repo_name, "indices") # Count number of databases/indices dumped to repository. cnt = len([ name for name in os.listdir(dir_path) if os.path.isdir(os.path.join(dir_path, name)) ]) self.assertEqual(cnt, 1)
def test_create_repo(self): """Function: test_create_repo Description: Test create repo call. Arguments: """ cmdline = gen_libs.get_inst(sys) self.argv_list.append("-C") self.argv_list.append(self.repo_name) self.argv_list.append("-l") self.argv_list.append(self.repo_dir) cmdline.argv = self.argv_list elastic_db_repo.main() self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port, repo=self.repo_name) if self.repo_name in self.els.repo_dict: status = True else: status = False self.assertTrue(status)
def setUp(self): """Function: setUp Description: Initialization for unit testing. Arguments: """ self.base_dir = "test/integration/elastic_db_dump" self.test_path = os.path.join(os.getcwd(), self.base_dir) self.config_path = os.path.join(self.test_path, "config") self.cfg = gen_libs.load_module("elastic", self.config_path) self.phy_repo_dir = os.path.join(self.cfg.phy_repo_dir, self.cfg.repo_name) self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) if self.elr.repo_dict: print("ERROR: Test environment not clean - repositories exist.") self.skipTest("Pre-conditions not met.") else: _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) self.els = elastic_class.ElasticSearchDump(self.cfg.host, self.cfg.port, repo=self.cfg.repo_name)
def run_program(args_array, func_dict, **kwargs): """Function: run_program Description: Creates class instance and controls flow of the program. Create a program lock to prevent other instantiations from running. Arguments: (input) args_array -> Dict of command line options and values. (input) func_dict -> Dictionary list of functions and options. """ cmdline = gen_libs.get_inst(sys) args_array = dict(args_array) func_dict = dict(func_dict) cfg = gen_libs.load_module(args_array["-c"], args_array["-d"]) hostname = socket.gethostname().strip().split(".")[0] try: prog_lock = gen_class.ProgramLock(cmdline.argv, hostname) # Find which functions to call. for opt in set(args_array.keys()) & set(func_dict.keys()): els = elastic_class.ElasticSearchRepo(cfg.host, cfg.port, repo=args_array.get("-L")) func_dict[opt](els, args_array=args_array, **kwargs) del prog_lock except gen_class.SingleInstanceException: print("WARNING: elastic_db_repo lock in place for: %s" % (hostname))
def test_list_dumps(self): """Function: test_list_dumps Description: Test list dumps call. Arguments: """ global SKIP_PRINT global PRT_TEMPLATE global ERROR_PRINT cmdline = gen_libs.get_inst(sys) self.argv_list.append("-L") self.argv_list.append(self.repo_name) cmdline.argv = self.argv_list self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) status, msg = self.els.create_repo(self.repo_name, self.repo_dir) if status: print(ERROR_PRINT) print(PRT_TEMPLATE % (msg)) self.skipTest(SKIP_PRINT) with gen_libs.no_std_out(): self.assertFalse(elastic_db_repo.main())
def test_initate_dump_i_option(self): """Function: test_initate_dump_i_option Description: Test initiate dump call -i option. Arguments: """ cmdline = gen_libs.get_inst(sys) els = elastic_class.ElasticSearchDump(self.cfg.host, self.cfg.port) self.argv_list.append("-D") self.argv_list.append(self.cfg.repo_name) self.argv_list.append("-i") self.argv_list.append( str([x.split() for x in els.els.cat.indices().splitlines()][0][2])) cmdline.argv = self.argv_list self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) elastic_db_dump.main() dir_path = os.path.join(self.cfg.phy_repo_dir, self.cfg.repo_name, "indices") # Count number of databases/indices dumped to repository. cnt = len([ name for name in os.listdir(dir_path) if os.path.isdir(os.path.join(dir_path, name)) ]) self.assertEqual(cnt, 1)
def setUp(self): """Function: setUp Description: Initialization for unit testing. Arguments: """ global SKIP_PRINT self.base_dir = "test/integration/elastic_db_repo" self.test_path = os.path.join(os.getcwd(), self.base_dir) self.config_path = os.path.join(self.test_path, "config") self.cfg = gen_libs.load_module("elastic", self.config_path) self.argv_list = [ os.path.join(self.base_dir, "main.py"), "-c", "elastic", "-d", self.config_path ] self.repo_name = "TEST_INTR_REPO" self.repo_name2 = "TEST_INTR_REPO2" self.dump_name = "test_dump" self.repo_dir = os.path.join(self.cfg.log_repo_dir, self.repo_name) self.phy_repo_dir = os.path.join(self.cfg.phy_repo_dir, self.repo_name) self.els = None els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) if els.repo_dict: print("ERROR: Test environment not clean - repositories exist.") self.skipTest(SKIP_PRINT)
def test_disk_usage(self): """Function: test_disk_usage Description: Test disk usage call. Arguments: """ global SKIP_PRINT global PRT_TEMPLATE global ERROR_PRINT cmdline = gen_libs.get_inst(sys) self.argv_list.append("-U") cmdline.argv = self.argv_list self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) status, msg = self.els.create_repo(self.repo_name, self.repo_dir) if status: print(ERROR_PRINT) print(PRT_TEMPLATE % (msg)) self.skipTest(SKIP_PRINT) # Wait until the repo dir has been created. while True: if not os.path.isdir(self.phy_repo_dir): time.sleep(1) else: break with gen_libs.no_std_out(): self.assertFalse(elastic_db_repo.main())
def create_repo(els, **kwargs): """Function: create_repo Description: Create a repository for Elasticsearch database dumps. Arguments: (input) els -> ElasticSearch class instance. (input) **kwargs: args_array -> Dict of command line options and values. """ args_array = dict(kwargs.get("args_array")) repo_name = args_array.get("-C") repo_dir = args_array.get("-l") elr = elastic_class.ElasticSearchRepo(els.hosts, els.port) if repo_name in elr.repo_dict: print("ERROR: '%s' repository already exists at: '%s'" % (repo_name, repo_dir)) else: err_flag, msg = elr.create_repo(repo_name, os.path.join(repo_dir, repo_name)) if err_flag: print("Error detected for Repository: '%s' at '%s'" % (repo_name, repo_dir)) print("Reason: '%s'" % (msg))
def list_repos(els, **kwargs): """Function: list_repos Description: Lists the repositories present. Arguments: (input) els -> Elasticsearch class instance. """ elr = elastic_class.ElasticSearchRepo(els.hosts, els.port) elastic_libs.list_repos2(elr.repo_dict)
def test_rename_repo(self): """Function: test_rename_repo Description: Test rename repo call. Arguments: """ global SKIP_PRINT global PRT_TEMPLATE global ERROR_PRINT cmdline = gen_libs.get_inst(sys) self.argv_list.append("-M") self.argv_list.append(self.repo_name2) self.argv_list.append(self.repo_name) cmdline.argv = self.argv_list self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) status, msg = self.els.create_repo(self.repo_name2, self.repo_dir) if status: print(ERROR_PRINT) print(PRT_TEMPLATE % (msg)) self.skipTest(SKIP_PRINT) elastic_db_repo.main() self.els = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port, repo=self.repo_name) if self.repo_name in self.els.repo_dict: status = True else: status = False self.assertTrue(status)
def test_repo_create(self): """Function: test_repo_create Description: Test repository is created. Arguments: """ elastic_db_dump.create_repo(self.els, args_array=self.args_array) self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) self.assertTrue(self.cfg.repo_name in self.elr.repo_dict)
def test_create_repo(self): """Function: test_create_repo Description: Test create repo call. Arguments: """ self.args["-C"] = self.cfg.repo_name self.args["-l"] = self.cfg.repo_dir elastic_db_dump.run_program(self.args, self.func_dict) self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port, repo=self.cfg.repo_name) self.assertTrue(self.cfg.repo_name in self.elr.repo_dict)
def test_repo_init(self): """Function: test_repo_init Description: Test initialization of Elasticsearch class. Arguments: """ self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) with gen_libs.no_std_out(): self.assertFalse( elastic_db_dump.create_repo(self.els, args_array=self.args_array))
def test_list_repos(self): """Function: test_list_repos Description: Test list repos call. Arguments: """ self.args["-R"] = True self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) with gen_libs.no_std_out(): self.assertFalse( elastic_db_dump.run_program(self.args, self.func_dict))
def test_list_repos(self): """Function: test_list_repos Description: Test list repos call. Arguments: """ cmdline = gen_libs.get_inst(sys) self.argv_list.append("-R") cmdline.argv = self.argv_list self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) with gen_libs.no_std_out(): self.assertFalse(elastic_db_dump.main())
def test_initate_dump(self): """Function: test_initate_dump Description: Test initiate dump call. Arguments: """ self.args["-D"] = self.cfg.repo_name self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) elastic_db_dump.run_program(self.args, self.func_dict) els = elastic_class.ElasticSearchDump(self.cfg.host, self.cfg.port, repo=self.cfg.repo_name) self.assertTrue(els.dump_list)
def test_initate_dump(self): """Function: test_initate_dump Description: Test initiate dump call. Arguments: """ cmdline = gen_libs.get_inst(sys) self.argv_list.append("-D") self.argv_list.append(self.cfg.repo_name) cmdline.argv = self.argv_list self.elr = elastic_class.ElasticSearchRepo(self.cfg.host, self.cfg.port) _, _ = self.elr.create_repo( self.cfg.repo_name, os.path.join(self.cfg.repo_dir, self.cfg.repo_name)) elastic_db_dump.main() els = elastic_class.ElasticSearchDump(self.cfg.host, self.cfg.port, repo=self.cfg.repo_name) self.assertTrue(els.dump_list)