← Back to team overview

curtin-dev team mailing list archive

[Merge] ~mwhudson/curtin:integration-test-preserve into curtin:master

 

Michael Hudson-Doyle has proposed merging ~mwhudson/curtin:integration-test-preserve into curtin:master.

Commit message:
add preserve: true support to the image action



Requested reviews:
  Server Team CI bot (server-team-bot): continuous-integration
  curtin developers (curtin-dev)

For more details, see:
https://code.launchpad.net/~mwhudson/curtin/+git/curtin/+merge/412614
-- 
Your team curtin developers is requested to review the proposed merge of ~mwhudson/curtin:integration-test-preserve into curtin:master.
diff --git a/curtin/commands/block_meta.py b/curtin/commands/block_meta.py
index 1913cb4..ef38abf 100644
--- a/curtin/commands/block_meta.py
+++ b/curtin/commands/block_meta.py
@@ -552,16 +552,29 @@ DEVS = set()
 
 def image_handler(info, storage_config, handlers):
     path = info['path']
-    if os.path.exists(path):
-        os.unlink(path)
+    size = int(util.human2bytes(info['size']))
+    if info.get('preserve', False):
+        actual_size = os.stat(path).st_size
+        if size != actual_size:
+            raise RuntimeError(
+                f'image at {path} was size {actual_size} not {size} as '
+                'expected.')
+    else:
+        if os.path.exists(path):
+            os.unlink(path)
+        try:
+            with open(path, 'wb') as fp:
+                fp.truncate(size)
+        except BaseException:
+            if os.path.exists(path):
+                os.unlink(path)
+            raise
     try:
-        with open(path, 'wb') as fp:
-            fp.truncate(int(util.human2bytes(info['size'])))
         dev = util.subp([
             'losetup', '--show', '--find', path],
             capture=True)[0].strip()
     except BaseException:
-        if os.path.exists(path):
+        if os.path.exists(path) and not info.get('preserve'):
             os.unlink(path)
         raise
     info['dev'] = dev
diff --git a/tests/integration/test_block_meta.py b/tests/integration/test_block_meta.py
index bd602b2..d543b68 100644
--- a/tests/integration/test_block_meta.py
+++ b/tests/integration/test_block_meta.py
@@ -76,6 +76,10 @@ class StorageConfigBuilder:
         action.update(**kw)
         self.config.append(action)
 
+    def set_preserve(self):
+        for action in self.config:
+            action['preserve'] = True
+
 
 class TestBlockMeta(IntegrationTestCase):
 
@@ -119,6 +123,8 @@ class TestBlockMeta(IntegrationTestCase):
                     PartData(
                         number=2, offset=(1 << 20) + psize, size=psize),
                 ])
+        config.set_preserve()
+        self.run_bm(config.render())
 
     def test_default_offsets_gpt(self):
         self._test_default_offsets('gpt')