Skip to content

Commit

Permalink
[tests/common/devices] Add FanoutHost (#1532)
Browse files Browse the repository at this point in the history
* [tests/common/devices] Add FanoutHost

- Add FanoutHost which aggregate different fanout switch host
- Add fanouthosts fixture shortcut to get fanouthost easily
- Support select host instance by os type
- Support get fanout host by both mgmtip and hostname

Co-authored-by: Jing Kan<jika@microsoft.com>
  • Loading branch information
Blueve authored Apr 9, 2020
1 parent ff709d2 commit 3047be2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
50 changes: 50 additions & 0 deletions tests/common/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,53 @@ def __init__(self, ansible_adhoc, hostname, user, passwd, gather_facts=False):
'ansible_password': passwd, \
'ansible_become_method': 'enable' }
self.host.options['variable_manager'].extra_vars.update(evars)

def shutdown(self, interface_name):
out = self.host.eos_config(
lines=['shutdown'],
parents='interface %s' % interface_name)
logging.info('Shut interface [%s]' % interface_name)
return out

def no_shutdown(self, interface_name):
out = self.host.eos_config(
lines=['no shutdown'],
parents='interface %s' % interface_name)
logging.info('No shut interface [%s]' % interface_name)
return out

class FanoutHost():
"""
@summary: Class for Fanout switch
For running ansible module on the Fanout switch
"""

def __init__(self, ansible_adhoc, os, hostname, device_type, user, passwd):
self.hostname = hostname
self.type = device_type
if os == 'sonic':
self.os = os
self.host = SonicHost(ansible_adhoc, hostname)
else:
# Use eos host if the os type is unknown
self.os = 'eos'
self.host = EosHost(ansible_adhoc, hostname, user, passwd)

def get_fanout_os(self):
return self.os

def get_fanout_type(self):
return self.type

def shutdown(self, interface_name):
self.host.shutdown(interface_name)

def no_shutdown(self, interface_name):
self.host.no_shutdown(interface_name)

def __str__(self):
return "{ os: '%s', hostname: '%s', device_type: '%s' }" % (self.os, self.hostname, self.type)

def __repr__(self):
return self.__str__()
35 changes: 33 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from ansible_host import AnsibleHost
from collections import defaultdict
from common.devices import SonicHost, Localhost, PTFHost, EosHost
from common.fixtures.conn_graph_facts import conn_graph_facts
from common.devices import SonicHost, Localhost, PTFHost, EosHost, FanoutHost

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -190,7 +191,7 @@ def ptfhost(testbed_devices):
@pytest.fixture(scope="module")
def nbrhosts(ansible_adhoc, testbed, creds):
"""
Shortcut fixture for getting PTF host
Shortcut fixture for getting VM host
"""

vm_base = int(testbed['vm_base'][2:])
Expand All @@ -199,6 +200,36 @@ def nbrhosts(ansible_adhoc, testbed, creds):
devices[k] = EosHost(ansible_adhoc, "VM%04d" % (vm_base + v['vm_offset']), creds['eos_login'], creds['eos_password'])
return devices

@pytest.fixture(scope="module")
def fanouthosts(ansible_adhoc, conn_graph_facts, creds):
"""
Shortcut fixture for getting Fanout hosts
"""

with open('../ansible/testbed-new.yaml') as stream:
testbed_doc = yaml.safe_load(stream)

fanout_types = ['FanoutLeaf', 'FanoutRoot']
devices = {}
for hostname in conn_graph_facts['device_info'].keys():
device_info = conn_graph_facts['device_info'][hostname]
if device_info['Type'] in fanout_types:
# Use EOS if the target OS type is unknown
os = 'eos' if 'os' not in testbed_doc['devices'][hostname] else testbed_doc['devices'][hostname]['os']
device_exists = False
try:
fanout_host = FanoutHost(ansible_adhoc, os, hostname, device_info['Type'], creds['fanout_admin_user'], creds['fanout_admin_password'])
device_exists = True
except:
logging.warning("Couldn't found the given host(%s) in inventory file" % hostname)

if device_exists:
# Index fanout host by both hostname and mgmt ip
devices[hostname] = fanout_host
devices[device_info['mgmtip']] = fanout_host

return devices

@pytest.fixture(scope='session')
def eos():
""" read and yield eos configuration """
Expand Down

0 comments on commit 3047be2

Please sign in to comment.