Пример #1
0
 def test_netmask(self):
     address = '1.1.1.1/24'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.0')
     address = '1.1.1.1/25'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.128')
     address = '1.12.1.34/32'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.255')
Пример #2
0
 def test_netmask(self):
     address = '1.1.1.1/24'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.0')
     address = '1.1.1.1/25'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.128')
     address = '1.12.1.34/32'
     self.assertEqual(ipaddr(address, 'netmask'), '255.255.255.255')
Пример #3
0
 def test_broadcast(self):
     address = '1.12.1.34/24'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.1.255')
     address = '1.12.1.34/16'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.255.255')
     address = '1.12.1.34/27'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.1.63')
     address = '1.12.1.34/32'
     self.assertEqual(ipaddr(address, 'broadcast'), None)
     address = '1.12.1.35/31'
     self.assertEqual(ipaddr(address, 'broadcast'), None)
Пример #4
0
 def test_broadcast(self):
     address = '1.12.1.34/24'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.1.255')
     address = '1.12.1.34/16'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.255.255')
     address = '1.12.1.34/27'
     self.assertEqual(ipaddr(address, 'broadcast'), '1.12.1.63')
     address = '1.12.1.34/32'
     self.assertEqual(ipaddr(address, 'broadcast'), None)
     address = '1.12.1.35/31'
     self.assertEqual(ipaddr(address, 'broadcast'), None)
Пример #5
0
 def test_network(self):
     address = '1.12.1.34/32'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.34/255.255.255.255'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.34'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.35/31'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.34/24'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.0')
Пример #6
0
 def test_network_wildcard(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.0 0.0.0.255')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.0 0.0.0.127')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.32 0.0.0.15')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.32 0.0.0.15')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.36 0.0.0.1')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.36 0.0.0.1')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.36 0.0.0.0')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'network_wildcard'),
                      '1.12.1.0 0.0.0.255')
Пример #7
0
 def test_network_netmask(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.0 255.255.255.0')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.0 255.255.255.128')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.32 255.255.255.240')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.32 255.255.255.240')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.36 255.255.255.254')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.36 255.255.255.254')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.36 255.255.255.255')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'network_netmask'),
                      '1.12.1.0 255.255.255.0')
Пример #8
0
 def test_range_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.1-1.12.1.254')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.1-1.12.1.126')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.33-1.12.1.46')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.33-1.12.1.46')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.36-1.12.1.37')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.36-1.12.1.37')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'range_usable'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'range_usable'),
                      '1.12.1.1-1.12.1.254')
Пример #9
0
 def test_network(self):
     # Unfixable in current state
     # address = '1.12.1.34/32'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.34/255.255.255.255'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.34'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.35/31'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.34/24'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.0')
 def test_network(self):
     # Unfixable in current state
     # address = '1.12.1.34/32'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.34/255.255.255.255'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.34'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     # address = '1.12.1.35/31'
     # self.assertEqual(ipaddr(address, 'network'), '1.12.1.34')
     address = '1.12.1.34/24'
     self.assertEqual(ipaddr(address, 'network'), '1.12.1.0')
Пример #11
0
    def _check_rule_data(rule_data, ipversion):
        """Check the rule data for compatible ip version information.

        This function uses the ansible ipaddr filter to validate IP
        information when a source or destination has been provided.

        :returns: boolean
        """

        kwargs_hash = {
            'ipv6': {
                'version': 6,
                'query': 'ipv6',
                'alias': 'ipv6'
            },
            'ipv4': {
                'version': 4,
                'query': 'ipv4',
                'alias': 'ipv4'
            }
        }

        for arg in ('source', 'destination'):
            ip_data = rule_data.get(arg)
            if ip_data:
                DISPLAY.v(
                    'Checking "{}" against "{}" with ip version "{}"'.format(
                        arg,
                        ip_data,
                        ipversion
                    )
                )
                ip_data_check = ipaddr.ipaddr(
                    value=ip_data,
                    **kwargs_hash[ipversion]
                )
                DISPLAY.vvv('ipaddr filter return "{}"'.format(ip_data_check))
                if not ip_data_check:
                    DISPLAY.v(
                        'Rule has a "{}" but the value "{}" is not applicable'
                        ' to ip version "{}"'.format(
                            arg,
                            ip_data,
                            ipversion

                        )
                    )
                    DISPLAY.vvv('Rule data: "{}"'.format(rule_data))
                    return False
        else:
            return True
Пример #12
0
 def test_ip_netmask(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.240')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.240')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.254')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.37 255.255.255.254')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.254 255.255.255.0')
Пример #13
0
 def test_ip_netmask(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.240')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.240')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.36 255.255.255.254')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.37 255.255.255.254')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'ip_netmask'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'ip_netmask'), '1.12.1.254 255.255.255.0')
Пример #14
0
 def test_previous_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.36/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.36')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.253')
Пример #15
0
 def test_range_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.1-1.12.1.254')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.1-1.12.1.126')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.33-1.12.1.46')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.33-1.12.1.46')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.36-1.12.1.37')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.36-1.12.1.37')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'range_usable'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'range_usable'), '1.12.1.1-1.12.1.254')
Пример #16
0
 def test_size_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'size_usable'), 254)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'size_usable'), 126)
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'size_usable'), 14)
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'size_usable'), 14)
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'size_usable'), 2)
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'size_usable'), 2)
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'size_usable'), 0)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'size_usable'), 254)
Пример #17
0
 def test_size_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'size_usable'), 254)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'size_usable'), 126)
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'size_usable'), 14)
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'size_usable'), 14)
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'size_usable'), 2)
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'size_usable'), 2)
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'size_usable'), 0)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'size_usable'), 254)
Пример #18
0
 def test_network_prefix(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/24')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/25')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.32/28')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.32/28')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/31')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/31')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/32')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/24')
Пример #19
0
 def test_wildcard(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.255')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.127')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.15')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.15')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.1')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.1')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.0')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'wildcard'), '0.0.0.255')
Пример #20
0
 def test_previous_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.36/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.35')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.36')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'previous_usable'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'previous_usable'), '1.12.1.253')
Пример #21
0
 def test_network_netmask(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.0 255.255.255.0')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.0 255.255.255.128')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.32 255.255.255.240')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.32 255.255.255.240')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.36 255.255.255.254')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.36 255.255.255.254')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.36 255.255.255.255')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'network_netmask'), '1.12.1.0 255.255.255.0')
Пример #22
0
 def test_network_prefix(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/24')
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/25')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.32/28')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.32/28')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/31')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/31')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.36/32')
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'network/prefix'), '1.12.1.0/24')
Пример #23
0
 def test_first_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.1')
     address = '1.12.1.36/24'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.1')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'first_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.33')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.33')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.36')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.36')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'first_usable'), None)
Пример #24
0
 def test_first_usable(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.1')
     address = '1.12.1.36/24'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.1')
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'first_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.33')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.33')
     address = '1.12.1.36/31'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.36')
     address = '1.12.1.37/31'
     self.assertEqual(ipaddr(address, 'first_usable'), '1.12.1.36')
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'first_usable'), None)
Пример #25
0
 def test_address_prefix(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/28')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/28')
     # address = '1.12.1.36/31'
     # self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/31') - unfixable?
     # address = '1.12.1.37/31'
     # self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.37/31') - unfixable?
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.254/24')
Пример #26
0
 def test_address_prefix(self):
     address = '1.12.1.0/24'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     address = '1.12.1.0/25'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     # address = '1.12.1.34'
     # self.assertFalse(ipaddr(address, 'last_usable'), 'Not a network address')
     address = '1.12.1.36/28'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/28')
     address = '1.12.1.36/255.255.255.240'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/28')
     # address = '1.12.1.36/31'
     # self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.36/31') - unfixable?
     # address = '1.12.1.37/31'
     # self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.37/31') - unfixable?
     address = '1.12.1.36/32'
     self.assertEqual(ipaddr(address, 'address/prefix'), None)
     address = '1.12.1.254/24'
     self.assertEqual(ipaddr(address, 'address/prefix'), '1.12.1.254/24')
Пример #27
0
 def test_peer(self):
     address = '1.12.1.0/31'
     self.assertEqual(ipaddr(address, 'peer'), '1.12.1.1')
     address = '1.12.1.1/31'
     self.assertEqual(ipaddr(address, 'peer'), '1.12.1.0')
     address = '1.12.1.1/30'
     self.assertEqual(ipaddr(address, 'peer'), '1.12.1.2')
     address = '1.12.1.2/30'
     self.assertEqual(ipaddr(address, 'peer'), '1.12.1.1')
     with self.assertRaises(AnsibleFilterError):
         address = '1.12.1.34'
         ipaddr(address, 'peer')
     with self.assertRaises(AnsibleFilterError):
         address = '1.12.1.33/29'
         ipaddr(address, 'peer')
     with self.assertRaises(AnsibleFilterError):
         address = '1.12.1.32/30'
         ipaddr(address, 'peer')
     with self.assertRaises(AnsibleFilterError):
         address = '1.12.1.35/30'
         ipaddr(address, 'peer')
     with self.assertRaises(AnsibleFilterError):
         address = '1.12.1.34/32'
         ipaddr(address, 'peer')
Пример #28
0
 def validate(self, path, value, schema):
     """Raise an error if value is not allowed by schema."""
     if self.is_dict(schema) and '_' in schema:
         schema = schema['_']
         if self.is_dict(schema):
             print path + '/_ has invalid schema: ' + repr(schema)
             print 'Schema for _ cannot be a dict.'
             raise XmlSchemaError
     try:
         if isinstance(schema, six.string_types):
             if schema.startswith('%'):
                 value = schema.format(value)
             elif schema.startswith('^') and schema.endswith('$'):
                 if re.match(schema, str(value)) is None:
                     raise XmlValueError
             elif schema[:4] == 'bool':
                 match = re.match('^bool(\(([^,]*)(,(.*))?\))?$', schema)
                 trueval = 'True' if match.group(
                     2) is None else match.group(2)
                 falseval = 'False' if match.group(
                     4) is None else match.group(4)
                 value = trueval if core.to_bool(value) else falseval
             elif schema == 'ipaddr':
                 value = ipaddr.ipaddr(value)
                 if value in (None, False):
                     raise XmlValueError
             elif schema == 'ipv4':
                 value = ipaddr.ipv4(value)
                 if value in (None, False):
                     raise XmlValueError
             elif schema == 'ipv6':
                 value = ipaddr.ipv6(value)
                 if value in (None, False):
                     raise XmlValueError
             elif schema[:5] == 'range':
                 m = re.match('^range(\(([^,]*)(,([^,]*)(,(.*))?)?\))?$',
                              schema)
                 if len(m.group(2)) > 0:
                     if float(value) < float(m.group(2)):
                         raise XmlValueError
                 if len(m.group(4)) > 0:
                     if float(value) > float(m.group(4)):
                         raise XmlValueError
                 if len(m.group(6)) > 0:
                     value = m.group(6).format(float(value))
             elif schema[:8] == 'strftime':
                 m = re.match('^strftime(\((.*)\))?$', schema)
                 datefmt = '%x' if m.group(2) is None else m.group(2)
                 value = dateutil.parser.parse(value).strftime(datefmt)
             else:
                 raise XmlSchemaError
         elif self.is_sequence(schema):
             if value not in schema:
                 raise XmlValueError
         elif schema is None:
             value = None
         else:
             raise XmlSchemaError
     except XmlSchemaError:
         print path + ' has invalid schema: ' + repr(schema)
         raise
     except XmlValueError:
         print path + '=' + repr(value) + ' does not match schema: ' + repr(
             schema)
         raise
     return value
Пример #29
0
 def iface_to_subnet_cidr(iface):
   return ipaddr(f"{iface['ipv4']['network']}/{iface['ipv4']['netmask']}", 'net')