1#
2# Copyright (c) 2015, Arista Networks, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#   Redistributions of source code must retain the above copyright notice,
10#   this list of conditions and the following disclaimer.
11#
12#   Redistributions in binary form must reproduce the above copyright
13#   notice, this list of conditions and the following disclaimer in the
14#   documentation and/or other materials provided with the distribution.
15#
16#   Neither the name of Arista Networks nor the names of its
17#   contributors may be used to endorse or promote products derived from
18#   this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
24# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#
32import os
33import unittest
34
35import sys
36sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))
37
38from systestlib import DutSystemTest
39from testlib import random_string
40
41
42class TestApiRoutemaps(DutSystemTest):
43
44    def test_get(self):
45        for dut in self.duts:
46            dut.config(['no route-map TEST deny 10',
47                        'route-map TEST deny 10',
48                        'set weight 100',
49                        'match tag 50'])
50            response = dut.api('routemaps').get('TEST')
51            self.assertIsNotNone(response)
52
53    def test_get_none(self):
54        for dut in self.duts:
55            dut.config('no route-map TEST deny 10')
56            result = dut.api('routemaps').get('TEST')
57            self.assertIsNone(result)
58
59    def test_getall(self):
60        for dut in self.duts:
61            dut.config(['no route-map TEST deny 10',
62                        'route-map TEST deny 10',
63                        'set weight 100',
64                        'no route-map TEST-2 permit 50',
65                        'route-map TEST-2 permit 50',
66                        'match tag 50'])
67            result = dut.api('routemaps').getall()
68            self.assertIn(('TEST'), result)
69            self.assertIn(('TEST-2'), result)
70
71    def test_create(self):
72        for dut in self.duts:
73            dut.config(['no route-map TEST deny 10'])
74            api = dut.api('routemaps')
75            self.assertIsNone(api.get('TEST'))
76            result = dut.api('routemaps').create('TEST', 'deny', 10)
77            self.assertTrue(result)
78            self.assertIsNotNone(api.get('TEST'))
79            dut.config(['no route-map TEST deny 10'])
80
81    def test_create_with_hyphen(self):
82        for dut in self.duts:
83            dut.config(['no route-map TEST-1 deny 10'])
84            api = dut.api('routemaps')
85            self.assertIsNone(api.get('TEST-1'))
86            result = dut.api('routemaps').create('TEST-1', 'deny', 10)
87            self.assertTrue(result)
88            self.assertIsNotNone(api.get('TEST-1'))
89            dut.config(['no route-map TEST-1 deny 10'])
90
91    def test_create_with_underscore(self):
92        for dut in self.duts:
93            dut.config(['no route-map TEST_1 deny 10'])
94            api = dut.api('routemaps')
95            self.assertIsNone(api.get('TEST_1'))
96            result = dut.api('routemaps').create('TEST_1', 'deny', 10)
97            self.assertTrue(result)
98            self.assertIsNotNone(api.get('TEST_1'))
99            dut.config(['no route-map TEST_1 deny 10'])
100
101    def test_delete(self):
102        for dut in self.duts:
103            dut.config(['no route-map TEST deny 10',
104                        'route-map TEST deny 10',
105                        'set weight 100'])
106            api = dut.api('routemaps')
107            self.assertIsNotNone(api.get('TEST'))
108            result = dut.api('routemaps').delete('TEST', 'deny', 10)
109            self.assertTrue(result)
110            self.assertIsNone(api.get('TEST'))
111
112    def test_default(self):
113        for dut in self.duts:
114            dut.config(['no route-map TEST deny 10',
115                        'route-map TEST deny 10',
116                        'set weight 100'])
117            api = dut.api('routemaps')
118            self.assertIsNotNone(api.get('TEST'))
119            result = dut.api('routemaps').default('TEST', 'deny', 10)
120            self.assertTrue(result)
121            self.assertIsNone(api.get('TEST'))
122
123    def test_set_description(self):
124        for dut in self.duts:
125            text = random_string()
126            dut.config(['no route-map TEST deny 10',
127                        'route-map TEST deny 10'])
128            api = dut.api('routemaps')
129            self.assertNotIn('description %s' % text,
130                             api.get_block('route-map TEST deny 10'))
131            result = dut.api('routemaps').set_description('TEST', 'deny', 10,
132                                                          text)
133            self.assertTrue(result)
134            self.assertIn('description %s' % text,
135                          api.get_block('route-map TEST deny 10'))
136
137    def test_set_match_statements(self):
138        for dut in self.duts:
139            dut.config(['no route-map TEST deny 10',
140                        'route-map TEST deny 10'])
141            api = dut.api('routemaps')
142            self.assertNotIn('match as 100',
143                             api.get_block('route-map TEST deny 10'))
144            result = dut.api('routemaps').set_match_statements('TEST', 'deny',
145                                                               10, ['as 100'])
146            self.assertTrue(result)
147            self.assertIn('match as 100',
148                          api.get_block('route-map TEST deny 10'))
149
150    def test_update_match_statement(self):
151        for dut in self.duts:
152            dut.config(['no route-map TEST deny 10',
153                        'route-map TEST deny 10',
154                        'match as 100'])
155            api = dut.api('routemaps')
156            self.assertIn('match as 100',
157                          api.get_block('route-map TEST deny 10'))
158            result = dut.api('routemaps').set_match_statements('TEST', 'deny',
159                                                               10, ['as 200'])
160            self.assertTrue(result)
161            self.assertNotIn('match as 100',
162                             api.get_block('route-map TEST deny 10'))
163            self.assertIn('match as 200',
164                          api.get_block('route-map TEST deny 10'))
165
166    def test_remove_match_statement(self):
167        for dut in self.duts:
168            dut.config(['no route-map TEST deny 10',
169                        'route-map TEST deny 10',
170                        'match as 100'])
171            api = dut.api('routemaps')
172            self.assertIn('match as 100',
173                          api.get_block('route-map TEST deny 10'))
174            result = dut.api('routemaps').set_match_statements('TEST', 'deny',
175                                                               10, ['tag 50'])
176            self.assertTrue(result)
177            self.assertNotIn('match as 100',
178                             api.get_block('route-map TEST deny 10'))
179            self.assertIn('match tag 50',
180                          api.get_block('route-map TEST deny 10'))
181
182    def test_set_set_statements(self):
183        for dut in self.duts:
184            dut.config(['no route-map TEST deny 10',
185                        'route-map TEST deny 10'])
186            api = dut.api('routemaps')
187            self.assertNotIn('set weight 100',
188                             api.get_block('route-map TEST deny 10'))
189            result = dut.api('routemaps').set_set_statements('TEST', 'deny',
190                                                             10, ['weight 100'])
191            self.assertTrue(result)
192            self.assertIn('set weight 100',
193                          api.get_block('route-map TEST deny 10'))
194
195    def test_update_set_statement(self):
196        for dut in self.duts:
197            dut.config(['no route-map TEST deny 10',
198                        'route-map TEST deny 10',
199                        'set weight 100'])
200            api = dut.api('routemaps')
201            self.assertIn('set weight 100',
202                          api.get_block('route-map TEST deny 10'))
203            result = dut.api('routemaps').set_set_statements('TEST', 'deny',
204                                                             10, ['weight 200'])
205            self.assertTrue(result)
206            self.assertNotIn('set weight 100',
207                             api.get_block('route-map TEST deny 10'))
208            self.assertIn('set weight 200',
209                          api.get_block('route-map TEST deny 10'))
210
211    def test_remove_set_statement(self):
212        for dut in self.duts:
213            dut.config(['no route-map TEST deny 10',
214                        'route-map TEST deny 10',
215                        'set weight 100'])
216            api = dut.api('routemaps')
217            self.assertIn('set weight 100',
218                          api.get_block('route-map TEST deny 10'))
219            result = dut.api('routemaps').set_set_statements('TEST', 'deny',
220                                                             10, ['tag 50'])
221            self.assertTrue(result)
222            self.assertNotIn('set weight 100',
223                             api.get_block('route-map TEST deny 10'))
224            self.assertIn('set tag 50',
225                          api.get_block('route-map TEST deny 10'))
226
227    def test_set_continue(self):
228        for dut in self.duts:
229            dut.config(['no route-map TEST deny 10',
230                        'route-map TEST deny 10'])
231            api = dut.api('routemaps')
232            self.assertNotIn('continue',
233                             api.get_block('route-map TEST deny 10'))
234            result = dut.api('routemaps').set_continue('TEST', 'deny', 10, 100)
235            self.assertTrue(result)
236            self.assertEqual(100, api.get('TEST')['deny'][10]['continue'])
237
238    def test_update_continue(self):
239        for dut in self.duts:
240            dut.config(['no route-map TEST deny 10',
241                        'route-map TEST deny 10',
242                        'continue 30'])
243            api = dut.api('routemaps')
244            self.assertIn('continue 30',
245                          api.get_block('route-map TEST deny 10'))
246            result = dut.api('routemaps').set_continue('TEST', 'deny', 10, 60)
247            self.assertTrue(result)
248            self.assertEqual(60, api.get('TEST')['deny'][10]['continue'])
249
250    def test_default_continue(self):
251        for dut in self.duts:
252            dut.config(['no route-map TEST deny 10',
253                        'route-map TEST deny 10',
254                        'continue 100'])
255            api = dut.api('routemaps')
256            self.assertIn('continue 100',
257                          api.get_block('route-map TEST deny 10'))
258            result = dut.api('routemaps').set_continue('TEST', 'deny', 10,
259                                                       default=True)
260            self.assertTrue(result)
261            self.assertEqual(None, api.get('TEST')['deny'][10]['continue'])
262
263    def test_negate_continue(self):
264        for dut in self.duts:
265            dut.config(['no route-map TEST deny 10',
266                        'route-map TEST deny 10',
267                        'continue 100'])
268            api = dut.api('routemaps')
269            self.assertIn('continue 100',
270                          api.get_block('route-map TEST deny 10'))
271            result = dut.api('routemaps').set_continue('TEST', 'deny', 10,
272                                                       disable=True)
273            self.assertTrue(result)
274            self.assertEqual(None, api.get('TEST')['deny'][10]['continue'])
275
276
277if __name__ == '__main__':
278    unittest.main()
279