File: test_api_vrrp.py

package info (click to toggle)
pyeapi 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: python: 10,118; makefile: 197
file content (809 lines) | stat: -rw-r--r-- 31,012 bytes parent folder | download | duplicates (3)
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#
# right (c) 2015, Arista Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
#   Neither the name of Arista Networks nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import sys
import os
import unittest

sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))

from testlib import get_fixture, function
from testlib import EapiConfigUnitTest

import pyeapi.api.vrrp

upd_intf = 'Vlan50'
upd_vrid = 10
upd_cmd = 'interface %s' % upd_intf
known_vrrps = {
    'Ethernet1': {
        10: {'priority': 175,
             'timers_advertise': 1,
             'mac_addr_adv_interval': 30,
             'preempt': True,
             'preempt_delay_min': 0,
             'preempt_delay_reload': 0,
             'delay_reload': 0,
             'primary_ip': '10.10.6.10',
             'secondary_ip': [],
             'description': 'vrrp 10 on Ethernet1',
             'enable': True,
             'track': [],
             'bfd_ip': '',
             'ip_version': 2}
    },
    'Port-Channel10': {
        10: {'priority': 150,
             'timers_advertise': 1,
             'mac_addr_adv_interval': 30,
             'preempt': True,
             'preempt_delay_min': 0,
             'preempt_delay_reload': 0,
             'delay_reload': 0,
             'primary_ip': '10.10.5.10',
             'secondary_ip': ['10.10.5.20'],
             'description': 'vrrp 10 on Port-Channel10',
             'enable': True,
             'track': [],
             'bfd_ip': '',
             'ip_version': 2}
    },
    'Vlan50': {
        10: {'priority': 200,
             'timers_advertise': 3,
             'mac_addr_adv_interval': 30,
             'preempt': True,
             'preempt_delay_min': 0,
             'preempt_delay_reload': 0,
             'delay_reload': 0,
             'primary_ip': '10.10.4.10',
             'secondary_ip': ['10.10.4.21', '10.10.4.22',
                              '10.10.4.23', '10.10.4.24'],
             'description': '',
             'enable': True,
             'track': [
                 {'name': 'Ethernet1', 'action': 'decrement', 'amount': 10},
                 {'name': 'Ethernet1', 'action': 'shutdown'},
                 {'name': 'Ethernet2', 'action': 'decrement', 'amount': 50},
                 {'name': 'Ethernet2', 'action': 'shutdown'},
                 {'name': 'Ethernet11', 'action': 'decrement', 'amount': 75},
                 {'name': 'Ethernet11', 'action': 'shutdown'},
             ],
             'bfd_ip': '',
             'ip_version': 2},
        20: {'priority': 100,
             'timers_advertise': 5,
             'mac_addr_adv_interval': 30,
             'preempt': False,
             'preempt_delay_min': 0,
             'preempt_delay_reload': 0,
             'delay_reload': 0,
             'primary_ip': '10.10.4.20',
             'secondary_ip': [],
             'description': '',
             'enable': False,
             'track': [
                 {'name': 'Ethernet1', 'action': 'shutdown'},
                 {'name': 'Ethernet2', 'action': 'decrement', 'amount': 1},
                 {'name': 'Ethernet2', 'action': 'shutdown'},
             ],
             'bfd_ip': '',
             'ip_version': 2},
        30: {'priority': 50,
             'timers_advertise': 1,
             'mac_addr_adv_interval': 30,
             'preempt': True,
             'preempt_delay_min': 0,
             'preempt_delay_reload': 0,
             'delay_reload': 0,
             'primary_ip': '10.10.4.30',
             'secondary_ip': [],
             'description': '',
             'enable': True,
             'track': [],
             'bfd_ip': '10.10.4.33',
             'ip_version': 2}
    }
}


class TestApiVrrp(EapiConfigUnitTest):

    maxDiff = None

    def __init__(self, *args, **kwargs):
        super(TestApiVrrp, self).__init__(*args, **kwargs)
        self.instance = pyeapi.api.vrrp.Vrrp(None)
        self.config = open(get_fixture('running_config.vrrp')).read()

    def test_instance(self):
        result = pyeapi.api.vrrp.instance(None)
        self.assertIsInstance(result, pyeapi.api.vrrp.Vrrp)

    def test_get(self):
        # Request various sets of vrrp configurations
        for interface in known_vrrps:
            known = known_vrrps.get(interface)
            for vrid in known:
                known[vrid] = self.instance.vrconf_format(known[vrid])
            result = self.instance.get(interface)
            self.assertEqual(result, known)

    def test_get_non_existent_interface(self):
        # Request vrrp configuration for an interface that
        # is not defined
        result = self.instance.get('Vlan2000')
        self.assertIsNone(result)

    def test_get_invalid_parameters(self):
        # Pass empty, None, or other invalid parameters to get()
        with self.assertRaises(ValueError):
            self.instance.get('')
        with self.assertRaises(ValueError):
            self.instance.get(None)

    def test_getall(self):
        # Get all the vrrp configurations from the config
        result = self.instance.getall()
        self.assertEqual(result, known_vrrps)

    def test_create(self):
        interface = 'Ethernet1'
        vrid = 10

        # Test create with a normal configuration
        configuration = {
            'primary_ip': '10.10.60.10',
            'priority': 200,
            'description': 'modified vrrp 10 on Ethernet1',
            'secondary_ip': ['10.10.60.20', '10.10.60.30'],
            'ip_version': 3,
            'timers_advertise': 2,
            'mac_addr_adv_interval': 3,
            'preempt': True,
            'preempt_delay_min': 1,
            'preempt_delay_reload': 1,
            'delay_reload': 1,
            'track': [
                {'name': 'Ethernet1', 'action': 'decrement', 'amount': 1},
                {'name': 'Ethernet1', 'action': 'shutdown'},
                {'name': 'Ethernet2', 'action': 'decrement', 'amount': 1},
                {'name': 'Ethernet2', 'action': 'shutdown'},
            ],
            'bfd_ip': '10.10.60.30',
        }

        cmds = [
            'interface Ethernet1',
            'vrrp 10 shutdown',
            'vrrp 10 ip 10.10.60.10',
            'vrrp 10 priority 200',
            'vrrp 10 description modified vrrp 10 on Ethernet1',
            'vrrp 10 ip version 3',
            'vrrp 10 ip 10.10.60.20 secondary',
            'vrrp 10 ip 10.10.60.30 secondary',
            'vrrp 10 timers advertise 2',
            'vrrp 10 mac-address advertisement-interval 3',
            'vrrp 10 preempt',
            'vrrp 10 preempt delay minimum 1',
            'vrrp 10 preempt delay reload 1',
            'vrrp 10 delay reload 1',
            'vrrp 10 track Ethernet1 decrement 1',
            'vrrp 10 track Ethernet1 shutdown',
            'vrrp 10 track Ethernet2 decrement 1',
            'vrrp 10 track Ethernet2 shutdown',
            'vrrp 10 bfd ip 10.10.60.30',
        ]
        func = function('create', interface, vrid, **configuration)

        self.eapi_positive_config_test(func, cmds)

        # Test create setting possible parameters to 'no'
        configuration = {
            'primary_ip': 'no',
            'priority': 'no',
            'description': 'no',
            'secondary_ip': [],
            'ip_version': 'no',
            'timers_advertise': 'no',
            'mac_addr_adv_interval': 'no',
            'preempt': 'no',
            'preempt_delay_min': 'no',
            'preempt_delay_reload': 'no',
            'delay_reload': 'no',
            'track': [],
            'bfd_ip': 'no',
        }

        cmds = [
            'interface Ethernet1',
            'vrrp 10 shutdown',
            'no vrrp 10 ip 10.10.6.10',
            'no vrrp 10 priority',
            'no vrrp 10 description',
            'no vrrp 10 ip version',
            'no vrrp 10 timers advertise',
            'no vrrp 10 mac-address advertisement-interval',
            'no vrrp 10 preempt',
            'no vrrp 10 preempt delay minimum',
            'no vrrp 10 preempt delay reload',
            'no vrrp 10 delay reload',
            'no vrrp 10 bfd ip',
        ]
        func = function('create', interface, vrid, **configuration)

        self.eapi_positive_config_test(func, cmds)

        # Test create setting possible parameters to 'default'
        configuration = {
            'primary_ip': 'default',
            'priority': 'default',
            'description': 'default',
            'secondary_ip': [],
            'ip_version': 'default',
            'timers_advertise': 'default',
            'mac_addr_adv_interval': 'default',
            'preempt': 'default',
            'preempt_delay_min': 'default',
            'preempt_delay_reload': 'default',
            'delay_reload': 'default',
            'track': [],
            'bfd_ip': 'default',
        }

        cmds = [
            'interface Ethernet1',
            'vrrp 10 shutdown',
            'default vrrp 10 ip 10.10.6.10',
            'default vrrp 10 priority',
            'default vrrp 10 description',
            'default vrrp 10 ip version',
            'default vrrp 10 timers advertise',
            'default vrrp 10 mac-address advertisement-interval',
            'default vrrp 10 preempt',
            'default vrrp 10 preempt delay minimum',
            'default vrrp 10 preempt delay reload',
            'default vrrp 10 delay reload',
            'default vrrp 10 bfd ip',
        ]
        func = function('create', interface, vrid, **configuration)

        self.eapi_positive_config_test(func, cmds)

    def test_delete(self):
        interface = 'Ethernet1'
        vrid = 10

        cmds = [
            'interface Ethernet1',
            'no vrrp 10',
        ]

        func = function('delete', interface, vrid)

        self.eapi_positive_config_test(func, cmds)

    def test_default(self):
        interface = 'Ethernet1'
        vrid = 10

        cmds = [
            'interface Ethernet1',
            'default vrrp 10',
        ]

        func = function('default', interface, vrid)

        self.eapi_positive_config_test(func, cmds)

    def test_set_enable(self):
        # no vrrp 10 shutdown

        # Test set_enable gives properly formatted commands
        cases = [
            (False, 'vrrp %d shutdown' % upd_vrid),
            (True, 'no vrrp %d shutdown' % upd_vrid),
        ]

        for (enable, cmd) in cases:
            func = function('set_enable', upd_intf, upd_vrid, value=enable)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError from invalid parameters
        cases = ['a', 200]

        for enable in cases:
            func = function('set_enable', upd_intf, upd_vrid, value=enable)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_primary_ip(self):
        # vrrp 10 ip 10.10.4.10

        # Test set_primary_ip gives properly formatted commands
        ip1 = '10.10.4.110'
        ipcurr = '10.10.4.10'
        cases = [
            (ip1, None, None, 'vrrp %d ip %s' % (upd_vrid, ip1)),
            (ip1, True, None, 'no vrrp %d ip %s' % (upd_vrid, ipcurr)),
            (ip1, None, True, 'default vrrp %d ip %s' % (upd_vrid, ipcurr)),
            (ip1, True, True, 'default vrrp %d ip %s' % (upd_vrid, ipcurr)),
        ]

        for (primary_ip, disable, default, cmd) in cases:
            func = function('set_primary_ip', upd_intf, upd_vrid,
                            value=primary_ip, disable=disable, default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError from invalid parameters
        cases = ['abc', 500, '101.101']

        for primary_ip in cases:
            func = function('set_primary_ip', upd_intf, upd_vrid,
                            value=primary_ip)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_priority(self):
        # vrrp 10 priority 200

        # Test set_primary_ip gives properly formatted commands
        cases = [
            (150, None, None, 'vrrp %d priority 150' % upd_vrid),
            (None, None, True, 'default vrrp %d priority' % upd_vrid),
            (None, True, True, 'default vrrp %d priority' % upd_vrid),
            (None, True, None, 'no vrrp %d priority' % upd_vrid),
        ]

        for (priority, disable, default, cmd) in cases:
            func = function('set_priority', upd_intf, upd_vrid,
                            value=priority, disable=disable, default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError from invalid parameters
        cases = ['abc', 500, False]

        for priority in cases:
            func = function('set_priority', upd_intf, upd_vrid,
                            value=priority)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_description(self):
        # no vrrp 10 description

        desc = 'test description'

        # Test set_description gives properly formatted commands
        cases = [
            (desc, None, None, 'vrrp %d description %s' % (upd_vrid, desc)),
            (None, None, True, 'default vrrp %d description' % upd_vrid),
            (None, True, True, 'default vrrp %d description' % upd_vrid),
            (None, True, None, 'no vrrp %d description' % upd_vrid),
        ]

        for (description, disable, default, cmd) in cases:
            func = function('set_description', upd_intf, upd_vrid,
                            value=description, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

    def test_set_ip_version(self):
        # vrrp 10 ip version 2

        # Test set_description gives properly formatted commands
        cases = [
            (2, None, None, 'vrrp %d ip version 2' % upd_vrid),
            (None, None, True, 'default vrrp %d ip version' % upd_vrid),
            (None, True, True, 'default vrrp %d ip version' % upd_vrid),
            (None, True, None, 'no vrrp %d ip version' % upd_vrid),
        ]

        for (ip_version, disable, default, cmd) in cases:
            func = function('set_ip_version', upd_intf, upd_vrid,
                            value=ip_version, disable=disable, default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 5]

        for ip_version in cases:
            func = function('set_ip_version', upd_intf, upd_vrid,
                            value=ip_version)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_secondary_ips(self):
        # vrrp 10 ip 10.10.4.21 secondary
        # vrrp 10 ip 10.10.4.22 secondary
        # vrrp 10 ip 10.10.4.23 secondary

        curr1 = '10.10.4.21'
        curr2 = '10.10.4.22'
        curr3 = '10.10.4.23'
        curr4 = '10.10.4.24'

        new1 = '10.10.4.31'
        new2 = '10.10.4.32'
        new3 = '10.10.4.33'
        new4 = curr4

        # Test set_secondary_ips gives properly formatted commands
        cases = [
            ([new1, new2, new3],
             {'add': [new1, new2, new3],
              'remove': [curr1, curr2, curr3, curr4]}),
            ([new1, new2, new4],
             {'add': [new1, new2],
              'remove': [curr1, curr2, curr3]}),
            ([],
             {'add': [],
              'remove': [curr1, curr2, curr3, curr4]}),
        ]

        for (secondary_ips, cmd_dict) in cases:
            cmds = []
            for sec_ip in cmd_dict['add']:
                cmds.append("vrrp %d ip %s secondary" % (upd_vrid, sec_ip))

            for sec_ip in cmd_dict['remove']:
                cmds.append("no vrrp %d ip %s secondary" % (upd_vrid, sec_ip))

            func = function('set_secondary_ips', upd_intf, upd_vrid,
                            secondary_ips)
            exp_cmds = [upd_cmd] + sorted(cmds)
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = [
            [new1, new2, 'abc'],
            [new1, new2, '10.10.10'],
            [new1, new2, True],
        ]

        for secondary_ips in cases:
            func = function('set_secondary_ips', upd_intf, upd_vrid,
                            secondary_ips)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_timers_advertise(self):
        # vrrp 10 timers advertise 3

        # Test set_timers_advertise gives properly formatted commands
        cases = [
            (50, None, None, 'vrrp %d timers advertise 50' % upd_vrid),
            (None, None, True, 'default vrrp %d timers advertise' % upd_vrid),
            (None, True, True, 'default vrrp %d timers advertise' % upd_vrid),
            (None, True, None, 'no vrrp %d timers advertise' % upd_vrid),
        ]

        for (timers_advertise, disable, default, cmd) in cases:
            func = function('set_timers_advertise', upd_intf, upd_vrid,
                            value=timers_advertise, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = [256, 0, 'a']

        for timers_advertise in cases:
            func = function('set_timers_advertise', upd_intf, upd_vrid,
                            value=timers_advertise)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_mac_addr_adv_interval(self):
        # vrrp 10 mac-address advertisement-interval 30

        # Test set_timers_advertise gives properly formatted commands
        maadvint = 'mac-address advertisement-interval'
        cases = [
            (50, None, None, 'vrrp %d %s 50' % (upd_vrid, maadvint)),
            (None, None, True, 'default vrrp %d %s' % (upd_vrid, maadvint)),
            (None, True, True, 'default vrrp %d %s' % (upd_vrid, maadvint)),
            (None, True, None, 'no vrrp %d %s' % (upd_vrid, maadvint)),
        ]

        for (mac_addr_adv_interval, disable, default, cmd) in cases:
            func = function('set_mac_addr_adv_interval', upd_intf, upd_vrid,
                            value=mac_addr_adv_interval, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 10000]

        for mac_addr_adv_interval in cases:
            func = function('set_mac_addr_adv_interval', upd_intf, upd_vrid,
                            value=mac_addr_adv_interval)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_preempt(self):
        # vrrp 10 preempt

        # Test set_description gives properly formatted commands
        cases = [
            (False, None, None, 'no vrrp %d preempt' % upd_vrid),
            (True, None, None, 'vrrp %d preempt' % upd_vrid),
            (None, None, True, 'default vrrp %d preempt' % upd_vrid),
            (None, True, True, 'default vrrp %d preempt' % upd_vrid),
            (None, True, None, 'no vrrp %d preempt' % upd_vrid),
        ]

        for (preempt, disable, default, cmd) in cases:
            func = function('set_preempt', upd_intf, upd_vrid,
                            value=preempt, disable=disable, default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 5]

        for preempt in cases:
            func = function('set_preempt', upd_intf, upd_vrid,
                            value=preempt)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_preempt_delay_min(self):
        # vrrp 10 preempt delay minimum 0

        # Test set_preempt_delay_min gives properly formatted commands
        cases = [
            (2500, None, None,
             'vrrp %d preempt delay minimum 2500' % upd_vrid),
            (None, None, True,
             'default vrrp %d preempt delay minimum' % upd_vrid),
            (None, True, True,
             'default vrrp %d preempt delay minimum' % upd_vrid),
            (None, True, None, 'no vrrp %d preempt delay minimum' % upd_vrid),
        ]

        for (preempt_delay_min, disable, default, cmd) in cases:
            func = function('set_preempt_delay_min', upd_intf, upd_vrid,
                            value=preempt_delay_min, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 3601]

        for preempt_delay_min in cases:
            func = function('set_preempt_delay_min', upd_intf, upd_vrid,
                            value=preempt_delay_min)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_preempt_delay_reload(self):
        # vrrp 10 preempt delay reload 0

        # Test set_preempt_delay_min gives properly formatted commands
        cases = [
            (1500, None, None,
             'vrrp %d preempt delay reload 1500' % upd_vrid),
            (None, None, True,
             'default vrrp %d preempt delay reload' % upd_vrid),
            (None, True, True,
             'default vrrp %d preempt delay reload' % upd_vrid),
            (None, True, None, 'no vrrp %d preempt delay reload' % upd_vrid),
        ]

        for (preempt_delay_reload, disable, default, cmd) in cases:
            func = function('set_preempt_delay_reload', upd_intf, upd_vrid,
                            value=preempt_delay_reload, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 3601]

        for preempt_delay_reload in cases:
            func = function('set_preempt_delay_reload', upd_intf, upd_vrid,
                            value=preempt_delay_reload)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_delay_reload(self):
        # vrrp 10 delay reload 0

        # Test set_delay_min gives properly formatted commands
        cases = [
            (1750, None, None, 'vrrp %d delay reload 1750' % upd_vrid),
            (None, None, True, 'default vrrp %d delay reload' % upd_vrid),
            (None, True, True, 'default vrrp %d delay reload' % upd_vrid),
            (None, True, None, 'no vrrp %d delay reload' % upd_vrid),
        ]

        for (delay_reload, disable, default, cmd) in cases:
            func = function('set_delay_reload', upd_intf, upd_vrid,
                            value=delay_reload, disable=disable,
                            default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = ['a', 3601]

        for delay_reload in cases:
            func = function('set_delay_reload', upd_intf, upd_vrid,
                            value=delay_reload)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_tracks(self):
        # vrrp 10 track Ethernet1 decrement 10
        # vrrp 10 track Ethernet1 shutdown
        # vrrp 10 track Ethernet2 decrement 50
        # vrrp 10 track Ethernet2 shutdown
        # vrrp 10 track Ethernet11 decrement 75
        # vrrp 10 track Ethernet11 shutdown

        curr1 = {'name': 'Ethernet1', 'action': 'decrement', 'amount': 10}
        curr2 = {'name': 'Ethernet1', 'action': 'shutdown'}
        curr3 = {'name': 'Ethernet2', 'action': 'decrement', 'amount': 50}
        curr4 = {'name': 'Ethernet2', 'action': 'shutdown'}
        curr5 = {'name': 'Ethernet11', 'action': 'decrement', 'amount': 75}
        curr6 = {'name': 'Ethernet11', 'action': 'shutdown'}

        new1 = curr1
        new2 = {'name': 'Ethernet2', 'action': 'decrement', 'amount': 49}
        new3 = {'name': 'Ethernet3', 'action': 'shutdown'}
        new4 = {'name': 'Ethernet4', 'action': 'decrement', 'amount': 50}
        new5 = {'name': 'Ethernet5', 'action': 'shutdown'}
        new6 = {'name': 'Ethernet9', 'action': 'decrement', 'amount': 75}

        # Test set_track gives properly formatted commands
        cases = [
            ([curr6, curr5, new1, new2],
             {'add': [new2],
              'remove': [curr2, curr3, curr4]}),
            ([new2, new3, new4, new5, new6],
             {'add': [new2, new3, new4, new5, new6],
              'remove': [curr1, curr2, curr3, curr4, curr5, curr6]}),
            ([],
             {'add': [],
              'remove': [curr1, curr2, curr3, curr4, curr5, curr6]}),
        ]

        for (tracks, cmd_dict) in cases:
            cmds = []
            for add in cmd_dict['add']:
                tr_obj = add['name']
                action = add['action']
                amount = add['amount'] if 'amount' in add else ''
                cmd = ("vrrp %d track %s %s %s"
                       % (upd_vrid, tr_obj, action, amount))
                cmds.append(cmd.rstrip())

            for remove in cmd_dict['remove']:
                tr_obj = remove['name']
                action = remove['action']
                amount = remove['amount'] if 'amount' in remove else ''
                cmd = ("no vrrp %d track %s %s %s"
                       % (upd_vrid, tr_obj, action, amount))
                cmds.append(cmd.rstrip())

            func = function('set_tracks', upd_intf, upd_vrid, tracks)
            exp_cmds = [upd_cmd] + sorted(cmds)
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError by entering invalid parameters
        cases = [
            [{'name': 'Ethernet1', 'action': 'disable', 'amount': 10}],
            [{'name': 'Ethernet1', 'action': 'decrement', 'amount': True}],
            [{'name': 'Ethernet1', 'action': 'shutdown', 'amount': 10}],
            [{'action': 'decrement', 'amount': 10}],
            [{'name': 'Ethernet1', 'action': 'decrement',
              'amount': 10, 'bad': 1}],
        ]

        for tracks in cases:
            func = function('set_tracks', upd_intf, upd_vrid, tracks)
            self.eapi_exception_config_test(func, ValueError)

    def test_set_bfd_ip(self):
        # no vrrp 10 bfd ip

        bfd_addr = '10.10.4.101'

        # Test bfd_ip gives properly formatted commands
        cases = [
            (bfd_addr, None, None, 'vrrp %d bfd ip %s' % (upd_vrid, bfd_addr)),
            (None, True, None, 'no vrrp %d bfd ip' % upd_vrid),
            (None, None, True, 'default vrrp %d bfd ip' % upd_vrid),
            (None, True, True, 'default vrrp %d bfd ip' % upd_vrid),
        ]

        for (bfd_ip, disable, default, cmd) in cases:
            func = function('set_bfd_ip', upd_intf, upd_vrid,
                            value=bfd_ip, disable=disable, default=default)
            exp_cmds = [upd_cmd] + [cmd]
            self.eapi_positive_config_test(func, exp_cmds)

        # Test raising ValueError from invalid parameters
        cases = ['abc', 500, '101.101']

        for bfd_ip in cases:
            func = function('set_bfd_ip', upd_intf, upd_vrid,
                            value=bfd_ip)
            self.eapi_exception_config_test(func, ValueError)

    def test_vrconf_format(self):
        # Test the function to format a vrrp configuration to
        # match the output from get/getall
        vrconf = {
            'priority': None,
            'timers_advertise': None,
            'mac_addr_adv_interval': None,
            'preempt': 'default',
            'preempt_delay_min': None,
            'preempt_delay_reload': None,
            'delay_reload': None,
            'primary_ip': None,
            'secondary_ip': ['10.10.4.22', '10.10.4.21'],
            'description': None,
            'enable': True,
            'track': [
                {'name': 'Ethernet1', 'action': 'shutdown'},
                {'name': 'Ethernet1', 'action': 'decrement', 'amount': 10},
            ],
            'bfd_ip': None,
            'ip_version': None}

        fixed = {
            'priority': 100,
            'timers_advertise': 1,
            'mac_addr_adv_interval': 30,
            'preempt': False,
            'preempt_delay_min': 0,
            'preempt_delay_reload': 0,
            'delay_reload': 0,
            'primary_ip': '0.0.0.0',
            'secondary_ip': ['10.10.4.21', '10.10.4.22'],
            'description': None,
            'enable': True,
            'track': [
                {'name': 'Ethernet1', 'action': 'decrement', 'amount': 10},
                {'name': 'Ethernet1', 'action': 'shutdown'},
            ],
            'bfd_ip': '',
            'ip_version': 2}

        # Get the vrconf_format method from the library
        func = getattr(self.instance, 'vrconf_format')
        # Call the method with the vrconf dictionary
        result = func(vrconf)
        # And verify the result is a properly formatted dictionary
        self.assertEqual(fixed, result)


if __name__ == '__main__':
    unittest.main()