Best Python code snippet using avocado_python
test_fileutils.py
Source:test_fileutils.py  
...94class WriteToTempfileTestCase(test_base.BaseTestCase):95    def setUp(self):96        super(WriteToTempfileTestCase, self).setUp()97        self.content = 'testing123'.encode('ascii')98    def check_file_content(self, path):99        with open(path, 'r') as fd:100            ans = fd.read()101            self.assertEqual(self.content, six.b(ans))102    def test_file_without_path_and_suffix(self):103        res = fileutils.write_to_tempfile(self.content)104        self.assertTrue(os.path.exists(res))105        (basepath, tmpfile) = os.path.split(res)106        self.assertTrue(basepath.startswith(tempfile.gettempdir()))107        self.assertTrue(tmpfile.startswith('tmp'))108        self.check_file_content(res)109    def test_file_with_not_existing_path(self):110        random_dir = uuid.uuid4().hex111        path = '/tmp/%s/test1' % random_dir112        res = fileutils.write_to_tempfile(self.content, path=path)113        self.assertTrue(os.path.exists(res))114        (basepath, tmpfile) = os.path.split(res)115        self.assertEqual(basepath, path)116        self.assertTrue(tmpfile.startswith('tmp'))117        self.check_file_content(res)118        shutil.rmtree('/tmp/' + random_dir)119    def test_file_with_not_default_suffix(self):120        suffix = '.conf'121        res = fileutils.write_to_tempfile(self.content, suffix=suffix)122        self.assertTrue(os.path.exists(res))123        (basepath, tmpfile) = os.path.split(res)124        self.assertTrue(basepath.startswith(tempfile.gettempdir()))125        self.assertTrue(tmpfile.startswith('tmp'))126        self.assertTrue(tmpfile.endswith('.conf'))127        self.check_file_content(res)128    def test_file_with_not_existing_path_and_not_default_suffix(self):129        suffix = '.txt'130        random_dir = uuid.uuid4().hex131        path = '/tmp/%s/test2' % random_dir132        res = fileutils.write_to_tempfile(self.content,133                                          path=path,134                                          suffix=suffix)135        self.assertTrue(os.path.exists(res))136        (basepath, tmpfile) = os.path.split(res)137        self.assertTrue(tmpfile.startswith('tmp'))138        self.assertEqual(basepath, path)139        self.assertTrue(tmpfile.endswith(suffix))140        self.check_file_content(res)141        shutil.rmtree('/tmp/' + random_dir)142    def test_file_with_not_default_prefix(self):143        prefix = 'test'144        res = fileutils.write_to_tempfile(self.content, prefix=prefix)145        self.assertTrue(os.path.exists(res))146        (basepath, tmpfile) = os.path.split(res)147        self.assertTrue(tmpfile.startswith(prefix))148        self.assertTrue(basepath.startswith(tempfile.gettempdir()))...test_control_nagios.py
Source:test_control_nagios.py  
...107        filename = (self.base_dir108                    +"etc/objects/hosts/"109                    +self.hostname110                    +".cfg")111        self.assertTrue(utils.check_file_content(filename,112                                                 self.hostname)113                       )114        self.assertTrue(utils.check_file_content(filename,115                                                 self.hostip)116                       )117    def test_ndo2db_set(self):118        """ Test nagios ndo2db setting """119        msgmnb = "/proc/sys/kernel/msgmnb"120        msgmni = "/proc/sys/kernel/msgmni"121        self.assertTrue(utils.check_file_content(msgmnb,122                                                 self.kernMsgmnb)123                       )124        self.assertTrue(utils.check_file_content(msgmni,125                                                 self.kernMsgmni)126                       )127        ndo2db_file = self.base_dir+"etc/ndo2db.cfg"128        self.assertTrue(utils.check_file_content(ndo2db_file,129                                                 self.db_name)130                       )131        self.assertTrue(utils.check_file_content(ndo2db_file,132                                                 self.db_user)133                       )134        self.assertTrue(utils.check_file_content(ndo2db_file,135                                                 self.db_pass)136                       )137        nagios_file = self.base_dir+"etc/nagios.cfg"138        self.assertTrue(utils.check_file_content(nagios_file,139                                                 self.broker)140                       )141    def test_nrpe_set(self):142        """ Test nagios nrpe setting """143        nrpe_file = "/etc/xinetd.d/nrpe"144        self.assertTrue(utils.check_file_content(nrpe_file,145                                                 self.hostip)146                       )147    def test_service_initd(self):148        """ Test service initd """149        for initd in self.initd:150            self.assertTrue(os.path.lexists(initd))151    def test_service_ps(self):152        """ Test service ps """153        for ps in self.service_ps.values():154            self.assertIsNotNone(155                subprocess.check_output('ps -ef | grep '+ps,156                                        shell=True)157                )158    def test_service_port(self):...test_file_content.py
Source:test_file_content.py  
1from pii_secret_check_hooks.check_file.file_content import (2    CheckFileContent,3)4def test_entropy_check():5    check_file_content = CheckFileContent(6        excluded_file_list=None,7        custom_regex_list=None,8    )9    assert check_file_content._entropy_check(10        # High entropy random chars11        "6F28CA8A9CBD6FA88FC29A35D8257D0B1717C2D9384D4B25F5F29B82C07A2EB2",12    )13    assert not check_file_content._entropy_check(14        "No entropy here",15    )16def test_trufflehog_check():17    check_file_content = CheckFileContent(18        excluded_file_list=None,19        custom_regex_list=None,20    )21    # Use AWS key string as regex is present in TruffleHog regex project22    assert check_file_content._trufflehog_check(23        "test AKIA11111111AAAAAAAA test"24    )25def test_pii_regex():26    check_file_content = CheckFileContent(27        excluded_file_list=None,28        custom_regex_list=None,29    )30    assert check_file_content._pii_regex(31        "I am a line, with Buckingham Palace's postcode - SW1A 1AA"32    )33    assert not check_file_content._pii_regex(34        "I do not contain any PII"35    )36def test_custom_regex_checks():37    check_file_content = CheckFileContent(38        excluded_file_list=None,39        custom_regex_list=[40            "dog name=(\s*)dog(\s*)name(\s*)"41        ],42    )43    assert check_file_content._custom_regex_checks(44        "My dog name is Rover"45    )46    assert not check_file_content._custom_regex_checks(47        "I do not contain any names of dogd"...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
