def test_exchange_views(self): p1 = PartialView("First IP", 4, 3) p1.add_peer(PodDescriptor("172.0.1.6", 0)) p1.add_peer(PodDescriptor("172.0.1.3", 2)) p1.add_peer(PodDescriptor("172.0.1.5", 3)) p1.add_peer(PodDescriptor("Second IP", 5)) p2 = PartialView("Second IP", 4, 3) p2.add_peer(PodDescriptor("172.0.1.3", 0)) p2.add_peer(PodDescriptor("172.0.1.5", 1)) p2.add_peer(PodDescriptor("172.0.1.2", 2)) p2.add_peer(PodDescriptor("172.0.1.1", 4)) ######################## # P1 starts the exchange ######################## # 1) Increase by one the age of all neighbors p1.increment() # 2) Select neighbor Q with the highest age among all neighbors. oldest = p1.get_oldest_peer() # 3) Select l - 1 other random neighbors (meaning avoid oldest). request = p1.select_neighbors_for_request(oldest) # 4) Replace Q's entry with a new entry of age 0 and with P's address. request.add_peer_ip(p1.ip, allow_self_ip=True) self.assertTrue(request.is_full()) self.assertEqual(request.size, p1.shuffle_length) ################################################ # P2 receives neighbors and prepares a reply ################################################ reply = p2.select_neighbors_for_reply() self.assertTrue(request.is_full()) self.assertEqual(request.size, p1.shuffle_length) # Note that in p1 the oldest is p2 # p1 and p2 know two peers in common # p2 does not have an entry with p1's ip # p1.merge should: # - Discard 172.0.1.3 and 172.0.1.5 # - Put in unknown list 172.0.1.2, 172.0.1.1 # 6) I remove the oldest peer from my view p1.remove_peer(oldest) p1.merge(request, reply) self.assertTrue(p1.is_full()) for peer in reply.get_peer_list(): self.assertTrue(p1.contains(peer)) self.assertLessEqual(self.partialView.size, self.partialView.limit)
def setUp(self): self.partialView = PartialView("172.0.1.0") self.descriptors = [] self.ips = [ "172.0.1.1", "172.0.1.2", "172.0.1.3", "172.0.1.4", "172.0.1.5" ] for ip in self.ips: self.descriptors.append(PodDescriptor(ip))
def test_add_peer_ip_with_allow_self_should_allow_self_entry(self): ip = "my ip" p1 = PartialView(ip) size = self.partialView.size success = p1.add_peer_ip(ip, True) self.assertTrue(success) self.assertTrue(p1.contains_ip(ip)) self.assertEqual(p1.size, size + 1)
def test_add_peer_should_not_allow_self_entry(self): ip = "my ip" p1 = PartialView(ip) peer = PodDescriptor(ip) size = self.partialView.size success = p1.add_peer(peer) self.assertFalse(success) self.assertFalse(p1.contains_ip(ip)) self.assertEqual(p1.size, size)
def test_message_can_carry_partial_view(self): partial_view = PartialView("172.0.1.0") partial_view.add_peer_ip("172.0.1.1") partial_view.add_peer_ip("172.0.1.2") m = Message("source_ip", "destination_ip", partial_view) self.assertEqual(m.source, "source_ip") self.assertEqual(m.destination, "destination_ip") self.assertEqual(m.data, partial_view)
def test_new_message_to_json(self): partial_view = PartialView("172.0.1.0") partial_view.add_peer_ip("172.0.1.1") partial_view.add_peer_ip("172.0.1.2") m = Message("source_ip", "destination_ip", partial_view) # Transform it into json json_path = os.path.join(os.path.dirname(__file__), "message.json") with open(json_path) as json_file: d = json.load(json_file) self.assertEqual(m.to_json(), d) json_file.close()
def __init__(self): self.ip = os.environ['MY_POD_IP'] self.k8s = KubernetesClient() self.api_version = 'v1' self.partialView = PartialView(self.ip) self.bootstrap()