Best Python code snippet using avocado_python
test_partition.py
Source:test_partition.py  
...78            f"{sys.executable} -c 'import time; "79            f'f = open("{self.use_mnt_file}", "w"); '80            f"time.sleep(60)'"81        )82    def run_process_to_use_mnt(self):83        proc = process.SubProcess(self.use_mnt_cmd, sudo=True)84        proc.start()85        self.assertTrue(86            wait.wait_for(87                lambda: os.path.exists(self.use_mnt_file),88                timeout=1,89                first=0.1,90                step=0.1,91            ),92            "File was not created within mountpoint",93        )94        return proc95    @unittest.skipIf(missing_binary("lsof"), "requires running lsof")96    @unittest.skipIf(97        not process.can_sudo(sys.executable + " -c ''"),98        "requires running Python as a privileged user",99    )100    @unittest.skipIf(101        not process.can_sudo("kill -l"), "requires running kill as a privileged user"102    )103    def test_force_unmount(self):104        """Test force-unmount feature"""105        with open("/proc/mounts") as proc_mounts_file:  # pylint: disable=W1514106            proc_mounts = proc_mounts_file.read()107        self.assertIn(self.mountpoint, proc_mounts)108        proc = self.run_process_to_use_mnt()109        self.assertTrue(self.disk.unmount())110        # Process should return -9, or when sudo is used111        # return code is 137112        self.assertIn(113            proc.poll(),114            [-9, 137],115            "Unexpected return code when trying to kill process "116            "using the mountpoint",117        )118        with open("/proc/mounts") as proc_mounts_file:  # pylint: disable=W1514119            proc_mounts = proc_mounts_file.read()120        self.assertNotIn(self.mountpoint, proc_mounts)121    @unittest.skipIf(122        not process.can_sudo(sys.executable + " -c ''"),123        "requires running Python as a privileged user",124    )125    @unittest.skipUnless(missing_binary("lsof"), "requires not having lsof")126    def test_force_unmount_no_lsof(self):127        """Checks that a force-unmount will fail on systems without lsof"""128        with open("/proc/mounts") as proc_mounts_file:  # pylint: disable=W1514129            proc_mounts = proc_mounts_file.read()130        self.assertIn(self.mountpoint, proc_mounts)131        proc = self.run_process_to_use_mnt()132        self.assertRaises(partition.PartitionError, self.disk.unmount)133        proc.wait(timeout=1)134    @unittest.skipIf(135        not process.can_sudo(sys.executable + " -c ''"),136        "requires running Python as a privileged user",137    )138    def test_force_unmount_get_pids_fail(self):139        """Checks PartitionError is raised if there's no lsof to get pids"""140        with open("/proc/mounts") as proc_mounts_file:  # pylint: disable=W1514141            proc_mounts = proc_mounts_file.read()142        self.assertIn(self.mountpoint, proc_mounts)143        proc = self.run_process_to_use_mnt()144        with unittest.mock.patch(145            "avocado.utils.partition.process.run", side_effect=process.CmdError146        ):147            with unittest.mock.patch(148                "avocado.utils.partition.process.system_output", side_effect=OSError149            ) as mocked_system_output:150                self.assertRaises(partition.PartitionError, self.disk.unmount)151                mocked_system_output.assert_called_with(152                    "lsof " + self.mountpoint, sudo=True153                )154        self.disk.unmount()155        proc.wait(timeout=1)156    def test_double_mount(self):157        """Check the attempt for second mount fails"""...test_utils_partition.py
Source:test_utils_partition.py  
...66        self.use_mnt_file = os.path.join(self.mountpoint, 'file')67        self.use_mnt_cmd = ("%s -c 'import time; f = open(\"%s\", \"w\"); "68                            "time.sleep(60)'" % (sys.executable,69                                                 self.use_mnt_file))70    def run_process_to_use_mnt(self):71        proc = process.SubProcess(self.use_mnt_cmd, sudo=True)72        proc.start()73        self.assertTrue(wait.wait_for(lambda: os.path.exists(self.use_mnt_file),74                                      timeout=1, first=0.1, step=0.1),75                        "File was not created within mountpoint")76        return proc77    @unittest.skipIf(missing_binary('lsof'), "requires running lsof")78    @unittest.skipIf(not process.can_sudo(sys.executable + " -c ''"),79                     "requires running Python as a privileged user")80    @unittest.skipIf(not process.can_sudo('kill -l'),81                     "requires running kill as a privileged user")82    def test_force_unmount(self):83        """ Test force-unmount feature """84        with open("/proc/mounts") as proc_mounts_file:85            proc_mounts = proc_mounts_file.read()86        self.assertIn(self.mountpoint, proc_mounts)87        proc = self.run_process_to_use_mnt()88        self.assertTrue(self.disk.unmount())89        # Process should return -9, or when sudo is used90        # return code is 13791        self.assertIn(proc.poll(), [-9, 137],92                      "Unexpected return code when trying to kill process "93                      "using the mountpoint")94        with open("/proc/mounts") as proc_mounts_file:95            proc_mounts = proc_mounts_file.read()96        self.assertNotIn(self.mountpoint, proc_mounts)97    @unittest.skipIf(not process.can_sudo(sys.executable + " -c ''"),98                     "requires running Python as a privileged user")99    @unittest.skipUnless(missing_binary('lsof'), "requires not having lsof")100    def test_force_unmount_no_lsof(self):101        """ Checks that a force-unmount will fail on systems without lsof """102        with open("/proc/mounts") as proc_mounts_file:103            proc_mounts = proc_mounts_file.read()104        self.assertIn(self.mountpoint, proc_mounts)105        proc = self.run_process_to_use_mnt()106        self.assertRaises(partition.PartitionError, self.disk.unmount)107        proc.wait(timeout=1)108    @unittest.skipIf(not process.can_sudo(sys.executable + " -c ''"),109                     "requires running Python as a privileged user")110    def test_force_unmount_get_pids_fail(self):111        """ Checks PartitionError is raised if there's no lsof to get pids """112        with open("/proc/mounts") as proc_mounts_file:113            proc_mounts = proc_mounts_file.read()114        self.assertIn(self.mountpoint, proc_mounts)115        proc = self.run_process_to_use_mnt()116        with unittest.mock.patch('avocado.utils.partition.process.run',117                                 side_effect=process.CmdError):118            with unittest.mock.patch('avocado.utils.partition.process.system_output',119                                     side_effect=OSError) as mocked_system_output:120                self.assertRaises(partition.PartitionError, self.disk.unmount)121                mocked_system_output.assert_called_with('lsof ' + self.mountpoint,122                                                        sudo=True)123        self.disk.unmount()124        proc.wait(timeout=1)125    def test_double_mount(self):126        """ Check the attempt for second mount fails """127        with open("/proc/mounts") as proc_mounts_file:128            proc_mounts = proc_mounts_file.read()129        self.assertIn(self.mountpoint, proc_mounts)...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!!
