comparison tests/e2e/conftest.py @ 4305:4cd4922de876

tests: reformat tests using black.
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:11:56 +0200
parents d78b5eae912a
children
comparison
equal deleted inserted replaced
4304:92a886f31581 4305:4cd4922de876
58 58
59 The file will be in self.dest_path. 59 The file will be in self.dest_path.
60 The file should be deleted after use. If not, it will be deleted at the end of 60 The file should be deleted after use. If not, it will be deleted at the end of
61 session with the whole temporary test files directory. 61 session with the whole temporary test files directory.
62 """ 62 """
63 name = ''.join(random.choices(self.ALPHABET, k=8)) 63 name = "".join(random.choices(self.ALPHABET, k=8))
64 return self.dest_files / name 64 return self.dest_files / name
65 65
66 def size(self, size: int, use_cache: bool = True): 66 def size(self, size: int, use_cache: bool = True):
67 """Create a file of requested size, and returns its path 67 """Create a file of requested size, and returns its path
68 68
70 """ 70 """
71 dest_path = self.source_files / str(size) 71 dest_path = self.source_files / str(size)
72 if not use_cache or not dest_path.exists(): 72 if not use_cache or not dest_path.exists():
73 hash_ = hashlib.sha256() 73 hash_ = hashlib.sha256()
74 remaining = size 74 remaining = size
75 with dest_path.open('wb') as f: 75 with dest_path.open("wb") as f:
76 while remaining: 76 while remaining:
77 if remaining > self.BUF_SIZE: 77 if remaining > self.BUF_SIZE:
78 to_get = self.BUF_SIZE 78 to_get = self.BUF_SIZE
79 else: 79 else:
80 to_get = remaining 80 to_get = remaining
90 return self.hashes[source_file] 90 return self.hashes[source_file]
91 91
92 def get_dest_hash(self, dest_file: Path) -> str: 92 def get_dest_hash(self, dest_file: Path) -> str:
93 """Calculate hash of file at given path""" 93 """Calculate hash of file at given path"""
94 hash_ = hashlib.sha256() 94 hash_ = hashlib.sha256()
95 with dest_file.open('rb') as f: 95 with dest_file.open("rb") as f:
96 while True: 96 while True:
97 buf = f.read(self.BUF_SIZE) 97 buf = f.read(self.BUF_SIZE)
98 if not buf: 98 if not buf:
99 break 99 break
100 hash_.update(buf) 100 hash_.update(buf)
103 103
104 class TestMessage(EmailMessage): 104 class TestMessage(EmailMessage):
105 105
106 @property 106 @property
107 def subject(self): 107 def subject(self):
108 return self['subject'] 108 return self["subject"]
109 109
110 @property 110 @property
111 def from_(self): 111 def from_(self):
112 return self['from'] 112 return self["from"]
113 113
114 @property 114 @property
115 def to(self): 115 def to(self):
116 return self['to'] 116 return self["to"]
117 117
118 @property 118 @property
119 def body(self): 119 def body(self):
120 return self.get_payload(decode=True).decode() 120 return self.get_payload(decode=True).decode()
121 121
148 accounts_by_servers = { 148 accounts_by_servers = {
149 1: 1, 149 1: 1,
150 2: 1, 150 2: 1,
151 3: 1, 151 3: 1,
152 } 152 }
153 for server_idx in range(1, nb_servers+1): 153 for server_idx in range(1, nb_servers + 1):
154 account_stop = accounts_by_servers[server_idx] + 1 154 account_stop = accounts_by_servers[server_idx] + 1
155 for account_idx in range(1, account_stop): 155 for account_idx in range(1, account_stop):
156 profile_suff = f"_s{server_idx}" if server_idx>1 else "" 156 profile_suff = f"_s{server_idx}" if server_idx > 1 else ""
157 profile = f"account{account_idx}{profile_suff}" 157 profile = f"account{account_idx}{profile_suff}"
158 profiles.append(profile) 158 profiles.append(profile)
159 try: 159 try:
160 li.account.create( 160 li.account.create(
161 f"account{account_idx}@server{server_idx}.test", 161 f"account{account_idx}@server{server_idx}.test",
162 "test", 162 "test",
163 profile=profile, 163 profile=profile,
164 host=f"server{server_idx}.test" 164 host=f"server{server_idx}.test",
165 ) 165 )
166 except sh.ErrorReturnCode_19: 166 except sh.ErrorReturnCode_19:
167 # this is the conlict exit code, this can happen when tests are run 167 # this is the conlict exit code, this can happen when tests are run
168 # inside a container when --keep-profiles is used with run_e2e.py. 168 # inside a container when --keep-profiles is used with run_e2e.py.
169 pass 169 pass
184 Both nodes will be created with "account1" profile, named "test" and have an "open" 184 Both nodes will be created with "account1" profile, named "test" and have an "open"
185 access model. 185 access model.
186 One node will be on account1's PEP, the other one on pubsub.server1.test. 186 One node will be on account1's PEP, the other one on pubsub.server1.test.
187 """ 187 """
188 li.pubsub.node.create( 188 li.pubsub.node.create(
189 "-f", "access_model", "open", 189 "-f", "access_model", "open", node="test", profile="account1", connect=True
190 node="test",
191 profile="account1", connect=True
192 ) 190 )
193 li.pubsub.node.create( 191 li.pubsub.node.create(
194 "-f", "access_model", "open", 192 "-f",
195 service="pubsub.server1.test", node="test", 193 "access_model",
196 profile="account1" 194 "open",
195 service="pubsub.server1.test",
196 node="test",
197 profile="account1",
197 ) 198 )
198 yield 199 yield
200 li.pubsub.node.delete(node="test", profile="account1", connect=True, force=True)
199 li.pubsub.node.delete( 201 li.pubsub.node.delete(
200 node="test", 202 service="pubsub.server1.test", node="test", profile="account1", force=True
201 profile="account1", connect=True,
202 force=True
203 )
204 li.pubsub.node.delete(
205 service="pubsub.server1.test", node="test",
206 profile="account1",
207 force=True
208 ) 203 )
209 204
210 205
211 @pytest.fixture(scope="session") 206 @pytest.fixture(scope="session")
212 def fake_file(): 207 def fake_file():