File: dbus.py

package info (click to toggle)
netplan.io 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,268 kB
  • sloc: python: 34,640; ansic: 14,096; xml: 4,989; javascript: 2,165; sh: 513; makefile: 118
file content (219 lines) | stat: -rw-r--r-- 7,168 bytes parent folder | download | duplicates (3)
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
218
219
#!/usr/bin/python3
#
# Integration tests for netplan-dbus
#
# These need to be run in a VM and do change the system
# configuration.
#
# Copyright (C) 2018-2023 Canonical, Ltd.
# Author: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
# Author: Lukas Märdian <slyon@ubuntu.com>
# Author: Danilo Egea Gondolfo <danilo.egea.gondolfo@canonical.com>
#
# This program 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; version 3.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

import json
import os
import signal
import sys
import subprocess
import unittest

from base import IntegrationTestsBase

BUSCTL_CONFIG = [
        'busctl',
        '-j',
        'call',
        '--system',
        'io.netplan.Netplan',
        '/io/netplan/Netplan',
        'io.netplan.Netplan',
        'Config'
        ]

BUSCTL_CONFIG_GET = [
        'busctl',
        '-j',
        'call',
        '--system',
        'io.netplan.Netplan',
        'PLACEHOLDER',
        'io.netplan.Netplan.Config',
        'Get'
        ]

BUSCTL_CONFIG_APPLY = [
        'busctl',
        '-j',
        'call',
        '--system',
        'io.netplan.Netplan',
        'PLACEHOLDER',
        'io.netplan.Netplan.Config',
        'Apply'
        ]


class _CommonTests():

    def setUp(self):
        super().setUp()

        # If netplan-dbus is already running let's terminate it to
        # be sure the process is not from a binary from an old package
        # (before the installation of the one being tested)
        cmd = ['ps', '-C', 'netplan-dbus', '-o', 'pid=']
        out = subprocess.run(cmd, capture_output=True, text=True)
        if out.returncode == 0:
            pid = out.stdout.strip()
            os.kill(int(pid), signal.SIGTERM)

    def test_dbus_config_get(self):
        NETPLAN_YAML = '''network:
  version: 2
  ethernets:
    %(nic)s:
      dhcp4: true
'''

        with open(self.config, 'w') as f:
            f.write(NETPLAN_YAML % {'nic': self.dev_e_client})

        out = subprocess.run(BUSCTL_CONFIG, capture_output=True, text=True)

        self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        config_path = out_dict.get('data')[0]
        self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")

        # The path has the following format: /io/netplan/Netplan/config/WM6X01
        BUSCTL_CONFIG_GET[5] = config_path

        # Retrieving the config
        out = subprocess.run(BUSCTL_CONFIG_GET, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Get() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        netplan_data = out_dict.get('data')[0]

        self.assertNotEqual(netplan_data, "", msg="Got an empty response from DBUS")
        self.assertEqual(netplan_data, NETPLAN_YAML % {'nic': self.dev_e_client},
                         msg="The original YAML is different from the one returned by DBUS")

    def test_dbus_config_set(self):
        BUSCTL_CONFIG_SET = [
            'busctl',
            '-j',
            'call',
            '--system',
            'io.netplan.Netplan',
            'PLACEHOLDER',
            'io.netplan.Netplan.Config',
            'Set',
            'ss',
            'ethernets.%(nic)s.dhcp4=false' % {'nic': self.dev_e_client},
            '',
        ]

        NETPLAN_YAML_BEFORE = '''network:
  version: 2
  ethernets:
    %(nic)s:
      dhcp4: true
'''

        NETPLAN_YAML_AFTER = '''network:
  version: 2
  ethernets:
    %(nic)s:
      dhcp4: false
'''
        with open(self.config, 'w') as f:
            f.write(NETPLAN_YAML_BEFORE % {'nic': self.dev_e_client})

        out = subprocess.run(BUSCTL_CONFIG, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        config_path = out_dict.get('data')[0]

        self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")

        # The path has the following format: /io/netplan/Netplan/config/WM6X01
        BUSCTL_CONFIG_GET[5] = config_path
        BUSCTL_CONFIG_SET[5] = config_path
        BUSCTL_CONFIG_APPLY[5] = config_path

        # Changing the configuration
        out = subprocess.run(BUSCTL_CONFIG_SET, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Set() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        self.assertEqual(out_dict.get('data')[0], True, msg="Set command failed")

        # Retrieving the configuration
        out = subprocess.run(BUSCTL_CONFIG_GET, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Get() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        netplan_data = out_dict.get('data')[0]

        self.assertNotEqual(netplan_data, "", msg="Got an empty response from DBUS")
        self.assertEqual(NETPLAN_YAML_AFTER % {'nic': self.dev_e_client},
                         netplan_data, msg="The final YAML is different than expected")

        # Applying the configuration
        out = subprocess.run(BUSCTL_CONFIG_APPLY, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Apply() failed with error: {out.stderr}")

    def test_dbus_config_apply(self):
        NETPLAN_YAML = '''network:
  version: 2
  bridges:
    br1234:
      dhcp4: false
'''

        self.addCleanup(subprocess.call, ['ip', 'link', 'delete', 'br1234'], stderr=subprocess.DEVNULL)

        with open(self.config, 'w') as f:
            f.write(NETPLAN_YAML)

        out = subprocess.run(BUSCTL_CONFIG, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Config() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        config_path = out_dict.get('data')[0]

        self.assertNotEqual(config_path, "", msg="Got an empty response from DBUS")

        # The path has the following format: /io/netplan/Netplan/config/WM6X01
        BUSCTL_CONFIG_APPLY[5] = config_path

        # Applying the configuration
        out = subprocess.run(BUSCTL_CONFIG_APPLY, capture_output=True, text=True)
        self.assertEqual(out.returncode, 0, msg=f"Busctl Apply() failed with error: {out.stderr}")

        out_dict = json.loads(out.stdout)
        self.assertEqual(out_dict.get('data')[0], True, msg="Apply command failed")

        self.assert_iface('br1234')


class TestNetworkd(IntegrationTestsBase, _CommonTests):
    pass


unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))