def TestIPAlias()

in image_test/network/network-tester.py [0:0]


def TestIPAlias(instance, ip_alias, ip_mask):
  testee_hostname = RequestHostname(instance)
  cidr = "%s/%s" % (ip_alias, ip_mask)

  # caching ips out of range for using it on negative tests
  ips = [str(ip) for ip in netaddr.IPNetwork(
      "{}/{}".format(ip_alias, ip_mask))]

  # positive test on expected IPs
  for ip in ips:
    try:
      if testee_hostname != RequestHostname(ip):
        raise Exception("Alias hostname should be the same from host machine.")
    except urllib.error.URLError:
      raise RequestError("ip_alias", instance, cidr)

  # negative test
  superset_ips = [str(ip) for ip in netaddr.IPNetwork(
      "{}/{}".format(ip_alias, int(ip_mask) - 1))]
  invalid_ip = superset_ips[superset_ips.index(ips[0]) - 1]
  try:
    if testee_hostname == RequestHostname(invalid_ip):
      raise RequestError("ip_alias", instance, cidr, positive_test=False)
  except urllib.error.URLError:
    # great, it didn't even respond! So no machine is using that ip.
    pass