1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.vyos.vyos.plugins.cliconf_utils.vyosconf import (
KEEP_EXISTING_VALUES,
VyosConf,
)
class TestListElements(unittest.TestCase):
def test_add(self):
conf = VyosConf()
conf.set_entry(["a", "b"], "c")
self.assertEqual(conf.config, {"a": {"b": {"c": {}}}})
conf.set_entry(["a", "b"], "d")
self.assertEqual(conf.config, {"a": {"b": {"c": {}, "d": {}}}})
conf.set_entry(["a", "c"], "b")
self.assertEqual(
conf.config,
{"a": {"b": {"c": {}, "d": {}}, "c": {"b": {}}}},
)
conf.set_entry(["a", "c", "b"], "d")
self.assertEqual(
conf.config,
{"a": {"b": {"c": {}, "d": {}}, "c": {"b": {"d": {}}}}},
)
def test_del(self):
conf = VyosConf()
conf.set_entry(["a", "b"], "c")
conf.set_entry(["a", "c", "b"], "d")
conf.set_entry(["a", "b"], "d")
self.assertEqual(
conf.config,
{"a": {"b": {"c": {}, "d": {}}, "c": {"b": {"d": {}}}}},
)
conf.del_entry(["a", "c", "b"], "d")
self.assertEqual(conf.config, {"a": {"b": {"c": {}, "d": {}}}})
conf.set_entry(["a", "b", "c"], "d")
conf.del_entry(["a", "b", "c"], "d")
self.assertEqual(conf.config, {"a": {"b": {"d": {}}}})
def test_del_missing_leaf_is_noop(self):
"""
Deleting a leaf that was never set must leave the config unchanged.
Regression test: del_entry() used to raise KeyError when the leaf's
parent had siblings, and could delete an unrelated ancestor subtree
(or the entire config) when the parent path had no siblings.
"""
# parent has siblings: previously raised KeyError
conf = VyosConf()
conf.set_entry(["a", "b"], "c")
conf.set_entry(["a", "b"], "d")
conf.del_entry(["a", "b"], "nonexistent")
self.assertEqual(conf.config, {"a": {"b": {"c": {}, "d": {}}}})
# parent path is an unbranched chain: previously deleted the
# entire config instead of no-op'ing
conf = VyosConf()
conf.set_entry(["a", "b"], "d")
conf.del_entry(["a", "b"], "c")
self.assertEqual(conf.config, {"a": {"b": {"d": {}}}})
# missing intermediate path element already behaved correctly;
# confirm it still does
conf = VyosConf()
conf.set_entry(["a", "b"], "c")
conf.del_entry(["a", "x"], "c")
self.assertEqual(conf.config, {"a": {"b": {"c": {}}}})
def test_parse(self):
conf = VyosConf()
self.assertListEqual(
conf.parse_line("set a b c"),
["set", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line('set a b "c"'),
["set", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line("set a b 'c d'"),
["set", ["a", "b"], "c d"],
)
self.assertListEqual(
conf.parse_line("set a b 'c'"),
["set", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line("delete a b 'c'"),
["delete", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line("del a b 'c'"),
["del", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line("set a b '\"c'"),
["set", ["a", "b"], '"c'],
)
self.assertListEqual(
conf.parse_line("set a b 'c' #this is a comment"),
["set", ["a", "b"], "c"],
)
self.assertListEqual(
conf.parse_line("set a b '#c'"),
["set", ["a", "b"], "#c"],
)
def test_run_commands(self):
self.assertEqual(
VyosConf(["set a b 'c'", "set a c 'b'"]).config,
{"a": {"b": {"c": {}}, "c": {"b": {}}}},
)
self.assertEqual(
VyosConf(["set a b c 'd'", "set a c 'b'", "del a b c d"]).config,
{"a": {"c": {"b": {}}}},
)
def test_build_commands(self):
self.assertEqual(
sorted(
VyosConf(
[
"set a b 'c a'",
"set a c a",
"set a c b",
"delete a c a",
],
).build_commands(),
),
sorted(["set a b 'c a'", "set a c b"]),
)
self.assertEqual(
sorted(
VyosConf(
[
"set a b 10.0.0.1/24",
"set a c ABCabc123+/=",
"set a d $6$ABC.abc.123.+./=..",
],
).build_commands(),
),
sorted(
[
"set a b 10.0.0.1/24",
"set a c 'ABCabc123+/='",
"set a d '$6$ABC.abc.123.+./=..'",
],
),
)
def test_check_commands(self):
conf = VyosConf(["set a b 'c a'", "set a c b"])
self.assertListEqual(
conf.check_commands(
["set a b 'c a'", "del a c b", "set a b 'c'", "del a a a"],
),
[True, False, False, True],
)
def test_diff_commands_to(self):
conf = VyosConf(["set a b 'c a'", "set a c b"])
self.assertListEqual(
conf.diff_commands_to(VyosConf(["set a c b"])),
["delete a b"],
)
self.assertListEqual(
conf.diff_commands_to(VyosConf(["set a b 'c a'", "set a c b"])),
[],
)
# KEEP_EXISTING_VALUES is no longer reachable via 'set'/'delete'
# command text (see #6): a literal "..." leaf is now an ordinary
# value, not a sentinel, so nothing is suppressed here.
self.assertListEqual(
conf.diff_commands_to(VyosConf(["set a b ..."])),
["delete a b 'c a'", "delete a c", "set a b ..."],
)
def test_diff_commands_to_keep_existing_values_sentinel(self):
# KEEP_EXISTING_VALUES is only reachable via the Python API now.
# Build the candidate tree directly to prove diff_to() still
# honours it when used that way.
conf = VyosConf(["set a b 'c a'", "set a c b"])
candidate = VyosConf()
candidate.config = {"a": {"b": {KEEP_EXISTING_VALUES: {}}}}
self.assertListEqual(
conf.diff_commands_to(candidate),
["delete a c"],
)
if __name__ == "__main__":
unittest.main()
|