Skip to content

Commit 656b8ab

Browse files
committed
Fix: only delete network config on setup/clean
1 parent bbe5c2a commit 656b8ab

File tree

3 files changed

+64
-64
lines changed

3 files changed

+64
-64
lines changed

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ def upgrade():
6666
"""
6767
return _upgrade()
6868

69-
def delete():
69+
def deleteNetworkConfig():
7070
"""
7171
Delete the network configuration file.
7272
7373
:return: True or False
7474
:rtype: bool
7575
"""
76-
legacyNetworkConfigFleDeleted = fileHelper.deleteFile(constants.legacyNetworkConfigFilePath)
77-
configFileDeleted = fileHelper.deleteFile(constants.configFilePath)
78-
return legacyNetworkConfigFleDeleted and configFileDeleted
79-
76+
config = _readConfig()
77+
if config is None or "network" not in config:
78+
return True
79+
80+
del config["network"]
81+
return _writeConfig(config)
8082

8183
# --------------------
8284
# - Helper functions -

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ def _cleanOldDomainJoins():
210210
if not fileHelper.deleteFilesWithExtension("/var/lib/samba/private/tls", ".pem"):
211211
return False
212212

213-
# remove configuration
214-
logging.info(f"Deleting configuration files if exist ...")
215-
if not config.delete():
213+
# remove network configuration
214+
logging.info(f"Deleting network configuration ...")
215+
if not config.deleteNetworkConfig():
216216
return False
217217

218218
return True

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_config.py

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from unittest import mock
33
from .. import config
44

5-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf")
5+
MOCK_FILE_PATH = f"{os.path.dirname(os.path.realpath(__file__))}/files/config"
6+
7+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
68
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
79
def test_network_legacy():
810
rc, networkConfig = config.network()
@@ -12,16 +14,16 @@ def test_network_legacy():
1214
assert networkConfig["realm"] == "LINUXMUSTER.LEGACY"
1315

1416
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/config.yml")
15-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml")
17+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
1618
def test_network():
1719
rc, networkConfig = config.network()
1820
assert rc
1921
assert networkConfig["serverHostname"] == "server.linuxmuster.lan"
2022
assert networkConfig["domain"] == "linuxmuster.lan"
2123
assert networkConfig["realm"] == "LINUXMUSTER.LAN"
2224

23-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf")
24-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml")
25+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
26+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
2527
def test_network_both():
2628
rc, networkConfig = config.network()
2729
assert rc
@@ -38,14 +40,14 @@ def test_network_none():
3840
assert networkConfig is None
3941

4042
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
41-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.invalid-network.yml")
43+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.invalid-network.yml")
4244
def test_network_invalid():
4345
rc, networkConfig = config.network()
4446
assert not rc
4547
assert networkConfig is None
4648

4749
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
48-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml")
50+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
4951
def test_shares():
5052
sharesConfig = config.shares()
5153
assert "nameTemplate" in sharesConfig
@@ -59,27 +61,23 @@ def test_shares_none():
5961
assert sharesConfig["nameTemplate"] == config.constants.defaultShareNameTemplate
6062

6163
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
62-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.no-shares.yml")
64+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.no-shares.yml")
6365
def test_shares_missing():
6466
sharesConfig = config.shares()
6567
assert "nameTemplate" in sharesConfig
6668
assert sharesConfig["nameTemplate"] == config.constants.defaultShareNameTemplate
6769

6870
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
69-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.invalid-syntax.yml")
71+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.invalid-syntax.yml")
7072
def test_syntax_invalid():
7173
rc, networkConfig = config.network()
7274
assert not rc
7375
assert networkConfig is None
7476

7577
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
7678
def test_writeNetworkConfig():
77-
if os.path.exists("/tmp/config.yml"):
78-
os.remove("/tmp/config.yml")
79-
80-
with open(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml", "r") as fsrc:
81-
with open("/tmp/config.yml", "w") as fdst:
82-
fdst.write(fsrc.read())
79+
_deleteFile("/tmp/config.yml")
80+
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
8381

8482
newNetworkConfig = {
8583
"serverHostname": "server.linuxmuster.new",
@@ -98,12 +96,8 @@ def test_writeNetworkConfig():
9896

9997
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
10098
def test_writeNetworkConfig_invalid():
101-
if os.path.exists("/tmp/config.yml"):
102-
os.remove("/tmp/config.yml")
103-
104-
with open(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml", "r") as fsrc:
105-
with open("/tmp/config.yml", "w") as fdst:
106-
fdst.write(fsrc.read())
99+
_deleteFile("/tmp/config.yml")
100+
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
107101

108102
newNetworkConfig = {
109103
"sserverHostname": "server.linuxmuster.new",
@@ -130,8 +124,7 @@ def test_writeNetworkConfig_invalidPath():
130124

131125
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
132126
def test_writeNetworkConfig_empty():
133-
if os.path.exists("/tmp/config.yml"):
134-
os.remove("/tmp/config.yml")
127+
_deleteFile("/tmp/config.yml")
135128

136129
newNetworkConfig = {
137130
"serverHostname": "server.linuxmuster.new",
@@ -149,15 +142,9 @@ def test_writeNetworkConfig_empty():
149142
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/tmp/network.conf")
150143
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
151144
def test_upgrade():
152-
if os.path.exists("/tmp/config.yml"):
153-
os.remove("/tmp/config.yml")
154-
155-
if os.path.exists("/tmp/network.conf"):
156-
os.remove("/tmp/network.conf")
157-
158-
with open(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf", "r") as fsrc:
159-
with open("/tmp/network.conf", "w") as fdst:
160-
fdst.write(fsrc.read())
145+
_deleteFile("/tmp/config.yml")
146+
_deleteFile("/tmp/network.conf")
147+
_copyFile(f"{MOCK_FILE_PATH}/network.conf", "/tmp/network.conf")
161148

162149
assert config.upgrade()
163150
assert not os.path.exists("/tmp/network.conf")
@@ -173,8 +160,8 @@ def test_upgrade():
173160
assert yamlContent["network"]["realm"] == "LINUXMUSTER.LEGACY"
174161

175162

176-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf")
177-
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml")
163+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
164+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
178165
def test_upgrade_alreadyUpToDate():
179166
assert config.upgrade()
180167

@@ -187,43 +174,54 @@ def test_upgrade_alreadyUpToDate():
187174
assert yamlContent["network"]["domain"] == "linuxmuster.lan"
188175
assert yamlContent["network"]["realm"] == "LINUXMUSTER.LAN"
189176

190-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.invalid.conf")
177+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.invalid.conf")
191178
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
192179
def test_upgrade_invalid():
193-
if os.path.exists("/tmp/config.yml"):
194-
os.remove("/tmp/config.yml")
180+
_deleteFile("/tmp/config.yml")
195181

196182
assert not config.upgrade()
197-
assert os.path.exists(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.invalid.conf")
183+
assert os.path.exists(f"{MOCK_FILE_PATH}/network.invalid.conf")
198184
assert not os.path.exists("/tmp/config.yml")
199185

200-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf")
186+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
201187
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
202188
def test_upgrade_unwritable():
203189
assert not config.upgrade()
204-
assert os.path.exists(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.invalid.conf")
190+
assert os.path.exists(f"{MOCK_FILE_PATH}/network.invalid.conf")
205191
assert not os.path.exists("/tmp/config.yml")
206192

207193
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
208194
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
209195
def test_upgrade_nonexistent():
210196
assert not config.upgrade()
211197

212-
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/tmp/network.conf")
198+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
213199
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
214-
def test_delete():
215-
if os.path.exists("/tmp/network.conf"):
216-
os.remove("/tmp/network.conf")
217-
if os.path.exists("/tmp/config.yml"):
218-
os.remove("/tmp/config.yml")
219-
220-
with open(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/network.conf", "r") as fsrc:
221-
with open("/tmp/network.conf", "w") as fdst:
222-
fdst.write(fsrc.read())
223-
with open(f"{os.path.dirname(os.path.realpath(__file__))}/files/config/config.yml", "r") as fsrc:
224-
with open("/tmp/config.yml", "w") as fdst:
225-
fdst.write(fsrc.read())
226-
227-
assert config.delete()
228-
assert not os.path.exists("/tmp/network.conf")
229-
assert not os.path.exists("/tmp/config.yml")
200+
def test_deleteNetworkConfig():
201+
_deleteFile("/tmp/network.conf")
202+
_deleteFile("/tmp/config.yml")
203+
204+
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
205+
206+
assert config.deleteNetworkConfig()
207+
assert os.path.exists("/tmp/config.yml")
208+
assert config.network() == (False, None)
209+
assert config.shares() == {"nameTemplate": "{label}_{letter}"}
210+
211+
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
212+
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
213+
def test_deleteNetworkConfig_nonexistent():
214+
assert config.deleteNetworkConfig()
215+
216+
# --------------------
217+
# - Helper functions -
218+
# --------------------
219+
220+
def _deleteFile(path):
221+
if os.path.exists(path):
222+
os.remove(path)
223+
224+
def _copyFile(src, dst):
225+
with open(src, "r") as fsrc:
226+
with open(dst, "w") as fdst:
227+
fdst.write(fsrc.read())

0 commit comments

Comments
 (0)