Mercurial > libervia-backend
annotate tests/e2e/conftest.py @ 3429:d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
- moved jp tests to `e2e/jp`
- new fixtures
- adapted docker-compose
- improved `run_e2e` with several flags + report on failure
- doc to come
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 27 Nov 2020 16:39:40 +0100 |
parents | 814e118d9ef3 |
children | f9011d62a87a |
rev | line source |
---|---|
3415 | 1 #!/usr/bin/env python3 |
2 | |
3 # SàT: an XMPP client | |
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 import os | |
20 import tempfile | |
21 import string | |
22 import hashlib | |
23 import random | |
24 from pathlib import Path | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
25 from aiosmtpd.controller import Controller |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
26 from aiosmtpd.handlers import Message |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
27 from email.message import EmailMessage |
3415 | 28 from sh import jp |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
29 import pytest |
3415 | 30 |
31 | |
32 class FakeFile: | |
33 ALPHABET = f"{string.ascii_letters}{string.digits}_" | |
34 BUF_SIZE = 65535 | |
35 | |
36 def __init__(self): | |
37 self.tmp_dir_obj = tempfile.TemporaryDirectory(prefix="sat_e2e_test_files_") | |
38 self.tmp_dir_path = Path(self.tmp_dir_obj.name) | |
39 self.source_files = self.tmp_dir_path / "source" | |
40 self.source_files.mkdir() | |
41 self.dest_files = self.tmp_dir_path / "dest" | |
42 self.dest_files.mkdir() | |
43 self.hashes = {} | |
44 | |
45 @property | |
46 def dest_path(self): | |
47 """Path of a directory where files can be received | |
48 | |
49 The directory will be deleted at the end of session. | |
50 Files from other test can be present, be sure to create a unique subdirectory or | |
51 to use a unique destination file name | |
52 """ | |
53 return self.dest_files | |
54 | |
55 def new_dest_file(self) -> Path: | |
56 """Path to a randomly named destination file | |
57 | |
58 The file will be in self.dest_path. | |
59 The file should be deleted after use. If not, it will be deleted at the end of | |
60 session with the whole temporary test files directory. | |
61 """ | |
62 name = ''.join(random.choices(self.ALPHABET, k=8)) | |
63 return self.dest_files / name | |
64 | |
65 def size(self, size: int, use_cache: bool = True): | |
66 """Create a file of requested size, and returns its path | |
67 | |
68 @param use_cache: if True and a file of this size already exists, it is re-used | |
69 """ | |
70 dest_path = self.source_files / str(size) | |
71 if not use_cache or not dest_path.exists(): | |
72 hash_ = hashlib.sha256() | |
73 remaining = size | |
74 with dest_path.open('wb') as f: | |
75 while remaining: | |
76 if remaining > self.BUF_SIZE: | |
77 to_get = self.BUF_SIZE | |
78 else: | |
79 to_get = remaining | |
80 buf = os.urandom(to_get) | |
81 f.write(buf) | |
82 hash_.update(buf) | |
83 remaining -= to_get | |
84 self.hashes[dest_path] = hash_.hexdigest() | |
85 return dest_path | |
86 | |
87 def get_source_hash(self, source_file: Path) -> str: | |
88 """Retrieve hash calculated for a generated source file""" | |
89 return self.hashes[source_file] | |
90 | |
91 def get_dest_hash(self, dest_file: Path) -> str: | |
92 """Calculate hash of file at given path""" | |
93 hash_ = hashlib.sha256() | |
94 with dest_file.open('rb') as f: | |
95 while True: | |
96 buf = f.read(self.BUF_SIZE) | |
97 if not buf: | |
98 break | |
99 hash_.update(buf) | |
100 return hash_.hexdigest() | |
101 | |
102 | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
103 class TestMessage(EmailMessage): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
104 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
105 @property |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
106 def subject(self): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
107 return self['subject'] |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
108 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
109 @property |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
110 def from_(self): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
111 return self['from'] |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
112 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
113 @property |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
114 def to(self): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
115 return self['to'] |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
116 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
117 @property |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
118 def body(self): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
119 return self.get_payload(decode=True).decode() |
3415 | 120 |
121 | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
122 class SMTPMessageHandler(Message): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
123 messages = [] |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
124 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
125 def __init__(self): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
126 super().__init__(message_class=TestMessage) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
127 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
128 def handle_message(self, message): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
129 self.messages.append(message) |
3415 | 130 |
131 | |
132 @pytest.fixture(scope="session") | |
133 def test_profiles(): | |
134 """Test accounts created using in-band registration | |
135 | |
136 They will be removed at the end of session. | |
137 The number of account per servers is set in the "accounts_by_servers" dict. | |
138 Jids are in the form "account[x]@server[y].test". | |
139 The profiles used are in the form "account[x]" for server1.test, and | |
140 "account[x]_s[y]" for other servers. | |
141 Password is "test" for all profiles and XMPP accounts. | |
142 "account1" is connected and set as default profile | |
143 Profiles created are returned as a tuple | |
144 """ | |
145 profiles = [] | |
146 nb_servers = 3 | |
147 accounts_by_servers = { | |
148 1: 1, | |
149 2: 1, | |
150 3: 0, | |
151 } | |
152 for server_idx in range(1, nb_servers+1): | |
153 account_stop = accounts_by_servers[server_idx] + 1 | |
154 for account_idx in range(1, account_stop): | |
155 profile_suff = f"_s{server_idx}" if server_idx>1 else "" | |
156 profile = f"account{account_idx}{profile_suff}" | |
157 profiles.append(profile) | |
158 jp.account.create( | |
159 f"account{account_idx}@server{server_idx}.test", | |
160 "test", | |
161 profile=profile, | |
162 host=f"server{server_idx}.test" | |
163 ) | |
164 jp.profile.modify(profile="account1", default=True, connect=True) | |
165 jp.profile.connect(profile="account1_s2", connect=True) | |
166 yield tuple(profiles) | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
167 # This environment may be used during tests development |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
168 if os.getenv("SAT_TEST_E2E_KEEP_PROFILES") == None: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
169 for profile in profiles: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
170 jp.account.delete(profile=profile, connect=True, force=True) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
171 jp.profile.delete(profile, force=True) |
3415 | 172 |
173 | |
174 @pytest.fixture(scope="class") | |
175 def pubsub_nodes(test_profiles): | |
176 """Create 2 testing nodes | |
177 | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
178 Both nodes will be created with "account1" profile, named "test" and have an "open" |
3415 | 179 access model. |
180 One node will account1's PEP, the other one on pubsub.server1.test. | |
181 """ | |
182 jp.pubsub.node.create( | |
183 "-f", "access_model", "open", | |
184 node="test", | |
185 profile="account1", connect=True | |
186 ) | |
187 jp.pubsub.node.create( | |
188 "-f", "access_model", "open", | |
189 service="pubsub.server1.test", node="test", | |
190 profile="account1" | |
191 ) | |
192 yield | |
193 jp.pubsub.node.delete( | |
194 node="test", | |
195 profile="account1", connect=True, | |
196 force=True | |
197 ) | |
198 jp.pubsub.node.delete( | |
199 service="pubsub.server1.test", node="test", | |
200 profile="account1", | |
201 force=True | |
202 ) | |
203 | |
204 | |
205 @pytest.fixture(scope="session") | |
206 def fake_file(): | |
207 """Manage dummy files creation and destination path""" | |
208 return FakeFile() | |
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
209 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
210 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
211 @pytest.fixture(scope="session") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
212 def test_files(): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
213 """Return a Path to test files directory""" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
214 return Path(__file__).parent.parent / "_files" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
215 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
216 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
217 @pytest.fixture(scope="session") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
218 def fake_smtp(): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
219 """Create a fake STMP server to check sent emails""" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
220 controller = Controller(SMTPMessageHandler()) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
221 controller.hostname = "0.0.0.0" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
222 controller.start() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
223 yield |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
224 controller.stop() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
225 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
226 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
227 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
228 def sent_emails(fake_smtp): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
229 """Catch email sent during the tests""" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
230 SMTPMessageHandler.messages.clear() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
3415
diff
changeset
|
231 return SMTPMessageHandler.messages |