This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
sonic-buildimage/src/sonic-yang-models/tests/yang_model_pytests/test_smart_switch.py
Oleksandr Ivantsiv 5f2a2b9051
[YANG] Add an infrastructure to write pytest tests for the YANG model. (#17767)
### Why I did it
The existing test cases and test data for YANG model tests are defined in JSON files. The tests are limited and require a huge amount of copy-paste to provide good coverage.
For the features that have a lot of similar attributes that need to be covered with the tests, the JSON files can be quite big and hard to maintain (for example CRM).
The debuggability of the JSON tests is almost zero. Which makes implementation of the new test cases much harder than it should be.  When the single test case fails it generates thousands of lines of output that make it hard to understand the issue. It is hard to find what failed, and what is the error message. It is impossible to re-run single test cases. Etc.

The Pytest provides an alternative way to implement the tests.
Both JSON tests and Pytests can co-exist. But it is suggested to use Pytest for the new tests.
The tests written in Pytest are compact, readable, and human-friendly. 
They give more flexibility in the testing of corner cases allow to leverage Python and Pytest capabilities to generate configuration and data for better coverage.

### How I did it
Add an infrastructure that allows to write model tests in Python.
Conver CRM, DASH CRM, and SmartSwitch tests to Pytest.

#### How to verify it
Build the YANG model package.
2024-03-18 09:52:25 -07:00

111 lines
3.5 KiB
Python

import pytest
class TestSmartSwitch:
def test_valid_data(self, yang_model):
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:MID_PLANE_BRIDGE": {
"GLOBAL": {
"bridge": "bridge_midplane",
"ip_prefix": "169.254.200.254/24"
}
},
"sonic-smart-switch:DPUS": {
"DPUS_LIST": [
{
"dpu_name": "dpu0",
"midplane_interface": "dpu0"
},
{
"dpu_name": "dpu1",
"midplane_interface": "dpu1"
}
]
}
}
}
yang_model.load_data(data)
@pytest.mark.parametrize(
"bridge_name, error_message", [
("bridge_midplane", None),
("wrong_name", 'Value "wrong_name" does not satisfy the constraint "bridge_midplane"')]
)
def test_bridge_name(self, yang_model, bridge_name, error_message):
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:MID_PLANE_BRIDGE": {
"GLOBAL": {
"bridge": bridge_name,
"ip_prefix": "169.254.200.254/24"
}
}
}
}
yang_model.load_data(data, error_message)
@pytest.mark.parametrize(
"ip_prefix, error_message", [
("169.254.200.254/24", None),
("169.254.xyz.254/24", 'Value "169.254.xyz.254/24" does not satisfy the constraint')]
)
def test_bridge_ip_prefix(self, yang_model, ip_prefix, error_message):
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:MID_PLANE_BRIDGE": {
"GLOBAL": {
"bridge": "bridge_midplane",
"ip_prefix": ip_prefix
}
}
}
}
yang_model.load_data(data, error_message)
@pytest.mark.parametrize(
"dpu_name, error_message", [
("dpu0", None),
("xyz", 'Value "xyz" does not satisfy the constraint "dpu[0-9]+')]
)
def test_dpu_name(self, yang_model, dpu_name, error_message):
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:DPUS": {
"DPUS_LIST": [
{
"dpu_name": dpu_name,
"midplane_interface": "dpu0"
}
]
}
}
}
yang_model.load_data(data, error_message)
@pytest.mark.parametrize(
"midplane_interface, error_message", [
("dpu0", None),
("xyz", 'Value "xyz" does not satisfy the constraint "dpu[0-9]+')]
)
def test_dpu_midplane_interface(self, yang_model, midplane_interface, error_message):
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:DPUS": {
"DPUS_LIST": [
{
"dpu_name": "dpu0",
"midplane_interface": midplane_interface
}
]
}
}
}
yang_model.load_data(data, error_message)