Esempio n. 1
0
 def test_validate_endpoint_mainline_fip(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA:bb:cc:dd:ee:ff",
         "ipv4_nat": [{"int_ip": "10.0.0.1", "ext_ip": "192.168.1"}],
         "ipv4_nets": ["10.0.0.1/32"],
         "ipv4_gateway": "11.0.0.1",
         "ipv6_nat": [{"int_ip": "2001::1", "ext_ip": "2001::2"}],
         "ipv6_nets": ["2001::1/128"],
         "ipv6_gateway": "fe80:0::1",
         "profile_id": "prof1",
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(endpoint, {
         'state': 'active',
         'name': 'tap1234',
         'mac': 'aa:bb:cc:dd:ee:ff',
         'ipv4_nets': ['10.0.0.1/32'],
         "ipv4_nat": [{"int_ip": "10.0.0.1", "ext_ip": "192.168.0.1"}],
         'ipv4_gateway': '11.0.0.1',
         'ipv6_nat': [{'int_ip': '2001::1', 'ext_ip': '2001::2'}],
         'ipv6_nets': ['2001::1/128'],
         'ipv6_gateway': 'fe80::1',
         'profile_ids':['prof1'],
     })
Esempio n. 2
0
def parse_endpoint(config, combined_id, raw_json):
    endpoint = safe_decode_json(raw_json, log_tag="endpoint %s" % combined_id.endpoint)
    try:
        common.validate_endpoint(config, combined_id, endpoint)
    except ValidationFailed as e:
        _log.warning("Validation failed for endpoint %s, treating as " "missing: %s", combined_id, e.message)
        endpoint = None
    else:
        _log.debug("Validated endpoint : %s", endpoint)
    return endpoint
Esempio n. 3
0
def parse_endpoint(config, endpoint_id, raw_json):
    endpoint = json_decoder.decode(raw_json)
    try:
        common.validate_endpoint(config, endpoint_id, endpoint)
    except ValidationFailed as e:
        _log.warning("Validation failed for endpoint %s, treating as "
                     "missing: %s", endpoint_id, e.message)
        endpoint = None
    else:
        _log.debug("Validated endpoint : %s", endpoint)
    return endpoint
Esempio n. 4
0
def parse_endpoint(config, combined_id, raw_json):
    endpoint = safe_decode_json(raw_json,
                                log_tag="endpoint %s" % combined_id.endpoint)
    try:
        common.validate_endpoint(config, combined_id, endpoint)
    except ValidationFailed as e:
        _log.warning("Validation failed for endpoint %s, treating as "
                     "missing: %s; %r", combined_id, e.message, raw_json)
        endpoint = None
    else:
        _log.debug("Validated endpoint : %s", endpoint)
    return endpoint
Esempio n. 5
0
 def assert_endpoint_valid(self, original_endpoint):
     endpoint = copy.deepcopy(original_endpoint)
     try:
         # First pass at validation, may canonicalise the data.
         common.validate_endpoint(self.m_config, self.m_id, endpoint)
         canonical_endpoint = copy.deepcopy(endpoint)
         # Second pass, should make no changes.
         common.validate_endpoint(self.m_config, self.m_id, canonical_endpoint)
         self.assertEqual(endpoint, canonical_endpoint)
     except common.ValidationFailed as e:
         _log.exception("Validation unexpectedly failed for %s",
                        original_endpoint)
         self.fail("Validation unexpectedly failed for %s: %r" %
                   (original_endpoint, e))
Esempio n. 6
0
 def assert_endpoint_valid(self, original_endpoint):
     endpoint = copy.deepcopy(original_endpoint)
     try:
         # First pass at validation, may canonicalise the data.
         common.validate_endpoint(self.m_config, self.m_id, endpoint)
         canonical_endpoint = copy.deepcopy(endpoint)
         # Second pass, should make no changes.
         common.validate_endpoint(self.m_config, self.m_id, canonical_endpoint)
         self.assertEqual(endpoint, canonical_endpoint)
     except common.ValidationFailed as e:
         _log.exception("Validation unexpectedly failed for %s",
                        original_endpoint)
         self.fail("Validation unexpectedly failed for %s: %r" %
                   (original_endpoint, e))
Esempio n. 7
0
 def test_validate_endpoint_mainline_profile_ids(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA-bb-cc-dd-ee-ff",
         "ipv4_nets": ["10.0.1/32"],
         "profile_ids": ["prof1", "prof2"],
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(endpoint, {
         'state': 'active',
         'name': 'tap1234',
         'mac': 'aa:bb:cc:dd:ee:ff',
         'ipv4_nets': ['10.0.1.0/32'],
         "ipv6_nets": [],
         "profile_ids": ["prof1", "prof2"],
     })
Esempio n. 8
0
 def test_validate_endpoint_mainline_profile_ids(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA-bb-cc-dd-ee-ff",
         "ipv4_nets": ["10.0.1/32"],
         "profile_ids": ["prof1", "prof2"],
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(endpoint, {
         'state': 'active',
         'name': 'tap1234',
         'mac': 'aa:bb:cc:dd:ee:ff',
         'ipv4_nets': ['10.0.1.0/32'],
         "ipv6_nets": [],
         "profile_ids": ["prof1", "prof2"],
     })
Esempio n. 9
0
 def test_validate_endpoint_mainline_fip(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA:bb:cc:dd:ee:ff",
         "ipv4_nat": [{
             "int_ip": "10.0.0.1",
             "ext_ip": "192.168.1"
         }],
         "ipv4_nets": ["10.0.0.1/32"],
         "ipv4_gateway": "11.0.0.1",
         "ipv6_nat": [{
             "int_ip": "2001::1",
             "ext_ip": "2001::2"
         }],
         "ipv6_nets": ["2001::1/128"],
         "ipv6_gateway": "fe80:0::1",
         "profile_id": "prof1",
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(
         endpoint, {
             'state': 'active',
             'name': 'tap1234',
             'mac': 'aa:bb:cc:dd:ee:ff',
             'ipv4_nets': ['10.0.0.1/32'],
             "ipv4_nat": [{
                 "int_ip": "10.0.0.1",
                 "ext_ip": "192.168.0.1"
             }],
             'ipv4_gateway': '11.0.0.1',
             'ipv6_nat': [{
                 'int_ip': '2001::1',
                 'ext_ip': '2001::2'
             }],
             'ipv6_nets': ['2001::1/128'],
             'ipv6_gateway': 'fe80::1',
             'profile_ids': ['prof1'],
         })
Esempio n. 10
0
 def test_validate_endpoint_mainline(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA:bb:cc:dd:ee:ff",
         "ipv4_nets": ["10.0.1/32"],
         "ipv4_gateway": "11.0.0.1",
         "ipv6_nets": ["2001:0::1/64"],
         "ipv6_gateway": "fe80:0::1",
         "profile_id": "prof1",
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(endpoint, {
         'state': 'active',
         'name': 'tap1234',
         'mac': 'aa:bb:cc:dd:ee:ff',
         'ipv4_nets': ['10.0.1.0/32'],
         'ipv4_gateway': '11.0.0.1',
         'ipv6_nets': ['2001::1/64'],
         'ipv6_gateway': 'fe80::1',
         'profile_ids':['prof1'],
     })
Esempio n. 11
0
 def test_validate_endpoint_mainline(self):
     endpoint = {
         "state": "active",
         "name": "tap1234",
         "mac": "AA:bb:cc:dd:ee:ff",
         "ipv4_nets": ["10.0.1/32"],
         "ipv4_gateway": "11.0.0.1",
         "ipv6_nets": ["2001:0::1/64"],
         "ipv6_gateway": "fe80:0::1",
         "profile_id": "prof1",
     }
     common.validate_endpoint(self.m_config, self.m_id, endpoint)
     self.assertEqual(endpoint, {
         'state': 'active',
         'name': 'tap1234',
         'mac': 'aa:bb:cc:dd:ee:ff',
         'ipv4_nets': ['10.0.1.0/32'],
         'ipv4_gateway': '11.0.0.1',
         'ipv6_nets': ['2001::1/64'],
         'ipv6_gateway': 'fe80::1',
         'profile_ids':['prof1'],
     })
Esempio n. 12
0
def parse_if_endpoint(config, etcd_node):
    m = ENDPOINT_KEY_RE.match(etcd_node.key)
    if m:
        # Got an endpoint.
        host = m.group("hostname")
        orch = m.group("orchestrator")
        workload_id = m.group("workload_id")
        endpoint_id = m.group("endpoint_id")
        if etcd_node.action == "delete":
            _log.debug("Found deleted endpoint %s", endpoint_id)
            endpoint = None
        else:
            endpoint = json_decoder.decode(etcd_node.value)
            try:
                common.validate_endpoint(config, endpoint)
            except ValidationFailed as e:
                _log.warning("Validation failed for endpoint %s, treating as "
                             "missing: %s", endpoint_id, e.message)
                endpoint = None
            else:
                _log.debug("Validated endpoint : %s", endpoint)
        return EndpointId(host, orch, workload_id, endpoint_id), endpoint
    return None, None
Esempio n. 13
0
    def test_validate_endpoint(self):
        combined_id = EndpointId("host", "orchestrator", "workload",
                                 "valid_name-ok.")
        endpoint_dict = {
            'profile_id': "valid.prof-name",
            'state': "active",
            'name': "tapabcdef",
            'mac': "78:2b:cb:9f:ae:1c",
            'ipv4_nets': [],
            'ipv6_nets': []
        }
        config = Config('tap', 'localhost')
        ep_copy = endpoint_dict.copy()
        common.validate_endpoint(config, combined_id, ep_copy)
        self.assertTrue(ep_copy.get('profile_id') is None)
        self.assertEqual(ep_copy.get('profile_ids'), ["valid.prof-name"])

        # Now break it various ways.
        # Bad endpoint ID.
        for bad_str in ("with spaces", "$stuff", "^%@"):
            bad_id = EndpointId("host", "orchestrator", "workload", bad_str)
            with self.assertRaisesRegexp(ValidationFailed,
                                         "Invalid endpoint ID"):
                common.validate_endpoint(config, bad_id, endpoint_dict.copy())

        # Bad dictionary.
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected endpoint to be a dict"):
            common.validate_endpoint(config, combined_id, [1, 2, 3])

        # No state, invalid state.
        bad_dict = endpoint_dict.copy()
        del bad_dict['state']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'state' field"):
            common.validate_endpoint(config, combined_id, bad_dict)
        bad_dict['state'] = "invalid"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected 'state' to be"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Missing mac and name; both must be reported as two errors
        bad_dict = endpoint_dict.copy()
        del bad_dict['name']
        del bad_dict['mac']
        with self.assertRaisesRegexp(ValidationFailed, "Missing 'name' field"):
            common.validate_endpoint(config, combined_id, bad_dict)
        with self.assertRaisesRegexp(ValidationFailed, "Missing 'mac' field"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict['name'] = [1, 2, 3]
        bad_dict['mac'] = 73
        with self.assertRaisesRegexp(
                ValidationFailed, "Expected 'name' to be a string.*" +
                "Expected 'mac' to be a string"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Bad profile ID
        bad_dict = endpoint_dict.copy()
        bad_dict['profile_id'] = "str£ing"
        with self.assertRaisesRegexp(ValidationFailed, "Invalid profile ID"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'profile_id\(s\)' field"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        bad_dict['profile_ids'] = [1, 2, 3]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected profile IDs to be strings"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Bad interface name - acceptable if not local.
        bad_dict = endpoint_dict.copy()
        bad_dict['name'] = "vethabcdef"
        common.validate_endpoint(config, combined_id, bad_dict)

        local_id = EndpointId("localhost", "orchestrator", "workload",
                              "valid_name-ok.")
        with self.assertRaisesRegexp(ValidationFailed, "does not start with"):
            common.validate_endpoint(config, local_id, bad_dict)

        # Valid networks.
        good_dict = endpoint_dict.copy()
        good_dict['ipv4_nets'] = ["1.2.3.4/32", "172.0.0.0/8", "3.4.5.6"]
        good_dict['ipv6_nets'] = ["::1/128", "::", "2001:db8:abc:1400::/54"]
        common.validate_endpoint(config, combined_id, good_dict.copy())

        # Invalid networks
        bad_dict = good_dict.copy()
        bad_dict['ipv4_nets'] = [
            "1.2.3.4/32", "172.0.0.0/8", "2001:db8:abc:1400::/54"
        ]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv4_nets'] = ["1.2.3.4/32", "172.0.0.0/8", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_nets'] = ["::1/128", "::", "1.2.3.4/8"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv6_nets'] = ["::1/128", "::", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        # Gateway IPs.
        good_dict['ipv4_gateway'] = "1.2.3.4"
        good_dict['ipv6_gateway'] = "2001:db8:abc:1400::"
        common.validate_endpoint(config, combined_id, good_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv4_gateway'] = "2001:db8:abc:1400::"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv4_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_gateway'] = "1.2.3.4"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv6_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        # Labels, empty.
        good_dict["labels"] = {}
        common.validate_endpoint(config, combined_id, good_dict)
        self.assertEqual(good_dict["labels"], {})
        # Labels, valid.
        good_dict["labels"] = {"a": "b"}
        common.validate_endpoint(config, combined_id, good_dict)
        self.assertEqual(good_dict["labels"], {"a": "b"})
        # Labels, bad type.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = []
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected labels to be a dict"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        # Labels, bad value.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = {"a": {}}
        with self.assertRaisesRegexp(ValidationFailed, "Invalid label value"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        # Labels, bad key.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = {"a+|%": {}}
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Invalid label name 'a+|%'."):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
Esempio n. 14
0
    def test_validate_endpoint(self):
        combined_id = EndpointId("host", "orchestrator",
                                 "workload", "valid_name-ok.")
        endpoint_dict = {'profile_id': "valid.prof-name",
                         'state': "active",
                         'name': "tapabcdef",
                         'mac': "78:2b:cb:9f:ae:1c",
                         'ipv4_nets': [],
                         'ipv6_nets': []}
        config = Config('tap', 'localhost')
        ep_copy = endpoint_dict.copy()
        common.validate_endpoint(config, combined_id, ep_copy)
        self.assertTrue(ep_copy.get('profile_id') is None)
        self.assertEqual(ep_copy.get('profile_ids'), ["valid.prof-name"])

        # Now break it various ways.
        # Bad endpoint ID.
        for bad_str in ("with spaces", "$stuff", "^%@"):
            bad_id = EndpointId("host", "orchestrator", "workload", bad_str)
            with self.assertRaisesRegexp(ValidationFailed,
                                         "Invalid endpoint ID"):
                common.validate_endpoint(config, bad_id, endpoint_dict.copy())

        # Bad dictionary.
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected endpoint to be a dict"):
            common.validate_endpoint(config, combined_id, [1,2,3])

        # No state, invalid state.
        bad_dict = endpoint_dict.copy()
        del bad_dict['state']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'state' field"):
            common.validate_endpoint(config, combined_id, bad_dict)
        bad_dict['state'] = "invalid"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected 'state' to be"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Missing mac and name; both must be reported as two errors
        bad_dict = endpoint_dict.copy()
        del bad_dict['name']
        del bad_dict['mac']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'name' field"):
            common.validate_endpoint(config, combined_id, bad_dict)
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'mac' field"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict['name'] = [1, 2, 3]
        bad_dict['mac'] = 73
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected 'name' to be a string.*" +
                                     "Expected 'mac' to be a string"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Bad profile ID
        bad_dict = endpoint_dict.copy()
        bad_dict['profile_id'] = "str£ing"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Invalid profile ID"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'profile_id\(s\)' field"):
            common.validate_endpoint(config, combined_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        bad_dict['profile_ids'] = [1, 2, 3]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected profile IDs to be strings"):
            common.validate_endpoint(config, combined_id, bad_dict)

        # Bad interface name - acceptable if not local.
        bad_dict = endpoint_dict.copy()
        bad_dict['name'] = "vethabcdef"
        common.validate_endpoint(config, combined_id, bad_dict)

        local_id = EndpointId("localhost", "orchestrator",
                              "workload", "valid_name-ok.")
        with self.assertRaisesRegexp(ValidationFailed,
                                     "does not start with"):
            common.validate_endpoint(config, local_id, bad_dict)

        # Valid networks.
        good_dict = endpoint_dict.copy()
        good_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "3.4.5.6"]
        good_dict['ipv6_nets'] = [ "::1/128", "::", "2001:db8:abc:1400::/54"]
        common.validate_endpoint(config, combined_id, good_dict.copy())

        # Invalid networks
        bad_dict = good_dict.copy()
        bad_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "2001:db8:abc:1400::/54"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_nets'] = [ "::1/128", "::", "1.2.3.4/8"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv6_nets'] = [ "::1/128", "::", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        # Gateway IPs.
        good_dict['ipv4_gateway'] = "1.2.3.4"
        good_dict['ipv6_gateway'] = "2001:db8:abc:1400::"
        common.validate_endpoint(config, combined_id, good_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv4_gateway'] = "2001:db8:abc:1400::"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv4_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_gateway'] = "1.2.3.4"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        bad_dict['ipv6_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())

        # Labels, empty.
        good_dict["labels"] = {}
        common.validate_endpoint(config, combined_id, good_dict)
        self.assertEqual(good_dict["labels"], {})
        # Labels, valid.
        good_dict["labels"] = {"a": "b"}
        common.validate_endpoint(config, combined_id, good_dict)
        self.assertEqual(good_dict["labels"], {"a": "b"})
        # Labels, bad type.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = []
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected labels to be a dict"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        # Labels, bad value.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = {"a": {}}
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Invalid label value"):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
        # Labels, bad key.
        bad_dict = good_dict.copy()
        bad_dict["labels"] = {"a+|%": {}}
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Invalid label name 'a+|%'."):
            common.validate_endpoint(config, combined_id, bad_dict.copy())
Esempio n. 15
0
    def test_validate_endpoint(self):
        endpoint_id = "valid_name-ok."
        endpoint_dict = {'profile_id': "valid.prof-name",
                         'state': "active",
                         'name': "tapabcdef",
                         'mac': "78:2b:cb:9f:ae:1c",
                         'ipv4_nets': [],
                         'ipv6_nets': []}
        config = Config('tap')
        ep_copy = endpoint_dict.copy()
        common.validate_endpoint(config, endpoint_id, ep_copy)
        self.assertTrue(ep_copy.get('profile_id') is None)
        self.assertEqual(ep_copy.get('profile_ids'), ["valid.prof-name"])

        # Now break it various ways.
        # Bad endpoint ID.
        for bad_id in ("with spaces", "$stuff", "^%@"):
            with self.assertRaisesRegexp(ValidationFailed,
                                         "Invalid endpoint ID"):
                common.validate_endpoint(config, bad_id, endpoint_dict.copy())

        # Bad dictionary.
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected endpoint to be a dict"):
            common.validate_endpoint(config, endpoint_id, [1,2,3])

        # No state, invalid state.
        bad_dict = endpoint_dict.copy()
        del bad_dict['state']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'state' field"):
            common.validate_endpoint(config, endpoint_id, bad_dict)
        bad_dict['state'] = "invalid"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected 'state' to be"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        # Missing mac and name; both must be reported as two errors
        bad_dict = endpoint_dict.copy()
        del bad_dict['name']
        del bad_dict['mac']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'name' field"):
            common.validate_endpoint(config, endpoint_id, bad_dict)
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'mac' field"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        bad_dict['name'] = [1, 2, 3]
        bad_dict['mac'] = 73
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected 'name' to be a string.*" +
                                     "Expected 'mac' to be a string"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        # Bad profile ID
        bad_dict = endpoint_dict.copy()
        bad_dict['profile_id'] = "str£ing"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Invalid profile ID"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Missing 'profile_id\(s\)' field"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        bad_dict = endpoint_dict.copy()
        del bad_dict['profile_id']
        bad_dict['profile_ids'] = [1, 2, 3]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "Expected profile IDs to be strings"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        # Bad interface name.
        bad_dict = endpoint_dict.copy()
        bad_dict['name'] = "vethabcdef"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "does not start with"):
            common.validate_endpoint(config, endpoint_id, bad_dict)

        # Valid networks.
        good_dict = endpoint_dict.copy()
        good_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "3.4.5.6"]
        good_dict['ipv6_nets'] = [ "::1/128", "::", "2001:db8:abc:1400::/54"]
        common.validate_endpoint(config, endpoint_id, good_dict.copy())

        # Invalid networks
        bad_dict = good_dict.copy()
        bad_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "2001:db8:abc:1400::/54"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())
        bad_dict['ipv4_nets'] = [ "1.2.3.4/32", "172.0.0.0/8", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 CIDR"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_nets'] = [ "::1/128", "::", "1.2.3.4/8"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())
        bad_dict['ipv6_nets'] = [ "::1/128", "::", "nonsense"]
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 CIDR"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())

        # Gateway IPs.
        good_dict['ipv4_gateway'] = "1.2.3.4"
        good_dict['ipv6_gateway'] = "2001:db8:abc:1400::"
        common.validate_endpoint(config, endpoint_id, good_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv4_gateway'] = "2001:db8:abc:1400::"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())
        bad_dict['ipv4_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv4 gateway"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())

        bad_dict = good_dict.copy()
        bad_dict['ipv6_gateway'] = "1.2.3.4"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())
        bad_dict['ipv6_gateway'] = "nonsense"
        with self.assertRaisesRegexp(ValidationFailed,
                                     "not a valid IPv6 gateway"):
            common.validate_endpoint(config, endpoint_id, bad_dict.copy())
Esempio n. 16
0
 def validate_endpoint(self, *args, **kwargs):
     common.validate_endpoint(*args, **kwargs)