| |
@@ -25,8 +25,10 @@
|
| |
import unittest
|
| |
import copy
|
| |
|
| |
- import mock
|
| |
- from mock import patch, Mock, MagicMock
|
| |
+ try:
|
| |
+ from unittest import mock
|
| |
+ except ImportError:
|
| |
+ import mock
|
| |
|
| |
from odcs.client.odcs import AuthMech
|
| |
from odcs.client.odcs import (
|
| |
@@ -86,7 +88,7 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.resource_path = "composes/"
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_raise_error(self, requests):
|
| |
requests.get.return_value.status_code = 401
|
| |
|
| |
@@ -96,8 +98,8 @@
|
| |
self.assertEqual(requests.get.return_value, r)
|
| |
requests.get.return_value.raise_for_status.assert_called_once()
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
- @patch("odcs.client.odcs.HTTPKerberosAuth")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.HTTPKerberosAuth")
|
| |
def test_with_kerberos_auth(self, HTTPKerberosAuth, requests):
|
| |
requests.post.return_value.status_code = 200
|
| |
expected_auth = HTTPKerberosAuth.return_value
|
| |
@@ -112,7 +114,7 @@
|
| |
allow_redirects=False,
|
| |
)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_with_openidc_auth(self, requests):
|
| |
fake_openidc_token = "1234567890"
|
| |
requests.post.return_value.status_code = 200
|
| |
@@ -135,7 +137,7 @@
|
| |
allow_redirects=False,
|
| |
)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_with_ssl_auth(self, requests):
|
| |
requests.post.return_value.status_code = 200
|
| |
|
| |
@@ -156,7 +158,7 @@
|
| |
allow_redirects=False,
|
| |
)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_do_not_verify_ssl(self, requests):
|
| |
requests.post.return_value.status_code = 200
|
| |
|
| |
@@ -176,7 +178,7 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.odcs = ODCS(self.server_url)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_get_compose(self, requests):
|
| |
fake_compose = {
|
| |
"flags": [],
|
| |
@@ -192,7 +194,7 @@
|
| |
"time_submitted": "2017-07-11T13:05:40Z",
|
| |
"time_to_expire": "2017-07-12T13:05:40Z",
|
| |
}
|
| |
- requests.get = Mock()
|
| |
+ requests.get = mock.Mock()
|
| |
requests.get.return_value.status_code = 200
|
| |
requests.get.return_value.json.return_value = fake_compose
|
| |
|
| |
@@ -212,9 +214,9 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.odcs = ODCS(self.server_url)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_about(self, requests):
|
| |
- requests.get = Mock()
|
| |
+ requests.get = mock.Mock()
|
| |
requests.get.return_value.status_code = 200
|
| |
fake_result = {
|
| |
"allowed_clients": {"groups": {}, "users": {}},
|
| |
@@ -241,7 +243,7 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.odcs = ODCS(self.server_url)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_compose_id_not_found(self, requests):
|
| |
fake_response = {
|
| |
"status": 404,
|
| |
@@ -255,7 +257,7 @@
|
| |
|
| |
requests.delete.return_value.raise_for_status.assert_called_once()
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_delete_compose(self, requests):
|
| |
fake_response = {
|
| |
"status": 202,
|
| |
@@ -272,7 +274,7 @@
|
| |
)
|
| |
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
class TestNewCompose(unittest.TestCase):
|
| |
"""Test ODCS.new_compose"""
|
| |
|
| |
@@ -399,7 +401,7 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.odcs = ODCS(self.server_url)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_renew_a_compose(self, requests):
|
| |
fake_renew_compose = {
|
| |
"flags": [],
|
| |
@@ -436,7 +438,7 @@
|
| |
self.server_url = "http://localhost/"
|
| |
self.odcs = ODCS(self.server_url)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_find_composes_without_pagination(self, requests):
|
| |
fake_found_composes = {
|
| |
"items": [
|
| |
@@ -483,7 +485,7 @@
|
| |
allow_redirects=True,
|
| |
)
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
def test_find_composes_the_second_page(self, requests):
|
| |
fake_found_composes = {
|
| |
"items": [
|
| |
@@ -531,8 +533,8 @@
|
| |
)
|
| |
|
| |
|
| |
- @patch("time.sleep")
|
| |
- @patch("odcs.client.odcs.ODCS.get_compose")
|
| |
+ @mock.patch("time.sleep")
|
| |
+ @mock.patch("odcs.client.odcs.ODCS.get_compose")
|
| |
class TestWaitForCompose(unittest.TestCase):
|
| |
"""Test ODCS.wait_for_compose"""
|
| |
|
| |
@@ -556,13 +558,13 @@
|
| |
self.assertEqual(sleep.mock_calls, [mock.call(1), mock.call(2)])
|
| |
self.assertEqual(get_compose.mock_calls, [mock.call(1)] * 3)
|
| |
|
| |
- @patch("time.time")
|
| |
+ @mock.patch("time.time")
|
| |
def test_wait_for_compose_timeout(self, time_travel, get_compose, sleep):
|
| |
get_compose.side_effect = [{"state_name": "wait"}] * 2
|
| |
time_travel.side_effect = [1, 301]
|
| |
self.assertRaises(RuntimeError, self.odcs.wait_for_compose, 1)
|
| |
|
| |
- @patch("time.time")
|
| |
+ @mock.patch("time.time")
|
| |
def test_wait_for_compose_elapsed_close_to_timeout(self, _time, get_compose, sleep):
|
| |
TestWaitForCompose._TIME_TMP_VAR = 0
|
| |
|
| |
@@ -586,7 +588,7 @@
|
| |
sleep.mock_calls, [mock.call(1), mock.call(2), mock.call(3), mock.call(4)]
|
| |
)
|
| |
|
| |
- @patch("odcs.client.odcs.ComposeLog.read")
|
| |
+ @mock.patch("odcs.client.odcs.ComposeLog.read")
|
| |
def test_wait_for_compose_watch_logs(self, log_read, get_compose, sleep):
|
| |
get_compose.side_effect = [
|
| |
{"state_name": "wait", "toplevel_url": "http://localhost/composes/odcs-1"},
|
| |
@@ -604,7 +606,7 @@
|
| |
self.assertEqual(len(log_read.mock_calls), 2)
|
| |
|
| |
|
| |
- @patch("odcs.client.odcs.requests")
|
| |
+ @mock.patch("odcs.client.odcs.requests")
|
| |
class TestComposeLog(unittest.TestCase):
|
| |
"""Test ODCS.wait_for_compose"""
|
| |
|
| |
@@ -623,10 +625,10 @@
|
| |
|
| |
def test_compose_log_multiple_calls(self, requests):
|
| |
responses = [
|
| |
- MagicMock(status_code=200, text="line\n"),
|
| |
- MagicMock(status_code=200, text="another line\n"),
|
| |
- MagicMock(status_code=416, text=""),
|
| |
- MagicMock(status_code=200, text="another line\n"),
|
| |
+ mock.MagicMock(status_code=200, text="line\n"),
|
| |
+ mock.MagicMock(status_code=200, text="another line\n"),
|
| |
+ mock.MagicMock(status_code=416, text=""),
|
| |
+ mock.MagicMock(status_code=200, text="another line\n"),
|
| |
]
|
| |
requests.get.side_effect = responses
|
| |
length = 0
|
| |
This can remove the python3-mock dependency when building rpm with
python3 and we can remove try...except after fully drop python2 support.
JIRA: RHELCMP-8813
Signed-off-by: Haibo Lin hlin@redhat.com