Skip to content

Commit a0ce6eb

Browse files
Add Testcases with Set Replace (#386)
* Add TestCases for SetReplace * Document SetReplace Testclasses * Add Demo tests with SetReplace
1 parent 1ffabc6 commit a0ce6eb

File tree

5 files changed

+162
-12
lines changed

5 files changed

+162
-12
lines changed

oc_config_validate/demo/config_tests.yaml

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ tests:
7070
}
7171

7272
- !TestCase
73-
name: "Bad Compare mgmt Interface state JSON"
73+
name: "FAIL Compare mgmt Interface state JSON"
7474
class_name: get.GetCompare
7575
args:
7676
xpath: "interfaces/interface[name=mgmt]/state"
7777
want: {"openconfig-interfaces:enabled": false}
7878

7979
- !TestCase
80-
name: "Set Hostname"
80+
name: "Update Hostname"
8181
class_name: set.SetUpdate
8282
args:
8383
xpath: "system/config/hostname"
@@ -95,29 +95,40 @@ tests:
9595
}
9696

9797
- !TestCase
98-
name: "Bad model to compare System config"
98+
name: "Replace System Config"
99+
class_name: set.SetReplace
100+
args:
101+
xpath: "system/config"
102+
value: {
103+
"openconfig-system:hostname": "atestdevice",
104+
"openconfig-system:domain-name": "foo.bar.com"
105+
}
106+
107+
- !TestCase
108+
name: "FAIL Check System config - model"
99109
class_name: get.GetJsonCheckCompare
100110
args:
101111
xpath: "system/config"
102-
model: system.openconfig_system
112+
model: interfaces.openconfig_interfaces
103113
want_json: {
104114
"openconfig-system:domain-name": "foo.bar.com",
105115
"openconfig-system:hostname": "foobar"
106116
}
107117

108118
- !TestCase
109-
name: "Bad data to compare System config"
119+
name: "FAIL Compare System config - data"
110120
class_name: get.GetJsonCheckCompare
111121
args:
112122
xpath: "system/config"
113123
model: system.openconfig_system
114124
want_json: {
115125
"openconfig-system:domain-name": "la.la.la.com",
116-
"openconfig-system:hostname": "lalala"
126+
"openconfig-system:hostname": "lalala",
127+
"openconfig-system:motd-banner": "Welcome"
117128
}
118129

119130
- !TestCase
120-
name: "Set timezone with a valid Json blob"
131+
name: "Update timezone with a valid Json blob"
121132
class_name: set.JsonCheckSetUpdate
122133
args:
123134
xpath: "system/clock/config"
@@ -137,7 +148,7 @@ tests:
137148
}
138149

139150
- !TestCase
140-
name: "Set timezone with a valid Json blob"
151+
name: "Update timezone with a valid Json blob"
141152
class_name: setget.SetGetJsonCheck
142153
args:
143154
xpath: "system/clock/config"
@@ -147,7 +158,7 @@ tests:
147158
}
148159

149160
- !TestCase
150-
name: "Set timezone with a valid Json blob, and check it is Zurich"
161+
name: "Update timezone with a valid Json blob, and check it is Zurich"
151162
class_name: setget.SetGetJsonCheckCompare
152163
args:
153164
xpath: "system/clock/config"
@@ -157,11 +168,49 @@ tests:
157168
}
158169

159170
- !TestCase
160-
name: "Bad Set Clock Check State"
171+
name: "FAIL Update Clock Check State"
161172
class_name: config_state.SetConfigCheckState
162173
args:
163174
xpath: "system/clock"
164175
model: system.openconfig_system
165176
json_value: {
166177
"openconfig-system:timezone-name": "Europe/Paris"
167178
}
179+
180+
- !TestCase
181+
name: "FAIL Replace System Config - model"
182+
class_name: set.JsonCheckSetReplace
183+
args:
184+
xpath: "system/config"
185+
model: system.openconfig_system
186+
json_value: {
187+
"host": "zz-tri-dev01",
188+
"domain": "foo.bar.com",
189+
}
190+
191+
- !TestCase
192+
name: "Replace System Config"
193+
class_name: setget.SetGetJsonCheck
194+
args:
195+
xpath: "system/config"
196+
model: system.openconfig_system
197+
set_replace: True
198+
json_value: {
199+
"openconfig-system:hostname": "zz-tri-dev01",
200+
"openconfig-system:domain-name": "foo.bar.com",
201+
"openconfig-system:login-banner": "This device is for authorized use only",
202+
"openconfig-system:motd-banner": "Welcome to Open vSwitch"
203+
}
204+
205+
- !TestCase
206+
name: "Replace and Get System Config"
207+
class_name: setget.SetGetJsonCheckCompare
208+
args:
209+
xpath: "system/config"
210+
model: system.openconfig_system
211+
set_replace: True
212+
json_value: {
213+
"openconfig-system:hostname": "zz-tri-dev01",
214+
"openconfig-system:domain-name": "foo.bar.com",
215+
"openconfig-system:motd-banner": "Welcome to Open vSwitch"
216+
}

oc_config_validate/docs/testclasses.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
* `xpath`: gNMI path to delete.
6565

6666

67+
* `set.SetReplace`
68+
69+
Sends gNMI Set Replace of an xpath with a value.
70+
71+
Args:
72+
* `xpath`: gNMI path to update.
73+
* `value`: Value to set; can be numeric, string or JSON-IETF.
74+
75+
6776
* `set.JsonCheckSetUpdate`
6877

6978
Sends gNMI Set with a schema-checked JSON-IETF value.
@@ -74,6 +83,18 @@
7483
* `model`: Python binding class to check the JSON reply against.
7584
The binding classes are in the `oc_config_validate.models` package.
7685

86+
87+
* `set.JsonCheckSetReplace`
88+
89+
Sends gNMI Set with a schema-checked JSON-IETF value.
90+
91+
Args:
92+
* `xpath`: gNMI path to read.
93+
* `json_value`: JSON-IETF value to check and set.
94+
* `model`: Python binding class to check the JSON reply against.
95+
The binding classes are in the `oc_config_validate.models` package.
96+
97+
7798
### Module setget
7899

79100
* `setget.SetGetJsonCheck`
@@ -92,6 +113,7 @@
92113
The binding classes are in the `oc_config_validate.models` package.
93114
* `retries`: Optional. Number of retries if the assertion fails.
94115
* `retry_delay`: Optional. Delay, in seconds, between retries. Default 10.
116+
* `set_replace`: Optional. True to use gNMI SetReplace instead of SetUpdate. Default False.
95117

96118

97119
* `setget.SetGetJsonCheckCompare`
@@ -111,6 +133,7 @@
111133
The binding classes are in the `oc_config_validate.models` package.
112134
* `retries`: Optional. Number of retries if the assertion fails.
113135
* `retry_delay`: Optional. Delay, in seconds, between retries. Default 10.
136+
* `set_replace`: Optional. True to use gNMI SetReplace instead of SetUpdate. Default False.
114137

115138
### Module config_state
116139

oc_config_validate/oc_config_validate/testbase.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,27 @@ def gNMISetDelete(self, xpath: str) -> bool:
249249
return False
250250
return True
251251

252+
def gNMISetReplace(self, xpath: str, value: Any) -> bool:
253+
"""Send a gNMI Set Replace message to the test's target
254+
255+
Args:
256+
xpath: The gNMI path to update.
257+
value: Value to set; can be numeric, string or JSON-IETF.
258+
259+
Returns:
260+
False if the gNMI Set did not succeed.
261+
"""
262+
if LOG_GNMI:
263+
msg = ("gNMI Set Replace(%s) => %s", xpath, value)
264+
self.log(*msg)
265+
logging.info(*msg)
266+
try:
267+
self.test_target.gNMISetReplace(xpath, value)
268+
except Exception as err:
269+
self.log("Set(%s) <= Error: %s", xpath, err)
270+
return False
271+
return True
272+
252273
def gNMISubsOnce(self, xpaths: List[str]) -> Optional[
253274
List[gnmi_pb2.Notification]]:
254275
"""Send a gNMI Subscribe message using ONCE mode.

oc_config_validate/oc_config_validate/testcases/set.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ def testSetDelete(self):
4343
"gNMI Set did not succeed.")
4444

4545

46+
class SetReplace(testbase.TestCase):
47+
"""Sends gNMI Set of an xpath with a value.
48+
49+
All arguments are read from the Test YAML description.
50+
51+
Args:
52+
xpath: gNMI path to write.
53+
value: Value to set; can be numeric, string or JSON-IETF.
54+
"""
55+
xpath = ""
56+
value = None
57+
58+
def testSetReplace(self):
59+
""""""
60+
self.assertArgs(["xpath", "value"])
61+
self.assertXpath(self.xpath)
62+
self.assertTrue(self.gNMISetReplace(self.xpath, self.value),
63+
"gNMI Set did not succeed.")
64+
65+
4666
class JsonCheckSetUpdate(testbase.TestCase):
4767
"""Sends gNMI Set with a schema-checked JSON-IETF value.
4868
@@ -71,3 +91,33 @@ def testJsonCheckSetUpdate(self):
7191
"JSON to Set does not match the model")
7292
self.assertTrue(self.gNMISetUpdate(self.xpath, self.json_value),
7393
"gNMI Set did not succeed.")
94+
95+
96+
class JsonCheckSetReplace(testbase.TestCase):
97+
"""Sends gNMI Set with a schema-checked JSON-IETF value.
98+
99+
The intended JSON-IETF configuration is first checked for schema validity.
100+
101+
All arguments are read from the Test YAML description.
102+
103+
Args:
104+
xpath: gNMI path to write.
105+
json_value: JSON-IETF value to check and set.
106+
model: Python binding class to check the JSON reply against.
107+
"""
108+
xpath = ""
109+
json_value = None
110+
model = ""
111+
112+
def testJsonCheckSetReplace(self):
113+
""""""
114+
self.assertArgs(["xpath", "json_value", "model", ])
115+
self.assertXpath(self.xpath)
116+
self.assertModelXpath(self.model, self.xpath)
117+
self.assertIsInstance(self.json_value, dict,
118+
"The value is not a valid JSON object")
119+
model = schema.ocContainerFromPath(self.model, self.xpath)
120+
self.assertJsonModel(json.dumps(self.json_value), model,
121+
"JSON to Set does not match the model")
122+
self.assertTrue(self.gNMISetReplace(self.xpath, self.json_value),
123+
"gNMI Set did not succeed.")

oc_config_validate/oc_config_validate/testcases/setget.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class TestCase(testbase.TestCase):
1212
xpath: gNMI path to write and read.
1313
json_value: JSON-IETF value to check, set and get.
1414
model: Python binding class to check the JSON reply against.
15+
set_replace: True to use gNMI SetReplace instead of SetUpdate.
1516
"""
1617
xpath = None
1718
json_value = None
1819
model = None
20+
set_replace = False
1921

2022
def setUp(self):
2123
# Check testcase arguments
@@ -27,8 +29,8 @@ def setUp(self):
2729
model = schema.ocContainerFromPath(self.model, self.xpath)
2830
self.assertJsonModel(json.dumps(self.json_value), model,
2931
"JSON value to Set does not match the model")
30-
# Set Update
31-
self.assertTrue(self.gNMISetUpdate(self.xpath, self.json_value),
32+
_gnmi_set = self.gNMISetReplace if self.set_replace else self.gNMISetUpdate
33+
self.assertTrue(_gnmi_set(self.xpath, self.json_value),
3234
"gNMI Set did not succeed.")
3335

3436

@@ -46,6 +48,7 @@ class SetGetJsonCheck(TestCase):
4648
xpath: gNMI path to write and read.
4749
json_value: JSON-IETF value to check, set and get.
4850
model: Python binding class to check the JSON reply against.
51+
set_replace: True to use gNMI SetReplace instead of SetUpdate.
4952
"""
5053

5154
@testbase.retryAssertionError
@@ -72,6 +75,7 @@ class SetGetJsonCheckCompare(TestCase):
7275
xpath: gNMI path to write and read.
7376
json_value: JSON-IETF value to check set, get and compare.
7477
model: Python binding class to check the JSON reply against.
78+
set_replace: True to use gNMI SetReplace instead of SetUpdate.
7579
"""
7680

7781
@testbase.retryAssertionError
@@ -83,3 +87,6 @@ def testSetGetJsonCheckCompare(self):
8387
got = json.loads(self.resp_val)
8488
cmp, diff = schema.intersectCmp(self.json_value, got)
8589
self.assertTrue(cmp, diff)
90+
if self.set_replace:
91+
cmp, diff = schema.intersectCmp(got, self.json_value)
92+
self.assertTrue(cmp, diff)

0 commit comments

Comments
 (0)