Skip to content

Commit de7489d

Browse files
committed
add permissions tests (should fail)
1 parent 6a718fe commit de7489d

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

.github/workflows/pytest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: |
4848
# sometimes the scripts hang because the channels can't be closed,
4949
# so run under a short timeout
50-
timeout 60s pytest -v --cov-report=term-missing --cov=varys
50+
timeout 120s pytest -v --cov-report=term-missing --cov=varys
5151
- name: "Upload Logfile"
5252
uses: actions/upload-artifact@v4
5353
with:

tests/test_varys.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ def quick_turnaround(self):
118118
self.setUp()
119119
# timeout seems to need to be at least 0.01s
120120
received_messages = [
121-
message.body.decode()[1:-1] for message in self.v.receive_batch('test_varys', queue_suffix='q', timeout=0.1)
121+
message.body.decode()[1:-1]
122+
for message in self.v.receive_batch(
123+
"test_varys", queue_suffix="q", timeout=0.1
124+
)
122125
]
123126

124127
self.assertEqual(received_messages, sent_messages)
@@ -226,6 +229,75 @@ def test_quick_turnaround(self):
226229
self.quick_turnaround()
227230

228231

232+
class TestVarysPermissions(unittest.TestCase):
233+
234+
def setUp(self):
235+
config = {
236+
"version": "0.1",
237+
"profiles": {
238+
"test": {
239+
"username": "guest2",
240+
"password": "guest",
241+
"amqp_url": "localhost",
242+
"port": 5671,
243+
"use_tls": True,
244+
"ca_certificate": ".rabbitmq/ca_certificate.pem",
245+
"client_certificate": ".rabbitmq/client_certificate.pem",
246+
"client_key": ".rabbitmq/client_key.pem",
247+
}
248+
},
249+
}
250+
251+
with open(TMP_FILENAME, "w") as f:
252+
json.dump(config, f, ensure_ascii=False)
253+
254+
self.v = Varys("test", LOG_FILENAME, config_path=TMP_FILENAME)
255+
256+
def tearDown(self):
257+
# this seems to prevent some hanging
258+
# or errors related to closing connections that haven't opened yet
259+
# I presume because some operations are so fast
260+
# that we try to close the connections before they've opened
261+
# 0.01s seems to be sufficient; 0.1s is just a bit conservative
262+
time.sleep(0.1)
263+
264+
self.v.close()
265+
os.remove(TMP_FILENAME)
266+
time.sleep(0.1)
267+
268+
credentials = pika.PlainCredentials("guest", "guest")
269+
270+
connection = pika.BlockingConnection(
271+
pika.ConnectionParameters("localhost", credentials=credentials)
272+
)
273+
channel = connection.channel()
274+
275+
channel.queue_purge(queue="")
276+
277+
connection.close()
278+
time.sleep(0.5)
279+
280+
# check that all file handles were dropped
281+
logger = logging.getLogger("test_varys")
282+
self.assertEqual(len(logger.handlers), 0)
283+
284+
def test_send_receive_extant_queue(self):
285+
self.v.send(TEXT, "test-exchange", queue_suffix="test_queue")
286+
message = self.v.receive("test-exchange", queue_suffix="test_queue")
287+
self.assertEqual(TEXT, json.loads(message.body))
288+
289+
logger = logging.getLogger("test_varys")
290+
self.assertEqual(len(logger.handlers), 1)
291+
292+
def test_send_nonextant_queue(self):
293+
self.v.send(TEXT, "test-exchange", queue_suffix="test_queue_2")
294+
message = self.v.receive("test-exchange", queue_suffix="test_queue_2")
295+
self.assertEqual(TEXT, json.loads(message.body))
296+
297+
logger = logging.getLogger("test_varys")
298+
self.assertEqual(len(logger.handlers), 1)
299+
300+
229301
class TestVarysConfig(unittest.TestCase):
230302
def tearDown(self):
231303
os.remove(TMP_FILENAME)

0 commit comments

Comments
 (0)