-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_config.py
More file actions
240 lines (199 loc) · 10.6 KB
/
test_config.py
File metadata and controls
240 lines (199 loc) · 10.6 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os, yaml
from unittest import mock
from .. import config
MOCK_FILE_PATH = f"{os.path.dirname(os.path.realpath(__file__))}/files/config"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_network_legacy():
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.legacy"
assert networkConfig["domain"] == "linuxmuster.legacy"
assert networkConfig["realm"] == "LINUXMUSTER.LEGACY"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/config.yml")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
def test_network():
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.lan"
assert networkConfig["domain"] == "linuxmuster.lan"
assert networkConfig["realm"] == "LINUXMUSTER.LAN"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
def test_network_both():
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.lan"
assert networkConfig["domain"] == "linuxmuster.lan"
assert networkConfig["realm"] == "LINUXMUSTER.LAN"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
@mock.patch("linuxmusterLinuxclient7.config.logging.exception")
def test_network_none(mockLoggingException):
# make sure that no confusing error message is logged when config file does not exist
rc, networkConfig = config.network()
assert not rc
assert networkConfig is None
assert not mockLoggingException.called
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.invalid.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
@mock.patch("linuxmusterLinuxclient7.config.logging.exception")
def test_network_legacy_invalid(mockLoggingException):
# make sure that an error message is logged when the legacy config file exists but is invalid
rc, networkConfig = config.network()
assert not rc
assert networkConfig is None
assert mockLoggingException.called
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.invalid-network.yml")
def test_network_invalid():
rc, networkConfig = config.network()
assert not rc
assert networkConfig is None
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
def test_shares():
sharesConfig = config.shares()
assert "letterTemplate" in sharesConfig
assert sharesConfig["letterTemplate"] == "_{letter}"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_shares_none():
sharesConfig = config.shares()
assert "letterTemplate" in sharesConfig
assert sharesConfig["letterTemplate"] == config.constants.defaultShareLetterTemplate
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.no-shares.yml")
def test_shares_missing():
sharesConfig = config.shares()
assert "letterTemplate" in sharesConfig
assert sharesConfig["letterTemplate"] == config.constants.defaultShareLetterTemplate
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.invalid-syntax.yml")
def test_syntax_invalid():
rc, networkConfig = config.network()
assert not rc
assert networkConfig is None
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_writeNetworkConfig():
_deleteFile("/tmp/config.yml")
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
newNetworkConfig = {
"serverHostname": "server.linuxmuster.new",
"domain": "linuxmuster.new",
"realm": "LINUXMUSTER.NEW"
}
assert config.writeNetworkConfig(newNetworkConfig)
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.new"
assert networkConfig["domain"] == "linuxmuster.new"
assert networkConfig["realm"] == "LINUXMUSTER.NEW"
assert config.shares() == {"letterTemplate": "_{letter}"}
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_writeNetworkConfig_invalid():
_deleteFile("/tmp/config.yml")
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
newNetworkConfig = {
"sserverHostname": "server.linuxmuster.new",
"domain": "linuxmuster.new",
"realm": "LINUXMUSTER.NEW"
}
assert not config.writeNetworkConfig(newNetworkConfig)
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.lan"
assert networkConfig["domain"] == "linuxmuster.lan"
assert networkConfig["realm"] == "LINUXMUSTER.LAN"
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_writeNetworkConfig_invalidPath():
newNetworkConfig = {
"serverHostname": "server.linuxmuster.new",
"domain": "linuxmuster.new",
"realm": "LINUXMUSTER.NEW"
}
assert not config.writeNetworkConfig(newNetworkConfig)
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_writeNetworkConfig_empty():
_deleteFile("/tmp/config.yml")
newNetworkConfig = {
"serverHostname": "server.linuxmuster.new",
"domain": "linuxmuster.new",
"realm": "LINUXMUSTER.NEW"
}
assert config.writeNetworkConfig(newNetworkConfig)
rc, networkConfig = config.network()
assert rc
assert networkConfig["serverHostname"] == "server.linuxmuster.new"
assert networkConfig["domain"] == "linuxmuster.new"
assert networkConfig["realm"] == "LINUXMUSTER.NEW"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/tmp/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_upgrade():
_deleteFile("/tmp/config.yml")
_deleteFile("/tmp/network.conf")
_copyFile(f"{MOCK_FILE_PATH}/network.conf", "/tmp/network.conf")
assert config.upgrade()
assert not os.path.exists("/tmp/network.conf")
assert os.path.exists("/tmp/config.yml")
with open("/tmp/config.yml", "r") as f:
content = f.read()
print(content)
yamlContent = yaml.safe_load(content)
assert "network" in yamlContent
assert yamlContent["network"]["serverHostname"] == "server.linuxmuster.legacy"
assert yamlContent["network"]["domain"] == "linuxmuster.legacy"
assert yamlContent["network"]["realm"] == "LINUXMUSTER.LEGACY"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"{MOCK_FILE_PATH}/config.yml")
def test_upgrade_alreadyUpToDate():
assert config.upgrade()
with open(config.constants.configFilePath, "r") as f:
content = f.read()
print(content)
yamlContent = yaml.safe_load(content)
assert "network" in yamlContent
assert yamlContent["network"]["serverHostname"] == "server.linuxmuster.lan"
assert yamlContent["network"]["domain"] == "linuxmuster.lan"
assert yamlContent["network"]["realm"] == "LINUXMUSTER.LAN"
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.invalid.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_upgrade_invalid():
_deleteFile("/tmp/config.yml")
assert not config.upgrade()
assert os.path.exists(f"{MOCK_FILE_PATH}/network.invalid.conf")
assert not os.path.exists("/tmp/config.yml")
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"{MOCK_FILE_PATH}/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_upgrade_unwritable():
assert not config.upgrade()
assert os.path.exists(f"{MOCK_FILE_PATH}/network.invalid.conf")
assert not os.path.exists("/tmp/config.yml")
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_upgrade_nonexistent():
assert not config.upgrade()
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/tmp/config.yml")
def test_deleteNetworkConfig():
_deleteFile("/tmp/network.conf")
_deleteFile("/tmp/config.yml")
_copyFile(f"{MOCK_FILE_PATH}/config.yml", "/tmp/config.yml")
assert config.deleteNetworkConfig()
assert os.path.exists("/tmp/config.yml")
assert config.network() == (False, None)
assert config.shares() == {"letterTemplate": "_{letter}"}
@mock.patch("linuxmusterLinuxclient7.config.constants.legacyNetworkConfigFilePath", f"/does/not/exist/network.conf")
@mock.patch("linuxmusterLinuxclient7.config.constants.configFilePath", f"/does/not/exist/config.yml")
def test_deleteNetworkConfig_nonexistent():
assert config.deleteNetworkConfig()
# --------------------
# - Helper functions -
# --------------------
def _deleteFile(path):
if os.path.exists(path):
os.remove(path)
def _copyFile(src, dst):
with open(src, "r") as fsrc:
with open(dst, "w") as fdst:
fdst.write(fsrc.read())