def test_hd1_add_file(self): test_file = temp_file(contents=random_text()) test_file_name = file_name(test_file) file_in_hd1 = self.storage.hd1_add_file(test_file) assert file_exists(file_in_hd1) assert file_contents(file_in_hd1) == file_contents(test_file) assert self.storage.hd1_file_path(test_file_name) == file_in_hd1
def test_file_write(self): target = temp_file() text = "this is a string" assert file_contents(file_write(target, text)) == text assert file_bytes(file_write(target, text)) == text.encode() assert file_contents(file_write(target, text.encode(), mode='wb')) == text assert file_bytes(file_write(target, b"\x89PNG___", mode='wb')) == b"\x89PNG___"
def test_file_create(self): target = temp_file() text = random_string() assert file_delete(target) is True assert file_exists(target) is False assert file_create(target, text) == target assert file_exists(target) is True assert file_contents(target) == text empty_file = file_create() assert file_exists(empty_file) is True assert file_contents(empty_file) == ''
def test_upload__download(self): local_file = temp_file(file_contents="This is a local file - " + random_string()) # create local temp file self.datastore_file.upload(local_file) # upload file to server tmp_file = self.datastore_file.download() # download file from server assert file_exists(tmp_file) # confirm it exists assert file_contents(local_file) == file_contents( tmp_file ) # confirm content matches the randomly generated temp content assert self.datastore_file.delete( ) is True # delete temp file from data_store file_delete(local_file) # delete local temp file
def test_file_copy(self): text_a = random_string() text_b = random_string() file_a = temp_file(contents=text_a) file_b = temp_file(contents=text_b) assert file_exists(file_a) is True assert file_exists(file_b) is True assert file_contents(file_a) == text_a assert file_contents(file_b) == text_b assert file_delete(file_b) is True assert file_exists(file_b) is False file_copy(file_a, file_b) assert file_exists(file_b) is True assert file_contents(file_b) == text_a
async def test_set_chrome_log_file(self): log_file = '/tmp/chrome_logfile.log' file_delete(log_file) chrome = Chrome().headless(True) chrome.chrome_args.enable_logging(log_file) await chrome.browser() assert 'Could not get the download directory.' in file_contents( log_file).split('\n').pop(0)
def test_yaml_save__load(self): yaml = yaml_parse(yaml_code) yaml_file = yaml_save(yaml) assert file_exists(yaml_file) is True assert file_contents(yaml_file) == yaml_code assert yaml_load(yaml_file) == yaml
def load_file(path): """ Loads json data from file Note: will not throw errors and will return {} as default errors are logged to Json.log """ json_data = file_contents(path) return json_loads(json_data)
def test_key_pair_create_to_file(self): key_name = 'osbot-unit-tests-temp_key_pair' result = self.ec2.key_pair_create_to_file(key_name=key_name) key_pair_id = result.get('key_pair').get('KeyPairId') path_key_pair = result.get('path_key_pair') assert file_exists(path_key_pair) is True assert file_contents(path_key_pair).find('BEGIN RSA PRIVATE KEY') > -1 assert self.ec2.key_pair_delete(key_pair_id=key_pair_id) is True assert file_delete(path_key_pair) is True
def get_log_file(self, log_file, size=0): path = f'/var/log/{log_file}.log' if size > 0: return self.esxi_ssh.tail(path, size) log_file = self.esxi_ssh.exec_scp_command(path) if log_file: log_data = file_contents(log_file) file_delete(log_file) return log_data
def test_folder_zip(self): folder = temp_folder_with_temp_file(file_contents=random_string()) print() zip_file = folder_zip(folder) assert file_exists(zip_file) assert file_extension(zip_file) == '.zip' unziped_folder = Files.unzip_file(zip_file) source_files = folder_files(folder) target_files = folder_files(unziped_folder) assert len(source_files) == 1 assert len(target_files) == 1 assert source_files[0] != target_files[0] assert file_contents(source_files[0]) == file_contents(target_files[0]) assert zip_file_list(zip_file) == ['temp_file.txt']
def test_upload(self): tmp_folder = Temp_Folder_With_Lambda_File( self.lambda_name).create_temp_file() (self.aws_lambda.set_s3_bucket(self.s3_bucket).set_s3_key( self.s3_key).set_folder_code(tmp_folder.folder)) downloaded_file = self.aws_lambda.s3().file_download( self.s3_bucket, self.s3_key) # download file uploaded assert file_exists(downloaded_file) is True unzip_location = tmp_folder.folder + '_unzipped' Files.unzip_file(downloaded_file, unzip_location) # unzip it assert file_contents(Files.find(unzip_location + '/*').pop( )) == tmp_folder.lambda_code # confirm unzipped file's contents self.aws_lambda.s3().file_delete(self.s3_bucket, self.s3_key)
def test_get_file(self): headless = True file_size = 10 # goes up nicely to more that 1Mb random_text = random_string(length=file_size) with Temp_File(contents=random_text) as temp_file: root_folder = temp_file.tmp_folder file_to_download = temp_file.tmp_file with Temp_Web_Server(root_folder=root_folder) as web_server: with Temp_Browser(headless=headless) as browser: browser.set_auto_close(False) browser.open(web_server.url()) full_link = urljoin(web_server.url(), file_to_download) page = browser.page() wait_for_download = Wait_For_Download(page) wait_for_download.sync_set_browser_download_folder() wait_for_download.sync_trigger_download(full_link) downloaded_file = wait_for_download.sync_get_file() assert random_text == file_contents(downloaded_file)
def server_in_known_hosts(self): home = os.environ.get('HOME') known_hosts = file_contents(f'{home}/.ssh/known_hosts') target = self.ssh_config.get('server') return target in known_hosts
def test_save_string_as_file(self): data = random_string() temp_file = save_string_as_file(data) assert file_contents(temp_file) == data
def test_save_file_pretty(self): data = {'answer': 42} print() assert file_contents(json_save_file(data)) == '{"answer": 42}' assert file_contents( json_save_file_pretty(data)) == '{\n "answer": 42\n}'
def test_logger_add_handler__file(self): log_file = log_to_file() log_debug('debug') log_error('error') log_info('info') assert file_contents(log_file) == 'error\ninfo\n'