From 8f9e57c2ecdcc5b66286084660a254fdd0d768f4 Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Tue, 14 Jul 2020 20:34:40 +0800 Subject: [PATCH 1/6] add float --- src/azure_devtools/scenario_tests/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/azure_devtools/scenario_tests/base.py b/src/azure_devtools/scenario_tests/base.py index 6b4fd00..657e7c6 100644 --- a/src/azure_devtools/scenario_tests/base.py +++ b/src/azure_devtools/scenario_tests/base.py @@ -35,13 +35,16 @@ def create_temp_file(self, size_kb, full_random=False): os.close(fd) self.addCleanup(lambda: os.remove(path)) + import math with open(path, mode='r+b') as f: if full_random: chunk = os.urandom(1024) else: chunk = bytearray([0] * 1024) - for _ in range(size_kb): + for _ in range(math.floor(size_kb)): f.write(chunk) + chunk = os.urandom(int(1024 * (size_kb % 1))) + f.write(chunk) return path From b60034996e1d7f9ead86a29776467be65dd2a2bd Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Tue, 21 Jul 2020 15:50:29 +0800 Subject: [PATCH 2/6] add description --- src/azure_devtools/scenario_tests/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/azure_devtools/scenario_tests/base.py b/src/azure_devtools/scenario_tests/base.py index 657e7c6..cd98957 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)) From 1d279886d921f9ca4ab2e00097023fb4ac9925eb Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Tue, 21 Jul 2020 16:27:43 +0800 Subject: [PATCH 3/6] add test --- .../scenario_tests/tests/test_integration_test_base.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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..218cb83 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,6 +42,15 @@ 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), 2) # 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(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)) From c82ab731aab690145440c361299dc9f715aa252d Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Thu, 23 Jul 2020 10:38:49 +0800 Subject: [PATCH 4/6] fix test --- .../scenario_tests/tests/test_integration_test_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 218cb83..86f619c 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 @@ -46,7 +46,7 @@ def sample_test(self): 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), 2) # pylint: disable=protected-access + 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(8.5 * 1024) if b != '\x00')) @@ -54,7 +54,7 @@ def sample_test(self): 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): From 33caf90538dbfa777b870c934515e71d83d8f609 Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Thu, 23 Jul 2020 10:51:27 +0800 Subject: [PATCH 5/6] int input --- .../scenario_tests/tests/test_integration_test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 86f619c..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 @@ -49,7 +49,7 @@ def sample_test(self): 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(8.5 * 1024) if b != '\x00')) + 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)) From b753ebc0a7e774c0ef8f4a31b38cd77934992a02 Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Thu, 23 Jul 2020 11:04:51 +0800 Subject: [PATCH 6/6] fix 2.7 --- src/azure_devtools/scenario_tests/base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/azure_devtools/scenario_tests/base.py b/src/azure_devtools/scenario_tests/base.py index cd98957..60fe576 100644 --- a/src/azure_devtools/scenario_tests/base.py +++ b/src/azure_devtools/scenario_tests/base.py @@ -38,13 +38,12 @@ def create_temp_file(self, size_kb, full_random=False): os.close(fd) self.addCleanup(lambda: os.remove(path)) - import math with open(path, mode='r+b') as f: if full_random: chunk = os.urandom(1024) else: chunk = bytearray([0] * 1024) - for _ in range(math.floor(size_kb)): + for _ in range(int(size_kb)): f.write(chunk) chunk = os.urandom(int(1024 * (size_kb % 1))) f.write(chunk)