def test_wipe_quickly_failed(self): dev_name = factory.make_name("disk").encode("ascii") mock_check_output = self.patch(subprocess, "check_output") mock_check_output.side_effect = subprocess.CalledProcessError( 1, "wipefs" ) mock_os_open = self.patch(builtins, "open") mock_os_open.side_effect = OSError(-2, "No such file or directory") wipe_quickly(dev_name) self.assertThat( self.print_flush, MockCallsMatch( call("%s: starting quick wipe." % dev_name.decode("ascii")), call("%s: wipefs failed (1)" % dev_name.decode("ascii")), call( "%s: OS error while wiping beginning/end of disk (No such file or directory)" % dev_name.decode("ascii") ), call( "%s: failed to be quickly wiped." % dev_name.decode("ascii") ), ), )
def test_wipe_quickly_successful_but_wipefs_failed(self): tmp_dir = self.make_dir() dev_path = (tmp_dir + "/%s").encode("ascii") self.patch(maas_wipe, "DEV_PATH", dev_path) dev_name = factory.make_name("disk").encode("ascii") file_path = dev_path % dev_name self.make_empty_file(file_path, content=b"T") mock_check_output = self.patch(subprocess, "check_output") mock_check_output.side_effect = subprocess.CalledProcessError( 1, "wipefs" ) wipe_quickly(dev_name) buf_size = 1024 * 1024 with open(file_path, "rb") as fp: first_buf = fp.read(buf_size) fp.seek(-buf_size, 2) last_buf = fp.read(buf_size) zero_buf = b"\0" * 1024 * 1024 self.assertEqual(zero_buf, first_buf, "First 1 MiB was not wiped.") self.assertEqual(zero_buf, last_buf, "Last 1 MiB was not wiped.") self.assertThat( self.print_flush, MockCallsMatch( call("%s: starting quick wipe." % dev_name.decode("ascii")), call("%s: wipefs failed (1)" % dev_name.decode("ascii")), call( "%s: successfully quickly wiped." % dev_name.decode("ascii") ), ), )
def test_wipe_quickly(self): tmp_dir = self.make_dir() dev_path = (tmp_dir + "/%s").encode("ascii") self.patch(maas_wipe, "DEV_PATH", dev_path) dev_name = factory.make_name("disk").encode("ascii") file_path = dev_path % dev_name self.make_empty_file(file_path, content=b"T") wipe_quickly(dev_name) buf_size = 1024 * 1024 with open(file_path, "rb") as fp: first_buf = fp.read(buf_size) fp.seek(-buf_size, 2) last_buf = fp.read(buf_size) zero_buf = b"\0" * 1024 * 1024 self.assertEqual(zero_buf, first_buf, "First 1 MiB was not wiped.") self.assertEqual(zero_buf, last_buf, "Last 1 MiB was not wiped.")