Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi asic]:parameterize asic_index and dut_index #2245

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions ansible/library/config_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def create_maps(config):
}


def get_running_config(module):

rt, out, err = module.run_command("sonic-cfggen -d --print-data")
def get_running_config(module, namespace):
cmd = "sonic-cfggen -d --print-data"
if namespace:
cmd += " -n {}".format(namespace)
rt, out, err = module.run_command(cmd)
if rt != 0:
module.fail_json(msg="Failed to dump running config! {}".format(err))
json_info = json.loads(out)
Expand All @@ -111,14 +113,15 @@ def main():
host=dict(required=True),
source=dict(required=True, choices=["running", "persistent"]),
filename=dict(),
namespace=dict(default=None),
),
supports_check_mode=True
)

m_args = module.params
try:
config = {}

namespace = m_args['namespace']
if m_args["source"] == "persistent":
if 'filename' in m_args and m_args['filename'] is not None:
cfg_file_path = "%s" % m_args['filename']
Expand All @@ -127,7 +130,7 @@ def main():
with open(cfg_file_path, "r") as f:
config = json.load(f)
elif m_args["source"] == "running":
config = get_running_config(module)
config = get_running_config(module, namespace)
results = get_facts(config)
module.exit_json(ansible_facts=results)
except Exception as e:
Expand Down
21 changes: 12 additions & 9 deletions tests/bgp/test_bgp_fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
pytest.mark.device_type('vs')
]

def test_bgp_facts(duthost):
def test_bgp_facts(duthosts, dut_index, asic_index):
"""compare the bgp facts between observed states and target state"""

bgp_facts = duthost.bgp_facts()['ansible_facts']
mg_facts = duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
duthost = duthosts[dut_index]
bgp_facts =duthost.bgp_facts(instance_id=asic_index)['ansible_facts']
namespace = duthost.get_namespace_from_asic_id(asic_index)
config_facts = duthost.config_facts(host=duthost.hostname, source="running",namespace=namespace)['ansible_facts']

for k, v in bgp_facts['bgp_neighbors'].items():
# Verify bgp sessions are established
assert v['state'] == 'established'
# Verify locat ASNs in bgp sessions
assert v['local AS'] == mg_facts['minigraph_bgp_asn']
assert v['local AS'] == int(config_facts['DEVICE_METADATA']['localhost']['bgp_asn'].decode("utf-8"))

for k, v in config_facts['BGP_NEIGHBOR'].items():
# Compare the bgp neighbors name with config db bgp neigbhors name
assert v['name'] == bgp_facts['bgp_neighbors'][k]['description']
# Compare the bgp neighbors ASN with config db
assert int(v['asn'].decode("utf-8")) == bgp_facts['bgp_neighbors'][k]['remote AS']

for v in mg_facts['minigraph_bgp']:
# Compare the bgp neighbors name with minigraph bgp neigbhors name
assert v['name'] == bgp_facts['bgp_neighbors'][v['addr'].lower()]['description']
# Compare the bgp neighbors ASN with minigraph
assert v['asn'] == bgp_facts['bgp_neighbors'][v['addr'].lower()]['remote AS']
7 changes: 6 additions & 1 deletion tests/common/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from errors import RunAnsibleModuleFail
from errors import UnsupportedAnsibleModule

from tests.common.helpers.constants import DEFAULT_ASIC_ID, DEFAULT_NAMESPACE, NAMESPACE_PREFIX

# HACK: This is a hack for issue https://github.com/Azure/sonic-mgmt/issues/1941 and issue
# https://github.com/ansible/pytest-ansible/issues/47
Expand Down Expand Up @@ -1016,6 +1016,11 @@ def show_and_parse(self, show_cmd, **kwargs):
"""
output = self.shell(show_cmd, **kwargs)["stdout_lines"]
return self._parse_show(output)

def get_namespace_from_asic_id(self, asic_id):
if asic_id is DEFAULT_ASIC_ID:
return DEFAULT_NAMESPACE
return "{}{}".format(NAMESPACE_PREFIX, asic_id)


class EosHost(AnsibleHostBase):
Expand Down
6 changes: 6 additions & 0 deletions tests/common/helpers/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

DEFAULT_ASIC_ID = None
DEFAULT_NAMESPACE = None
NAMESPACE_PREFIX = 'asic'
ASIC_PARAM_TYPE_ALL = 'num_asics'
ASIC_PARAM_TYPE_FRONTEND = 'frontend_asics'
50 changes: 50 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from tests.common.fixtures.conn_graph_facts import conn_graph_facts
from tests.common.devices import SonicHost, Localhost
from tests.common.devices import PTFHost, EosHost, FanoutHost
from tests.common.helpers.constants import ASIC_PARAM_TYPE_ALL, ASIC_PARAM_TYPE_FRONTEND, DEFAULT_ASIC_ID

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -455,3 +456,52 @@ def disable_container_autorestart(duthost, request):
for name, state in container_autorestart_states.items():
if state == "enabled":
duthost.command(cmd_enable.format(name))

def generate_param_asic_index(request, dut_indices, param_type):
logging.info("generating {} asic indicies for DUT [{}] in ".format(param_type, dut_indices))
inv_file = request.config.getoption("ansible_inventory")
tbname = request.config.getoption("--testbed")
tbfile = request.config.getoption("--testbed_file")
if tbname is None or tbfile is None:
raise ValueError("testbed and testbed_file are required!")

with open(inv_file, 'r') as fh:
inv = yaml.safe_load(fh)

hosts = inv['all']['children']['sonic']['hosts']
tbinfo = TestbedInfo(tbfile)

#if the params are not present treat the device as a single asic device
asic_index_params = [DEFAULT_ASIC_ID]

for dut_id in dut_indices:
dut = tbinfo.testbed_topo[tbname]["duts"][dut_id]
inv_data = hosts[dut]
if param_type == ASIC_PARAM_TYPE_ALL and ASIC_PARAM_TYPE_ALL in inv_data:
asic_index_params = range(int(inv_data[ASIC_PARAM_TYPE_ALL]))
if param_type == ASIC_PARAM_TYPE_FRONTEND and ASIC_PARAM_TYPE_ALL in inv_data:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use elif here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in the latest commit

asic_index_params = inv_data[ASIC_PARAM_TYPE_ALL]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more robust to use ansible.inventory.manager.InventoryManager to load inventory file and get host variables. Example: https://github.com/Azure/sonic-mgmt/blob/master/ansible/library/testbed_vm_info.py#L91
Using yaml.safe_load assumes that the inventory file must be yaml format. But anyway it is OK to use yaml.safe_load for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use the inventory manager.

logging.info("dut_index {} dut name {} asics params = {}".format(
dut_id, dut, asic_index_params))
return asic_index_params

def generate_params_dut_index(request):
tbname = request.config.getoption("--testbed")
tbfile = request.config.getoption("--testbed_file")
if tbname is None or tbfile is None:
raise ValueError("testbed and testbed_file are required!")
tbinfo = TestbedInfo(tbfile)
num_duts = len(tbinfo.testbed_topo[tbname]["duts"])
logging.info("Num of duts in testbed topology {}".format(num_duts))
return range(num_duts)

def pytest_generate_tests(metafunc):
# The topology always has atleast 1 dut
dut_indices = [0]
if "dut_index" in metafunc.fixturenames:
dut_indices = generate_params_dut_index(metafunc)
metafunc.parametrize("dut_index",dut_indices)
if "asic_index" in metafunc.fixturenames:
metafunc.parametrize("asic_index",generate_param_asic_index(metafunc, dut_indices, ASIC_PARAM_TYPE_ALL))
if "frontend_asic_index" in metafunc.fixturenames:
metafunc.parametrize("frontend_asic_index",generate_param_asic_index(metafunc, dut_indices, ASIC_PARAM_TYPE_FRONTEND))