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
|
# This file is part of cloud-init. See LICENSE file for license information.
from pathlib import Path
from cloudinit.config import cc_vyos_userdata
from tests.unittests.helpers import CiTestCase, mock
class TestVyosUserdata(CiTestCase):
def test_get_tag_nodes_discovers_template_paths(self):
templates_dir = Path(self.tmp_dir())
(templates_dir / "interfaces" / "ethernet" / "node.tag").mkdir(
parents=True
)
with mock.patch.object(
cc_vyos_userdata, "TEMPLATES_DIR", str(templates_dir)
):
self.assertEqual(
[("interfaces", "ethernet")],
cc_vyos_userdata.get_tag_nodes(),
)
def test_get_multi_nodes_accepts_trailing_metadata(self):
templates_dir = Path(self.tmp_dir())
multi_node = templates_dir / "system" / "name-server"
ordinary_node = templates_dir / "system" / "host-name"
multi_node.mkdir(parents=True)
ordinary_node.mkdir(parents=True)
(multi_node / "node.def").write_text(
"multi: true\nhelp: System name server\n"
)
(ordinary_node / "node.def").write_text("help: Host name\n")
with mock.patch.object(
cc_vyos_userdata, "TEMPLATES_DIR", str(templates_dir)
):
self.assertEqual(
[("system", "name-server")],
cc_vyos_userdata.get_multi_nodes(),
)
def test_string_to_command_parses_set_and_delete(self):
self.assertEqual(
{
"cmd_action": "set",
"cmd_path": ["system", "host-name"],
"cmd_value": "edge-a",
},
cc_vyos_userdata.string_to_command(
"set system host-name 'edge-a'"
),
)
self.assertEqual(
{
"cmd_action": "delete",
"cmd_path": ["system", "name-server"],
"cmd_value": "192.0.2.53",
},
cc_vyos_userdata.string_to_command(
"delete system name-server '192.0.2.53'"
),
)
self.assertEqual(
{
"cmd_action": "delete",
"cmd_path": ["system", "host-name"],
"cmd_value": None,
},
cc_vyos_userdata.string_to_command("delete system host-name"),
)
def test_string_to_command_rejects_malformed_input(self):
self.assertIsNone(cc_vyos_userdata.string_to_command("show version"))
def test_apply_command_set_replaces_ordinary_node(self):
config = mock.Mock()
command = {
"cmd_path": ["system", "host-name"],
"cmd_value": "edge-a",
}
cc_vyos_userdata.apply_command_set(config, [], [], command)
config.set.assert_called_once_with(
["system", "host-name"], "edge-a", replace=True
)
config.set_tag.assert_not_called()
def test_apply_command_set_appends_multi_node_value(self):
config = mock.Mock()
command = {
"cmd_path": ["system", "name-server"],
"cmd_value": "192.0.2.53",
}
cc_vyos_userdata.apply_command_set(
config, [], [("system", "name-server")], command
)
config.set.assert_called_once_with(
["system", "name-server"], "192.0.2.53", replace=False
)
def test_apply_command_set_preserves_nested_tag_node_values(self):
config = mock.Mock()
marked_paths = []
config.set_tag.side_effect = lambda path: marked_paths.append(
list(path)
)
command = {
"cmd_path": [
"vrf",
"name",
"customer",
"protocols",
"bgp",
"address-family",
"ipv4-unicast",
"network",
],
"cmd_value": "10.180.0.0/25",
}
tag_nodes = [
("vrf", "name"),
(
"vrf",
"name",
"node.tag",
"protocols",
"bgp",
"address-family",
"ipv4-unicast",
"network",
),
]
cc_vyos_userdata.apply_command_set(config, tag_nodes, [], command)
config.set.assert_called_once_with(
command["cmd_path"], "10.180.0.0/25", replace=False
)
self.assertEqual(
[
["vrf", "name"],
command["cmd_path"],
],
marked_paths,
)
def test_apply_command_delete_uses_value_or_path(self):
config = mock.Mock()
cc_vyos_userdata.apply_command_delete(
config,
{
"cmd_path": ["system", "name-server"],
"cmd_value": "192.0.2.53",
},
)
cc_vyos_userdata.apply_command_delete(
config,
{"cmd_path": ["system", "host-name"], "cmd_value": None},
)
config.delete_value.assert_called_once_with(
["system", "name-server"], "192.0.2.53"
)
config.delete.assert_called_once_with(["system", "host-name"])
@mock.patch("cloudinit.config.cc_vyos_userdata.get_multi_nodes")
@mock.patch("cloudinit.config.cc_vyos_userdata.get_tag_nodes")
def test_apply_commands_routes_valid_commands_and_skips_malformed(
self, tag_nodes, multi_nodes
):
config = mock.Mock()
tag_nodes.return_value = []
multi_nodes.return_value = []
cc_vyos_userdata.apply_commands(
config,
[
"set system host-name 'edge-a'",
"show version",
"delete system name-server '192.0.2.53'",
"delete system host-name",
],
)
config.set.assert_called_once_with(
["system", "host-name"], "edge-a", replace=True
)
config.delete_value.assert_called_once_with(
["system", "name-server"], "192.0.2.53"
)
config.delete.assert_called_once_with(["system", "host-name"])
|