forked from modoboa/modoboa-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
96 lines (83 loc) · 3 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
"""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."""
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()
)
if __name__ == "__main__":
unittest.main()