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