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)
Exemple #2
0
    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_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
Exemple #4
0
    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 add_test_files(self, count=5, text_size=10, execute_stage_1=False):
     added_files = []
     #random_blog = random_text(length=count*text_size)
     for i in range(1, count + 1):
         text = random_text(
         ) + '_' * text_size  # much better performance than using random_text for the full string
         test_file = Test_Data().create_test_pdf(text=text,
                                                 file_key=f"temp_file_{i}")
         added_files.append(self.storage.hd1_add_file(test_file))
         file_delete(test_file)
     if execute_stage_1:
         self.execute_stage_1()
     return added_files
Exemple #6
0
    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_is_hash(self):
        test_file   = temp_file(contents='aaaa')
        file_hash   = Metadata_Utils().file_hash(test_file)                         # create hash from file
        text_hash   = str_sha256('asd')                                             # create hash from string

        assert self.hash_json.is_hash(file_hash         ) is True                   # confirm both are valid hashes
        assert self.hash_json.is_hash(text_hash         ) is True

        assert self.hash_json.is_hash(None              ) is False                  # testing all sorts of conner cases
        assert self.hash_json.is_hash(''                ) is False                  # empty strings
        assert self.hash_json.is_hash('aaaa'            ) is False                  # non hash string
        assert self.hash_json.is_hash(file_hash + 'aaaa') is False                  # confirm only exact matches work
        assert self.hash_json.is_hash(text_hash + 'aaaa') is False
        assert self.hash_json.is_hash('aaa' + file_hash ) is False
        assert self.hash_json.is_hash(text_hash + '\nb`') is False                  # confirm content in new lines is also not a match
        assert self.hash_json.is_hash('a\n' + file_hash ) is False

        file_delete(test_file)
Exemple #8
0
    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
Exemple #9
0
    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))
Exemple #10
0
    def setup_test_environment__Deploy_Lambda(
            cls):  # todo: refactor into separate class
        STS().check_current_session_credentials()
        cls.lambda_name = "osbot_test_deploy_lambda"
        cls.lambda_code = Temp_Folder_With_Lambda_File(cls.lambda_name)
        cls.code_folder = cls.lambda_code.folder

        lambda_file = cls.lambda_code.tmp_file
        module_folder = path_combine(cls.code_folder,
                                     "osbot_test_deploy_lambda")
        lambda_in_module = path_combine(module_folder, file_name(lambda_file))
        folder_create(module_folder)
        file_copy(lambda_file,
                  lambda_in_module)  # todo add a file_move to OSBot_Utils
        file_delete(lambda_file)
        file_create(path_combine(module_folder, '__init__.py'), "")

        sys.path.append(cls.code_folder)

        cls.lambda_module = importlib.import_module(
            "osbot_test_deploy_lambda.osbot_test_deploy_lambda")
        cls.lambda_function = cls.lambda_module.run
    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
    async def test_keep_open(self):
        file_delete(Chrome_Setup(None, None).file_tmp_last_chrome_session)
        url_1 = 'https://www.google.com/404'
        url_2 = 'https://www.google.com/ABC'
        chrome_1 = Chrome().keep_open(
        )  # 1st chrome object (with keep_open setting)
        browser_1 = await chrome_1.browser(
        )  # open process and get browser object
        page_1 = (await browser_1.pages()).pop()  # get first page
        await page_1.goto(url_1)  # open 404 in google
        assert page_1.url == url_1  # confirm url

        chrome_2 = Chrome().keep_open()  # create 2nd chrome object
        browser_2 = await chrome_2.browser(
        )  # connect to chrome and get browser object
        page_2 = (await browser_2.pages()).pop()  # get page object
        assert page_2.url == url_1  # confirm url

        await page_2.goto(url_2)  # open another page in browser_2
        assert page_1.url == url_2  # confirm it was opened in browser 1
        assert page_2.url == url_2  # and in browser_2

        chrome_3 = Chrome().keep_open()  # create 3rd chrome object
        browser_3 = await chrome_3.browser(
        )  # connect to chrome and get browser object
        page_3 = (await browser_3.pages()).pop()  # get page object
        assert page_3.url == url_2  # confirm url

        assert self.chrome.chrome_setup.connect_method(
        ) == 'No browser open or connected'
        assert chrome_1.chrome_setup.connect_method(
        ) == 'Started chrome process'
        assert chrome_2.chrome_setup.connect_method(
        ) == 'Connected to running chrome process'
        assert chrome_3.chrome_setup.connect_method(
        ) == 'Connected to running chrome process'
        #await browser_1.close()
        await browser_3.close()  # close browser
Exemple #13
0
    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 prepare_hd2_hash_folder_zip(self,path_list, zip_name):
        try:
            self.temp_folder = temp_folder()

            for hash_folder_path in path_list:
                name = ntpath.basename(hash_folder_path)
                dst_path = os.path.join(self.temp_folder, name)

                if os.path.isdir(hash_folder_path):
                    folder_copy(hash_folder_path, dst_path)

                    hd2_source_file = os.path.join(dst_path, "source")
                    if os.path.isfile(hd2_source_file):
                        file_delete(hd2_source_file)

            target_file_path = os.path.join(self.zip_folder, zip_name)
            zip_files(self.temp_folder, file_pattern='*.*', target_file = target_file_path)
            folder_delete_all(self.temp_folder)

            return target_file_path

        except Exception as error:
            logger.error(f"File_Distributor: prepare_zip : {error}")
            raise error
Exemple #15
0
    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 tearDownClass(cls) -> None:
     file_delete(cls.test_file)
 def tearDownClass(cls) -> None:
     super().tearDownClass()
     file_delete(cls.test_file)
Exemple #18
0
 def tearDown(self) -> None:
     file_delete(self.file_path)
 def tearDownClass(cls) -> None:
     file_delete(cls.file_path)
     file_delete(cls.file_copy_path)
Exemple #20
0
 def tearDown(self):
     file_delete(self.path_temp_png)  # delete file is created
 def tearDownClass(cls) -> None:
     file_delete(cls.test_file)
     folder_delete_all(cls.temp_dir)
     Metadata(file_hash=cls.file_hash).delete()