This file is indexed.

/usr/lib/python2.7/dist-packages/neutron_lbaas/tests/unit/services/loadbalancer/drivers/haproxy/test_namespace_driver.py is in python-neutron-lbaas 2:8.0.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# Copyright 2013 New Dream Network, LLC (DreamHost)
#
#    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 contextlib

import mock
from neutron_lib import exceptions
import six

from neutron_lbaas.services.loadbalancer.drivers.haproxy \
    import namespace_driver
from neutron_lbaas.tests import base


class TestHaproxyNSDriver(base.BaseTestCase):
    def setUp(self):
        super(TestHaproxyNSDriver, self).setUp()

        conf = mock.Mock()
        conf.haproxy.loadbalancer_state_path = '/the/path'
        conf.interface_driver = 'intdriver'
        conf.haproxy.user_group = 'test_group'
        conf.haproxy.send_gratuitous_arp = 3
        self.conf = conf
        self.rpc_mock = mock.Mock()
        with mock.patch(
                'neutron.common.utils.load_class_by_alias_or_classname'):
            self.driver = namespace_driver.HaproxyNSDriver(
                conf,
                self.rpc_mock
            )
        self.vif_driver = mock.Mock()
        self.driver.vif_driver = self.vif_driver

        self.fake_config = {
            'pool': {'id': 'pool_id', 'status': 'ACTIVE',
                     'admin_state_up': True},
            'vip': {'id': 'vip_id', 'port': {'id': 'port_id'},
                    'address': '10.0.0.2',
                    'status': 'ACTIVE', 'admin_state_up': True}
        }

    def _ip_mock_call(self, ns=None):
        kwargs = {}
        if ns:
            kwargs['namespace'] = ns
        return mock.call(**kwargs)

    def test_get_name(self):
        self.assertEqual(namespace_driver.DRIVER_NAME, self.driver.get_name())

    def test_create(self):
        with mock.patch.object(self.driver, '_plug') as plug:
            with mock.patch.object(self.driver, '_spawn') as spawn:
                self.driver.create(self.fake_config)

                plug.assert_called_once_with(
                    'qlbaas-pool_id', {'id': 'port_id'}, '10.0.0.2'
                )
                spawn.assert_called_once_with(self.fake_config)

    def test_update(self):
        with contextlib.nested(
            mock.patch.object(self.driver, '_get_state_file_path'),
            mock.patch.object(self.driver, '_spawn'),
            mock.patch.object(six.moves.builtins, 'open')
        ) as (gsp, spawn, mock_open):
            mock_open.return_value = ['5']

            self.driver.update(self.fake_config)

            mock_open.assert_called_once_with(gsp.return_value, 'r')
            spawn.assert_called_once_with(self.fake_config, ['-sf', '5'])

    def test_spawn(self):
        with contextlib.nested(
            mock.patch.object(namespace_driver.hacfg, 'save_config'),
            mock.patch.object(self.driver, '_get_state_file_path'),
            mock.patch('neutron.agent.linux.ip_lib.IPWrapper')
        ) as (mock_save, gsp, ip_wrap):
            gsp.side_effect = lambda x, y: y

            self.driver._spawn(self.fake_config)

            mock_save.assert_called_once_with('conf', self.fake_config,
                                              'sock', 'test_group')
            cmd = ['haproxy', '-f', 'conf', '-p', 'pid']
            ip_wrap.assert_has_calls([
                self._ip_mock_call('qlbaas-pool_id'),
                mock.call().netns.execute(cmd)
            ])

    def test_undeploy_instance(self):
        with contextlib.nested(
            mock.patch.object(self.driver, '_get_state_file_path'),
            mock.patch.object(namespace_driver, 'kill_pids_in_file'),
            mock.patch.object(self.driver, '_unplug'),
            mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
            mock.patch('os.path.isdir'),
            mock.patch('shutil.rmtree')
        ) as (gsp, kill, unplug, ip_wrap, isdir, rmtree):
            gsp.side_effect = lambda x, y: '/pool/' + y

            self.driver.pool_to_port_id['pool_id'] = 'port_id'
            isdir.return_value = True

            self.driver.undeploy_instance('pool_id', delete_namespace=True)

            kill.assert_called_once_with('/pool/pid')
            unplug.assert_called_once_with('qlbaas-pool_id', 'port_id')
            isdir.assert_called_once_with('/pool')
            rmtree.assert_called_once_with('/pool')
            ip_wrap.assert_has_calls([
                self._ip_mock_call('qlbaas-pool_id'),
                mock.call().garbage_collect_namespace()
            ])

    def test_undeploy_instance_with_ns_cleanup(self):
        with contextlib.nested(
            mock.patch.object(self.driver, '_get_state_file_path'),
            mock.patch.object(self.driver, 'vif_driver'),
            mock.patch.object(namespace_driver, 'kill_pids_in_file'),
            mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
            mock.patch('os.path.isdir'),
            mock.patch('shutil.rmtree')
        ) as (gsp, vif, kill, ip_wrap, isdir, rmtree):
            device = mock.Mock()
            device_name = 'port_device'
            device.name = device_name
            ip_wrap.return_value.get_devices.return_value = [device]

            self.driver.undeploy_instance('pool_id', cleanup_namespace=True)
            vif.unplug.assert_called_once_with(device_name,
                                               namespace='qlbaas-pool_id')

    def test_remove_orphans(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'exists'),
            mock.patch.object(self.driver, 'undeploy_instance'),
            mock.patch('os.listdir'),
            mock.patch('os.path.exists')
        ) as (exists, undeploy, listdir, path_exists):
            known = ['known1', 'known2']
            unknown = ['unknown1', 'unknown2']
            listdir.return_value = known + unknown
            exists.side_effect = lambda x: x == 'unknown2'

            self.driver.remove_orphans(known)

            undeploy.assert_called_once_with('unknown2',
                                             cleanup_namespace=True)

    def test_exists(self):
        with contextlib.nested(
            mock.patch.object(self.driver, '_get_state_file_path'),
            mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
            mock.patch('socket.socket'),
            mock.patch('os.path.exists'),
        ) as (gsp, ip_wrap, socket, path_exists):
            gsp.side_effect = lambda x, y, z: '/pool/' + y

            ip_wrap.return_value.netns.exists.return_value = True
            path_exists.return_value = True

            self.driver.exists('pool_id')

            ip_wrap.assert_has_calls([
                self._ip_mock_call(),
                mock.call().netns.exists('qlbaas-pool_id')
            ])

            self.assertTrue(self.driver.exists('pool_id'))

    def test_get_stats(self):
        raw_stats = ('# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,'
                     'dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,'
                     'act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,'
                     'sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,'
                     'check_status,check_code,check_duration,hrsp_1xx,'
                     'hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,'
                     'req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,\n'
                     '8e271901-69ed-403e-a59b-f53cf77ef208,BACKEND,1,2,3,4,0,'
                     '10,7764,2365,0,0,,0,0,0,0,UP,1,1,0,,0,103780,0,,1,2,0,,0'
                     ',,1,0,,0,,,,0,0,0,0,0,0,,,,,0,0,\n\n'
                     'a557019b-dc07-4688-9af4-f5cf02bb6d4b,'
                     '32a6c2a3-420a-44c3-955d-86bd2fc6871e,0,0,0,1,,7,1120,'
                     '224,,0,,0,0,0,0,UP,1,1,0,0,1,2623,303,,1,2,1,,7,,2,0,,'
                     '1,L7OK,200,98,0,7,0,0,0,0,0,,,,0,0,\n'
                     'a557019b-dc07-4688-9af4-f5cf02bb6d4b,'
                     'd9aea044-8867-4e80-9875-16fb808fa0f9,0,0,0,2,,12,0,0,,'
                     '0,,0,0,8,4,DOWN,1,1,0,9,2,308,675,,1,2,2,,4,,2,0,,2,'
                     'L4CON,,2999,0,0,0,0,0,0,0,,,,0,0,\n')
        raw_stats_empty = ('# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,'
                           'bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,'
                           'status,weight,act,bck,chkfail,chkdown,lastchg,'
                           'downtime,qlimit,pid,iid,sid,throttle,lbtot,'
                           'tracked,type,rate,rate_lim,rate_max,check_status,'
                           'check_code,check_duration,hrsp_1xx,hrsp_2xx,'
                           'hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,'
                           'req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,'
                           '\n')
        with contextlib.nested(
                mock.patch.object(self.driver, '_get_state_file_path'),
                mock.patch('socket.socket'),
                mock.patch('os.path.exists'),
        ) as (gsp, socket, path_exists):
            gsp.side_effect = lambda x, y, z: '/pool/' + y
            path_exists.return_value = True
            socket.return_value = socket
            socket.recv.return_value = raw_stats

            exp_stats = {'connection_errors': '0',
                         'active_connections': '3',
                         'current_sessions': '3',
                         'bytes_in': '7764',
                         'max_connections': '4',
                         'max_sessions': '4',
                         'bytes_out': '2365',
                         'response_errors': '0',
                         'total_sessions': '10',
                         'total_connections': '10',
                         'members': {
                             '32a6c2a3-420a-44c3-955d-86bd2fc6871e': {
                                 'status': 'ACTIVE',
                                 'health': 'L7OK',
                                 'failed_checks': '0'
                             },
                             'd9aea044-8867-4e80-9875-16fb808fa0f9': {
                                 'status': 'INACTIVE',
                                 'health': 'L4CON',
                                 'failed_checks': '9'
                             }
                         }
                         }
            stats = self.driver.get_stats('pool_id')
            self.assertEqual(exp_stats, stats)

            socket.recv.return_value = raw_stats_empty
            self.assertEqual({'members': {}}, self.driver.get_stats('pool_id'))

            path_exists.return_value = False
            socket.reset_mock()
            self.assertEqual({}, self.driver.get_stats('pool_id'))
            self.assertFalse(socket.called)

    def test_plug(self):
        test_port = {'id': 'port_id',
                     'network_id': 'net_id',
                     'mac_address': 'mac_addr',
                     'fixed_ips': [{'ip_address': '10.0.0.2',
                                    'subnet': {'cidr': '10.0.0.0/24',
                                               'gateway_ip': '10.0.0.1'}}]}
        test_address = '10.0.0.2'
        with contextlib.nested(
                mock.patch('neutron.agent.linux.ip_lib.device_exists'),
                mock.patch('netaddr.IPNetwork'),
                mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
        ) as (dev_exists, ip_net, ip_wrap):
            self.vif_driver.get_device_name.return_value = 'test_interface'
            dev_exists.return_value = False
            ip_net.return_value = ip_net
            ip_net.prefixlen = 24

            self.driver._plug('test_ns', test_port, test_address)
            self.rpc_mock.plug_vip_port.assert_called_once_with(
                test_port['id'])
            self.assertTrue(dev_exists.called)
            self.vif_driver.plug.assert_called_once_with('net_id', 'port_id',
                                                         'test_interface',
                                                         'mac_addr',
                                                         namespace='test_ns')
            self.vif_driver.init_l3.assert_called_once_with(
                'test_interface',
                ['10.0.0.2/24'],
                namespace='test_ns'
            )
            cmd = ['route', 'add', 'default', 'gw', '10.0.0.1']
            cmd_arping = ['arping', '-U', '-I',
                          'test_interface', '-c',
                          self.conf.haproxy.send_gratuitous_arp, '10.0.0.2']
            ip_wrap.assert_has_calls([
                self._ip_mock_call('test_ns'),
                mock.call().netns.execute(cmd, check_exit_code=False),
                mock.call().netns.execute(cmd_arping, check_exit_code=False),
            ])

            dev_exists.return_value = True
            self.assertRaises(exceptions.PreexistingDeviceFailure,
                              self.driver._plug, 'test_ns', test_port,
                              test_address, False)

    def test_plug_not_send_gratuitous_arp(self):
        self.conf.haproxy.send_gratuitous_arp = 0
        test_port = {'id': 'port_id',
                     'network_id': 'net_id',
                     'mac_address': 'mac_addr',
                     'fixed_ips': [{'ip_address': '10.0.0.2',
                                    'subnet': {'cidr': '10.0.0.0/24',
                                               'gateway_ip': '10.0.0.1'}}]}
        test_address = '10.0.0.2'
        with contextlib.nested(
                mock.patch('neutron.agent.linux.ip_lib.device_exists'),
                mock.patch('netaddr.IPNetwork'),
                mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
        ) as (dev_exists, ip_net, ip_wrap):
            self.vif_driver.get_device_name.return_value = 'test_interface'
            dev_exists.return_value = False
            ip_net.return_value = ip_net
            ip_net.prefixlen = 24

            self.driver._plug('test_ns', test_port, test_address)
            cmd = ['route', 'add', 'default', 'gw', '10.0.0.1']
            expected = [
                self._ip_mock_call('test_ns'),
                mock.call().netns.execute(cmd, check_exit_code=False)]
            self.assertEqual(expected, ip_wrap.mock_calls)

    def test_plug_no_gw(self):
        test_port = {'id': 'port_id',
                     'network_id': 'net_id',
                     'mac_address': 'mac_addr',
                     'fixed_ips': [{'ip_address': '10.0.0.2',
                                    'subnet': {'cidr': '10.0.0.0/24'}}]}
        test_address = '10.0.0.2'
        with contextlib.nested(
                mock.patch('neutron.agent.linux.ip_lib.device_exists'),
                mock.patch('netaddr.IPNetwork'),
                mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
        ) as (dev_exists, ip_net, ip_wrap):
            self.vif_driver.get_device_name.return_value = 'test_interface'
            dev_exists.return_value = False
            ip_net.return_value = ip_net
            ip_net.prefixlen = 24

            self.driver._plug('test_ns', test_port, test_address)
            self.rpc_mock.plug_vip_port.assert_called_once_with(
                test_port['id'])
            self.assertTrue(dev_exists.called)
            self.vif_driver.plug.assert_called_once_with('net_id', 'port_id',
                                                         'test_interface',
                                                         'mac_addr',
                                                         namespace='test_ns')
            self.vif_driver.init_l3.assert_called_once_with(
                'test_interface',
                ['10.0.0.2/24'],
                namespace='test_ns'
            )
            self.assertFalse(ip_wrap.called)
            dev_exists.return_value = True
            self.assertRaises(exceptions.PreexistingDeviceFailure,
                              self.driver._plug, 'test_ns', test_port,
                              test_address, False)

    def test_plug_gw_in_host_routes(self):
        test_port = {'id': 'port_id',
                     'network_id': 'net_id',
                     'mac_address': 'mac_addr',
                     'fixed_ips': [{'ip_address': '10.0.0.2',
                                    'subnet': {'cidr': '10.0.0.0/24',
                                               'host_routes':
                                               [{'destination': '0.0.0.0/0',
                                                 'nexthop': '10.0.0.1'}]}}]}
        test_address = '10.0.0.2'
        with contextlib.nested(
                mock.patch('neutron.agent.linux.ip_lib.device_exists'),
                mock.patch('netaddr.IPNetwork'),
                mock.patch('neutron.agent.linux.ip_lib.IPWrapper'),
        ) as (dev_exists, ip_net, ip_wrap):
            self.vif_driver.get_device_name.return_value = 'test_interface'
            dev_exists.return_value = False
            ip_net.return_value = ip_net
            ip_net.prefixlen = 24

            self.driver._plug('test_ns', test_port, test_address)
            self.rpc_mock.plug_vip_port.assert_called_once_with(
                test_port['id'])
            self.assertTrue(dev_exists.called)
            self.vif_driver.plug.assert_called_once_with('net_id', 'port_id',
                                                         'test_interface',
                                                         'mac_addr',
                                                         namespace='test_ns')
            self.vif_driver.init_l3.assert_called_once_with(
                'test_interface',
                ['10.0.0.2/24'],
                namespace='test_ns'
            )
            cmd = ['route', 'add', 'default', 'gw', '10.0.0.1']
            ip_wrap.assert_has_calls([
                self._ip_mock_call('test_ns'),
                mock.call().netns.execute(cmd, check_exit_code=False),
            ])

    def test_unplug(self):
        self.vif_driver.get_device_name.return_value = 'test_interface'

        self.driver._unplug('test_ns', 'port_id')
        self.rpc_mock.unplug_vip_port.assert_called_once_with('port_id')
        self.vif_driver.unplug('test_interface', namespace='test_ns')

    def test_kill_pids_in_file(self):
        with contextlib.nested(
            mock.patch('os.path.exists'),
            mock.patch.object(six.moves.builtins, 'open'),
            mock.patch('neutron.agent.linux.utils.execute'),
            mock.patch.object(namespace_driver.LOG, 'exception'),
        ) as (path_exists, mock_open, mock_execute, mock_log):
            file_mock = mock.MagicMock()
            mock_open.return_value = file_mock
            file_mock.__enter__.return_value = file_mock
            file_mock.__iter__.return_value = iter(['123'])

            path_exists.return_value = False
            namespace_driver.kill_pids_in_file('test_path')
            path_exists.assert_called_once_with('test_path')
            self.assertFalse(mock_open.called)
            self.assertFalse(mock_execute.called)

            path_exists.return_value = True
            mock_execute.side_effect = RuntimeError
            namespace_driver.kill_pids_in_file('test_path')
            self.assertTrue(mock_log.called)
            mock_execute.assert_called_once_with(
                ['kill', '-9', '123'], run_as_root=True)

    def test_get_state_file_path(self):
        with mock.patch('os.makedirs') as mkdir:
            path = self.driver._get_state_file_path('pool_id', 'conf')
            self.assertEqual('/the/path/pool_id/conf', path)
            mkdir.assert_called_once_with('/the/path/pool_id', 0o755)

    def test_deploy_instance(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            with mock.patch.object(self.driver, 'update') as update:
                self.driver.deploy_instance(self.fake_config)
                exists.assert_called_once_with(self.fake_config['pool']['id'])
                update.assert_called_once_with(self.fake_config)

    def test_deploy_instance_non_existing(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            with mock.patch.object(self.driver, 'create') as create:
                exists.return_value = False
                self.driver.deploy_instance(self.fake_config)
                exists.assert_called_once_with(self.fake_config['pool']['id'])
                create.assert_called_once_with(self.fake_config)

    def test_deploy_instance_vip_status_non_active(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            self.fake_config['vip']['status'] = 'NON_ACTIVE'
            self.driver.deploy_instance(self.fake_config)
            self.assertFalse(exists.called)

    def test_deploy_instance_vip_admin_state_down(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            self.fake_config['vip']['admin_state_up'] = False
            self.driver.deploy_instance(self.fake_config)
            self.assertFalse(exists.called)

    def test_deploy_instance_no_vip(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            del self.fake_config['vip']
            self.driver.deploy_instance(self.fake_config)
            self.assertFalse(exists.called)

    def test_deploy_instance_pool_status_non_active(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            self.fake_config['pool']['status'] = 'NON_ACTIVE'
            self.driver.deploy_instance(self.fake_config)
            self.assertFalse(exists.called)

    def test_deploy_instance_pool_admin_state_down(self):
        with mock.patch.object(self.driver, 'exists') as exists:
            with mock.patch.object(self.driver, 'update') as update:
                self.fake_config['pool']['admin_state_up'] = False
                self.driver.deploy_instance(self.fake_config)
                exists.assert_called_once_with(self.fake_config['pool']['id'])
                update.assert_called_once_with(self.fake_config)

    def test_refresh_device(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'deploy_instance'),
            mock.patch.object(self.driver, 'undeploy_instance')
        ) as (deploy, undeploy):
            pool_id = 'pool_id1'
            self.driver._refresh_device(pool_id)
            self.rpc_mock.get_logical_device.assert_called_once_with(pool_id)
            deploy.assert_called_once_with(
                self.rpc_mock.get_logical_device.return_value)
            self.assertFalse(undeploy.called)

    def test_refresh_device_not_deployed(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'deploy_instance'),
            mock.patch.object(self.driver, 'exists'),
            mock.patch.object(self.driver, 'undeploy_instance')
        ) as (deploy, exists, undeploy):
            pool_id = 'pool_id1'
            deploy.return_value = False
            exists.return_value = True
            self.driver._refresh_device(pool_id)
            undeploy.assert_called_once_with(pool_id)

    def test_refresh_device_non_existing(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'deploy_instance'),
            mock.patch.object(self.driver, 'exists'),
            mock.patch.object(self.driver, 'undeploy_instance')
        ) as (deploy, exists, undeploy):
            pool_id = 'pool_id1'
            deploy.return_value = False
            exists.return_value = False
            self.driver._refresh_device(pool_id)
            self.assertFalse(undeploy.called)

    def test_create_vip(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.create_vip({'pool_id': '1'})
            refresh.assert_called_once_with('1')

    def test_update_vip(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.update_vip({}, {'pool_id': '1'})
            refresh.assert_called_once_with('1')

    def test_delete_vip(self):
        with mock.patch.object(self.driver, 'undeploy_instance') as undeploy:
            self.driver.delete_vip({'pool_id': '1'})
            undeploy.assert_called_once_with('1')

    def test_create_pool(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.create_pool({'id': '1'})
            self.assertFalse(refresh.called)

    def test_update_pool(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.update_pool({}, {'id': '1'})
            refresh.assert_called_once_with('1')

    def test_delete_pool_existing(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'undeploy_instance'),
            mock.patch.object(self.driver, 'exists'),
        ) as (undeploy, exists):
            exists.return_value = True
            self.driver.delete_pool({'id': '1'})
            undeploy.assert_called_once_with('1', delete_namespace=True)

    def test_delete_pool_non_existing(self):
        with contextlib.nested(
            mock.patch.object(self.driver, 'undeploy_instance'),
            mock.patch.object(self.driver, 'exists'),
        ) as (undeploy, exists):
            exists.return_value = False
            self.driver.delete_pool({'id': '1'})
            self.assertFalse(undeploy.called)

    def test_create_member(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.create_member({'pool_id': '1'})
            refresh.assert_called_once_with('1')

    def test_update_member(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.update_member({}, {'pool_id': '1'})
            refresh.assert_called_once_with('1')

    def test_delete_member(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.delete_member({'pool_id': '1'})
            refresh.assert_called_once_with('1')

    def test_create_pool_health_monitor(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.create_pool_health_monitor('', '1')
            refresh.assert_called_once_with('1')

    def test_update_pool_health_monitor(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.update_pool_health_monitor('', '', '1')
            refresh.assert_called_once_with('1')

    def test_delete_pool_health_monitor(self):
        with mock.patch.object(self.driver, '_refresh_device') as refresh:
            self.driver.delete_pool_health_monitor('', '1')
            refresh.assert_called_once_with('1')