def ip_ver(request): return { "outer_ipv4": to_bool(request.config.getoption("outer_ipv4")), "outer_ipv6": to_bool(request.config.getoption("outer_ipv6")), "inner_ipv4": to_bool(request.config.getoption("inner_ipv4")), "inner_ipv6": to_bool(request.config.getoption("inner_ipv6")), }
def run(self, terms, variables, **kwargs): result = [] for term in terms: if isinstance(term, str): result.append(self._templar.template('{{(' + term + ')|bool}}')) else: result.append(to_bool(term)) return result
def babylon_extract_parameter_vars(vars_dict): """ Filter parameter variables from all values provided for provision. Most values will be included in the AnarchyGovernor as they are not parameters which are expected to be different with each requested environment. The `__meta__.catalag.parameters` provides a list of expected parameters along with data type validation that will be applied. Variables are converted to the types expected by the validation to prevent them from being rejected. """ parameter_vars = dict() parameters = vars_dict.get('__meta__', {}).get('catalog', {}).get('parameters') # Cloud tags may passed as a YAML string which must be interpreted. # Strip out guid and uuid from cloud tags as these will conflict with Babylon assignment. if 'cloud_tags' in vars_dict and isinstance(vars_dict['cloud_tags'], str): vars_dict['cloud_tags'] = { k: v for k, v in yaml.safe_load(vars_dict['cloud_tags']).items() if k not in ('guid', 'uuid') } if parameters == None: # No parameters configured, so must pass all vars as parameter vars for varname, value in vars_dict.items(): if varname not in ('__meta__', 'agnosticv_meta', 'guid', 'uuid'): parameter_vars[varname] = value else: # Pass parameter vars with expected type conversions for parameter in parameters: # Use explicit variable name for parameter if set, otherwise use parameter name as variable name # unless parameter sets an annotation. varname = parameter.get('variable', None if 'annotation' in parameter else parameter['name']) vartype = parameter.get('openAPIV3Schema', {}).get('type') if varname and varname in vars_dict: raw_value = vars_dict[varname] try: if vartype == 'boolean': parameter_vars[varname] = to_bool(raw_value) elif vartype == 'integer': parameter_vars[varname] = int(raw_value) elif vartype == 'number': parameter_vars[varname] = float(raw_value) elif vartype == 'string': parameter_vars[varname] = str(raw_value) else: parameter_vars[varname] = raw_value except ValueError: raise AnsibleFilterError( 'Invalid value for {}: "{}" is cannot be parsed as {}'.format(varname, raw_value, vartype) ) return parameter_vars
def _handle_specifics_presub(self, cfg, my_subcfg, cfgpath_abs): active = my_subcfg.get('activate', CONFIG_KEYWORD_AUTODETECT) active_autodect = False if active == CONFIG_KEYWORD_AUTODETECT: active_autodect = True active = True else: active = to_bool(active) my_subcfg['activate'] = active if not active: ## explicitly disabled java cert handling, nothing more to do here return my_subcfg return self._handle_specifics_presub_specific(cfg, my_subcfg, cfgpath_abs, active_autodect)
def setup_teardown(request, tbinfo, duthost, ptfhost): # Initialize parameters dscp_mode = "pipe" ecn_mode = "copy_from_outer" ttl_mode = "pipe" # The hostvars dict has definitions defined in ansible/group_vars/sonic/variables hostvars = duthost.host.options["variable_manager"]._hostvars[ duthost.hostname] sonic_hwsku = duthost.facts["hwsku"] mellanox_hwskus = hostvars["mellanox_hwskus"] if sonic_hwsku in mellanox_hwskus: dscp_mode = "uniform" ecn_mode = "standard" # Gather some facts cfg_facts = duthost.config_facts(host=duthost.hostname, source="persistent")["ansible_facts"] lo_ip = None lo_ipv6 = None for addr in cfg_facts["LOOPBACK_INTERFACE"]["Loopback0"]: ip = IPNetwork(addr).ip if ip.version == 4 and not lo_ip: lo_ip = ip elif ip.version == 6 and not lo_ipv6: lo_ipv6 = ip logger.info("lo_ip={}, lo_ipv6={}".format(str(lo_ip), str(lo_ipv6))) vlan_ip = None vlan_ipv6 = None if "VLAN_INTERFACE" in cfg_facts: for addr in cfg_facts["VLAN_INTERFACE"]["Vlan1000"]: ip = IPNetwork(addr).ip if ip.version == 4 and not vlan_ip: vlan_ip = ip elif ip.version == 6 and not vlan_ipv6: vlan_ipv6 = ip logger.info("vlan_ip={}, vlan_ipv6={}".format(str(vlan_ip), str(vlan_ipv6))) # config decap decap_conf_template = Template( open("../ansible/roles/test/templates/decap_conf.j2").read()) src_ports = set() topology = tbinfo["topo"]["properties"]["topology"] if "host_interfaces" in topology: src_ports.update(topology["host_interfaces"]) if "disabled_host_interfaces" in topology: for intf in topology["disabled_host_interfaces"]: src_ports.discard(intf) if "VMs" in topology: for k, v in topology["VMs"].items(): src_ports.update(v["vlans"]) decap_conf_vars = { "outer_ipv4": to_bool(request.config.getoption("outer_ipv4")), "outer_ipv6": to_bool(request.config.getoption("outer_ipv6")), "inner_ipv4": to_bool(request.config.getoption("inner_ipv4")), "inner_ipv6": to_bool(request.config.getoption("inner_ipv6")), "lo_ip": str(lo_ip), "lo_ipv6": str(lo_ipv6), "op": "SET", "dscp_mode": dscp_mode, "ecn_mode": ecn_mode, "ttl_mode": ttl_mode, } duthost.copy(content=decap_conf_template.render(**decap_conf_vars), dest="/tmp/decap_conf.json") duthost.shell("docker cp /tmp/decap_conf.json swss:/decap_conf.json") duthost.shell('docker exec swss sh -c "swssconfig /decap_conf.json"') # Prepare PTFf docker prepare_ptf(ptfhost, tbinfo, cfg_facts) setup_info = { "src_ports": ",".join([str(port) for port in src_ports]), "router_mac": cfg_facts["DEVICE_METADATA"]["localhost"]["mac"], "vlan_ip": str(vlan_ip) if vlan_ip else "", "vlan_ipv6": str(vlan_ipv6) if vlan_ipv6 else "", } setup_info.update(decap_conf_vars) logger.info(json.dumps(setup_info, indent=2)) yield setup_info # Remove decap configuration decap_conf_vars["op"] = "DEL" duthost.copy(content=decap_conf_template.render(**decap_conf_vars), dest="/tmp/decap_conf.json") duthost.shell("docker cp /tmp/decap_conf.json swss:/decap_conf.json") duthost.shell('docker exec swss sh -c "swssconfig /decap_conf.json"')
def validate(self, path, value, schema): """Raise an error if value is not allowed by schema.""" if self.is_dict(schema) and '_' in schema: schema = schema['_'] if self.is_dict(schema): print path + '/_ has invalid schema: ' + repr(schema) print 'Schema for _ cannot be a dict.' raise XmlSchemaError try: if isinstance(schema, six.string_types): if schema.startswith('%'): value = schema.format(value) elif schema.startswith('^') and schema.endswith('$'): if re.match(schema, str(value)) is None: raise XmlValueError elif schema[:4] == 'bool': match = re.match('^bool(\(([^,]*)(,(.*))?\))?$', schema) trueval = 'True' if match.group( 2) is None else match.group(2) falseval = 'False' if match.group( 4) is None else match.group(4) value = trueval if core.to_bool(value) else falseval elif schema == 'ipaddr': value = ipaddr.ipaddr(value) if value in (None, False): raise XmlValueError elif schema == 'ipv4': value = ipaddr.ipv4(value) if value in (None, False): raise XmlValueError elif schema == 'ipv6': value = ipaddr.ipv6(value) if value in (None, False): raise XmlValueError elif schema[:5] == 'range': m = re.match('^range(\(([^,]*)(,([^,]*)(,(.*))?)?\))?$', schema) if len(m.group(2)) > 0: if float(value) < float(m.group(2)): raise XmlValueError if len(m.group(4)) > 0: if float(value) > float(m.group(4)): raise XmlValueError if len(m.group(6)) > 0: value = m.group(6).format(float(value)) elif schema[:8] == 'strftime': m = re.match('^strftime(\((.*)\))?$', schema) datefmt = '%x' if m.group(2) is None else m.group(2) value = dateutil.parser.parse(value).strftime(datefmt) else: raise XmlSchemaError elif self.is_sequence(schema): if value not in schema: raise XmlValueError elif schema is None: value = None else: raise XmlSchemaError except XmlSchemaError: print path + ' has invalid schema: ' + repr(schema) raise except XmlValueError: print path + '=' + repr(value) + ' does not match schema: ' + repr( schema) raise return value
def setup_teardown(request, duthosts, fib_info_files, duts_running_config_facts, ttl_dscp_params): is_multi_asic = duthosts[0].sonichost.is_multi_asic ecn_mode = "copy_from_outer" dscp_mode = ttl_dscp_params['dscp'] ttl_mode = ttl_dscp_params['ttl'] # The hostvars dict has definitions defined in ansible/group_vars/sonic/variables hostvars = duthosts[0].host.options["variable_manager"]._hostvars[ duthosts[0].hostname] sonic_hwsku = duthosts[0].sonichost.facts["hwsku"] mellanox_hwskus = hostvars.get("mellanox_hwskus", []) if sonic_hwsku in mellanox_hwskus: dscp_mode = "uniform" ecn_mode = "standard" setup_info = { "outer_ipv4": to_bool(request.config.getoption("outer_ipv4")), "outer_ipv6": to_bool(request.config.getoption("outer_ipv6")), "inner_ipv4": to_bool(request.config.getoption("inner_ipv4")), "inner_ipv6": to_bool(request.config.getoption("inner_ipv6")), "dscp_mode": dscp_mode, "ecn_mode": ecn_mode, "ttl_mode": ttl_mode, "fib_info_files": fib_info_files[:3], # Test at most 3 DUTs in case of multi-DUT "ignore_ttl": True if is_multi_asic else False, "max_internal_hops": 3 if is_multi_asic else 0, } # config decap decap_conf_template = Template( open("../ansible/roles/test/templates/decap_conf.j2").read()) lo_ips = [] lo_ipv6s = [] for duthost in duthosts: cfg_facts = duts_running_config_facts[duthost.hostname] lo_ip = None lo_ipv6 = None # Loopback0 ip is same on all ASICs for addr in cfg_facts[0]["LOOPBACK_INTERFACE"]["Loopback0"]: ip = IPNetwork(addr).ip if ip.version == 4 and not lo_ip: lo_ip = str(ip) elif ip.version == 6 and not lo_ipv6: lo_ipv6 = str(ip) lo_ips.append(lo_ip) lo_ipv6s.append(lo_ipv6) decap_conf_vars = {"lo_ip": lo_ip, "lo_ipv6": lo_ipv6, "op": "SET"} decap_conf_vars.update(setup_info) duthost.copy(content=decap_conf_template.render(**decap_conf_vars), dest="/tmp/decap_conf.json") decap_conf_vars["op"] = "DEL" duthost.copy(content=decap_conf_template.render(**decap_conf_vars), dest="/tmp/decap_conf_del.json") for asic_id in duthost.get_frontend_asic_ids(): duthost.shell( "docker cp /tmp/decap_conf.json swss{}:/decap_conf.json". format(asic_id if asic_id is not None else "")) duthost.shell( 'docker exec swss{} sh -c "swssconfig /decap_conf.json"'. format(asic_id if asic_id is not None else "")) setup_info['lo_ips'] = lo_ips setup_info['lo_ipv6s'] = lo_ipv6s setup_info['router_macs'] = [ duthost.facts['router_mac'] for duthost in duthosts ] logger.info(json.dumps(setup_info, indent=2)) yield setup_info # Remove decap configuration for duthost in duthosts: for asic_id in duthost.get_frontend_asic_ids(): duthost.shell( "docker cp /tmp/decap_conf_del.json swss{}:/decap_conf_del.json" .format(asic_id if asic_id is not None else "")) duthost.shell( 'docker exec swss{} sh -c "swssconfig /decap_conf_del.json"'. format(asic_id if asic_id is not None else ""))