コード例 #1
0
 def setUp(self):
     self.port2alias = load_module_from_source('port2alias',
                                               port2alias_path)
     self.ports = {
         "Ethernet1": {
             "alias": "fortyG0/1"
         },
         "Ethernet2": {
             "alias": "fortyG0/2"
         },
         "Ethernet10": {
             "alias": "fortyG0/10"
         },
         "Ethernet_11": {
             "alias": "fortyG0/11"
         },
     }
コード例 #2
0
import json
import os
import sys
from io import StringIO
from unittest import mock

from utilities_common.general import load_module_from_source

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)

# Load the file under test
aclshow_path = os.path.join(scripts_path, 'aclshow')
aclshow = load_module_from_source('aclshow', aclshow_path)

from .mock_tables import dbconnector

# Expected output for aclshow
default_output = """\
RULE NAME     TABLE NAME      PRIO    PACKETS COUNT    BYTES COUNT
------------  ------------  ------  ---------------  -------------
RULE_1        DATAACL         9999              101            100
RULE_2        DATAACL         9998              201            200
RULE_3        DATAACL         9997              301            300
RULE_4        DATAACL         9996              401            400
RULE_7        DATAACL         9993              701            700
RULE_9        DATAACL         9991              901            900
RULE_10       DATAACL         9989             1001           1000
DEFAULT_RULE  DATAACL            1                2              1
コード例 #3
0
from click.testing import CliRunner
from unittest import mock
from utilities_common.db import Db
from utilities_common.general import load_module_from_source
from minigraph import minigraph_encoder

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(SCRIPT_DIR, "config_override_input")
EMPTY_INPUT = os.path.join(DATA_DIR, "empty_input.json")
PARTIAL_CONFIG_OVERRIDE = os.path.join(DATA_DIR, "partial_config_override.json")
NEW_FEATURE_CONFIG = os.path.join(DATA_DIR, "new_feature_config.json")
FULL_CONFIG_OVERRIDE = os.path.join(DATA_DIR, "full_config_override.json")

# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
sonic_cfggen = load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen')


def write_init_config_db(cfgdb, config):
    tables = cfgdb.get_config()
    # delete all tables then write config to configDB
    for table in tables:
        cfgdb.delete_table(table)
    data = dict()
    sonic_cfggen.deep_update(
        data, sonic_cfggen.FormatConverter.to_deserialized(config))
    cfgdb.mod_config(sonic_cfggen.FormatConverter.output_to_db(data))
    return


def read_config_db(cfgdb):
コード例 #4
0
import pytest
from click.testing import CliRunner
from utilities_common.general import load_module_from_source

from .mock_tables import dbconnector

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, 'scripts')
sys.path.insert(0, modules_path)

sys.modules['sonic_platform'] = mock.MagicMock()

decode_syseeprom_path = os.path.join(scripts_path, 'decode-syseeprom')
decode_syseeprom = load_module_from_source('decode-syseeprom',
                                           decode_syseeprom_path)

# Replace swsscommon objects with mocked objects
decode_syseeprom.SonicV2Connector = dbconnector.SonicV2Connector

SAMPLE_TLV_DICT = {
    'header': {
        'id': 'TlvInfo',
        'version': '1',
        'length': '170'
    },
    'tlv_list': [{
        'code': '0x21',
        'name': 'Product Name',
        'length': '8',
        'value': 'S6100-ON'
コード例 #5
0
from click.testing import CliRunner
from unittest import mock

import show.main as show
import clear.main as clear

from .utils import get_result_and_return_code
from utilities_common.general import load_module_from_source

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, scripts_path)

flow_counters_stat_path = os.path.join(scripts_path, 'flow_counters_stat')
flow_counters_stat = load_module_from_source('flow_counters_stat', flow_counters_stat_path)

expect_show_output = """\
  Trap Name    Packets    Bytes      PPS
-----------  ---------  -------  -------
       dhcp        100    2,000  50.25/s
"""

expect_show_output_json = """\
{
    "dhcp": {
        "Bytes": "2,000",
        "PPS": "50.25/s",
        "Packets": "100"
    }
}
コード例 #6
0
import json
import os
import re
from unittest import mock

import pytest
from click.testing import CliRunner
from utilities_common.db import Db
from utilities_common.general import load_module_from_source

import config.main as config

# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
sonic_cfggen = load_module_from_source('sonic_cfggen',
                                       '/usr/local/bin/sonic-cfggen')

# Import config_mgmt.py
config_mgmt_py_path = os.path.join(os.path.dirname(__file__), '..', 'config',
                                   'config_mgmt.py')
config_mgmt = load_module_from_source('config_mgmt', config_mgmt_py_path)

# Sample platform.json for Test
BRKOUT_CFG_FILE_JSON = {
    "interfaces": {
        "Ethernet0": {
            "index": "1,1,1,1",
            "lanes": "65,66,67,68",
            "breakout_modes": {
                "1x100G[40G]": ["Eth1"],
                "2x50G": ["Eth1/1", "Eth1/3"],
                "4x25G[10G]": ["Eth1/1", "Eth1/2", "Eth1/3", "Eth1/4"]
コード例 #7
0
import os
import sys
from json import dump
from copy import deepcopy
from unittest import mock, TestCase

import pytest
from utilities_common.general import load_module_from_source

# Import file under test i.e., config_mgmt.py
config_mgmt_py_path = os.path.join(os.path.dirname(__file__), '..', 'config', 'config_mgmt.py')
config_mgmt = load_module_from_source('config_mgmt', config_mgmt_py_path)

model = "module test_name {namespace urn:test_urn; prefix test_prefix;}"

class TestConfigMgmt(TestCase):
    '''
        Test Class for config_mgmt.py
    '''

    def setUp(self):
        config_mgmt.CONFIG_DB_JSON_FILE = "startConfigDb.json"
        config_mgmt.DEFAULT_CONFIG_DB_JSON_FILE = "portBreakOutConfigDb.json"
        return

    def test_config_get_module_check(self):
        curConfig = deepcopy(configDbJson)
        self.writeJson(curConfig, config_mgmt.CONFIG_DB_JSON_FILE)
        cm = config_mgmt.ConfigMgmt(source=config_mgmt.CONFIG_DB_JSON_FILE)
        assert cm.get_module_name(model) == "test_name"
        return
コード例 #8
0
import subprocess
import sys
from unittest import mock

import pytest
from swsscommon.swsscommon import ConfigDBConnector
from utilities_common.general import load_module_from_source

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)

# Load the file under test
neighbor_advertiser_path = os.path.join(scripts_path, 'neighbor_advertiser')
neighbor_advertiser = load_module_from_source('neighbor_advertiser',
                                              neighbor_advertiser_path)


class TestNeighborAdvertiser(object):
    @pytest.fixture
    def set_up(self):
        neighbor_advertiser.connect_config_db()
        neighbor_advertiser.connect_app_db()

    def test_neighbor_advertiser_slice(self, set_up):
        neighbor_advertiser.get_link_local_addr = mock.MagicMock(
            return_value='fe80::1e34:daff:fe1e:2800')
        output = neighbor_advertiser.construct_neighbor_advertiser_slice()
        expected_output = dict({
            'respondingSchemes': {
                'durationInSec': 300
コード例 #9
0
 def setUp(self):
     self.port2alias = load_module_from_source('port2alias',
                                               port2alias_path)
コード例 #10
0
import pytest
from click.testing import CliRunner
from utilities_common.general import load_module_from_source

from .mock_tables import dbconnector

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, 'scripts')
sys.path.insert(0, modules_path)

sys.modules['sonic_platform'] = mock.MagicMock()

# Load the file under test
psushow_path = os.path.join(scripts_path, 'psushow')
psushow = load_module_from_source('psushow', psushow_path)

# Replace swsscommon objects with mocked objects
psushow.SonicV2Connector = dbconnector.SonicV2Connector


class TestPsushow(object):
    def test_get_psu_status_list(self):
        expected_psu_status_list = [{
            'index': '1',
            'name': 'PSU 1',
            'presence': 'true',
            'status': 'OK',
            'led_status': 'green',
            'model': '0J6J4K',
            'serial': 'CN-0J6J4K-17972-5AF-0086-A00',
コード例 #11
0
import os

from click.testing import CliRunner
from utilities_common.general import load_module_from_source

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")

# Load the file under test
lldpshow_path = os.path.join(scripts_path, 'lldpshow')
lldpshow = load_module_from_source('lldpshow', lldpshow_path)

# Expected output for 2 remote MACs on same physical interface
expected_2MACs_Ethernet0_output = \
('Capability codes: (R) Router, (B) Bridge, (O) Other\n'
 'LocalPort    RemoteDevice    RemotePortID       Capability    '
 'RemotePortDescr\n'
 '-----------  --------------  -----------------  ------------  '
 '-----------------\n'
 'Ethernet0    dummy           00:00:00:00:00:01  BR            First MAC\n'
 'Ethernet0    dummy           00:00:00:00:00:02  R             Second MAC\n'
 '--------------------------------------------------\n'
 'Total entries displayed:  2')

expected_lldpctl_xml_output = \
['<?xml version="1.0" encoding="UTF-8"?>\n\
        <lldp label="LLDP neighbors">\n\
        <interface label="Interface" name="Ethernet0" via="LLDP" rid="2" age="7 days, 22:11:33">\n\
            <chassis label="Chassis">\n\
                <id label="ChassisID" type="mac">00:00:00:00:00:01</id>\n\
コード例 #12
0
from utilities_common.general import load_module_from_source

TESTS_DIR_PATH = os.path.dirname(os.path.abspath(__file__))
UTILITY_DIR_PATH = os.path.dirname(TESTS_DIR_PATH)
SCRIPTS_DIR_PATH = os.path.join(UTILITY_DIR_PATH, "scripts")
sys.path.append(SCRIPTS_DIR_PATH)

ABOOT_MACHINE_CFG_PLATFORM = "aboot_platform=x86_64-arista_7050cx3_32s"
ABOOT_MACHINE_CFG_ARCH = "aboot_arch=x86_64"
KERNEL_BOOTING_CFG_KDUMP_DISABLED = "loop=image-20201231.63/fs.squashfs loopfstype=squashfs"
KERNEL_BOOTING_CFG_KDUMP_ENABLED = "loop=image-20201231.63/fs.squashfs loopfstype=squashfs crashkernel=0M-2G:256MB"

logger = logging.getLogger(__name__)
# Load `sonic-kdump-config` module from source since `sonic-kdump-config` does not have .py extension.
sonic_kdump_config_path = os.path.join(SCRIPTS_DIR_PATH, "sonic-kdump-config")
sonic_kdump_config = load_module_from_source("sonic_kdump_config",
                                             sonic_kdump_config_path)


class TestSonicKdumpConfig(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        print("SETUP")

    @patch("sonic_kdump_config.run_command")
    def test_read_num_kdumps(self, mock_run_cmd):
        """Tests the function `read_num_kdumps(...)` in script `sonic-kdump-config`.
        """
        mock_run_cmd.return_value = (0, ["0"], None)
        num_dumps = sonic_kdump_config.read_num_dumps()
        assert num_dumps == 0
コード例 #13
0
import os
import sys
import pytest
from unittest import mock
from .mock_tables import dbconnector
from utilities_common.general import load_module_from_source

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, 'scripts')
sys.path.insert(0, scripts_path)

memory_threshold_check_path = os.path.join(scripts_path,
                                           'memory_threshold_check.py')
memory_threshold_check = load_module_from_source('memory_threshold_check.py',
                                                 memory_threshold_check_path)


@pytest.fixture()
def setup_dbs_regular_mem_usage():
    cfg_db = dbconnector.dedicated_dbs.get('CONFIG_DB')
    state_db = dbconnector.dedicated_dbs.get('STATE_DB')
    dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(
        test_path, 'memory_threshold_check', 'config_db')
    dbconnector.dedicated_dbs['STATE_DB'] = os.path.join(
        test_path, 'memory_threshold_check', 'state_db')
    yield
    dbconnector.dedicated_dbs['CONFIG_DB'] = cfg_db
    dbconnector.dedicated_dbs['STATE_DB'] = state_db

コード例 #14
0
import os
import sys
from unittest import TestCase

from utilities_common.general import load_module_from_source

# Load the file under test
port2alias_path = os.path.join(os.path.dirname(__file__), '..', 'scripts',
                               'port2alias')
port2alias = load_module_from_source('port2alias', port2alias_path)


class TestPort2Alias(TestCase):
    def setUp(self):
        self.ports = {
            "Ethernet1": {
                "alias": "fortyG0/1"
            },
            "Ethernet2": {
                "alias": "fortyG0/2"
            },
            "Ethernet10": {
                "alias": "fortyG0/10"
            },
            "Ethernet_11": {
                "alias": "fortyG0/11"
            },
        }

    def test_translate_line_single_word(self):
        self.assertEqual(port2alias.translate_line("1", self.ports), "1")