File: test_api_staticroute.py

package info (click to toggle)
pyeapi 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,564 kB
  • sloc: python: 10,906; makefile: 197
file content (304 lines) | stat: -rw-r--r-- 11,572 bytes parent folder | download | duplicates (5)
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
#
# Copyright (c) 2014, 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 random import choice
from testlib import get_fixture, function, random_int, random_string
from testlib import EapiConfigUnitTest

import pyeapi.api.staticroute

IP_DESTS = ['11.111.11.0/24', '222.22.222.0/24', '33.34.35.0/24']
NEXT_HOPS = [('Ethernet1', '3.3.3.3'), ('Ethernet2', '2.2.2.2'),
             ('Null0', None), ('44.44.44.0', None)]
DISTANCES = TAGS = ROUTE_NAMES = [None, True]


class TestApiStaticroute(EapiConfigUnitTest):

    def __init__(self, *args, **kwargs):
        super(TestApiStaticroute, self).__init__(*args, **kwargs)
        self.instance = pyeapi.api.staticroute.StaticRoute(None)
        self.config = open(get_fixture('running_config.text')).read()

    def test_instance(self):
        result = pyeapi.api.staticroute.instance(None)
        self.assertIsInstance(result, pyeapi.api.staticroute.StaticRoute)

    def test_get(self):
        # Test retrieval of a specific static route entry
        # Assumes running_config.text file contains the following
        # ip route specifications, and that no additional routes
        # are specified.

        # ip route 0.0.0.0/0 192.68.1.254 1 tag 0
        # ip route 1.2.3.0/24 Ethernet1 1.1.1.1 1 tag 1 name test1
        # ip route 1.2.3.0/24 Ethernet1 1.1.1.1 10 tag 1 name test1
        # ip route 1.2.3.0/24 Ethernet1 10.1.1.1 20 tag 1 name test1

        # Get the route(s) for ip_dest 0.0.0.0/24
        ip_dest = '0.0.0.0/0'
        routes = {
            '192.68.1.254': {
                None: {
                    1: {'route_name': None,
                        'tag': 0}
                }
            }
        }
        result = self.instance.get(ip_dest)
        self.assertEqual(result, routes)

        # Get the route(s) for ip_dest 1.2.3.0/24
        ip_dest = '1.2.3.0/24'
        routes = {
            'Ethernet1': {
                '1.1.1.1': {
                    1: {
                        'route_name': 'test1',
                        'tag': 1},
                    10: {
                        'route_name': 'test1',
                        'tag': 1}
                },
                '10.1.1.1': {
                    20: {
                        'route_name': 'test1',
                        'tag': 1}
                }
            }
        }
        result = self.instance.get(ip_dest)
        self.assertEqual(result, routes)

    def test_getall(self):
        # Test retrieval of all static route entries
        # Assumes running_config.text file contains the following
        # ip route specifications, and that no additional routes
        # are specified.

        # ip route 0.0.0.0/0 192.68.1.254 1 tag 0
        # ip route 1.2.3.0/24 Ethernet1 1.1.1.1 1 tag 1 name test1
        # ip route 1.2.3.0/24 Ethernet1 1.1.1.1 10 tag 1 name test1
        # ip route 1.2.3.0/24 Ethernet1 10.1.1.1 20 tag 1 name test1

        routes = {
            '0.0.0.0/0': {
                '192.68.1.254': {
                    None: {
                        1: {'route_name': None,
                            'tag': 0}
                    }
                }
            },
            '1.2.3.0/24': {
                'Ethernet1': {
                    '1.1.1.1': {
                        1: {
                            'route_name': 'test1',
                            'tag': 1},
                        10: {
                            'route_name': 'test1',
                            'tag': 1}
                    },
                    '10.1.1.1': {
                        20: {
                            'route_name': 'test1',
                            'tag': 1}
                    }
                }
            }
        }

        self.maxDiff = None
        result = self.instance.getall()
        self.assertEqual(result, routes)

    def test_create(self):
        # Test passing in a full set of parameters to 'create'
        # Some parameters may be not set: None
        for ip_dest in IP_DESTS:
            # Get the parameters for the call
            (next_hop, next_hop_ip) = choice(NEXT_HOPS)
            distance = choice(DISTANCES)
            if distance:
                distance = random_int(0, 255)
            tag = choice(TAGS)
            if tag:
                tag = random_int(0, 255)
            route_name = choice(ROUTE_NAMES)
            if route_name:
                route_name = random_string(minchar=4, maxchar=10)

            func = function('create', ip_dest, next_hop,
                            next_hop_ip=next_hop_ip,
                            distance=distance,
                            tag=tag,
                            route_name=route_name)

            # Build the expected string for comparison
            # A value of None will default to an empty string, and
            # add the tag or name keywords where appropriate
            cmd_next_hop_ip = cmd_distance = cmd_tag = cmd_route_name = ''
            if next_hop_ip is not None:
                cmd_next_hop_ip = " %s" % next_hop_ip
            if distance is not None:
                cmd_distance = " %d" % distance
            if tag is not None:
                cmd_tag = " tag %d" % tag
            if route_name is not None:
                cmd_route_name = " name %s" % route_name
            cmds = "ip route %s %s%s%s%s%s" % \
                   (ip_dest, next_hop, cmd_next_hop_ip, cmd_distance,
                    cmd_tag, cmd_route_name)

            self.eapi_positive_config_test(func, cmds)

    def test_delete(self):
        # Test passing in a full set of parameters to 'delete'
        # Some parameters may be not set: None
        for ip_dest in IP_DESTS:
            (next_hop, next_hop_ip) = choice(NEXT_HOPS)
            distance = choice(DISTANCES)
            if distance:
                distance = random_int(0, 255)
            tag = choice(TAGS)
            if tag:
                tag = random_int(0, 255)
            route_name = choice(ROUTE_NAMES)
            if route_name:
                route_name = random_string(minchar=4, maxchar=10)

            func = function('delete', ip_dest, next_hop,
                            next_hop_ip=next_hop_ip,
                            distance=distance,
                            tag=tag,
                            route_name=route_name)

            # Build the expected string for comparison
            # A value of None will default to an empty string, and
            # add the tag or name keywords where appropriate
            cmd_next_hop_ip = cmd_distance = cmd_tag = cmd_route_name = ''
            if next_hop_ip is not None:
                cmd_next_hop_ip = " %s" % next_hop_ip
            if distance is not None:
                cmd_distance = " %d" % distance
            if tag is not None:
                cmd_tag = " tag %d" % tag
            if route_name is not None:
                cmd_route_name = " name %s" % route_name
            cmds = "no ip route %s %s%s%s%s%s" % \
                   (ip_dest, next_hop, cmd_next_hop_ip, cmd_distance,
                    cmd_tag, cmd_route_name)

            self.eapi_positive_config_test(func, cmds)

    def test_default(self):
        # Test passing in a full set of parameters to 'default'
        # Some parameters may be not set: None
        for ip_dest in IP_DESTS:
            (next_hop, next_hop_ip) = choice(NEXT_HOPS)
            distance = choice(DISTANCES)
            if distance:
                distance = random_int(0, 255)
            tag = choice(TAGS)
            if tag:
                tag = random_int(0, 255)
            route_name = choice(ROUTE_NAMES)
            if route_name:
                route_name = random_string(minchar=4, maxchar=10)

            func = function('default', ip_dest, next_hop,
                            next_hop_ip=next_hop_ip,
                            distance=distance,
                            tag=tag,
                            route_name=route_name)

            # Build the expected string for comparison
            # A value of None will default to an empty string, and
            # add the tag or name keywords where appropriate
            cmd_next_hop_ip = cmd_distance = cmd_tag = cmd_route_name = ''
            if next_hop_ip is not None:
                cmd_next_hop_ip = " %s" % next_hop_ip
            if distance is not None:
                cmd_distance = " %d" % distance
            if tag is not None:
                cmd_tag = " tag %d" % tag
            if route_name is not None:
                cmd_route_name = " name %s" % route_name
            cmds = "default ip route %s %s%s%s%s%s" % \
                   (ip_dest, next_hop, cmd_next_hop_ip, cmd_distance,
                    cmd_tag, cmd_route_name)

            self.eapi_positive_config_test(func, cmds)

    def test_set_tag(self):
        # Test passing in a new tag to the set_tag function
        ip_dest = '1.2.3.0/24'
        next_hop = 'Ethernet1'
        next_hop_ip = '1.1.1.1'
        distance = 10
        tag = '99'

        func = function('set_tag', ip_dest, next_hop,
                        next_hop_ip=next_hop_ip,
                        distance=distance, tag=tag)

        cmd = "ip route %s %s %s %s tag %s" % \
            (ip_dest, next_hop, next_hop_ip, distance, tag)

        self.eapi_positive_config_test(func, cmd)

    def test_set_route_name(self):
        # Test passing in a new tag to the set_tag function
        ip_dest = '1.2.3.0/24'
        next_hop = 'Ethernet1'
        next_hop_ip = '1.1.1.1'
        distance = 10
        route_name = 'test99'

        func = function('set_route_name', ip_dest, next_hop,
                        next_hop_ip=next_hop_ip,
                        distance=distance, route_name=route_name)

        cmd = "ip route %s %s %s %s name %s" % \
            (ip_dest, next_hop, next_hop_ip, distance, route_name)

        self.eapi_positive_config_test(func, cmd)


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