File: test_utils.py

package info (click to toggle)
python-ovsdbapp 2.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,040 kB
  • sloc: python: 9,222; sh: 138; makefile: 21
file content (71 lines) | stat: -rw-r--r-- 2,855 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
#    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 netaddr
import uuid

from ovsdbapp.tests import base
from ovsdbapp import utils


class TestUtils(base.TestCase):

    def test_normalize_ip(self):
        good = [
            ('4.4.4.4', '4.4.4.4'),
            ('10.0.0.0', '10.0.0.0'),
            ('123', '0.0.0.123'),
            ('2001:0db8:85a3:0000:0000:8a2e:0370:7334',
             '2001:db8:85a3::8a2e:370:7334')
        ]
        bad = ('256.1.3.2', 'bad', '192.168.1.1:80')
        for before, after in good:
            norm = utils.normalize_ip(before)
            self.assertEqual(after, norm,
                             "%s does not match %s" % (after, norm))
        for val in bad:
            self.assertRaises(netaddr.AddrFormatError, utils.normalize_ip, val)

    def test_normalize_ip_port(self):
        good = [
            ('4.4.4.4:53', '4.4.4.4:53'),
            ('10.0.0.0:7', '10.0.0.0:7'),
            ('123:12', '0.0.0.123:12'),
            ('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80',
             '[2001:db8:85a3::8a2e:370:7334]:80')
        ]
        bad = ('1.2.3.4:0', '1.2.3.4:99000',
               '2001:0db8:85a3:0000:0000:8a2e:0370:7334:80')
        for before, after in good:
            norm = utils.normalize_ip_port(before)
            self.assertEqual(after, norm,
                             "%s does not match %s" % (after, norm))
        for val in bad:
            self.assertRaises(netaddr.AddrFormatError,
                              utils.normalize_ip_port, val)

    def test_is_uuid_like(self):
        self.assertTrue(utils.is_uuid_like(str(uuid.uuid4())))
        self.assertTrue(utils.is_uuid_like(
            '{12345678-1234-1234-1234-123456781234}'))
        self.assertTrue(utils.is_uuid_like(
            '12345678123412341234123456781234'))
        self.assertTrue(utils.is_uuid_like(
            'urn:uuid:12345678-1234-1234-1234-123456781234'))
        self.assertTrue(utils.is_uuid_like(
            'urn:bbbaaaaa-aaaa-aaaa-aabb-bbbbbbbbbbbb'))
        self.assertTrue(utils.is_uuid_like(
            'uuid:bbbaaaaa-aaaa-aaaa-aabb-bbbbbbbbbbbb'))
        self.assertFalse(utils.is_uuid_like(
            'uuid:batrdbaa-aaaa-aaaa-aabb-bbbbbbbbbbbb'))
        self.assertFalse(utils.is_uuid_like(
            '123456781234123412341234567812345678'))