def test_json_load_file_gz__json_save_file_pretty_gz(self): data = {'answer': 42} gz_file = json_save_file_pretty_gz(data) assert json_load_file_gz(gz_file) == data assert file_exists(gz_file) is True assert json_load_file_gz_and_delete(gz_file) == data assert file_exists(gz_file) is False
def test_add_file(self): metadata = self.metadata file_paths = metadata.data.get('original_file_paths') assert self.metadata.exists() is False # metadata folder doesn't exist # adding file first time assert metadata.add_file( self.file_path ) == self.file_hash # add file and get file hash as return value assert metadata.exists() is True # confirm metadata folder now exists assert folder_exists(metadata.metadata_folder_path() ) # confirm metadata folder now exists assert file_exists( metadata.metadata_file_path()) # confirm metadata json file exists assert file_exists(metadata.source_file_path() ) # confirm source file was correctly put in place assert metadata.file_hash == self.metadata_utils.file_hash( metadata.source_file_path() ) # confirm hash of source file matches hash of file_path assert metadata.metadata_file_path() == path_combine( metadata.metadata_folder_path(), DEFAULT_METADATA_FILENAME ) # confirm metadata file is place in correct location file_paths = metadata.data.get('original_file_paths') assert file_paths == [ self.file_path ] # confirms that in this mode the entire path is preserved # adding same file 2nd time (with same hash and same name) assert metadata.add_file( self.file_path) == self.file_hash # adding the same file again file_paths = metadata.data.get('original_file_paths') assert file_paths == [self.file_path ] # should not impact this value (same as above) # adding same file 3nd time (with same hash but different name) assert metadata.add_file( self.file_copy_path ) == self.file_hash # adding the same file again (with different name) file_paths = metadata.data.get('original_file_paths') assert file_paths == [self.file_path, self.file_copy_path ] # will make the new file path be added # adding same file 4th time (with self.path_hd1 set to parent folder of path) file_parent_folder = parent_folder( self.file_path) # get parent folder of test file self.metadata.path_hd1 = file_parent_folder # assign it to the metadata variable used to calculate virtual paths assert metadata.add_file(self.file_path) == self.file_hash file_paths = metadata.data.get('original_file_paths') assert file_paths == [ self.file_path, self.file_copy_path, file_name(self.file_path) ] # confirm that the virtual file path was added as the 3rd item (in this case the file name) #clean up assert self.metadata.delete() is True assert folder_not_exists(self.metadata.metadata_folder_path())
def test_download(self): path_screenshot_1 = self.screenshot.download() assert file_exists(path_screenshot_1) # download to specific location target_file = temp_file(extension='.png') #target_file = '/tmp/vm_screenshot_2.png' #self.screenshot.vm.wait(2) path_screenshot_2 = VM_Screenshot(self.vm, target_file).download() assert file_exists(path_screenshot_2)
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_save_png_bytes_to_file(self): with Log_To_String(logger_png) as log_to_string: assert file_not_exists(self.path_temp_png) png_file = save_png_bytes_to_file(bytes=TEST_PNG_BYTES, png_file=self.path_temp_png) assert png_file == self.path_temp_png assert file_exists(self.path_temp_png) assert log_to_string.contents( ) == f'Png data with size 148 saved to {png_file}\n' with Log_To_String(logger_png) as log_to_string: png_file = save_png_bytes_to_file(bytes=TEST_PNG_BYTES) assert file_exists(png_file) assert log_to_string.contents( ) == f'Png data with size 148 saved to {png_file}\n'
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
def test_GET_bytes_to_file(self): target = temp_file(extension="png") assert file_not_exists(target) assert GET_bytes_to_file(self.url_png, target) assert file_exists(target) assert file_size(target) == 17575 assert file_bytes(target)[:4] == b"\x89PNG"
def test_copy_file(self): assert file_exists(self.test_file) assert folder_exists(self.new_folder) self.dst = os.path.join(self.new_folder, "image2.jpg") self.file_service.copy_file(self.test_file, self.dst) assert os.path.exists(self.dst) is True
def test_file_create_and_deletion(self): with Temp_S3_Zip_With_Lambda_File() as temp_s3_zip: assert folder_exists(temp_s3_zip.folder) assert file_exists(temp_s3_zip.tmp_file) assert temp_s3_zip.s3_prefix == 'lambdas/unit_tests/temp_zips' assert 'temp_zip_file_' in temp_s3_zip.file_name assert temp_s3_zip.s3_key == f'{temp_s3_zip.s3_prefix}/{temp_s3_zip.file_name}.zip'
async def test_args_set_user_data_dir__enable_logging(self): user_data = temp_folder() chrome = Chrome().headless(True) (chrome.chrome_args.args_set_user_data_dir(user_data).enable_logging()) await chrome.browser() log_file = path_combine(user_data, 'Default/chrome_debug.log') assert file_exists(log_file)
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 upload_ova(self, ova_path): sdk = self.sdk assert file_exists(ova_path) ovf_handle = OvfHandler(ova_path) ovfManager = sdk.content().ovfManager rp = sdk.resource_pool() ds = sdk.datastore() dc = sdk.datacenter() cisp = pyVmomi.vim.OvfManager.CreateImportSpecParams() cisr = ovfManager.CreateImportSpec(ovf_handle.get_descriptor(), rp, ds, cisp) ovf_handle.set_spec(cisr) lease = rp.ImportVApp(cisr.importSpec, dc.vmFolder) while lease.state == pyVmomi.vim.HttpNfcLease.State.initializing: print("Waiting for lease to be ready...") time.sleep(1) if lease.state == pyVmomi.vim.HttpNfcLease.State.error: print("Lease error: %s" % lease.error) return 1 if lease.state == pyVmomi.vim.HttpNfcLease.State.done: return 0 host = sdk.server_details().get('host') print("Starting deploy...") result = ovf_handle.upload_disks(lease, host) print(result)
def test_lambda_upload_file(self): deploy_lambda = Deploy_Lambda(self.lambda_handler) package = deploy_lambda.package aws_lambda = package.aws_lambda deploy_lambda.add_function_source_code() assert run.__module__ == self.expected_module assert run.__name__ == 'run' assert '/osbot_aws/lambdas/dev/hello_world.py' in package.get_files() assert len(files_list(aws_lambda.folder_code)) == len( package.get_files()) assert file_exists( path_combine(aws_lambda.folder_code, 'osbot_aws/lambdas/dev/hello_world.py')) assert aws_lambda.s3_bucket == f'{self.expected_account_id}-osbot-lambdas' assert aws_lambda.s3_key == f'lambdas/{self.expected_module}.zip' assert self.s3.file_exists(bucket=aws_lambda.s3_bucket, key=aws_lambda.s3_key) is True assert self.s3.file_delete(bucket=aws_lambda.s3_bucket, key=aws_lambda.s3_key) is True assert self.s3.file_exists(bucket=aws_lambda.s3_bucket, key=aws_lambda.s3_key) is False self.s3.folder_upload(folder=aws_lambda.folder_code, s3_bucket=aws_lambda.s3_bucket, s3_key=aws_lambda.s3_key) assert self.s3.file_exists(bucket=aws_lambda.s3_bucket, key=aws_lambda.s3_key) is True
def test_upload_ova_file(self): self.ova.download_ova_file(self.url, self.ova_path) assert file_exists(self.ova_path) self.ova.upload_ova(self.ova_path) self.vm = self.ova.sdk.find_by_name(self.vm_name) assert self.vm is not None self.vm.task().delete()
def test_image_build(self): target_image = 'centos' expected_size = 209348126 folder_dockerFile = path_combine(self.path_docker_images, target_image) path_dockerfile = path_combine(folder_dockerFile, 'Dockerfile') repository = "osbot_docker__test_image_build" tag = "abc" image_name = f"{repository}:{tag}" assert folder_exists(folder_dockerFile) assert file_exists(path_dockerfile) result = self.api_docker.image_build(folder_dockerFile, repository, tag) build_logs = result.get('build_logs') image = result.get('image') status = result.get('status') tags = result.get('tags') assert self.api_docker.image_exists(repository, tag) assert status == 'ok' assert image_name in tags assert image_name in self.api_docker.images_names() assert image.get('Size') == expected_size assert next(build_logs) == {'stream': 'Step 1/3 : FROM centos:8'} assert self.api_docker.image_delete(repository, tag) is True assert image_name not in self.api_docker.images_names()
def test_screenshot(self): self.vm.power_on() #self.vm.wait(1) # todo: when OCR serverless function is available add check to confirm text from image is the correct one self.vm.send_key('KEY_F2') # will trigger setup page #self.vm.wait(4) # todo: when OVR is available use this to detect the correct use of F2 self.vm.send_enter() path_screenshot = self.vm.screenshot() assert file_exists(path_screenshot)
def test_deployment(self): deployment_file = path_combine('../../test_files/deployment', 'nginx-deployment.yaml') assert file_exists(deployment_file) deployment = yaml_load(deployment_file) resp = self.cluster.api_apps().create_namespaced_deployment( body=deployment, namespace="default") print("Deployment created. status='%s'" % resp.metadata.name)
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 test_icap_process_file(self): file_to_rebuild = Files_To_Rebuild().file_word_with_macros() file_to_process = file_to_rebuild.get('local_path') expected_file_md5 = file_to_rebuild.get('md5s').get('rebuilt') expected_file_size = file_to_rebuild.get('file_sizes').get('rebuilt') result = self.icap_client.icap_process_file(self.target_ip, self.target_service, file_to_process) config = result.get('config') original_file = config.get('local_config').get('input_file') rebuilt_file = config.get('local_config').get('output_file') assert file_exists(original_file) assert file_exists(rebuilt_file) assert result.get('file_sizes').get('rebuilt') == expected_file_size assert result.get('md5s').get('rebuilt') == expected_file_md5 pprint(result)
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 test_set_hd3_location(self): hd3_location = path_combine(temp_folder(), 'cccc') assert file_not_exists(hd3_location) self.config.set_hd3_location(hd3_location) assert self.config.hd3_location == hd3_location assert file_exists(hd3_location) self.config.load_values() assert abspath( self.config.hd3_location) == abspath('./test_data/scenario-1/hd3') assert folder_delete_all(hd3_location)
def test_prepare_zip(self): zip_name = self.zip_name response = self.file_distributor.prepare_zip(self.test_file, zip_name) # zip a file assert response == os.path.join(self.file_distributor.zip_folder, zip_name) assert file_exists( os.path.join(self.file_distributor.zip_folder, zip_name)) file_delete(os.path.join(self.file_distributor.zip_folder, zip_name)) response = self.file_distributor.prepare_zip(self.test_folder, zip_name) # zip a folder assert response == os.path.join(self.file_distributor.zip_folder, zip_name) assert file_exists( os.path.join(self.file_distributor.zip_folder, zip_name)) file_delete(os.path.join(self.file_distributor.zip_folder, zip_name))
def test_save(self): target_file = temp_file() # temp file to save data assert file_not_exists(target_file) # confirm it doesn't exist with patch.object(Hash_Json, 'get_file_path', return_value=target_file): # patch get_file_path to return temp file path assert self.hash_json.get_file_path() == target_file # confirm patch is in place self.hash_json.save() # call write_to_file assert file_exists(target_file) # confirm temp file now exists assert self.hash_json.load() == self.hash_json.data() # confirm reloaded data is correct assert json_load_file(target_file) == self.hash_json.data() # also confirm using direct json load of temp file assert self.hash_json.get_file_path() != target_file # confirm pathc is not there (after 'with' ends) file_delete(target_file) # delete temp file
def test_prepare_hd2_hash_folder_zip(self): zip_name = self.zip_name test_path_list = [self.test_folder] response = self.file_distributor.prepare_hd2_hash_folder_zip( test_path_list, zip_name) assert response == os.path.join(self.file_distributor.zip_folder, zip_name) assert file_exists( os.path.join(self.file_distributor.zip_folder, zip_name)) file_delete(os.path.join(self.file_distributor.zip_folder, zip_name))
def test_get_access_key_for_user( self): # test CI_Setup().get_access_key_for_user method path_temp_credentials = self.ci_setup.path_temp_credentials # store path in local var to make it easier to read assert file_delete( path_temp_credentials ) is True # make sure temp file with access keys doesn't exist credential_1 = self.ci_setup.get_access_key_for_user( ) # create first test credential assert file_exists( path_temp_credentials) # confirm temp file now exists assert len(self.ci_setup.iam.user_access_keys() ) == 1 # confirm there is only one access key for this user file_delete(path_temp_credentials) # delete temp file credential_2 = self.ci_setup.get_access_key_for_user( delete_keys=False ) # create another credential with delete_keys set to false assert len(self.ci_setup.iam.user_access_keys() ) == 2 # confirm that there are 2 keys now in this user assert credential_1 != credential_2 # confirm that first and second credentials don't match assert file_exists( path_temp_credentials) # confirm that temp file exists credential_3 = self.ci_setup.get_access_key_for_user( delete_keys=True ) # create 3rd credential (with delete_keys set to True) assert len(self.ci_setup.iam.user_access_keys() ) == 1 # confirm that there is only 1 key for this user assert credential_1 != credential_3 # and the that the credentials don't match credential_4 = self.ci_setup.get_access_key_for_user( new_key=False ) # with new_key=False we should get the value from temp file assert credential_1 != credential_4 # which means that they shouldn't match with first credential assert credential_3 == credential_4 # but should match with the 3rd assert len(self.ci_setup.iam.user_access_keys( )) == 1 # finally confirm there is only one valid key for this user
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__enter__leave__(self): temp_file = Temp_File() temp_web_server = Temp_Web_Server(root_folder=temp_file.tmp_folder) temp_browser = Temp_Browser(open_page=temp_web_server.url()) full_link = urljoin(temp_web_server.url(), temp_file.tmp_file) with temp_file: with temp_web_server: with temp_browser: with Wait_For_Download(page=temp_browser.page()) as _: _.sync_trigger_download(full_link) _.sync_get_file() assert _.status is True assert _.wait_count > 0 assert len(_.screenshots) == 0 assert file_exists(_.downloaded_file) with Wait_For_Download(page=temp_browser.page(), capture_screenshots=True) as _: _.sync_trigger_download(full_link) _.sync_get_file() assert _.status is True assert _.wait_count > 0 assert len(_.screenshots) == 1 assert file_exists(_.downloaded_file)
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 add_file(self, file_path): if file_exists(file_path): tik = datetime.now() self.set_file_hash(self.metadata_utils.file_hash(file_path)) tok = datetime.now() delta = tok - tik self.set_file_hash_calculation_time(delta.total_seconds()) if self.exists(): self.get_from_file() else: self.create(file_path) self.add_file_path(file_path) self.save() return self.file_hash
def test_set_hd2_location(self): hd2_location = path_combine(temp_folder(), 'bbb') assert file_not_exists(hd2_location) self.config.set_hd2_location(hd2_location) assert self.config.hd2_location == hd2_location assert self.config.hd2_data_location == path_combine( hd2_location, DEFAULT_HD2_DATA_NAME) assert self.config.hd2_status_location == path_combine( hd2_location, DEFAULT_HD2_STATUS_NAME) assert self.config.hd2_processed_location == path_combine( hd2_location, DEFAULT_HD2_PROCESSED_NAME) assert file_exists(hd2_location) self.config.load_values() assert abspath( self.config.hd2_location) == abspath('./test_data/scenario-1/hd2') assert folder_delete_all(hd2_location)