Add expect_response_code to base network client

This commit adds a new kwarg called expect_response_code to the
base network client, since it currently does not allow the
expected response code to be overriden. This is problematic
because commits like Icfff444ee7638a3220d228330f9162044673636c
rely on different response codes. For example, creating a network
tag uses a PUT but expects a 201 (since it does creation) --
so it is impossible to use update_resource.

This commit does the following:
  - adds new kwarg expect_response_code to create/update functions
    in base network client
  - adds unit tests for base network client that test the functions
    including with the new kwarg
  - update ``create_tag`` client method in network ``tags_client``
    to use update_resource with expect_response_code=201

Change-Id: I9b4685e7ceb3005219e6c9831c1f4a293c73a340
diff --git a/tempest/lib/services/network/base.py b/tempest/lib/services/network/base.py
index b6f9c91..fe8b244 100644
--- a/tempest/lib/services/network/base.py
+++ b/tempest/lib/services/network/base.py
@@ -54,7 +54,8 @@
         self.expected_success(200, resp.status)
         return rest_client.ResponseBody(resp, body)
 
-    def create_resource(self, uri, post_data, expect_empty_body=False):
+    def create_resource(self, uri, post_data, expect_empty_body=False,
+                        expect_response_code=201):
         req_uri = self.uri_prefix + uri
         req_post_data = json.dumps(post_data)
         resp, body = self.post(req_uri, req_post_data)
@@ -65,10 +66,11 @@
             body = json.loads(body)
         else:
             body = None
-        self.expected_success(201, resp.status)
+        self.expected_success(expect_response_code, resp.status)
         return rest_client.ResponseBody(resp, body)
 
-    def update_resource(self, uri, post_data, expect_empty_body=False):
+    def update_resource(self, uri, post_data, expect_empty_body=False,
+                        expect_response_code=200):
         req_uri = self.uri_prefix + uri
         req_post_data = json.dumps(post_data)
         resp, body = self.put(req_uri, req_post_data)
@@ -79,5 +81,5 @@
             body = json.loads(body)
         else:
             body = None
-        self.expected_success(200, resp.status)
+        self.expected_success(expect_response_code, resp.status)
         return rest_client.ResponseBody(resp, body)
diff --git a/tempest/lib/services/network/tags_client.py b/tempest/lib/services/network/tags_client.py
index 20c2c11..5d49a79 100644
--- a/tempest/lib/services/network/tags_client.py
+++ b/tempest/lib/services/network/tags_client.py
@@ -27,14 +27,10 @@
         For more information, please refer to the official API reference:
         http://developer.openstack.org/api-ref/networking/v2/index.html#add-a-tag
         """
-        # NOTE(felipemonteiro): Cannot use ``update_resource`` method because
-        # this API requires self.put but returns 201 instead of 200 expected
-        # by ``update_resource``.
-        uri = '%s/%s/%s/tags/%s' % (
-            self.uri_prefix, resource_type, resource_id, tag)
-        resp, _ = self.put(uri, json.dumps({}))
-        self.expected_success(201, resp.status)
-        return rest_client.ResponseBody(resp)
+        uri = '/%s/%s/tags/%s' % (resource_type, resource_id, tag)
+        return self.update_resource(
+            uri, json.dumps({}), expect_response_code=201,
+            expect_empty_body=True)
 
     def check_tag_existence(self, resource_type, resource_id, tag):
         """Confirm that a given tag is set on the resource.
diff --git a/tempest/tests/lib/services/network/test_base_network_client.py b/tempest/tests/lib/services/network/test_base_network_client.py
new file mode 100644
index 0000000..e121cec
--- /dev/null
+++ b/tempest/tests/lib/services/network/test_base_network_client.py
@@ -0,0 +1,96 @@
+# Copyright 2017 AT&T Corporation.
+# 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.
+
+import mock
+
+from tempest.lib.services.network import base as base_network_client
+from tempest.tests.lib import fake_auth_provider
+from tempest.tests.lib import fake_http
+from tempest.tests.lib.services import base
+
+
+class TestBaseNetworkClient(base.BaseServiceTest):
+
+    def setUp(self):
+        super(TestBaseNetworkClient, self).setUp()
+        fake_auth = fake_auth_provider.FakeAuthProvider()
+        self.client = base_network_client.BaseNetworkClient(
+            fake_auth, 'compute', 'regionOne')
+
+        self.mock_expected_success = mock.patch.object(
+            self.client, 'expected_success').start()
+
+    def _assert_empty(self, resp):
+        self.assertEqual([], list(resp.keys()))
+
+    @mock.patch('tempest.lib.common.rest_client.RestClient.post')
+    def test_create_resource(self, mock_post):
+        response = fake_http.fake_http_response(headers=None, status=201)
+        mock_post.return_value = response, '{"baz": "qux"}'
+
+        post_data = {'foo': 'bar'}
+        resp = self.client.create_resource('/fake_url', post_data)
+
+        self.assertEqual({'status': '201'}, resp.response)
+        self.assertEqual("qux", resp["baz"])
+        mock_post.assert_called_once_with('v2.0/fake_url', '{"foo": "bar"}')
+        self.mock_expected_success.assert_called_once_with(
+            201, 201)
+
+    @mock.patch('tempest.lib.common.rest_client.RestClient.post')
+    def test_create_resource_expect_different_values(self, mock_post):
+        response = fake_http.fake_http_response(headers=None, status=200)
+        mock_post.return_value = response, '{}'
+
+        post_data = {'foo': 'bar'}
+        resp = self.client.create_resource('/fake_url', post_data,
+                                           expect_response_code=200,
+                                           expect_empty_body=True)
+
+        self.assertEqual({'status': '200'}, resp.response)
+        self._assert_empty(resp)
+        mock_post.assert_called_once_with('v2.0/fake_url', '{"foo": "bar"}')
+        self.mock_expected_success.assert_called_once_with(
+            200, 200)
+
+    @mock.patch('tempest.lib.common.rest_client.RestClient.put')
+    def test_update_resource(self, mock_put):
+        response = fake_http.fake_http_response(headers=None, status=200)
+        mock_put.return_value = response, '{"baz": "qux"}'
+
+        put_data = {'foo': 'bar'}
+        resp = self.client.update_resource('/fake_url', put_data)
+
+        self.assertEqual({'status': '200'}, resp.response)
+        self.assertEqual("qux", resp["baz"])
+        mock_put.assert_called_once_with('v2.0/fake_url', '{"foo": "bar"}')
+        self.mock_expected_success.assert_called_once_with(
+            200, 200)
+
+    @mock.patch('tempest.lib.common.rest_client.RestClient.put')
+    def test_update_resource_expect_different_values(self, mock_put):
+        response = fake_http.fake_http_response(headers=None, status=201)
+        mock_put.return_value = response, '{}'
+
+        put_data = {'foo': 'bar'}
+        resp = self.client.update_resource('/fake_url', put_data,
+                                           expect_response_code=201,
+                                           expect_empty_body=True)
+
+        self.assertEqual({'status': '201'}, resp.response)
+        self._assert_empty(resp)
+        mock_put.assert_called_once_with('v2.0/fake_url', '{"foo": "bar"}')
+        self.mock_expected_success.assert_called_once_with(
+            201, 201)