forked from modoboa/modoboa-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
137 lines (122 loc) · 4.31 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
"""Installer unit tests."""
import os
import shutil
import sys
import tempfile
import unittest
from six import StringIO
from six.moves import 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_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()
)
@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()