11308: Update assertion to accommodate different exception in py3.
[arvados.git] / sdk / python / tests / test_arv_keepdocker.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import absolute_import
5 import arvados
6 import hashlib
7 import mock
8 import os
9 import subprocess
10 import sys
11 import tempfile
12 import unittest
13 import logging
14
15 import arvados.commands.keepdocker as arv_keepdocker
16 from . import arvados_testutil as tutil
17 from . import run_test_server
18
19
20 class StopTest(Exception):
21     pass
22
23
24 class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
25     def run_arv_keepdocker(self, args, err):
26         sys.argv = ['arv-keepdocker'] + args
27         log_handler = logging.StreamHandler(err)
28         arv_keepdocker.logger.addHandler(log_handler)
29         try:
30             return arv_keepdocker.main()
31         finally:
32             arv_keepdocker.logger.removeHandler(log_handler)
33
34     def test_unsupported_arg(self):
35         with self.assertRaises(SystemExit):
36             self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
37
38     def test_version_argument(self):
39         with tutil.redirected_streams(
40                 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
41             with self.assertRaises(SystemExit):
42                 self.run_arv_keepdocker(['--version'], sys.stderr)
43         self.assertVersionOutput(out, err)
44
45     @mock.patch('arvados.commands.keepdocker.find_image_hashes',
46                 return_value=['abc123'])
47     @mock.patch('arvados.commands.keepdocker.find_one_image_hash',
48                 return_value='abc123')
49     def test_image_format_compatibility(self, _1, _2):
50         old_id = hashlib.sha256(b'old').hexdigest()
51         new_id = 'sha256:'+hashlib.sha256(b'new').hexdigest()
52         for supported, img_id, expect_ok in [
53                 (['v1'], old_id, True),
54                 (['v1'], new_id, False),
55                 (None, old_id, False),
56                 ([], old_id, False),
57                 ([], new_id, False),
58                 (['v1', 'v2'], new_id, True),
59                 (['v1'], new_id, False),
60                 (['v2'], new_id, True)]:
61
62             fakeDD = arvados.api('v1')._rootDesc
63             if supported is None:
64                 del fakeDD['dockerImageFormats']
65             else:
66                 fakeDD['dockerImageFormats'] = supported
67
68             err = tutil.StringIO()
69             out = tutil.StringIO()
70
71             with tutil.redirected_streams(stdout=out), \
72                  mock.patch('arvados.api') as api, \
73                  mock.patch('arvados.commands.keepdocker.popen_docker',
74                             return_value=subprocess.Popen(
75                                 ['echo', img_id],
76                                 stdout=subprocess.PIPE)), \
77                  mock.patch('arvados.commands.keepdocker.prep_image_file',
78                             side_effect=StopTest), \
79                  self.assertRaises(StopTest if expect_ok else SystemExit):
80
81                 api()._rootDesc = fakeDD
82                 self.run_arv_keepdocker(['--force', 'testimage'], err)
83
84             self.assertEqual(out.getvalue(), '')
85             if expect_ok:
86                 self.assertNotRegex(
87                     err.getvalue(), "refusing to store",
88                     msg=repr((supported, img_id)))
89             else:
90                 self.assertRegex(
91                     err.getvalue(), "refusing to store",
92                     msg=repr((supported, img_id)))
93             if not supported:
94                 self.assertRegex(
95                     err.getvalue(),
96                     "server does not specify supported image formats",
97                     msg=repr((supported, img_id)))
98
99         fakeDD = arvados.api('v1')._rootDesc
100         fakeDD['dockerImageFormats'] = ['v1']
101         err = tutil.StringIO()
102         out = tutil.StringIO()
103         with tutil.redirected_streams(stdout=out), \
104              mock.patch('arvados.api') as api, \
105              mock.patch('arvados.commands.keepdocker.popen_docker',
106                         return_value=subprocess.Popen(
107                             ['echo', new_id],
108                             stdout=subprocess.PIPE)), \
109              mock.patch('arvados.commands.keepdocker.prep_image_file',
110                         side_effect=StopTest), \
111              self.assertRaises(StopTest):
112             api()._rootDesc = fakeDD
113             self.run_arv_keepdocker(
114                 ['--force', '--force-image-format', 'testimage'], err)
115         self.assertRegex(err.getvalue(), "forcing incompatible image")