Merge branch '21762-flaky-search-test'
[arvados.git] / sdk / python / tests / test_arv_keepdocker.py
index 50a92b3e6025df1eb469654cd4a177be6e1d1d88..33c050ef86245f6f93f345ea33c2ffe9f8dea621 100644 (file)
@@ -4,16 +4,22 @@
 
 import arvados
 import collections
 
 import arvados
 import collections
+import collections.abc
 import copy
 import hashlib
 import logging
 import copy
 import hashlib
 import logging
-import mock
 import os
 import subprocess
 import sys
 import tempfile
 import unittest
 
 import os
 import subprocess
 import sys
 import tempfile
 import unittest
 
+from pathlib import Path
+from unittest import mock
+
+import parameterized
+import pytest
+
 import arvados.commands.keepdocker as arv_keepdocker
 from . import arvados_testutil as tutil
 
 import arvados.commands.keepdocker as arv_keepdocker
 from . import arvados_testutil as tutil
 
@@ -36,7 +42,7 @@ class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
         with tutil.redirected_streams(stdout=out, stderr=out), \
              self.assertRaises(SystemExit):
             self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
         with tutil.redirected_streams(stdout=out, stderr=out), \
              self.assertRaises(SystemExit):
             self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
-        self.assertRegex(out.getvalue(), 'unrecognized arguments')
+        self.assertRegex(out.getvalue(), r'unrecognized arguments')
 
     def test_version_argument(self):
         with tutil.redirected_streams(
 
     def test_version_argument(self):
         with tutil.redirected_streams(
@@ -89,16 +95,16 @@ class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
             self.assertEqual(out.getvalue(), '')
             if expect_ok:
                 self.assertNotRegex(
             self.assertEqual(out.getvalue(), '')
             if expect_ok:
                 self.assertNotRegex(
-                    err.getvalue(), "refusing to store",
+                    err.getvalue(), r"refusing to store",
                     msg=repr((supported, img_id)))
             else:
                 self.assertRegex(
                     msg=repr((supported, img_id)))
             else:
                 self.assertRegex(
-                    err.getvalue(), "refusing to store",
+                    err.getvalue(), r"refusing to store",
                     msg=repr((supported, img_id)))
             if not supported:
                 self.assertRegex(
                     err.getvalue(),
                     msg=repr((supported, img_id)))
             if not supported:
                 self.assertRegex(
                     err.getvalue(),
-                    "server does not specify supported image formats",
+                    r"server does not specify supported image formats",
                     msg=repr((supported, img_id)))
 
         fakeDD = arvados.api('v1')._rootDesc
                     msg=repr((supported, img_id)))
 
         fakeDD = arvados.api('v1')._rootDesc
@@ -117,13 +123,13 @@ class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
             api()._rootDesc = fakeDD
             self.run_arv_keepdocker(
                 ['--force', '--force-image-format', 'testimage'], err)
             api()._rootDesc = fakeDD
             self.run_arv_keepdocker(
                 ['--force', '--force-image-format', 'testimage'], err)
-        self.assertRegex(err.getvalue(), "forcing incompatible image")
+        self.assertRegex(err.getvalue(), r"forcing incompatible image")
 
     def test_tag_given_twice(self):
         with tutil.redirected_streams(stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
             with self.assertRaises(SystemExit):
                 self.run_arv_keepdocker(['myrepo:mytag', 'extratag'], sys.stderr)
 
     def test_tag_given_twice(self):
         with tutil.redirected_streams(stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
             with self.assertRaises(SystemExit):
                 self.run_arv_keepdocker(['myrepo:mytag', 'extratag'], sys.stderr)
-            self.assertRegex(err.getvalue(), "cannot add tag argument 'extratag'")
+            self.assertRegex(err.getvalue(), r"cannot add tag argument 'extratag'")
 
     def test_image_given_as_repo_colon_tag(self):
         with self.assertRaises(StopTest), \
 
     def test_image_given_as_repo_colon_tag(self):
         with self.assertRaises(StopTest), \
@@ -175,7 +181,7 @@ class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
             out = tutil.StringIO()
             with self.assertRaises(SystemExit):
                 self.run_arv_keepdocker([], sys.stderr, stdout=out)
             out = tutil.StringIO()
             with self.assertRaises(SystemExit):
                 self.run_arv_keepdocker([], sys.stderr, stdout=out)
-            self.assertRegex(out.getvalue(), '\nregistry.example:1234/repo +latest ')
+            self.assertRegex(out.getvalue(), r'\nregistry.example:1234/repo +latest ')
         finally:
             api.links().delete(uuid=taglink['uuid']).execute()
 
         finally:
             api.links().delete(uuid=taglink['uuid']).execute()
 
@@ -223,3 +229,39 @@ class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
         api().collections().update.assert_called_with(
             uuid=mocked_collection['uuid'],
             body={'properties': updated_properties})
         api().collections().update.assert_called_with(
             uuid=mocked_collection['uuid'],
             body={'properties': updated_properties})
+
+
+@parameterized.parameterized_class(('filename',), [
+    ('hello-world-ManifestV2.tar',),
+    ('hello-world-ManifestV2-OCILayout.tar',),
+])
+class ImageMetadataTestCase(unittest.TestCase):
+    DATA_PATH = Path(__file__).parent / 'data'
+
+    @classmethod
+    def setUpClass(cls):
+        cls.image_file = (cls.DATA_PATH / cls.filename).open('rb')
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.image_file.close()
+
+    def setUp(self):
+        self.manifest, self.config = arv_keepdocker.load_image_metadata(self.image_file)
+
+    def test_image_manifest(self):
+        self.assertIsInstance(self.manifest, collections.abc.Mapping)
+        self.assertEqual(self.manifest.get('RepoTags'), ['hello-world:latest'])
+
+    def test_image_config(self):
+        self.assertIsInstance(self.config, collections.abc.Mapping)
+        self.assertEqual(self.config.get('created'), '2023-05-02T16:49:27Z')
+
+
+def test_get_cache_dir(tmp_path):
+    actual = arv_keepdocker.get_cache_dir(lambda: tmp_path)
+    assert isinstance(actual, str)
+    actual = Path(actual)
+    assert actual.is_dir()
+    assert actual.name == 'docker'
+    assert actual.parent == tmp_path