def test_file_dont_clobber(): path = "/tmp/badadmin-tests-clobber" content = "Clobber?\n" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert test_file.write_contents(content) == True ensure = open(path, "r") assert ensure.read() == content ensure.close() assert os.path.exists(path) == True test_file2 = simple_file(path) assert test_file2.create() == False ensure = open(path, "r") assert ensure.read() == content ensure.close() assert os.path.exists(path) == True assert test_file.remove()
def storage_file(self, name): storage_path = "./modules/" + self.get_class_name() + "/storage/" sf = simple_file(storage_path + name) if sf.exists(): return sf else: return False
def test_dir_content(): path = test_location + "/badadmin-dir-content" files = ['file1', 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) assert test_dir.exists() == False assert test_dir.create() == True for item in files: current_file = simple_file(path + "/" + item) assert current_file.create() == True assert os.path.exists(path) == True assert os.path.isdir(path) == True assert len(test_dir.list()) == 2 assert 'file1' in test_dir.list() assert 'file2' in test_dir.list() assert test_dir.remove() == True assert os.path.exists(path) == False assert os.path.isdir(path) == False
def test_file_append(): path = "/tmp/badadmin-tests-append" content = "Start\n" content2 = "Addition\n" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.write_contents(content) == True assert test_file.get_contents() == content assert test_file.append_content(content2) == True assert test_file.get_contents() == content + content2 ensure = open(path, "r") assert ensure.read() == content + content2 ensure.close() assert test_file.remove() == True
def test_dir_content(): path = test_location + "/badadmin-dir-content" files = ['file1' , 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) assert test_dir.exists() == False assert test_dir.create() == True for item in files: current_file = simple_file(path + "/" + item) assert current_file.create() == True assert os.path.exists(path) == True assert os.path.isdir(path) == True assert len(test_dir.list()) == 2 assert 'file1' in test_dir.list() assert 'file2' in test_dir.list() assert test_dir.remove() == True assert os.path.exists(path) == False assert os.path.isdir(path) == False
def test_file_create_blank(): try: test_file = simple_file("") assert False except ValueError: assert True
def test_file_add_content(): path = "/tmp/badadmin-tests-content" content = "This is a test!\n" content2 = "Another test!\n" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.write_contents(content) == True assert test_file.get_contents() == content ensure = open(path, "r") assert ensure.read() == content ensure.close() assert test_file.write_contents(content2) == True assert test_file.get_contents() == content2 ensure = open(path, "r") assert ensure.read() == content2 ensure.close() assert test_file.remove() == True
def __detect_package_manager(self): pkg_manager_list = ['yum', 'apt-get'] if simple_file("/etc/yum.conf").exists(): self.__package_manager = "yum" elif simple_dir("/etc/apt").exists(): self.__package_manager = "apt"
def test_file_set_owner(): uid1 = pwd.getpwuid(1).pw_name uid2 = pwd.getpwuid(2).pw_name my_uid = os.getuid() my_gid = os.getgid() path = "/tmp/badadmin-tests-owner2" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.exists() == True test_file.set_ownership_by_id(1, 1) ownership = test_file.get_ownership() assert ownership["uid"] == 1 assert ownership["gid"] == 1 assert ownership["owner_name"] == uid1 assert ownership["group_name"] == uid1 test_file.set_ownership_by_name(uid2, uid2) ownership = test_file.get_ownership() assert ownership["owner_name"] == uid2 assert ownership["group_name"] == uid2 assert ownership["uid"] == 2 assert ownership["gid"] == 2 test_file.set_ownership_by_name(uid1) ownership = test_file.get_ownership() assert ownership["owner_name"] == uid1 assert ownership["group_name"] == uid2 assert ownership["uid"] == 1 assert ownership["gid"] == 2 test_file.set_ownership_by_name(u_name=None, g_name=uid1) ownership = test_file.get_ownership() assert ownership["owner_name"] == uid1 assert ownership["group_name"] == uid1 assert ownership["uid"] == 1 assert ownership["gid"] == 1 assert test_file.remove() == True
def __detect_package_manager(self): pkg_manager_list = [ 'yum', 'apt-get' ] if simple_file("/etc/yum.conf").exists(): self.__package_manager = "yum" elif simple_dir("/etc/apt").exists(): self.__package_manager = "apt"
def test_file_copy_nokeep(): path = "/tmp/badadmin-tests-copy1" path2 = "/tmp/badadmin-tests-copy2" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True test_file.copy(path2) assert os.path.exists(path) == True assert os.path.exists(path2) == True test_file2 = simple_file(path2) assert test_file2.remove() == True assert test_file.remove() == True
def test_file_on_dir(): path = "/tmp" assert os.path.exists(path) == True try: test_file = simple_file(path) assert False except ValueError: assert True
def test_file_create_invalid(): path = "/tmp/nope/badadmin-tests" assert os.path.exists(path) == False test_file = simple_file(path) assert isinstance(test_file, simple_file) == True assert test_file.create() == False assert os.path.exists(path) == False
def test_file_exists(): path = "/tmp/badadmin-tests-replace-exists" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.exists() == True assert test_file.remove() == True
def test_file_permissions_set_invalid(): path = "/tmp/badadmin-tests-permissions-invalid" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.exists() == True permissions = test_file.get_permissions() orig_owner = permissions["owner"] orig_group = permissions["group"] orig_other = permissions["other"] orig_special = permissions["special"] assert test_file.set_permissions(8, 0, 0, 0) == False permissions = test_file.get_permissions() assert orig_owner == permissions["owner"] assert orig_group == permissions["group"] assert orig_other == permissions["other"] assert orig_special == permissions["special"] assert test_file.set_permissions(2, 0, 0, -1) == False permissions = test_file.get_permissions() assert orig_owner == permissions["owner"] assert orig_group == permissions["group"] assert orig_other == permissions["other"] assert orig_special == permissions["special"] assert test_file.set_permissions(2, 10, -4, 0) == False permissions = test_file.get_permissions() assert orig_owner == permissions["owner"] assert orig_group == permissions["group"] assert orig_other == permissions["other"] assert orig_special == permissions["special"] assert test_file.remove() == True
def test_file_permissions_set(): path = "/tmp/badadmin-tests-permissions-set" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.exists() == True permissions = test_file.get_permissions() assert permissions["owner"] >= 0 and permissions["owner"] <= 7 assert permissions["group"] >= 0 and permissions["group"] <= 7 assert permissions["other"] >= 0 and permissions["other"] <= 7 assert permissions["special"] >= 0 and permissions["special"] <= 7 assert test_file.set_permissions(6, 0, 0, 0) == True permissions = test_file.get_permissions() assert permissions["owner"] == 6 assert permissions["group"] == 0 assert permissions["other"] == 0 assert permissions["special"] == 0 assert test_file.set_permissions(6, 0, 0, 1) == True permissions = test_file.get_permissions() assert permissions["owner"] == 6 assert permissions["group"] == 0 assert permissions["other"] == 0 assert permissions["special"] == 1 assert test_file.set_permissions(7, 7, 7, 0) == True permissions = test_file.get_permissions() assert permissions["owner"] == 7 assert permissions["group"] == 7 assert permissions["other"] == 7 assert permissions["special"] == 0 assert test_file.remove() == True
def test_file_replace(): path = "/tmp/badadmin-tests-replace" content = "ReplaceMe\nReplaceMe\nNope\nReplaceMe\n" replace_with = "Done!" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.write_contents(content) == True assert test_file.get_contents() == content new_content = test_file.replace("ReplaceMe", replace_with) assert new_content == "Done!\nDone!\nNope\nDone!\n" assert test_file.remove() == True
def test_file_owner(): my_uid = os.getuid() my_gid = os.getgid() path = "/tmp/badadmin-tests-owner" assert os.path.exists(path) == False test_file = simple_file(path) assert test_file.create() == True assert os.path.exists(path) == True assert test_file.exists() == True ownership = test_file.get_ownership() assert ownership["uid"] == my_uid assert ownership["gid"] == my_gid assert ownership["owner_name"] == getpass.getuser() assert ownership["group_name"] == getpass.getuser() assert test_file.remove() == True
def file(self, filename): return simple_file(filename)
def test_dir_recursive_chmod(): path = test_location + "/badadmin-r-chmod" dirs = ['dir1' , 'dir2'] files = ['file1', 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) test_dir.create() assert os.path.exists(path) == True test_dir2 = simple_dir(path + "/" + dirs[0]) test_dir2.create() assert os.path.exists(path + "/" + dirs[0]) == True test_dir3 = simple_dir(path + "/" + dirs[1]) test_dir3.create() assert os.path.exists(path + "/" + dirs[1]) == True test_file1 = simple_file(path + "/" + files[0]) test_file1.create() assert os.path.exists(path + "/" + files[0]) == True test_file2 = simple_file(path + "/" + dirs[1] + "/" + files[0]) test_file2.create() assert os.path.exists(path + "/" + dirs[1] + "/" + files[0]) == True test_file3 = simple_file(path + "/" + dirs[0] + "/" + files[0]) test_file3.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[0]) == True test_file4 = simple_file(path + "/" + dirs[0] + "/" + files[1]) test_file4.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[1]) == True test_dir.recursive_chmod(7,0,0) permissions = test_dir.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 permissions = test_file4.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 permissions = test_file1.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 assert test_dir.remove() == True
def test_dir_recursive_chmod(): uid1 = pwd.getpwuid(1).pw_name uid2 = pwd.getpwuid(2).pw_name path = test_location + "/badadmin-r-chown" dirs = ['dir1' , 'dir2'] files = ['file1', 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) test_dir.create() assert os.path.exists(path) == True test_dir2 = simple_dir(path + "/" + dirs[0]) test_dir2.create() assert os.path.exists(path + "/" + dirs[0]) == True test_dir3 = simple_dir(path + "/" + dirs[1]) test_dir3.create() assert os.path.exists(path + "/" + dirs[1]) == True test_file1 = simple_file(path + "/" + files[0]) test_file1.create() assert os.path.exists(path + "/" + files[0]) == True test_file2 = simple_file(path + "/" + dirs[1] + "/" + files[0]) test_file2.create() assert os.path.exists(path + "/" + dirs[1] + "/" + files[0]) == True test_file3 = simple_file(path + "/" + dirs[0] + "/" + files[0]) test_file3.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[0]) == True test_file4 = simple_file(path + "/" + dirs[0] + "/" + files[1]) test_file4.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[1]) == True test_dir.recursive_chown_by_id(1,1) ownership = test_dir.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 ownership = test_file3.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 ownership = test_file2.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 test_dir.recursive_chown_by_name(uid2, uid2) ownership = test_dir.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 ownership = test_file3.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 ownership = test_dir2.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 assert test_dir.remove() == True
def test_dir_recursive_chmod(): path = test_location + "/badadmin-r-chmod" dirs = ['dir1', 'dir2'] files = ['file1', 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) test_dir.create() assert os.path.exists(path) == True test_dir2 = simple_dir(path + "/" + dirs[0]) test_dir2.create() assert os.path.exists(path + "/" + dirs[0]) == True test_dir3 = simple_dir(path + "/" + dirs[1]) test_dir3.create() assert os.path.exists(path + "/" + dirs[1]) == True test_file1 = simple_file(path + "/" + files[0]) test_file1.create() assert os.path.exists(path + "/" + files[0]) == True test_file2 = simple_file(path + "/" + dirs[1] + "/" + files[0]) test_file2.create() assert os.path.exists(path + "/" + dirs[1] + "/" + files[0]) == True test_file3 = simple_file(path + "/" + dirs[0] + "/" + files[0]) test_file3.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[0]) == True test_file4 = simple_file(path + "/" + dirs[0] + "/" + files[1]) test_file4.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[1]) == True test_dir.recursive_chmod(7, 0, 0) permissions = test_dir.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 permissions = test_file4.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 permissions = test_file1.get_permissions() assert permissions['owner'] == 7 assert permissions['group'] == 0 assert permissions['other'] == 0 assert permissions['special'] == 0 assert test_dir.remove() == True
def test_dir_recursive_chmod(): uid1 = pwd.getpwuid(1).pw_name uid2 = pwd.getpwuid(2).pw_name path = test_location + "/badadmin-r-chown" dirs = ['dir1', 'dir2'] files = ['file1', 'file2'] assert os.path.exists(path) == False test_dir = simple_dir(path) test_dir.create() assert os.path.exists(path) == True test_dir2 = simple_dir(path + "/" + dirs[0]) test_dir2.create() assert os.path.exists(path + "/" + dirs[0]) == True test_dir3 = simple_dir(path + "/" + dirs[1]) test_dir3.create() assert os.path.exists(path + "/" + dirs[1]) == True test_file1 = simple_file(path + "/" + files[0]) test_file1.create() assert os.path.exists(path + "/" + files[0]) == True test_file2 = simple_file(path + "/" + dirs[1] + "/" + files[0]) test_file2.create() assert os.path.exists(path + "/" + dirs[1] + "/" + files[0]) == True test_file3 = simple_file(path + "/" + dirs[0] + "/" + files[0]) test_file3.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[0]) == True test_file4 = simple_file(path + "/" + dirs[0] + "/" + files[1]) test_file4.create() assert os.path.exists(path + "/" + dirs[0] + "/" + files[1]) == True test_dir.recursive_chown_by_id(1, 1) ownership = test_dir.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 ownership = test_file3.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 ownership = test_file2.get_ownership() assert ownership['uid'] == 1 assert ownership['gid'] == 1 assert ownership['owner_name'] == uid1 assert ownership['group_name'] == uid1 test_dir.recursive_chown_by_name(uid2, uid2) ownership = test_dir.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 ownership = test_file3.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 ownership = test_dir2.get_ownership() assert ownership['uid'] == 2 assert ownership['gid'] == 2 assert ownership['owner_name'] == uid2 assert ownership['group_name'] == uid2 assert test_dir.remove() == True