~niedbalski/ubuntu/vivid/neutron/fixes-1447803

« back to all changes in this revision

Viewing changes to neutron/tests/unit/bigswitch/test_servermanager.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-10-03 18:45:23 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20141003184523-4mt6dy1q3j8n30c9
Tags: 1:2014.2~rc1-0ubuntu1
* New upstream release candidate:
  - d/p/*: Refreshed.
  - d/control: Add python-requests-mock to BD's.
  - d/control: Align versioned requirements with upstream.
* Transition linuxbridge and openvswitch plugin users to modular
  layer 2 plugin (LP: #1323729):
  - d/control: Mark removed plugin packages as transitional, depend
    on neutron-plugin-ml2, mark oldlibs/extra.
  - d/neutron-plugin-{linuxbridge,openvswitch}.install: Drop.
  - d/control: Depend on neutron-plugin-ml2 for linuxbridge
    agent package.
  - d/neutron-plugin-linuxbridge-agent.upstart: Use ml2 plugin
    configuration files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
12
#    License for the specific language governing permissions and limitations
13
13
#    under the License.
14
 
#
15
 
# @author: Kevin Benton, kevin.benton@bigswitch.com
16
 
#
17
14
import contextlib
18
15
import httplib
19
16
import socket
22
19
import mock
23
20
from oslo.config import cfg
24
21
 
 
22
from neutron import context
25
23
from neutron import manager
26
24
from neutron.openstack.common import importutils
 
25
from neutron.openstack.common import jsonutils
27
26
from neutron.plugins.bigswitch import servermanager
28
27
from neutron.tests.unit.bigswitch import test_restproxy_plugin as test_rp
29
28
 
116
115
            rv.getresponse.return_value.getheader.return_value = 'HASHHEADER'
117
116
            rv.getresponse.return_value.status = 200
118
117
            rv.getresponse.return_value.read.return_value = ''
119
 
            with self.network():
 
118
            with self.network() as network:
120
119
                callheaders = rv.request.mock_calls[0][1][3]
121
120
                self.assertIn('X-BSN-BVS-HASH-MATCH', callheaders)
122
121
                # first call will be empty to indicate no previous state hash
123
122
                self.assertEqual(callheaders['X-BSN-BVS-HASH-MATCH'], '')
124
123
                # change the header that will be received on delete call
125
124
                rv.getresponse.return_value.getheader.return_value = 'HASH2'
126
 
 
 
125
            self._delete('networks', network['network']['id'])
127
126
            # net delete should have used header received on create
128
127
            callheaders = rv.request.mock_calls[1][1][3]
129
128
            self.assertEqual(callheaders['X-BSN-BVS-HASH-MATCH'], 'HASHHEADER')
142
141
            rv.getresponse.return_value.getheader.return_value = 'HASHHEADER'
143
142
            rv.getresponse.return_value.status = 200
144
143
            rv.getresponse.return_value.read.return_value = ''
145
 
            with self.network():
 
144
            with self.network() as net:
146
145
                # change the header that will be received on delete call
147
146
                rv.getresponse.return_value.getheader.return_value = 'EVIL'
148
147
                rv.getresponse.return_value.status = 'GARBAGE'
 
148
                self._delete('networks', net['network']['id'])
149
149
 
150
150
            # create again should not use header from delete call
151
151
            with self.network():
210
210
        self.assertIn('EXTRA-HEADER', callheaders)
211
211
        self.assertEqual(callheaders['EXTRA-HEADER'], 'HI')
212
212
 
 
213
    def test_req_context_header(self):
 
214
        sp = manager.NeutronManager.get_plugin().servers
 
215
        ncontext = context.Context('uid', 'tid')
 
216
        sp.set_context(ncontext)
 
217
        with mock.patch(HTTPCON) as conmock:
 
218
            rv = conmock.return_value
 
219
            rv.getresponse.return_value.getheader.return_value = 'HASHHEADER'
 
220
            sp.rest_action('GET', '/')
 
221
        callheaders = rv.request.mock_calls[0][1][3]
 
222
        self.assertIn(servermanager.REQ_CONTEXT_HEADER, callheaders)
 
223
        ctxdct = ncontext.to_dict()
 
224
        # auth token is not included
 
225
        ctxdct.pop('auth_token')
 
226
        self.assertEqual(
 
227
            ctxdct, jsonutils.loads(
 
228
                  callheaders[servermanager.REQ_CONTEXT_HEADER]))
 
229
 
213
230
    def test_capabilities_retrieval(self):
214
231
        sp = servermanager.ServerPool()
215
232
        with mock.patch(HTTPCON) as conmock:
373
390
        self.assertFalse(pl.servers.server_failure((404,),
374
391
                                                   ignore_codes=[404]))
375
392
 
 
393
    def test_retry_on_unavailable(self):
 
394
        pl = manager.NeutronManager.get_plugin()
 
395
        with contextlib.nested(
 
396
            mock.patch(SERVERMANAGER + '.ServerProxy.rest_call',
 
397
                       return_value=(httplib.SERVICE_UNAVAILABLE, 0, 0, 0)),
 
398
            mock.patch(SERVERMANAGER + '.time.sleep')
 
399
        ) as (srestmock, tmock):
 
400
            # making a call should trigger retries with sleeps in between
 
401
            pl.servers.rest_call('GET', '/', '', None, [])
 
402
            rest_call = [mock.call('GET', '/', '', None, False, reconnect=True,
 
403
                                   hash_handler=mock.ANY)]
 
404
            rest_call_count = (
 
405
                servermanager.HTTP_SERVICE_UNAVAILABLE_RETRY_COUNT + 1)
 
406
            srestmock.assert_has_calls(rest_call * rest_call_count)
 
407
            sleep_call = [mock.call(
 
408
                servermanager.HTTP_SERVICE_UNAVAILABLE_RETRY_INTERVAL)]
 
409
            # should sleep 1 less time than the number of calls
 
410
            sleep_call_count = rest_call_count - 1
 
411
            tmock.assert_has_calls(sleep_call * sleep_call_count)
 
412
 
376
413
    def test_conflict_triggers_sync(self):
377
414
        pl = manager.NeutronManager.get_plugin()
378
415
        with mock.patch(