diff --git a/src/azure_devtools/scenario_tests/base.py b/src/azure_devtools/scenario_tests/base.py index 6b4fd00..60fe576 100644 --- a/src/azure_devtools/scenario_tests/base.py +++ b/src/azure_devtools/scenario_tests/base.py @@ -30,7 +30,10 @@ def create_random_name(self, prefix, length): # pylint: disable=no-self-use return create_random_name(prefix=prefix, length=length) def create_temp_file(self, size_kb, full_random=False): - """ Create a temporary file for testing. The test harness will delete the file during tearing down. """ + """ + Create a temporary file for testing. The test harness will delete the file during tearing down. + :param float size_kb: specify the generated file size in kb. + """ fd, path = tempfile.mkstemp() os.close(fd) self.addCleanup(lambda: os.remove(path)) @@ -40,8 +43,10 @@ def create_temp_file(self, size_kb, full_random=False): chunk = os.urandom(1024) else: chunk = bytearray([0] * 1024) - for _ in range(size_kb): + for _ in range(int(size_kb)): f.write(chunk) + chunk = os.urandom(int(1024 * (size_kb % 1))) + f.write(chunk) return path diff --git a/src/azure_devtools/scenario_tests/tests/test_integration_test_base.py b/src/azure_devtools/scenario_tests/tests/test_integration_test_base.py index 4f1c47a..ee0c161 100644 --- a/src/azure_devtools/scenario_tests/tests/test_integration_test_base.py +++ b/src/azure_devtools/scenario_tests/tests/test_integration_test_base.py @@ -42,10 +42,19 @@ def sample_test(self): # the file is blank self.assertTrue(any(b for b in fq.read(8 * 1024) if b != '\x00')) + random_file_3 = tb.create_temp_file(size_kb=8.5) + self.addCleanup(lambda: os.remove(random_file_3)) + self.assertTrue(os.path.isfile(random_file_3)) + self.assertEqual(os.path.getsize(random_file_3), 8.5 * 1024) + self.assertEqual(len(tb._cleanups), 3) # pylint: disable=protected-access + with open(random_file_3, 'rb') as fq: + # the file is blank + self.assertTrue(any(b for b in fq.read(int(8.5 * 1024)) if b != '\x00')) + random_dir = tb.create_temp_dir() self.addCleanup(lambda: os.rmdir(random_dir)) self.assertTrue(os.path.isdir(random_dir)) - self.assertEqual(len(tb._cleanups), 3) # pylint: disable=protected-access + self.assertEqual(len(tb._cleanups), 4) # pylint: disable=protected-access def test_live_test_default_constructor(self): class MockTest(LiveTest):