def test_netns_check_dns(self): # Find nameserver address and use it fd = open('/etc/resolv.conf','r') self.nameserver_ip = None for elem in fd.readlines(): match = re.match('nameserver\s+(\S+)',elem) if match: logger.debug("Nameserver IP is {}" .format(match.group(1))) self.nameserver_ip = match.group(1) if self.nameserver_ip: fake_nameserver = "nameserver "+ self.nameserver_ip + "\n" else: fake_nameserver = "nameserver 1.1.1.1\n" new_resolvconf_hdl = tempfile.NamedTemporaryFile(delete=False) new_resolvconf_hdl_path = new_resolvconf_hdl.name new_resolvconf_hdl.write(fake_nameserver) new_resolvconf_hdl.close() logger.debug("Temp file name is :{}".format(new_resolvconf_hdl_path)) RwNetns.mount_bind_resolv_conf(new_resolvconf_hdl_path) a = urllib2.urlopen("http://www.google.com", timeout=1) self.assertEqual(a.getcode(),200) logger.debug("URL return code is :{}".format(a.getcode())) os.remove(new_resolvconf_hdl_path)
def test_netns_check_dns(self): # Find nameserver address and use it fd = open('/etc/resolv.conf', 'r') self.nameserver_ip = None for elem in fd.readlines(): match = re.match('nameserver\s+(\S+)', elem) if match: logger.debug("Nameserver IP is {}".format(match.group(1))) self.nameserver_ip = match.group(1) if self.nameserver_ip: fake_nameserver = "nameserver " + self.nameserver_ip + "\n" else: fake_nameserver = "nameserver 1.1.1.1\n" new_resolvconf_hdl = tempfile.NamedTemporaryFile(delete=False) new_resolvconf_hdl_path = new_resolvconf_hdl.name new_resolvconf_hdl.write(fake_nameserver) new_resolvconf_hdl.close() logger.debug("Temp file name is :{}".format(new_resolvconf_hdl_path)) RwNetns.mount_bind_resolv_conf(new_resolvconf_hdl_path) a = urllib2.urlopen("http://www.google.com", timeout=1) self.assertEqual(a.getcode(), 200) logger.debug("URL return code is :{}".format(a.getcode())) os.remove(new_resolvconf_hdl_path)
def _mount_resolv_conf(self): if not os.path.isfile(self.resolv_conf_path): self._log_event(MissingResolvConf()) raise NetworkNamespaceException("Network namespace resolv.conf file does not exist: %s" % self.resolv_conf_path) with open(self.resolv_conf_path) as resolv_conf_hdl: self._log_event(MountingResolvConf(), content=resolv_conf_hdl.read()) bind_rc = RwNetns.mount_bind_resolv_conf(self.resolv_conf_path) if bind_rc != 0: self._log_event(MountingResolvConfFailed, rc=bind_rc) raise NetworkNamespaceException("Could not mount netns resolv conf (%s). rc=%s" % (self.resolv_conf_path, bind_rc))
def _mount_resolv_conf(self): if not os.path.isfile(self.resolv_conf_path): self._log_event(MissingResolvConf()) raise NetworkNamespaceException( "Network namespace resolv.conf file does not exist: %s" % self.resolv_conf_path) with open(self.resolv_conf_path) as resolv_conf_hdl: self._log_event(MountingResolvConf(), content=resolv_conf_hdl.read()) bind_rc = RwNetns.mount_bind_resolv_conf(self.resolv_conf_path) if bind_rc != 0: self._log_event(MountingResolvConfFailed, rc=bind_rc) raise NetworkNamespaceException( "Could not mount netns resolv conf (%s). rc=%s" % (self.resolv_conf_path, bind_rc))