1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#!/usr/bin/python
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unittest for compat.py module."""
import sys
import google_compute_engine.compat
from google_compute_engine.test_compat import mock
from google_compute_engine.test_compat import reload_import
from google_compute_engine.test_compat import unittest
from google_compute_engine.test_compat import urlretrieve
class CompatTest(unittest.TestCase):
@mock.patch('google_compute_engine.compat.subprocess.check_call')
def testCurlRetrieve(self, mock_call):
url = 'http://www.example.com/script.sh'
filename = None
expected = ['curl', '--max-time', mock.ANY, '--retry', mock.ANY, '--', url]
if sys.version_info < (2, 7, 9):
urlretrieve.urlretrieve(url, filename)
mock_call.assert_called_with(expected)
else:
pass
@mock.patch('google_compute_engine.compat.subprocess.check_call')
def testCurlRetrieveFilename(self, mock_call):
url = 'http://www.example.com/script.sh'
filename = '/tmp/filename.txt'
expected = [
'curl', '--max-time', mock.ANY, '--retry', mock.ANY, '-o', filename,
'--', url,
]
if sys.version_info < (2, 7, 9):
urlretrieve.urlretrieve(url, filename)
mock_call.assert_called_with(expected)
else:
pass
@mock.patch('google_compute_engine.compat.subprocess.check_call')
@mock.patch('google_compute_engine.compat.urlretrieve.urlretrieve')
def testUrlRetrieve(self, mock_retrieve, mock_call):
url = 'http://www.example.com/script.sh'
filename = '/tmp/filename.txt'
args = ['arg1', 'arg2', 'arg3']
kwargs = {'kwarg1': 1, 'kwarg2': 2}
if sys.version_info >= (2, 7, 9):
urlretrieve.urlretrieve(url, filename, *args, **kwargs)
mock_retrieve.assert_called_once_with(url, filename, *args, **kwargs)
mock_call.assert_not_called()
else:
pass
@mock.patch('google_compute_engine.compat.distro.linux_distribution')
def testDistroCompatLinux(self, mock_call):
test_cases = {
('Fedora', '28', ''):
google_compute_engine.distro_lib.el_7.utils,
('debian', '8.10', ''):
google_compute_engine.distro_lib.debian_8.utils,
('debian', '9.3', ''):
google_compute_engine.distro_lib.debian_9.utils,
('debian', '10.3', ''):
google_compute_engine.distro_lib.debian_9.utils,
('SUSE Linux Enterprise Server', '11', 'x86_64'):
google_compute_engine.distro_lib.sles_11.utils,
('SUSE Linux Enterprise Server', '12', 'x86_64'):
google_compute_engine.distro_lib.sles_12.utils,
('SUSE Linux Enterprise Server', '13', 'x86_64'):
google_compute_engine.distro_lib.sles_12.utils,
('CentOS Linux', '6.4.3', 'Core'):
google_compute_engine.distro_lib.el_6.utils,
('CentOS Linux', '7.4.1708', 'Core'):
google_compute_engine.distro_lib.el_7.utils,
('CentOS Linux', '8.4.3', 'Core'):
google_compute_engine.distro_lib.el_7.utils,
('Red Hat Enterprise Linux Server', '6.3.2', ''):
google_compute_engine.distro_lib.el_6.utils,
('Red Hat Enterprise Linux Server', '7.4', ''):
google_compute_engine.distro_lib.el_7.utils,
('Red Hat Enterprise Linux Server', '8.5.1', ''):
google_compute_engine.distro_lib.el_7.utils,
('', '', ''):
google_compute_engine.distro_lib.debian_9.utils,
('xxxx', 'xxxx', 'xxxx'):
google_compute_engine.distro_lib.debian_9.utils,
}
for distro in test_cases:
mock_call.return_value = distro
reload_import(google_compute_engine.compat)
self.assertEqual(
test_cases[distro], google_compute_engine.compat.distro_utils)
@mock.patch('google_compute_engine.compat.sys.platform', 'freebsd')
@mock.patch('google_compute_engine.compat.distro.version')
def testDistroCompatFreeBSD(self, mock_call):
mock_call.return_value = 'FreeBSD 11.1-RELEASE-p4 #0: Tue Nov 14 06:12:40'
reload_import(google_compute_engine.compat)
self.assertEqual(
google_compute_engine.distro_lib.freebsd_11.utils,
google_compute_engine.compat.distro_utils)
if __name__ == '__main__':
unittest.main()
|