def test_dbs_is_successful(self): """Function: test_dbs_is_successful Description: Test dumping single database. Arguments: """ esr = elastic_class.ElasticSearchRepo( self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host, repo=self.repo_name) # Capture the first database/indice name in Elasticsearch. dbs = str([name.split() for name in esd.els.cat.indices().splitlines()][0][2]) err_flag, _ = esd.dump_db(dbs) dir_path = os.path.join(self.phy_repo_dir, "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))]) esr.delete_repo() self.assertFalse(err_flag) self.assertEqual(cnt, 1)
def test_dbs_multi_names(self): """Function: test_dbs_multi_names Description: Test dumping two databases. Arguments: """ esr = elastic_class.ElasticSearchRepo( self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host, repo=self.repo_name) # Capture 2 databases/indices name in Elasticsearch. dbs = ','.join( [str(y[2]) for y in [ x.split() for x in esd.els.cat.indices().splitlines()]][0:2]) err_flag, _ = esd.dump_db(dbs) dir_path = os.path.join(self.phy_repo_dir, "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))]) esr.delete_repo() self.assertFalse(err_flag) self.assertEqual(cnt, 2)
def test_dumps_are_deleted(self): """Function: test_dumps_are_deleted Description: Test to see if dumps are deleted. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host, repo=self.repo_name) esd.dump_name = self.dump_name esd.dump_db() esd.dump_name = self.dump_name2 esd.dump_db() status, msg = esr.delete_dump_all(dump_name=self.dump_name) esd2 = elastic_class.ElasticSearchDump(self.cfg.host, repo=self.repo_name) esr.delete_repo() self.assertEqual((status, msg, len(esd2.dump_list)), (False, None, 0))
def setUp(self): """Function: setUp Description: Initialization for unit testing. Arguments: """ self.base_dir = "test/integration/elastic_class" 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.repo_name = "TEST_INTR_REPO" self.repo_name2 = "TEST_INTR_REPO2" self.repo_dir = os.path.join(self.cfg.log_repo_dir, self.repo_name) self.repo_dir2 = os.path.join(self.cfg.log_repo_dir, self.repo_name2) self.phy_repo_dir = os.path.join(self.cfg.phy_repo_dir, self.repo_name) self.phy_repo_dir2 = os.path.join(self.cfg.phy_repo_dir, self.repo_name2) esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) if esr.repo_dict: print("ERROR: Test environment not clean - repositories exist.") self.skipTest("Pre-conditions not met.")
def test_connect(self): """Function: test_connect Description: Test to successfully connect to Elasticsearch. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host) self.assertTrue(not esr.repo and not esr.repo_dict)
def test_init(self): """Function: test_init Description: Test to see if class instance is created. Arguments: """ host_list = "Host_Name" esr = elastic_class.ElasticSearchRepo(host_list) self.assertTrue(not esr.repo)
def test_repo_not_active(self, mock_es): """Function: test_repo_not_active Description: Test with Elasticsearch not active. Arguments: """ mock_es.return_value = self.els els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual((els.hosts, els.repo, els.repo_dict), (self.host_list, self.repo, {}))
def test_repo_name_not_set(self): """Function: test_repo_name_not_set Description: Test to see if repo name is not set. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host) status, msg = esr.delete_repo() self.assertEqual((status, msg), (True, "ERROR: Missing repo or does not exist: None"))
def test_default(self, mock_es, mock_repo): """Function: test_default Description: Test with default settings. Arguments: """ mock_es.return_value = self.els mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual((els.hosts, els.repo, els.repo_dict), (self.host_list, self.repo, self.repo_dict))
def test_repo_is_set(self): """Function: test_repo_not_exist Description: Test if repo attributes are set properly. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) self.assertTrue(esr.repo == self.repo_name and esr.repo_dir == self.repo_dir)
def test_repo_name_not_set(self): """Function: test_repo_name_not_set Description: Test to see if repo name is not set. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host) status, msg = esr.delete_dump_all() self.assertEqual( (status, msg), (True, "ERROR: Repo: None is not present or missing argument."))
def test_no_repo_name(self, mock_es, mock_repo): """Function: test_no_repo_name Description: Test with no repo name passed. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict3] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_repo(), (False, None)) self.assertEqual(els.repo_dict, self.repo_dict3)
def test_default(self, mock_es, mock_repo): """Function: test_default Description: Test with default settings. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict2] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_repo(self.repo2), (False, None)) self.assertEqual(els.repo_dict, self.repo_dict2)
def test_not_deleted(self, mock_es, mock_repo): """Function: test_not_deleted Description: Test with repository not being deleted. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_repo(self.repo), (True, self.err_msg)) self.assertEqual(els.repo_dict, self.repo_dict)
def test_repo_name_is_passed(self): """Function: test_repo_name_is_passed Description: Test to see if repo name is passed. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host) status, msg = esr.delete_dump(repo_name=self.repo_name) self.assertEqual( (status, msg), (True, "ERROR: Missing arg/repo not exist, Repo: TEST_REPO, Dump: None"))
def test_no_repo_dir(self, mock_es, mock_repo): """Function: test_no_repo_dir Description: Test with no repo directory passed. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict2] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo, repo_dir=self.repo_dir) self.assertEqual(els.create_repo(self.repo), (False, None)) self.assertEqual(els.repo_dict, self.repo_dict2)
def test_repo_not_found(self): """Function: test_repo_not_found Description: Test to see if repository is not found. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name) status, msg = esr.delete_repo() self.assertEqual( (status, msg), (True, "ERROR: Missing repo or does not exist: TEST_REPO"))
def test_default(self, mock_es, mock_list, mock_repo): """Function: test_default Description: Test with default settings. Arguments: """ mock_es.return_value = self.els mock_list.side_effect = [[["dump1"], ["dump2"], ["dump3"]], [["dump1"], ["dump2"]]] mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_dump(self.repo, self.dump_name), (False, None))
def test_dump_detected(self, mock_es, mock_list, mock_repo): """Function: test_dump_detected Description: Test with dump detected after delete. Arguments: """ mock_es.return_value = self.els mock_list.side_effect = [[["dump1"], ["dump2"], ["dump3"]], [["dump1"], ["dump2"], ["dump3"]]] mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_dump(self.repo, self.dump_name), (True, "ERROR: Dump still detected: reponame, dump3"))
def test_repo_name_none(self, mock_es, mock_repo): """Function: test_repo_name_none Description: Test with repo name set to None. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict3] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) els.repo = None self.assertEqual(els.delete_repo(), (True, "ERROR: Missing repo or does not exist: None"))
def test_single_repo(self): """Function: test_single_repo Description: Test if single repo is present. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host) self.assertTrue(esd.repo_name == self.repo_name) esr.delete_repo()
def test_repo_name_failed(self, mock_es, mock_repo): """Function: test_repo_name_failed Description: Test with repo name failed to delete. Arguments: """ mock_es.return_value = self.els mock_repo.side_effect = [self.repo_dict, self.repo_dict] els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) els.repo_dict[self.repo] = True self.assertEqual( els.delete_repo(self.repo), (True, "ERROR: Repository deletion failed: reponame"))
def test_no_repo_name(self, mock_es, mock_list, mock_repo): """Function: test_no_repo_name Description: Test with no repo name passed. Arguments: """ mock_es.return_value = self.els mock_list.side_effect = [["dump1", "dump2", "dump3"], ["dump1", "dump2"]] mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual(els.delete_dump_all(), (False, None))
def test_repo_is_set(self): """Function: test_repo_is_set Description: Test if dump location is set. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host) self.assertTrue(esd.dump_loc == self.repo_dir) esr.delete_repo()
def test_no_dump(self, mock_es, mock_list, mock_repo): """Function: test_no_dump Description: Test with no dump to delete. Arguments: """ mock_es.return_value = self.els mock_list.side_effect = [[["dump1"], ["dump2"]], [["dump1"], ["dump2"]]] mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) self.assertEqual( els.delete_dump(self.repo, self.dump_name), (True, "ERROR: Dump: dump3 not in Repository: reponame"))
def test_repo_is_deleted(self): """Function: test_repo_is_deleted Description: Test to see if repo is deleted. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() status, msg = esr.delete_repo() self.assertEqual((status, msg), (False, None))
def test_repo_not_found(self): """Function: test_repo_not_found Description: Test to see if repository is not found. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name) status, msg = esr.delete_dump_all() self.assertEqual( (status, msg), (True, "ERROR: Repo: TEST_REPO is not present or missing argument."))
def test_repo_not_found(self): """Function: test_repo_not_found Description: Test to see if repository is not found. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name) status, msg = esr.delete_dump(dump_name=self.dump_name) self.assertEqual( (status, msg), (True, "ERROR: Missing arg/repo not exist, Repo: %s, Dump: %s" % (self.repo_name, self.dump_name)))
def test_dump_list_is_empty(self): """Function: test_dump_list_is_empty Description: Test if dump list is empty. Arguments: """ esr = elastic_class.ElasticSearchRepo(self.cfg.host, repo=self.repo_name, repo_dir=self.repo_dir) esr.create_repo() esd = elastic_class.ElasticSearchDump(self.cfg.host) self.assertTrue(not esd.dump_list) esr.delete_repo()
def test_delete_failed(self, mock_es, mock_list, mock_repo): """Function: test_delete_failed Description: Test with delete of dump failing. Arguments: """ mock_es.return_value = self.els mock_list.side_effect = [[["dump1"], ["dump2"], ["dump3"]], [["dump1"], ["dump2"]]] mock_repo.return_value = self.repo_dict els = elastic_class.ElasticSearchRepo(self.host_list, repo=self.repo) els.repo_dict[self.repo2] = True self.assertEqual( els.delete_dump(self.repo2, self.dump_name), (True, "ERROR: Dump deletion failed: reponame2, dump3"))