summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2015-01-16 17:38:59 +1000
committerJamie Lennox <jamielennox@redhat.com>2015-01-16 17:38:59 +1000
commit0669639b51383953a9164d95b76339a5dc928481 (patch)
tree7db668299f4987d73a46ca6437d5a037fec4aea8
parent1e99c7ed93f068efd460259ad9f03b88736967c8 (diff)
downloadpython-saharaclient-0669639b51383953a9164d95b76339a5dc928481.tar.gz
Use requests-mock for mocking
Use the requests-mock library for stubbing out our requests rather than matching via mock directly. Change-Id: Ifed4457619448c330044492b513c116c3f7e266a
-rw-r--r--saharaclient/tests/unit/base.py27
-rw-r--r--saharaclient/tests/unit/test_cluster_templates.py45
-rw-r--r--saharaclient/tests/unit/test_clusters.py69
-rw-r--r--saharaclient/tests/unit/test_data_sources.py45
-rw-r--r--saharaclient/tests/unit/test_httpclient.py53
-rw-r--r--saharaclient/tests/unit/test_images.py57
-rw-r--r--saharaclient/tests/unit/test_job_binaries.py51
-rw-r--r--saharaclient/tests/unit/test_job_binary_internals.py44
-rw-r--r--saharaclient/tests/unit/test_job_executions.py61
-rw-r--r--saharaclient/tests/unit/test_jobs.py51
-rw-r--r--saharaclient/tests/unit/test_node_group_templates.py46
-rw-r--r--saharaclient/tests/unit/test_plugins.py42
-rw-r--r--test-requirements.txt1
13 files changed, 267 insertions, 325 deletions
diff --git a/saharaclient/tests/unit/base.py b/saharaclient/tests/unit/base.py
index d161639..ec965bd 100644
--- a/saharaclient/tests/unit/base.py
+++ b/saharaclient/tests/unit/base.py
@@ -17,25 +17,20 @@ import testtools
from saharaclient.api import client
+from requests_mock.contrib import fixture
+
class BaseTestCase(testtools.TestCase):
- client = client.Client(sahara_url='http://localhost:8386',
- input_auth_token='token')
+
+ URL = 'http://localhost:8386'
+ TOKEN = 'token'
+
+ def setUp(self):
+ super(BaseTestCase, self).setUp()
+ self.responses = self.useFixture(fixture.Fixture())
+ self.client = client.Client(sahara_url=self.URL,
+ input_auth_token=self.TOKEN)
def assertFields(self, body, obj):
for key, value in six.iteritems(body):
self.assertEqual(value, getattr(obj, key))
-
-
-class FakeResponse(object):
- def __init__(self, status_code, content=None, response_key=None):
- self.status_code = status_code
- self.content = content or {}
- self.response_key = response_key
- self.name = 'name'
-
- def json(self):
- if self.response_key:
- return {self.response_key: self.content}
- else:
- return self.content
diff --git a/saharaclient/tests/unit/test_cluster_templates.py b/saharaclient/tests/unit/test_cluster_templates.py
index face307..bc35174 100644
--- a/saharaclient/tests/unit/test_cluster_templates.py
+++ b/saharaclient/tests/unit/test_cluster_templates.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import cluster_templates as ct
from saharaclient.tests.unit import base
@@ -34,48 +32,43 @@ class ClusterTemplateTest(base.BaseTestCase):
}
}
- @mock.patch('requests.post')
- def test_create_cluster_template(self, mpost):
- mpost.return_value = base.FakeResponse(
- 202, self.body, 'cluster_template')
+ def test_create_cluster_template(self):
+ url = self.URL + '/cluster-templates'
+ self.responses.post(url, status_code=202,
+ json={'cluster_template': self.body})
resp = self.client.cluster_templates.create(**self.body)
- self.assertEqual('http://localhost:8386/cluster-templates',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, ct.ClusterTemplate)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_cluster_template_list(self, mget):
- mget.return_value = base.FakeResponse(
- 200, [self.body], 'cluster_templates')
+ def test_cluster_template_list(self):
+ url = self.URL + '/cluster-templates'
+ self.responses.get(url, json={'cluster_templates': [self.body]})
resp = self.client.cluster_templates.list()
- self.assertEqual('http://localhost:8386/cluster-templates',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], ct.ClusterTemplate)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_cluster_template_get(self, mget):
- mget.return_value = base.FakeResponse(
- 200, self.body, 'cluster_template')
+ def test_cluster_template_get(self):
+ url = self.URL + '/cluster-templates/id'
+ self.responses.get(url, json={'cluster_template': self.body})
resp = self.client.cluster_templates.get('id')
- self.assertEqual('http://localhost:8386/cluster-templates/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, ct.ClusterTemplate)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_cluster_template_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_cluster_template_delete(self):
+ url = self.URL + '/cluster-templates/id'
+ self.responses.delete(url, status_code=204)
self.client.cluster_templates.delete('id')
- self.assertEqual('http://localhost:8386/cluster-templates/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_clusters.py b/saharaclient/tests/unit/test_clusters.py
index 66fd0bd..69683ff 100644
--- a/saharaclient/tests/unit/test_clusters.py
+++ b/saharaclient/tests/unit/test_clusters.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import clusters as cl
from saharaclient.tests.unit import base
@@ -28,63 +26,57 @@ class ClusterTest(base.BaseTestCase):
'cluster_template_id': 'id',
}
- @mock.patch('requests.post')
- def test_create_cluster_with_template(self, mpost):
- mpost.return_value = base.FakeResponse(
- 202, self.body, 'cluster')
+ def test_create_cluster_with_template(self,):
+ url = self.URL + '/clusters'
+ self.responses.post(url, status_code=202, json={'cluster': self.body})
resp = self.client.clusters.create(**self.body)
- self.assertEqual('http://localhost:8386/clusters',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, cl.Cluster)
self.assertFields(self.body, resp)
- @mock.patch('requests.post')
- def test_create_cluster_without_template(self, mpost):
+ def test_create_cluster_without_template(self):
body = self.body.copy()
del body['cluster_template_id']
body.update({'default_image_id': 'image_id', 'cluster_configs': {},
'node_groups': ['ng1', 'ng2']})
- mpost.return_value = base.FakeResponse(
- 202, body, 'cluster')
+
+ url = self.URL + '/clusters'
+ self.responses.post(url, status_code=202, json={'cluster': body})
resp = self.client.clusters.create(**body)
- self.assertEqual('http://localhost:8386/clusters',
- mpost.call_args[0][0])
- self.assertEqual(body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(body, json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, cl.Cluster)
self.assertFields(body, resp)
- @mock.patch('requests.get')
- def test_clusters_list(self, mget):
- mget.return_value = base.FakeResponse(
- 200, [self.body], 'clusters')
+ def test_clusters_list(self):
+ url = self.URL + '/clusters'
+ self.responses.get(url, json={'clusters': [self.body]})
resp = self.client.clusters.list()
- self.assertEqual('http://localhost:8386/clusters',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], cl.Cluster)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_clusters_get(self, mget):
- mget.return_value = base.FakeResponse(
- 200, self.body, 'cluster')
+ def test_clusters_get(self):
+ url = self.URL + '/clusters/id'
+ self.responses.get(url, json={'cluster': self.body})
resp = self.client.clusters.get('id')
- self.assertEqual('http://localhost:8386/clusters/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, cl.Cluster)
self.assertFields(self.body, resp)
- @mock.patch('requests.put')
- def test_clusters_scale(self, mput):
- mput.return_value = base.FakeResponse(202, self.body)
+ def test_clusters_scale(self):
+ url = self.URL + '/clusters/id'
+ self.responses.put(url, status_code=202, json=self.body)
scale_body = {
'resize_node_groups': [
@@ -104,17 +96,16 @@ class ClusterTest(base.BaseTestCase):
resp = self.client.clusters.scale('id', scale_body)
- self.assertEqual('http://localhost:8386/clusters/id',
- mput.call_args[0][0])
- self.assertEqual(scale_body, json.loads(mput.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(scale_body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, cl.Cluster)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_clusters_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_clusters_delete(self):
+ url = self.URL + '/clusters/id'
+ self.responses.delete(url, status_code=204)
self.client.clusters.delete('id')
- self.assertEqual('http://localhost:8386/clusters/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_data_sources.py b/saharaclient/tests/unit/test_data_sources.py
index 1621482..d661a5f 100644
--- a/saharaclient/tests/unit/test_data_sources.py
+++ b/saharaclient/tests/unit/test_data_sources.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import data_sources as ds
from saharaclient.tests.unit import base
@@ -41,48 +39,43 @@ class DataSourceTest(base.BaseTestCase):
}
}
- @mock.patch('requests.post')
- def test_create_data_sources(self, mpost):
- mpost.return_value = base.FakeResponse(202, self.response,
- 'data_source')
+ def test_create_data_sources(self):
+ url = self.URL + '/data-sources'
+ self.responses.post(url, status_code=202,
+ json={'data_source': self.response})
resp = self.client.data_sources.create(**self.body)
- self.assertEqual('http://localhost:8386/data-sources',
- mpost.call_args[0][0])
- self.assertEqual(self.response, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.response,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, ds.DataSources)
self.assertFields(self.response, resp)
- @mock.patch('requests.get')
- def test_data_sources_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.response],
- 'data_sources')
+ def test_data_sources_list(self):
+ url = self.URL + '/data-sources'
+ self.responses.get(url, json={'data_sources': [self.response]})
resp = self.client.data_sources.list()
- self.assertEqual('http://localhost:8386/data-sources',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], ds.DataSources)
self.assertFields(self.response, resp[0])
- @mock.patch('requests.get')
- def test_data_sources_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.response,
- 'data_source')
+ def test_data_sources_get(self):
+ url = self.URL + '/data-sources/id'
+ self.responses.get(url, json={'data_source': self.response})
resp = self.client.data_sources.get('id')
- self.assertEqual('http://localhost:8386/data-sources/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, ds.DataSources)
self.assertFields(self.response, resp)
- @mock.patch('requests.delete')
- def test_data_sources_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_data_sources_delete(self):
+ url = self.URL + '/data-sources/id'
+ self.responses.delete(url, status_code=204)
self.client.data_sources.delete('id')
- self.assertEqual('http://localhost:8386/data-sources/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_httpclient.py b/saharaclient/tests/unit/test_httpclient.py
index e09dcbc..dd64ceb 100644
--- a/saharaclient/tests/unit/test_httpclient.py
+++ b/saharaclient/tests/unit/test_httpclient.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-import mock
+from requests_mock.contrib import fixture
import testtools
from saharaclient.api import httpclient
@@ -21,36 +21,47 @@ from saharaclient.api import httpclient
class ResourceTest(testtools.TestCase):
- @mock.patch('requests.post')
- def test_post_json_content_type(self, mpost):
- client = httpclient.HTTPClient("http://localhost", "token")
+ URL = 'http://localhost'
+ TOKEN = 'token'
+
+ def setUp(self):
+ super(ResourceTest, self).setUp()
+ self.responses = self.useFixture(fixture.Fixture())
+
+ def test_post_json_content_type(self):
+ m = self.responses.post(self.URL + '/test')
+
+ client = httpclient.HTTPClient(self.URL, self.TOKEN)
client.post('/test', '{"json":"True"}')
- self.assertEqual(1, mpost.call_count)
+ self.assertEqual(1, m.call_count)
self.assertEqual('application/json',
- mpost.call_args[1]['headers']["content-type"])
+ m.last_request.headers['content-type'])
- @mock.patch('requests.put')
- def test_put_json_content_type(self, mput):
- client = httpclient.HTTPClient("http://localhost", "token")
+ def test_put_json_content_type(self):
+ m = self.responses.put(self.URL + '/test')
+
+ client = httpclient.HTTPClient(self.URL, self.TOKEN)
client.put('/test', '{"json":"True"}')
- self.assertEqual(1, mput.call_count)
+ self.assertEqual(1, m.call_count)
self.assertEqual('application/json',
- mput.call_args[1]['headers']["content-type"])
+ m.last_request.headers['content-type'])
+
+ def test_post_nonjson_content_type(self):
+ m = self.responses.post(self.URL + '/test')
- @mock.patch('requests.post')
- def test_post_nonjson_content_type(self, mpost):
- client = httpclient.HTTPClient("http://localhost", "token")
+ client = httpclient.HTTPClient(self.URL, self.TOKEN)
client.post('/test', 'nonjson', json=False)
- self.assertEqual(1, mpost.call_count)
- self.assertNotIn("content-type", mpost.call_args[1]['headers'])
+ self.assertEqual(1, m.call_count)
+ self.assertNotIn("content-type", m.last_request.headers)
+
+ def test_put_nonjson_content_type(self):
+ m = self.responses.put(self.URL + '/test')
- @mock.patch('requests.put')
- def test_put_nonjson_content_type(self, mput):
- client = httpclient.HTTPClient("http://localhost", "token")
+ client = httpclient.HTTPClient(self.URL, self.TOKEN)
client.put('/test', 'nonjson', json=False)
- self.assertEqual(1, mput.call_count)
- self.assertNotIn("content-type", mput.call_args[1]['headers'])
+ self.assertEqual(1, m.call_count)
+ self.assertNotIn("content-type", m.last_request.headers)
diff --git a/saharaclient/tests/unit/test_images.py b/saharaclient/tests/unit/test_images.py
index ae3c143..fbc9ba5 100644
--- a/saharaclient/tests/unit/test_images.py
+++ b/saharaclient/tests/unit/test_images.py
@@ -26,62 +26,59 @@ class ImageTest(base.BaseTestCase):
'description': 'descr'
}
- @mock.patch('requests.get')
- def test_images_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body], 'images')
+ def test_images_list(self):
+ url = self.URL + '/images'
+ self.responses.get(url, json={'images': [self.body]})
resp = self.client.images.list()
- self.assertEqual('http://localhost:8386/images',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], images.Image)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_images_get(self, mget):
- mget.return_value = base.FakeResponse(
- 200, self.body, 'image', )
+ def test_images_get(self):
+ url = self.URL + '/images/id'
+ self.responses.get(url, json={'image': self.body})
resp = self.client.images.get('id')
- self.assertEqual('http://localhost:8386/images/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, images.Image)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_unregister_image(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_unregister_image(self):
+ url = self.URL + '/images/id'
+ self.responses.delete(url, status_code=204)
self.client.images.unregister_image('id')
- self.assertEqual('http://localhost:8386/images/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
- @mock.patch('requests.post')
- def test_update_image(self, mpost):
- mpost.return_value = base.FakeResponse(
- 202, self.body, 'image')
+ def test_update_image(self):
+ url = self.URL + '/images/id'
+ self.responses.post(url, json={'image': self.body}, status_code=202)
self.client.images.update_image('id', 'name', 'descr')
- self.assertEqual('http://localhost:8386/images/id',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
@mock.patch('saharaclient.api.images.ImageManager.get')
- @mock.patch('requests.post')
- def test_update_tags(self, mpost, mget):
- mpost.return_value = base.FakeResponse(202)
+ def test_update_tags(self, mget):
+ tag_url = self.URL + '/images/id/tag'
+ untag_url = self.URL + '/images/id/untag'
+
+ self.responses.post(tag_url, status_code=202)
+ self.responses.post(untag_url, status_code=202)
+
image = mock.Mock()
mget.return_value = image
image.tags = []
self.client.images.update_tags('id', ['username', 'tag'])
- self.assertEqual('http://localhost:8386/images/id/tag',
- mpost.call_args[0][0])
+ self.assertEqual(tag_url, self.responses.last_request.url)
image.tags = ['username', 'tag']
self.client.images.update_tags('id', ['username'])
- self.assertEqual('http://localhost:8386/images/id/untag',
- mpost.call_args[0][0])
+ self.assertEqual(untag_url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_job_binaries.py b/saharaclient/tests/unit/test_job_binaries.py
index 8be3f51..fceb0ca 100644
--- a/saharaclient/tests/unit/test_job_binaries.py
+++ b/saharaclient/tests/unit/test_job_binaries.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import job_binaries as jb
from saharaclient.tests.unit import base
@@ -31,55 +29,52 @@ class JobBinaryTest(base.BaseTestCase):
}
}
- @mock.patch('requests.post')
- def test_create_job_binary(self, mpost):
- mpost.return_value = base.FakeResponse(202, self.body, 'job_binary')
+ def test_create_job_binary(self):
+ url = self.URL + '/job-binaries'
+ self.responses.post(url, status_code=202,
+ json={'job_binary': self.body})
resp = self.client.job_binaries.create(**self.body)
- self.assertEqual('http://localhost:8386/job-binaries',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, jb.JobBinaries)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_job_binary_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body], 'binaries')
+ def test_job_binary_list(self):
+ url = self.URL + '/job-binaries'
+ self.responses.get(url, json={'binaries': [self.body]})
resp = self.client.job_binaries.list()
- self.assertEqual('http://localhost:8386/job-binaries',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], jb.JobBinaries)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_job_binary_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.body, 'job_binary')
+ def test_job_binary_get(self):
+ url = self.URL + '/job-binaries/id'
+ self.responses.get(url, json={'job_binary': self.body})
resp = self.client.job_binaries.get('id')
- self.assertEqual('http://localhost:8386/job-binaries/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, jb.JobBinaries)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_job_binary_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_job_binary_delete(self):
+ url = self.URL + '/job-binaries/id'
+ self.responses.delete(url, status_code=204)
self.client.job_binaries.delete('id')
- self.assertEqual('http://localhost:8386/job-binaries/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
- @mock.patch('requests.get')
- def test_job_binary_get_file(self, mget):
- mget.return_value = base.FakeResponse(200, 'data')
+ def test_job_binary_get_file(self):
+ url = self.URL + '/job-binaries/id/data'
+ self.responses.get(url, text='data')
resp = self.client.job_binaries.get_file('id')
- self.assertEqual('http://localhost:8386/job-binaries/id/data',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertEqual('data', resp)
diff --git a/saharaclient/tests/unit/test_job_binary_internals.py b/saharaclient/tests/unit/test_job_binary_internals.py
index adeab79..e560223 100644
--- a/saharaclient/tests/unit/test_job_binary_internals.py
+++ b/saharaclient/tests/unit/test_job_binary_internals.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import job_binary_internals as jbi
from saharaclient.tests.unit import base
@@ -25,48 +23,42 @@ class JobBinaryInternalTest(base.BaseTestCase):
'id': 'id'
}
- @mock.patch('requests.put')
- def test_create_job_binary_internal(self, mput):
- mput.return_value = base.FakeResponse(202, self.body,
- 'job_binary_internal')
+ def test_create_job_binary_internal(self):
+ url = self.URL + '/job-binary-internals/name'
+ self.responses.put(url, status_code=202,
+ json={'job_binary_internal': self.body})
resp = self.client.job_binary_internals.create('name', 'data')
- self.assertEqual('http://localhost:8386/job-binary-internals/name',
- mput.call_args[0][0])
-
- self.assertEqual('data', mput.call_args[0][1])
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual('data', self.responses.last_request.body)
self.assertIsInstance(resp, jbi.JobBinaryInternal)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_job_binary_internal_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body], 'binaries')
+ def test_job_binary_internal_list(self):
+ url = self.URL + '/job-binary-internals'
+ self.responses.get(url, json={'binaries': [self.body]})
resp = self.client.job_binary_internals.list()
- self.assertEqual('http://localhost:8386/job-binary-internals',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], jbi.JobBinaryInternal)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_job_binary_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.body,
- 'job_binary_internal')
+ def test_job_binary_get(self):
+ url = self.URL + '/job-binary-internals/id'
+ self.responses.get(url, json={'job_binary_internal': self.body})
resp = self.client.job_binary_internals.get('id')
- self.assertEqual('http://localhost:8386/job-binary-internals/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, jbi.JobBinaryInternal)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_job_binary_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_job_binary_delete(self):
+ url = self.URL + '/job-binary-internals/id'
+ self.responses.delete(url, status_code=204)
self.client.job_binary_internals.delete('id')
- self.assertEqual('http://localhost:8386/job-binary-internals/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_job_executions.py b/saharaclient/tests/unit/test_job_executions.py
index 8574ec5..7f545b1 100644
--- a/saharaclient/tests/unit/test_job_executions.py
+++ b/saharaclient/tests/unit/test_job_executions.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import job_executions as je
from saharaclient.tests.unit import base
@@ -33,66 +31,63 @@ class JobExecutionTest(base.BaseTestCase):
'job_configs': {},
}
- @mock.patch('requests.post')
- def test_create_job_execution_with_io(self, mpost):
+ def test_create_job_execution_with_io(self):
+ url = self.URL + '/jobs/job_id/execute'
+
body = self.body.copy()
body.update({'input_id': 'input_id', 'output_id': 'output_id'})
response = self.response.copy()
response.update({'input_id': 'input_id', 'output_id': 'output_id'})
- mpost.return_value = base.FakeResponse(202, response,
- 'job_execution')
+ self.responses.post(url, status_code=202,
+ json={'job_execution': response})
resp = self.client.job_executions.create(**body)
- self.assertEqual('http://localhost:8386/jobs/job_id/execute',
- mpost.call_args[0][0])
- self.assertEqual(response, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(response,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, je.JobExecution)
self.assertFields(response, resp)
- @mock.patch('requests.post')
- def test_create_job_execution_without_io(self, mpost):
- mpost.return_value = base.FakeResponse(202, self.response,
- 'job_execution')
+ def test_create_job_execution_without_io(self):
+ url = self.URL + '/jobs/job_id/execute'
+
+ self.responses.post(url, status_code=202,
+ json={'job_execution': self.response})
resp = self.client.job_executions.create(**self.body)
- self.assertEqual('http://localhost:8386/jobs/job_id/execute',
- mpost.call_args[0][0])
- self.assertEqual(self.response, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.response,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, je.JobExecution)
self.assertFields(self.response, resp)
- @mock.patch('requests.get')
- def test_job_executions_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.response],
- 'job_executions')
+ def test_job_executions_list(self):
+ url = self.URL + '/job-executions'
+ self.responses.get(url, json={'job_executions': [self.response]})
resp = self.client.job_executions.list()
- self.assertEqual('http://localhost:8386/job-executions',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], je.JobExecution)
self.assertFields(self.response, resp[0])
- @mock.patch('requests.get')
- def test_job_executions_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.response,
- 'job_execution')
+ def test_job_executions_get(self):
+ url = self.URL + '/job-executions/id'
+ self.responses.get(url, json={'job_execution': self.response})
resp = self.client.job_executions.get('id')
- self.assertEqual('http://localhost:8386/job-executions/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, je.JobExecution)
self.assertFields(self.response, resp)
- @mock.patch('requests.delete')
- def test_job_executions_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_job_executions_delete(self):
+ url = self.URL + '/job-executions/id'
+ self.responses.delete(url, status_code=204)
self.client.job_executions.delete('id')
- self.assertEqual('http://localhost:8386/job-executions/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_jobs.py b/saharaclient/tests/unit/test_jobs.py
index 3741f2e..13dcfb2 100644
--- a/saharaclient/tests/unit/test_jobs.py
+++ b/saharaclient/tests/unit/test_jobs.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import jobs
from saharaclient.tests.unit import base
@@ -29,63 +27,58 @@ class JobTest(base.BaseTestCase):
'description': 'descr'
}
- @mock.patch('requests.post')
- def test_create_job(self, mpost):
- mpost.return_value = base.FakeResponse(202, self.body, 'job')
+ def test_create_job(self):
+ url = self.URL + '/jobs'
+ self.responses.post(url, status_code=202, json={'job': self.body})
resp = self.client.jobs.create(**self.body)
- self.assertEqual('http://localhost:8386/jobs',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, jobs.Job)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_jobs_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body], 'jobs')
+ def test_jobs_list(self):
+ url = self.URL + '/jobs'
+ self.responses.get(url, json={'jobs': [self.body]})
resp = self.client.jobs.list()
- self.assertEqual('http://localhost:8386/jobs',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], jobs.Job)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_jobs_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.body, 'job')
+ def test_jobs_get(self):
+ url = self.URL + '/jobs/id'
+ self.responses.get(url, json={'job': self.body})
resp = self.client.jobs.get('id')
- self.assertEqual('http://localhost:8386/jobs/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, jobs.Job)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_jobs_get_configs(self, mget):
+ def test_jobs_get_configs(self):
+ url = self.URL + '/jobs/config-hints/Pig'
response = {
"job_config": {
"args": [],
"configs": []
}
}
-
- mget.return_value = base.FakeResponse(200, response)
+ self.responses.get(url, json=response)
resp = self.client.jobs.get_configs('Pig')
- self.assertEqual('http://localhost:8386/jobs/config-hints/Pig',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, jobs.Job)
self.assertFields(response, resp)
- @mock.patch('requests.delete')
- def test_jobs_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_jobs_delete(self):
+ url = self.URL + '/jobs/id'
+ self.responses.delete(url, status_code=204)
self.client.jobs.delete('id')
- self.assertEqual('http://localhost:8386/jobs/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_node_group_templates.py b/saharaclient/tests/unit/test_node_group_templates.py
index 653c789..f8926f7 100644
--- a/saharaclient/tests/unit/test_node_group_templates.py
+++ b/saharaclient/tests/unit/test_node_group_templates.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import node_group_templates as ng
from saharaclient.tests.unit import base
@@ -32,48 +30,42 @@ class NodeGroupTemplateTest(base.BaseTestCase):
"node_processes": ["datanode"]
}
- @mock.patch('requests.post')
- def test_create_node_group_template(self, mpost):
- mpost.return_value = base.FakeResponse(202, self.body,
- 'node_group_template')
+ def test_create_node_group_template(self):
+ url = self.URL + '/node-group-templates'
+ self.responses.post(url, status_code=202,
+ json={'node_group_template': self.body})
resp = self.client.node_group_templates.create(**self.body)
- self.assertEqual('http://localhost:8386/node-group-templates',
- mpost.call_args[0][0])
- self.assertEqual(self.body, json.loads(mpost.call_args[0][1]))
+ self.assertEqual(url, self.responses.last_request.url)
+ self.assertEqual(self.body,
+ json.loads(self.responses.last_request.body))
self.assertIsInstance(resp, ng.NodeGroupTemplate)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_node_group_template_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body],
- 'node_group_templates')
+ def test_node_group_template_list(self):
+ url = self.URL + '/node-group-templates'
+ self.responses.get(url, json={'node_group_templates': [self.body]})
resp = self.client.node_group_templates.list()
- self.assertEqual('http://localhost:8386/node-group-templates',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], ng.NodeGroupTemplate)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_node_group_template_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.body,
- 'node_group_template')
-
+ def test_node_group_template_get(self):
+ url = self.URL + '/node-group-templates/id'
+ self.responses.get(url, json={'node_group_template': self.body})
resp = self.client.node_group_templates.get('id')
- self.assertEqual('http://localhost:8386/node-group-templates/id',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, ng.NodeGroupTemplate)
self.assertFields(self.body, resp)
- @mock.patch('requests.delete')
- def test_node_group_template_delete(self, mdelete):
- mdelete.return_value = base.FakeResponse(204)
+ def test_node_group_template_delete(self):
+ url = self.URL + '/node-group-templates/id'
+ self.responses.delete(url, status_code=204)
self.client.node_group_templates.delete('id')
- self.assertEqual('http://localhost:8386/node-group-templates/id',
- mdelete.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
diff --git a/saharaclient/tests/unit/test_plugins.py b/saharaclient/tests/unit/test_plugins.py
index e4dc4a7..512f119 100644
--- a/saharaclient/tests/unit/test_plugins.py
+++ b/saharaclient/tests/unit/test_plugins.py
@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import mock
-
from saharaclient.api import plugins
from saharaclient.tests.unit import base
@@ -25,51 +23,47 @@ class PluginTest(base.BaseTestCase):
'version': '1'
}
- @mock.patch('requests.get')
- def test_plugins_list(self, mget):
- mget.return_value = base.FakeResponse(200, [self.body], 'plugins')
+ def test_plugins_list(self):
+ url = self.URL + '/plugins'
+ self.responses.get(url, json={'plugins': [self.body]})
resp = self.client.plugins.list()
- self.assertEqual('http://localhost:8386/plugins',
- mget.call_args[0][0])
+
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp[0], plugins.Plugin)
self.assertFields(self.body, resp[0])
- @mock.patch('requests.get')
- def test_plugins_get(self, mget):
- mget.return_value = base.FakeResponse(200, self.body, 'plugin')
+ def test_plugins_get(self):
+ url = self.URL + '/plugins/name'
+ self.responses.get(url, json={'plugin': self.body})
resp = self.client.plugins.get('name')
- self.assertEqual('http://localhost:8386/plugins/name',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, plugins.Plugin)
self.assertFields(self.body, resp)
- @mock.patch('requests.get')
- def test_plugins_get_version_details(self, mget):
- mget.return_value = base.FakeResponse(200, self.body, 'plugin')
+ def test_plugins_get_version_details(self):
+ url = self.URL + '/plugins/name/1'
+ self.responses.get(url, json={'plugin': self.body})
resp = self.client.plugins.get_version_details('name', '1')
- self.assertEqual('http://localhost:8386/plugins/name/1',
- mget.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertIsInstance(resp, plugins.Plugin)
self.assertFields(self.body, resp)
- @mock.patch('requests.post')
- def test_convert_to_cluster_template(self, mpost):
+ def test_convert_to_cluster_template(self):
+ url = self.URL + '/plugins/plugin/1/convert-config/template'
response = {
'name': 'name',
'description': 'description',
'plugin_name': 'plugin',
'hadoop_version': '1',
}
- mpost.return_value = base.FakeResponse(202, response,
- 'cluster_template')
+ self.responses.post(url, status_code=202,
+ json={'cluster_template': response})
resp = self.client.plugins.convert_to_cluster_template(
'plugin', 1, 'template', 'file')
- self.assertEqual(
- 'http://localhost:8386/plugins/plugin/1/convert-config/template',
- mpost.call_args[0][0])
+ self.assertEqual(url, self.responses.last_request.url)
self.assertEqual(response, resp)
diff --git a/test-requirements.txt b/test-requirements.txt
index f3d675f..4a4e9fd 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -13,3 +13,4 @@ python-swiftclient>=2.2.0
python-novaclient>=2.18.0
python-neutronclient>=2.3.6,<3
oslo.config>=1.6.0 # Apache-2.0
+requests-mock>=0.5.1 # Apache-2.0