Skip to content

Commit 4e89bbf

Browse files
Danilo Egea Gondolfoslyon
Danilo Egea Gondolfo
authored andcommitted
tests: Add some integration tests for DBus
This tests can be executed as autopkgtests. They cover netplan get, set and apply. There is a copy of test_dbus_config_get() in the file integration/regressions.py that can be dropped once we adapt the netplan.io package to run the new test set.
1 parent a767391 commit 4e89bbf

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed

tests/integration/dbus.py

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#!/usr/bin/python3
2+
#
3+
# Integration tests for netplan-dbus
4+
#
5+
# These need to be run in a VM and do change the system
6+
# configuration.
7+
#
8+
# Copyright (C) 2018-2023 Canonical, Ltd.
9+
# Author: Mathieu Trudel-Lapierre <[email protected]>
10+
# Author: Lukas Märdian <[email protected]>
11+
# Author: Danilo Egea Gondolfo <[email protected]>
12+
#
13+
# This program is free software; you can redistribute it and/or modify
14+
# it under the terms of the GNU General Public License as published by
15+
# the Free Software Foundation; version 3.
16+
#
17+
# This program is distributed in the hope that it will be useful,
18+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
# GNU General Public License for more details.
21+
#
22+
# You should have received a copy of the GNU General Public License
23+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
25+
import json
26+
import os
27+
import signal
28+
import sys
29+
import subprocess
30+
import unittest
31+
32+
from base import IntegrationTestsBase
33+
34+
BUSCTL_CONFIG = [
35+
'busctl',
36+
'-j',
37+
'call',
38+
'--system',
39+
'io.netplan.Netplan',
40+
'/io/netplan/Netplan',
41+
'io.netplan.Netplan',
42+
'Config'
43+
]
44+
45+
BUSCTL_CONFIG_GET = [
46+
'busctl',
47+
'-j',
48+
'call',
49+
'--system',
50+
'io.netplan.Netplan',
51+
'PLACEHOLDER',
52+
'io.netplan.Netplan.Config',
53+
'Get'
54+
]
55+
56+
BUSCTL_CONFIG_APPLY = [
57+
'busctl',
58+
'-j',
59+
'call',
60+
'--system',
61+
'io.netplan.Netplan',
62+
'PLACEHOLDER',
63+
'io.netplan.Netplan.Config',
64+
'Apply'
65+
]
66+
67+
68+
class _CommonTests():
69+
70+
def setUp(self):
71+
super().setUp()
72+
73+
# If netplan-dbus is already running let's terminate it to
74+
# be sure the process is not from a binary from an old package
75+
# (before the installation of the one being tested)
76+
cmd = ['ps', '-C', 'netplan-dbus', '-o', 'pid=']
77+
out = subprocess.run(cmd, capture_output=True, universal_newlines=True)
78+
if out.returncode == 0:
79+
pid = out.stdout.strip()
80+
os.kill(int(pid), signal.SIGTERM)
81+
82+
def test_dbus_config_get(self):
83+
NETPLAN_YAML = '''network:
84+
version: 2
85+
ethernets:
86+
%(nic)s:
87+
dhcp4: true
88+
'''
89+
90+
with open(self.config, 'w') as f:
91+
f.write(NETPLAN_YAML % {'nic': self.dev_e_client})
92+
93+
out = subprocess.run(BUSCTL_CONFIG, capture_output=True, universal_newlines=True)
94+
95+
self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")
96+
97+
out_dict = json.loads(out.stdout)
98+
config_path = out_dict.get('data')[0]
99+
self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")
100+
101+
# The path has the following format: /io/netplan/Netplan/config/WM6X01
102+
BUSCTL_CONFIG_GET[5] = config_path
103+
104+
# Retrieving the config
105+
out = subprocess.run(BUSCTL_CONFIG_GET, capture_output=True, universal_newlines=True)
106+
self.assertEqual(out.returncode, 0, msg=f"Busctl Get() failed with error: {out.stderr}")
107+
108+
out_dict = json.loads(out.stdout)
109+
netplan_data = out_dict.get('data')[0]
110+
111+
self.assertNotEqual(netplan_data, "", msg="Got an empty response from DBUS")
112+
self.assertEqual(netplan_data, NETPLAN_YAML % {'nic': self.dev_e_client},
113+
msg="The original YAML is different from the one returned by DBUS")
114+
115+
def test_dbus_config_set(self):
116+
BUSCTL_CONFIG_SET = [
117+
'busctl',
118+
'-j',
119+
'call',
120+
'--system',
121+
'io.netplan.Netplan',
122+
'PLACEHOLDER',
123+
'io.netplan.Netplan.Config',
124+
'Set',
125+
'ss',
126+
'ethernets.%(nic)s.dhcp4=false' % {'nic': self.dev_e_client},
127+
'',
128+
]
129+
130+
NETPLAN_YAML_BEFORE = '''network:
131+
version: 2
132+
ethernets:
133+
%(nic)s:
134+
dhcp4: true
135+
'''
136+
137+
NETPLAN_YAML_AFTER = '''network:
138+
version: 2
139+
ethernets:
140+
%(nic)s:
141+
dhcp4: false
142+
'''
143+
with open(self.config, 'w') as f:
144+
f.write(NETPLAN_YAML_BEFORE % {'nic': self.dev_e_client})
145+
146+
out = subprocess.run(BUSCTL_CONFIG, capture_output=True, universal_newlines=True)
147+
self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")
148+
149+
out_dict = json.loads(out.stdout)
150+
config_path = out_dict.get('data')[0]
151+
152+
self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")
153+
154+
# The path has the following format: /io/netplan/Netplan/config/WM6X01
155+
BUSCTL_CONFIG_GET[5] = config_path
156+
BUSCTL_CONFIG_SET[5] = config_path
157+
158+
# Changing the configuration
159+
out = subprocess.run(BUSCTL_CONFIG_SET, capture_output=True, universal_newlines=True)
160+
self.assertEqual(out.returncode, 0, msg=f"Busctl Set() failed with error: {out.stderr}")
161+
162+
out_dict = json.loads(out.stdout)
163+
self.assertEqual(out_dict.get('data')[0], True, msg="Set command failed")
164+
165+
# Retrieving the configuration
166+
out = subprocess.run(BUSCTL_CONFIG_GET, capture_output=True, universal_newlines=True)
167+
self.assertEqual(out.returncode, 0, msg=f"Busctl Get() failed with error: {out.stderr}")
168+
169+
out_dict = json.loads(out.stdout)
170+
netplan_data = out_dict.get('data')[0]
171+
172+
self.assertNotEqual(netplan_data, "", msg="Got an empty response from DBUS")
173+
self.assertEqual(NETPLAN_YAML_AFTER % {'nic': self.dev_e_client},
174+
netplan_data, msg="The final YAML is different than expected")
175+
176+
def test_dbus_config_apply(self):
177+
NETPLAN_YAML = '''network:
178+
version: 2
179+
bridges:
180+
br1234:
181+
dhcp4: false
182+
'''
183+
184+
self.addCleanup(subprocess.call, ['ip', 'link', 'delete', 'br1234'], stderr=subprocess.DEVNULL)
185+
186+
with open(self.config, 'w') as f:
187+
f.write(NETPLAN_YAML)
188+
189+
out = subprocess.run(BUSCTL_CONFIG, capture_output=True, universal_newlines=True)
190+
self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")
191+
192+
out_dict = json.loads(out.stdout)
193+
config_path = out_dict.get('data')[0]
194+
195+
self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")
196+
197+
# The path has the following format: /io/netplan/Netplan/config/WM6X01
198+
BUSCTL_CONFIG_APPLY[5] = config_path
199+
200+
# Applying the configuration
201+
out = subprocess.run(BUSCTL_CONFIG_APPLY, capture_output=True, universal_newlines=True)
202+
self.assertEqual(out.returncode, 0, msg=f"Busctl Apply() failed with error: {out.stderr}")
203+
204+
out_dict = json.loads(out.stdout)
205+
self.assertEqual(out_dict.get('data')[0], True, msg="Apply command failed")
206+
207+
self.assert_iface('br1234')
208+
209+
210+
class TestNetworkd(IntegrationTestsBase, _CommonTests):
211+
pass
212+
213+
214+
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))

tests/integration/regressions.py

+54
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# You should have received a copy of the GNU General Public License
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323

24+
import json
2425
import os
2526
import sys
2627
import signal
@@ -145,4 +146,57 @@ class TestNetworkManager(IntegrationTestsBase, _CommonTests):
145146
backend = 'NetworkManager'
146147

147148

149+
class TestDbus(IntegrationTestsBase):
150+
# This test can be dropped when tests/integration/dbus.py is
151+
# integrated as an autopkgtest in the Debian package
152+
def test_dbus_config_get_lp1997467(self):
153+
154+
NETPLAN_YAML = '''network:
155+
version: 2
156+
ethernets:
157+
%(nic)s:
158+
dhcp4: true
159+
'''
160+
BUSCTL_CONFIG = [
161+
'busctl', '-j', 'call', '--system',
162+
'io.netplan.Netplan', '/io/netplan/Netplan',
163+
'io.netplan.Netplan', 'Config']
164+
165+
BUSCTL_CONFIG_GET = [
166+
'busctl', '-j', 'call', '--system',
167+
'io.netplan.Netplan', 'PLACEHOLDER',
168+
'io.netplan.Netplan.Config', 'Get']
169+
170+
# Terminates netplan-dbus if it is running already
171+
cmd = ['ps', '-C', 'netplan-dbus', '-o', 'pid=']
172+
out = subprocess.run(cmd, capture_output=True, universal_newlines=True)
173+
if out.returncode == 0:
174+
pid = out.stdout.strip()
175+
os.kill(int(pid), signal.SIGTERM)
176+
177+
with open(self.config, 'w') as f:
178+
f.write(NETPLAN_YAML % {'nic': self.dev_e_client})
179+
180+
out = subprocess.run(BUSCTL_CONFIG, capture_output=True, universal_newlines=True)
181+
self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")
182+
183+
out_dict = json.loads(out.stdout)
184+
config_path = out_dict.get('data')[0]
185+
self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")
186+
187+
# The path has the following format: /io/netplan/Netplan/config/WM6X01
188+
BUSCTL_CONFIG_GET[5] = config_path
189+
190+
# Retrieving the config
191+
out = subprocess.run(BUSCTL_CONFIG_GET, capture_output=True, universal_newlines=True)
192+
self.assertEqual(out.returncode, 0, msg=f"Busctl Get() failed with error: {out.stderr}")
193+
194+
out_dict = json.loads(out.stdout)
195+
netplan_data = out_dict.get('data')[0]
196+
197+
self.assertNotEqual(netplan_data, "", msg="Got an empty response from DBUS")
198+
self.assertEqual(netplan_data, NETPLAN_YAML % {'nic': self.dev_e_client},
199+
msg="The original YAML is different from the one returned by DBUS")
200+
201+
148202
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))

0 commit comments

Comments
 (0)