22from unittest import mock
33from .. 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" )
79def 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" )
1618def 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" )
2527def test_network_both ():
2628 rc , networkConfig = config .network ()
2729 assert rc
@@ -38,27 +40,44 @@ 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" )
4244def 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.invalid-syntax.yml" )
50+ @mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"{ MOCK_FILE_PATH } /config.yml" )
51+ def test_shares ():
52+ sharesConfig = config .shares ()
53+ assert "letterTemplate" in sharesConfig
54+ assert sharesConfig ["letterTemplate" ] == "_{letter}"
55+
56+ @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/does/not/exist/network.conf" )
57+ @mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/does/not/exist/config.yml" )
58+ def test_shares_none ():
59+ sharesConfig = config .shares ()
60+ assert "letterTemplate" in sharesConfig
61+ assert sharesConfig ["letterTemplate" ] == config .constants .defaultShareLetterTemplate
62+
63+ @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/does/not/exist/network.conf" )
64+ @mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"{ MOCK_FILE_PATH } /config.no-shares.yml" )
65+ def test_shares_missing ():
66+ sharesConfig = config .shares ()
67+ assert "letterTemplate" in sharesConfig
68+ assert sharesConfig ["letterTemplate" ] == config .constants .defaultShareLetterTemplate
69+
70+ @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/does/not/exist/network.conf" )
71+ @mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"{ MOCK_FILE_PATH } /config.invalid-syntax.yml" )
4972def test_syntax_invalid ():
5073 rc , networkConfig = config .network ()
5174 assert not rc
5275 assert networkConfig is None
5376
5477@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
5578def test_writeNetworkConfig ():
56- if os .path .exists ("/tmp/config.yml" ):
57- os .remove ("/tmp/config.yml" )
58-
59- with open (f"{ os .path .dirname (os .path .realpath (__file__ ))} /files/config/config.yml" , "r" ) as fsrc :
60- with open ("/tmp/config.yml" , "w" ) as fdst :
61- fdst .write (fsrc .read ())
79+ _deleteFile ("/tmp/config.yml" )
80+ _copyFile (f"{ MOCK_FILE_PATH } /config.yml" , "/tmp/config.yml" )
6281
6382 newNetworkConfig = {
6483 "serverHostname" : "server.linuxmuster.new" ,
@@ -72,17 +91,13 @@ def test_writeNetworkConfig():
7291 assert networkConfig ["serverHostname" ] == "server.linuxmuster.new"
7392 assert networkConfig ["domain" ] == "linuxmuster.new"
7493 assert networkConfig ["realm" ] == "LINUXMUSTER.NEW"
94+ assert config .shares () == {"letterTemplate" : "_{letter}" }
7595
76- # TODO: once there are more config options, test that they are preserved
7796
7897@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
7998def test_writeNetworkConfig_invalid ():
80- if os .path .exists ("/tmp/config.yml" ):
81- os .remove ("/tmp/config.yml" )
82-
83- with open (f"{ os .path .dirname (os .path .realpath (__file__ ))} /files/config/config.yml" , "r" ) as fsrc :
84- with open ("/tmp/config.yml" , "w" ) as fdst :
85- fdst .write (fsrc .read ())
99+ _deleteFile ("/tmp/config.yml" )
100+ _copyFile (f"{ MOCK_FILE_PATH } /config.yml" , "/tmp/config.yml" )
86101
87102 newNetworkConfig = {
88103 "sserverHostname" : "server.linuxmuster.new" ,
@@ -109,8 +124,7 @@ def test_writeNetworkConfig_invalidPath():
109124
110125@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
111126def test_writeNetworkConfig_empty ():
112- if os .path .exists ("/tmp/config.yml" ):
113- os .remove ("/tmp/config.yml" )
127+ _deleteFile ("/tmp/config.yml" )
114128
115129 newNetworkConfig = {
116130 "serverHostname" : "server.linuxmuster.new" ,
@@ -128,15 +142,9 @@ def test_writeNetworkConfig_empty():
128142@mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/tmp/network.conf" )
129143@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
130144def test_upgrade ():
131- if os .path .exists ("/tmp/config.yml" ):
132- os .remove ("/tmp/config.yml" )
133-
134- if os .path .exists ("/tmp/network.conf" ):
135- os .remove ("/tmp/network.conf" )
136-
137- with open (f"{ os .path .dirname (os .path .realpath (__file__ ))} /files/config/network.conf" , "r" ) as fsrc :
138- with open ("/tmp/network.conf" , "w" ) as fdst :
139- 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" )
140148
141149 assert config .upgrade ()
142150 assert not os .path .exists ("/tmp/network.conf" )
@@ -152,8 +160,8 @@ def test_upgrade():
152160 assert yamlContent ["network" ]["realm" ] == "LINUXMUSTER.LEGACY"
153161
154162
155- @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"{ os . path . dirname ( os . path . realpath ( __file__ )) } /files/config /network.conf" )
156- @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" )
157165def test_upgrade_alreadyUpToDate ():
158166 assert config .upgrade ()
159167
@@ -166,36 +174,54 @@ def test_upgrade_alreadyUpToDate():
166174 assert yamlContent ["network" ]["domain" ] == "linuxmuster.lan"
167175 assert yamlContent ["network" ]["realm" ] == "LINUXMUSTER.LAN"
168176
169- @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" )
170178@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
171179def test_upgrade_invalid ():
172- if os .path .exists ("/tmp/config.yml" ):
173- os .remove ("/tmp/config.yml" )
180+ _deleteFile ("/tmp/config.yml" )
181+
182+ assert not config .upgrade ()
183+ assert os .path .exists (f"{ MOCK_FILE_PATH } /network.invalid.conf" )
184+ assert not os .path .exists ("/tmp/config.yml" )
174185
186+ @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"{ MOCK_FILE_PATH } /network.conf" )
187+ @mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/does/not/exist/config.yml" )
188+ def test_upgrade_unwritable ():
175189 assert not config .upgrade ()
176- 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" )
177191 assert not os .path .exists ("/tmp/config.yml" )
178192
179193@mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/does/not/exist/network.conf" )
180194@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/does/not/exist/config.yml" )
181195def test_upgrade_nonexistent ():
182196 assert not config .upgrade ()
183197
184- @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/tmp /network.conf" )
198+ @mock .patch ("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath" , f"/does/not/exist /network.conf" )
185199@mock .patch ("linuxmusterLinuxclient7.config.constants.configFilePath" , f"/tmp/config.yml" )
186- def test_delete ():
187- if os .path .exists ("/tmp/network.conf" ):
188- os .remove ("/tmp/network.conf" )
189- if os .path .exists ("/tmp/config.yml" ):
190- os .remove ("/tmp/config.yml" )
191-
192- with open (f"{ os .path .dirname (os .path .realpath (__file__ ))} /files/config/network.conf" , "r" ) as fsrc :
193- with open ("/tmp/network.conf" , "w" ) as fdst :
194- fdst .write (fsrc .read ())
195- with open (f"{ os .path .dirname (os .path .realpath (__file__ ))} /files/config/config.yml" , "r" ) as fsrc :
196- with open ("/tmp/config.yml" , "w" ) as fdst :
197- fdst .write (fsrc .read ())
198-
199- assert config .delete ()
200- assert not os .path .exists ("/tmp/network.conf" )
201- 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 () == {"letterTemplate" : "_{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