-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
tests.py
178 lines (159 loc) · 5.51 KB
/
tests.py
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
"""Installer unit tests."""
import os
import shutil
import sys
import tempfile
import unittest
from io import StringIO
from pathlib import Path
try:
import configparser
except ImportError:
import ConfigParser as configparser
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import run
class ConfigFileTestCase(unittest.TestCase):
"""Test configuration file generation."""
def setUp(self):
"""Create temp dir."""
self.workdir = tempfile.mkdtemp()
self.cfgfile = os.path.join(self.workdir, "installer.cfg")
def tearDown(self):
"""Delete temp dir."""
shutil.rmtree(self.workdir)
def test_configfile_generation(self):
"""Check simple case."""
out = StringIO()
sys.stdout = out
run.main([
"--stop-after-configfile-check",
"--configfile", self.cfgfile,
"example.test"])
self.assertTrue(os.path.exists(self.cfgfile))
@patch("modoboa_installer.utils.user_input")
def test_interactive_mode(self, mock_user_input):
"""Check interactive mode."""
mock_user_input.side_effect = [
"0", "0", "", "", "", "", ""
]
with open(os.devnull, "w") as fp:
sys.stdout = fp
run.main([
"--stop-after-configfile-check",
"--configfile", self.cfgfile,
"--interactive",
"example.test"])
self.assertTrue(os.path.exists(self.cfgfile))
config = configparser.ConfigParser()
config.read(self.cfgfile)
self.assertEqual(config.get("certificate", "type"), "self-signed")
self.assertEqual(config.get("database", "engine"), "postgres")
@patch("modoboa_installer.utils.user_input")
def test_updating_configfile(self, mock_user_input):
"""Check configfile update mechanism."""
cfgfile_temp = os.path.join(self.workdir, "installer_old.cfg")
out = StringIO()
sys.stdout = out
run.main([
"--stop-after-configfile-check",
"--configfile", cfgfile_temp,
"example.test"])
self.assertTrue(os.path.exists(cfgfile_temp))
# Adding a dummy section
with open(cfgfile_temp, "a") as fp:
fp.write(
"""
[dummy]
weird_old_option = "hey
""")
mock_user_input.side_effect = ["y"]
out = StringIO()
sys.stdout = out
run.main([
"--stop-after-configfile-check",
"--configfile", cfgfile_temp,
"example.test"])
self.assertIn("dummy", out.getvalue())
self.assertTrue(Path(self.workdir).glob("*.old"))
self.assertIn("Update complete",
out.getvalue()
)
@patch("modoboa_installer.utils.user_input")
def test_interactive_mode_letsencrypt(self, mock_user_input):
"""Check interactive mode."""
mock_user_input.side_effect = [
"1", "[email protected]", "0", "", "", "", "", ""
]
with open(os.devnull, "w") as fp:
sys.stdout = fp
run.main([
"--stop-after-configfile-check",
"--configfile", self.cfgfile,
"--interactive",
"example.test"])
self.assertTrue(os.path.exists(self.cfgfile))
config = configparser.ConfigParser()
config.read(self.cfgfile)
self.assertEqual(config.get("certificate", "type"), "letsencrypt")
self.assertEqual(
config.get("letsencrypt", "email"), "[email protected]")
@patch("modoboa_installer.utils.user_input")
def test_configfile_loading(self, mock_user_input):
"""Check interactive mode."""
mock_user_input.side_effect = ["no"]
out = StringIO()
sys.stdout = out
run.main([
"--configfile", self.cfgfile,
"example.test"])
self.assertTrue(os.path.exists(self.cfgfile))
self.assertIn(
"modoboa automx amavis clamav dovecot nginx razor postfix"
" postwhite spamassassin uwsgi",
out.getvalue()
)
self.assertNotIn("It seems that your config file is outdated.",
out.getvalue()
)
@patch("modoboa_installer.utils.user_input")
def test_upgrade_mode(self, mock_user_input):
"""Test upgrade mode launch."""
mock_user_input.side_effect = ["no"]
# 1. Generate a config file
with open(os.devnull, "w") as fp:
sys.stdout = fp
run.main([
"--stop-after-configfile-check",
"--configfile", self.cfgfile,
"example.test"])
# 2. Run upgrade
out = StringIO()
sys.stdout = out
run.main([
"--configfile", self.cfgfile,
"--upgrade",
"example.test"])
self.assertIn(
"Your mail server is about to be upgraded and the following "
"components will be impacted:",
out.getvalue()
)
def test_upgrade_no_config_file(self):
"""Check config file existence check."""
out = StringIO()
sys.stdout = out
with self.assertRaises(SystemExit):
run.main([
"--configfile", self.cfgfile,
"--upgrade",
"example.test"
])
self.assertIn(
"You cannot upgrade an existing installation without a "
"configuration file.", out.getvalue()
)
if __name__ == "__main__":
unittest.main()