Skip to content

[sonic-cfggen]: Update UT to run yang validation #9700

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

Merged
merged 21 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
36 changes: 36 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import json
import subprocess
import os
import re
import sys

import tests.common_utils as utils

from unittest import TestCase

PY3x = sys.version_info >= (3, 0)
if PY3x:
import sonic_yang
YANG_MODELS_DIR = "/usr/local/yang-models"

TOR_ROUTER = 'ToRRouter'
BACKEND_TOR_ROUTER = 'BackEndToRRouter'
LEAF_ROUTER = 'LeafRouter'
Expand All @@ -14,6 +21,9 @@
class TestCfgGen(TestCase):

def setUp(self):
if PY3x:
self.yang_parser = sonic_yang.SonicYang(YANG_MODELS_DIR)
self.yang_parser.loadYangModel()
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen')
self.sample_graph = os.path.join(self.test_dir, 'sample_graph.xml')
Expand Down Expand Up @@ -49,6 +59,32 @@ def tearDown(self):

def run_script(self, argument, check_stderr=False, verbose=False):
print('\n Running sonic-cfggen ' + argument)
if PY3x and "-m" in argument:
pattern = r'-m\s+(\S+)\s*'
minigraph = re.findall(r'-m\s+(\S+)\s*', argument)
hwsku = re.findall(r'-S\s+(\S+)\s*', argument)
port_conf = re.findall(r'-p\s+(\S+)\s*', argument)
namespace = re.findall(r'-n\s+(\S+)\s*', argument)
if minigraph:
print('\n Validating yang schema')
cmd = self.script_file + ' -m ' + minigraph[0]
if hwsku:
cmd += ' -S ' + hwsku[0]
if port_conf:
cmd += ' -p ' + port_conf[0]
if namespace:
cmd += ' -n ' + namespace[0]
cmd += ' --print-data'
output = subprocess.check_output(cmd, shell=True).decode()
# "NULL": "NULL" is placeholder for redis, remove to pass yang validation.
output = output.replace("\"NULL\": \"NULL\"", "")
self.yang_parser.loadData(configdbJson=json.loads(output))
try:
self.yang_parser.validate_data_tree()
except sonic_yang.SonicYangException as e:
print("yang data generated from %s is not valid"%(minigraph[0]))
raise

if check_stderr:
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
else:
Expand Down
36 changes: 36 additions & 0 deletions src/sonic-config-engine/tests/test_minigraph_case.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import json
import os
import subprocess
import re
import sys

import tests.common_utils as utils
import minigraph

from unittest import TestCase

PY3x = sys.version_info >= (3, 0)
if PY3x:
import sonic_yang
YANG_MODELS_DIR = "/usr/local/yang-models"

TOR_ROUTER = 'ToRRouter'
BACKEND_TOR_ROUTER = 'BackEndToRRouter'

class TestCfgGenCaseInsensitive(TestCase):

def setUp(self):
if PY3x:
self.yang_parser = sonic_yang.SonicYang(YANG_MODELS_DIR)
self.yang_parser.loadYangModel()
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen')
self.sample_graph = os.path.join(self.test_dir, 'simple-sample-graph-case.xml')
Expand All @@ -23,6 +33,32 @@ def setUp(self):

def run_script(self, argument, check_stderr=False):
print('\n Running sonic-cfggen ' + argument)
if PY3x and "-m" in argument:
pattern = r'-m\s+(\S+)\s*'
minigraph = re.findall(r'-m\s+(\S+)\s*', argument)
hwsku = re.findall(r'-S\s+(\S+)\s*', argument)
port_conf = re.findall(r'-p\s+(\S+)\s*', argument)
namespace = re.findall(r'-n\s+(\S+)\s*', argument)
if minigraph:
print('\n Validating yang schema')
cmd = self.script_file + ' -m ' + minigraph[0]
if hwsku:
cmd += ' -S ' + hwsku[0]
if port_conf:
cmd += ' -p ' + port_conf[0]
if namespace:
cmd += ' -n ' + namespace[0]
cmd += ' --print-data'
output = subprocess.check_output(cmd, shell=True).decode()
# "NULL": "NULL" is placeholder for redis, remove to pass yang validation.
output = output.replace("\"NULL\": \"NULL\"", "")
self.yang_parser.loadData(configdbJson=json.loads(output))
try:
self.yang_parser.validate_data_tree()
except sonic_yang.SonicYangException as e:
print("yang data generated from %s is not valid"%(minigraph[0]))
raise

if check_stderr:
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
else:
Expand Down
35 changes: 35 additions & 0 deletions src/sonic-config-engine/tests/test_multinpu_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import subprocess
import unittest
import yaml
import re
import sys

import tests.common_utils as utils

from unittest import TestCase

PY3x = sys.version_info >= (3, 0)
if PY3x:
import sonic_yang
YANG_MODELS_DIR = "/usr/local/yang-models"

SKU = 'multi-npu-01'
ASIC_SKU = 'multi-npu-asic'
Expand All @@ -20,6 +26,9 @@
class TestMultiNpuCfgGen(TestCase):

def setUp(self):
if PY3x:
self.yang_parser = sonic_yang.SonicYang(YANG_MODELS_DIR)
self.yang_parser.loadYangModel()
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.test_data_dir = os.path.join(self.test_dir, 'multi_npu_data')
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen')
Expand All @@ -33,6 +42,32 @@ def setUp(self):

def run_script(self, argument, check_stderr=False):
print('\n Running sonic-cfggen ' + argument)
if PY3x and "-m" in argument:
pattern = r'-m\s+(\S+)\s*'
minigraph = re.findall(r'-m\s+(\S+)\s*', argument)
hwsku = re.findall(r'-S\s+(\S+)\s*', argument)
port_conf = re.findall(r'-p\s+(\S+)\s*', argument)
namespace = re.findall(r'-n\s+(\S+)\s*', argument)
if minigraph:
print('\n Validating yang schema')
cmd = self.script_file + ' -m ' + minigraph[0]
if hwsku:
cmd += ' -S ' + hwsku[0]
if port_conf:
cmd += ' -p ' + port_conf[0]
if namespace:
cmd += ' -n ' + namespace[0]
cmd += ' --print-data'
output = subprocess.check_output(cmd, shell=True).decode()
# "NULL": "NULL" is placeholder for redis, remove to pass yang validation.
output = output.replace("\"NULL\": \"NULL\"", "")
self.yang_parser.loadData(configdbJson=json.loads(output))
try:
self.yang_parser.validate_data_tree()
except sonic_yang.SonicYangException as e:
print("yang data generated from %s is not valid"%(minigraph[0]))
raise

if check_stderr:
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
else:
Expand Down