def test_to_json(self): """ Test to_json() method. """ endpoint1 = Endpoint("aabbccddeeff112233", "active", "11-22-33-44-55-66", if_name="eth0") assert_equal(endpoint1.ep_id, "aabbccddeeff112233") assert_equal(endpoint1.state, "active") assert_equal(endpoint1.mac, "11-22-33-44-55-66") assert_equal(endpoint1.profile_id, None) expected = {"state": "active", "name": "caliaabbccddeef", "mac": "11-22-33-44-55-66", "container:if_name": "eth0", "profile_id": None, "ipv4_nets": [], "ipv6_nets": [], "ipv4_gateway": None, "ipv6_gateway": None} assert_dict_equal(json.loads(endpoint1.to_json()), expected) endpoint1.profile_id = "TEST12" endpoint1.ipv4_nets.add(IPNetwork("192.168.1.23/32")) endpoint1.ipv4_gateway = IPAddress("192.168.1.1") expected["profile_id"] = "TEST12" expected["ipv4_nets"] = ["192.168.1.23/32"] expected["ipv4_gateway"] = "192.168.1.1" assert_dict_equal(json.loads(endpoint1.to_json()), expected)
def test_get_endpoint_exists(self): """ Test get_endpoint() for an endpoint that exists. """ ep = Endpoint(TEST_ENDPOINT_ID, "active", "11-22-33-44-55-66", if_name="eth1") self.etcd_client.read.side_effect = mock_read_for_endpoint(ep) ep2 = self.datastore.get_endpoint(TEST_HOST, TEST_CONT_ID, TEST_ENDPOINT_ID) assert_equal(ep.to_json(), ep2.to_json()) assert_equal(ep.ep_id, ep2.ep_id)
def test_operators(self): """ Test Endpoint operators __eq__, __ne__ and copy. """ endpoint1 = Endpoint("aabbccddeeff112233", "active", "11-22-33-44-55-66", if_name="eth0") endpoint2 = Endpoint("aabbccddeeff112233", "inactive", "11-22-33-44-55-66", if_name="eth0") endpoint3 = endpoint1.copy() assert_equal(endpoint1, endpoint3) assert_not_equal(endpoint1, endpoint2) assert_not_equal(endpoint1, 1) assert_false(endpoint1 == "this is not an endpoint")
def test_from_json(self): """ Test from_json() class method - Directly from JSON - From to_json() method of existing Endpoint. """ ep_id = "aabbccddeeff112233" expected = {"state": "active", "name": "caliaabbccddeef", "mac": "11-22-33-44-55-66", "container:if_name": "eth0", "profile_id": "TEST23", "ipv4_nets": ["192.168.3.2/32", "10.3.4.23/32"], "ipv6_nets": ["fd20::4:2:1/128"], "ipv4_gateway": "10.3.4.2", "ipv6_gateway": "2001:2:4a::1"} endpoint = Endpoint.from_json(ep_id, json.dumps(expected)) assert_equal(endpoint.state, "active") assert_equal(endpoint.ep_id, ep_id) assert_equal(endpoint.mac, "11-22-33-44-55-66") assert_equal(endpoint.profile_id, "TEST23") assert_equal(endpoint.ipv4_gateway, IPAddress("10.3.4.2")) assert_equal(endpoint.ipv6_gateway, IPAddress("2001:2:4a::1")) assert_set_equal(endpoint.ipv4_nets, {IPNetwork("192.168.3.2/32"), IPNetwork("10.3.4.23/32")}) assert_set_equal(endpoint.ipv6_nets, {IPNetwork("fd20::4:2:1/128")}) endpoint2 = Endpoint.from_json(ep_id, endpoint.to_json()) assert_equal(endpoint.state, endpoint2.state) assert_equal(endpoint.ep_id, endpoint2.ep_id) assert_equal(endpoint.mac, endpoint2.mac) assert_equal(endpoint.profile_id, endpoint2.profile_id) assert_equal(endpoint.ipv4_gateway, endpoint2.ipv4_gateway) assert_equal(endpoint.ipv6_gateway, endpoint2.ipv6_gateway) assert_set_equal(endpoint.ipv4_nets, endpoint2.ipv4_nets) assert_set_equal(endpoint.ipv6_nets, endpoint2.ipv6_nets)
def test_remove_workload_from_profile(self): """ Test remove_workload_from_profile() when the workload exists. """ ep = Endpoint.from_json(EP_12.ep_id, EP_12.to_json()) def mock_read(path): if path == TEST_CONT_ENDPOINT_PATH: return mock_read_2_ep_for_cont(path) elif path == TEST_CONT_ENDPOINT_PATH + ep.ep_id: return mock_read_for_endpoint(ep)(path) else: assert_true(False) self.etcd_client.read.side_effect = mock_read self.datastore.remove_workload_from_profile(TEST_HOST, TEST_CONT_ID) ep.profile_id = None expected_write_json = ep.to_json() self.etcd_client.write.assert_called_once_with(TEST_ENDPOINT_PATH, expected_write_json)
IPV4_POOLS_PATH = CALICO_V_PATH + "/ipam/v4/pool/" IPV6_POOLS_PATH = CALICO_V_PATH + "/ipam/v6/pool/" BGP_PEERS_PATH = CALICO_V_PATH + "/config/bgp_peer_rr_v4/" TEST_PROFILE_PATH = CALICO_V_PATH + "/policy/profile/TEST/" ALL_PROFILES_PATH = CALICO_V_PATH + "/policy/profile/" ALL_ENDPOINTS_PATH = CALICO_V_PATH + "/host/" ALL_HOSTS_PATH = CALICO_V_PATH + "/host/" TEST_ENDPOINT_PATH = CALICO_V_PATH + "/host/TEST_HOST/workload/docker/1234/" \ "endpoint/1234567890ab" TEST_CONT_ENDPOINT_PATH = CALICO_V_PATH + "/host/TEST_HOST/workload/docker/" \ "1234/endpoint/" TEST_CONT_PATH = CALICO_V_PATH + "/host/TEST_HOST/workload/docker/1234/" CONFIG_PATH = CALICO_V_PATH + "/config/" # 4 endpoints, with 2 TEST profile and 2 UNIT profile. EP_56 = Endpoint("567890abcdef", "active", "AA-22-BB-44-CC-66", if_name="eth0") EP_56.profile_id = "TEST" EP_78 = Endpoint("7890abcdef12", "active", "11-AA-33-BB-55-CC", if_name="eth0") EP_78.profile_id = "TEST" EP_90 = Endpoint("90abcdef1234", "active", "1A-2B-3C-4D-5E-6E", if_name="eth0") EP_90.profile_id = "UNIT" EP_12 = Endpoint(TEST_ENDPOINT_ID, "active", "11-22-33-44-55-66", if_name="eth0") EP_12.profile_id = "UNIT" # A complicated set of Rules JSON for testing serialization / deserialization. RULES_JSON = """ { "id": "PROF_GROUP1", "inbound_rules": [ {