File: test_api_acl.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 (275 lines) | stat: -rw-r--r-- 11,353 bytes parent folder | download | duplicates (4)
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
#
# 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 testlib import get_fixture, function
from testlib import EapiConfigUnitTest

import pyeapi.api.acl

class TestApiAclFunctions(unittest.TestCase):

    def test_mask_to_prefixlen(self):
        result = pyeapi.api.acl.mask_to_prefixlen('255.255.255.0')
        self.assertEqual(result, 24)

    def test_prefixlen_to_mask(self):
        result = pyeapi.api.acl.prefixlen_to_mask(24)
        self.assertEqual(result, '255.255.255.0')


class TestApiAcls(EapiConfigUnitTest):

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

    def test_instance(self):
        result = pyeapi.api.acl.instance(None)
        self.assertIsInstance(result, pyeapi.api.acl.Acls)

    def test_getall(self):
        result = self.instance.getall()
        self.assertIsInstance(result, dict)
        self.assertIn('exttest', result['extended'])
        self.assertIn('test', result['standard'])

    def test_get_not_configured(self):
        self.assertIsNone(self.instance.get('unconfigured'))

    def test_get(self):
        result = self.instance.get('test')
        keys = ['name', 'type', 'entries']
        self.assertEqual(sorted(keys), sorted(result.keys()))

    def test_get_instance(self):
        result = self.instance.get_instance('test')
        self.assertIsInstance(result, pyeapi.api.acl.StandardAcls)
        self.instance._instances['test'] = result
        result = self.instance.get_instance('exttest')
        self.assertIsInstance(result, pyeapi.api.acl.ExtendedAcls)
        result = self.instance.get_instance('unconfigured')
        self.assertIsInstance(result, dict)
        self.assertIsNone(result['unconfigured'])
        result = self.instance.get_instance('test')
        self.assertIsInstance(result, pyeapi.api.acl.StandardAcls)
        self.assertEqual(len(self.instance._instances), 2)

    def test_create_instance_standard(self):
        result = self.instance.create_instance('test', 'standard')
        self.assertIsInstance(result, pyeapi.api.acl.StandardAcls)
        self.assertEqual(len(self.instance._instances), 1)

    def test_create_instance_extended(self):
        result = self.instance.create_instance('exttest', 'extended')
        self.assertIsInstance(result, pyeapi.api.acl.ExtendedAcls)
        self.assertEqual(len(self.instance._instances), 1)

    def test_create_standard(self):
        cmds = 'ip access-list standard test'
        func = function('create', 'test')
        self.eapi_positive_config_test(func, cmds)

    def test_create_extended(self):
        cmds = 'ip access-list exttest'
        func = function('create', 'exttest', 'extended')
        self.eapi_positive_config_test(func, cmds)

    def test_create_unknown_type_creates_standard(self):
        cmds = 'ip access-list standard test'
        func = function('create', 'test', 'unknown')
        self.eapi_positive_config_test(func, cmds)

    def test_proxy_method_success(self):
        result = self.instance.remove_entry('test', '10')
        self.assertTrue(result)

    def test_proxy_method_raises_attribute_error(self):
        with self.assertRaises(AttributeError):
            self.instance.nonmethod('test', '10')


class TestApiStandardAcls(EapiConfigUnitTest):

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

    def test_get(self):
        result = self.instance.get('test')
        keys = ['name', 'type', 'entries']
        self.assertEqual(sorted(keys), sorted(result.keys()))
        self.assertEqual(result['type'], 'standard')

    def test_get_not_configured(self):
        self.assertIsNone(self.instance.get('unconfigured'))

    def test_acl_functions(self):
        for name in ['create', 'delete', 'default']:
            if name == 'create':
                cmds = 'ip access-list standard test'
            elif name == 'delete':
                cmds = 'no ip access-list standard test'
            elif name == 'default':
                cmds = 'default ip access-list standard test'
            func = function(name, 'test')
            self.eapi_positive_config_test(func, cmds)

    def test_update_entry(self):
        cmds = ['ip access-list standard test', 'no 10',
                '10 permit 0.0.0.0/32 log', 'exit']
        func = function('update_entry', 'test', '10', 'permit', '0.0.0.0',
                        '32', True)
        self.eapi_positive_config_test(func, cmds)

    def test_update_entry_no_log(self):
        cmds = ['ip access-list standard test', 'no 10',
                '10 permit 0.0.0.0/32', 'exit']
        func = function('update_entry', 'test', '10', 'permit', '0.0.0.0',
                        '32')
        self.eapi_positive_config_test(func, cmds)

    def test_remove_entry(self):
        cmds = ['ip access-list standard test', 'no 10', 'exit']
        func = function('remove_entry', 'test', '10')
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry(self):
        cmds = ['ip access-list standard test', 'permit 0.0.0.0/32 log',
                'exit']
        func = function('add_entry', 'test', 'permit', '0.0.0.0',
                        '32', True)
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry_no_log(self):
        cmds = ['ip access-list standard test', 'permit 0.0.0.0/32',
                'exit']
        func = function('add_entry', 'test', 'permit', '0.0.0.0',
                        '32')
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry_with_seqno(self):
        cmds = ['ip access-list standard test', '30 permit 0.0.0.0/32 log',
                'exit']
        func = function('add_entry', 'test', 'permit', '0.0.0.0',
                        '32', True, 30)
        self.eapi_positive_config_test(func, cmds)


class TestApiExtendedAcls(EapiConfigUnitTest):

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

    def test_get(self):
        result = self.instance.get('exttest')
        keys = ['name', 'type', 'entries']
        self.assertEqual(sorted(keys), sorted(result.keys()))
        self.assertEqual(result['type'], 'extended')
        self.assertIn('entries', result)
        self.assertIn('50', result['entries'])
        entry = dict(action='permit', dstaddr='1.1.1.2', dstlen=None,
                     dstport=None, other=None, protocol='ip', srcaddr='any',
                     srclen=None, srcport=None)
        self.assertEqual(entry, result['entries']['50'])
        self.assertIn('70', result['entries'])
        entry = dict(action='permit', dstaddr='3.3.3.3', dstlen=None,
                     dstport='lt ipp', protocol='tcp', srcaddr='8.8.8.0',
                     other='urg ttl eq 24 fragments tracked log',
                     srclen='24', srcport='neq irc')
        self.assertEqual(entry, result['entries']['70'])

    def test_get_not_configured(self):
        self.assertIsNone(self.instance.get('unconfigured'))

    def test_acl_functions(self):
        for name in ['create', 'delete', 'default']:
            if name == 'create':
                cmds = 'ip access-list exttest'
            elif name == 'delete':
                cmds = 'no ip access-list exttest'
            elif name == 'default':
                cmds = 'default ip access-list exttest'
            func = function(name, 'exttest')
            self.eapi_positive_config_test(func, cmds)

    def test_update_entry(self):
        cmds = ['ip access-list exttest', 'no 10',
                '10 permit ip 0.0.0.0/32 1.1.1.1/32 log', 'exit']
        func = function('update_entry', 'exttest', '10', 'permit', 'ip',
                        '0.0.0.0', '32', '1.1.1.1', '32', True)
        self.eapi_positive_config_test(func, cmds)

    def test_update_entry_no_log(self):
        cmds = ['ip access-list exttest', 'no 10',
                '10 permit ip 0.0.0.0/32 1.1.1.1/32', 'exit']
        func = function('update_entry', 'exttest', '10', 'permit', 'ip',
                        '0.0.0.0', '32', '1.1.1.1', '32')
        self.eapi_positive_config_test(func, cmds)

    def test_remove_entry(self):
        cmds = ['ip access-list exttest', 'no 10', 'exit']
        func = function('remove_entry', 'exttest', '10')
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry(self):
        cmds = ['ip access-list exttest',
                'permit ip 0.0.0.0/32 1.1.1.1/32 log', 'exit']
        func = function('add_entry', 'exttest', 'permit', 'ip', '0.0.0.0',
                        '32', '1.1.1.1', '32', True)
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry_no_log(self):
        cmds = ['ip access-list exttest', 'permit ip 0.0.0.0/32 1.1.1.1/32',
                'exit']
        func = function('add_entry', 'exttest', 'permit', 'ip', '0.0.0.0',
                        '32', '1.1.1.1', '32')
        self.eapi_positive_config_test(func, cmds)

    def test_add_entry_with_seqno(self):
        cmds = ['ip access-list exttest',
                '30 permit ip 0.0.0.0/32 1.1.1.1/32 log', 'exit']
        func = function('add_entry', 'exttest', 'permit', 'ip', '0.0.0.0',
                        '32', '1.1.1.1', '32', True, 30)
        self.eapi_positive_config_test(func, cmds)


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