9
9
import logging
10
10
import pickle
11
11
import re
12
+ import shutil
12
13
import signal
13
14
import subprocess
14
15
import sys
15
16
import time
17
+ from contextvars import ContextVar
16
18
from pathlib import Path
17
19
from threading import Thread
18
- from typing import Any , Generator , NoReturn
20
+ from typing import Any , Generator , NoReturn , Union
19
21
20
22
import pytest
21
23
import requests
34
36
35
37
logger = logging .getLogger (__name__ )
36
38
39
+ reset_broker_var = ContextVar ("reset_broker" , default = True )
40
+ """
41
+ This context variable is used to determine whether the Pact broker should be
42
+ cleaned up. It is used to ensure that the broker is only cleaned up once, even
43
+ if a step is run multiple times.
44
+
45
+ All scenarios which make use of the Pact broker should set this to `True` at the
46
+ start of the scenario.
47
+ """
48
+
37
49
38
50
@pytest .fixture ()
39
51
def verifier () -> Verifier :
40
52
"""Return a new Verifier."""
41
53
return Verifier ()
42
54
43
55
56
+ @pytest .fixture (scope = "session" )
57
+ def broker_url (request : pytest .FixtureRequest ) -> Generator [URL , Any , None ]:
58
+ """
59
+ Fixture to run the Pact broker.
60
+
61
+ This inspects whether the `--broker-url` option has been given. If it has,
62
+ it is assumed that the broker is already running and simply returns the
63
+ given URL.
64
+
65
+ Otherwise, the Pact broker is started in a container. The URL of the
66
+ containerised broker is then returned.
67
+ """
68
+ broker_url : Union [str , None ] = request .config .getoption ("--broker-url" )
69
+
70
+ # If we have been given a broker URL, there's nothing more to do here and we
71
+ # can return early.
72
+ if broker_url :
73
+ yield URL (broker_url )
74
+ return
75
+
76
+ with DockerCompose (
77
+ Path (__file__ ).parent / "util" ,
78
+ compose_file_name = "pact-broker.yml" ,
79
+ pull = True ,
80
+ ) as _ :
81
+ yield URL ("http://pactbroker:pactbroker@localhost:9292" )
82
+ return
83
+
84
+
44
85
################################################################################
45
86
## Scenario
46
87
################################################################################
@@ -76,6 +117,7 @@ def test_incorrect_request_is_made_to_provider() -> None:
76
117
)
77
118
def test_verifying_a_simple_http_request_via_a_pact_broker () -> None :
78
119
"""Verifying a simple HTTP request via a Pact broker."""
120
+ reset_broker_var .set (True ) # noqa: FBT003
79
121
80
122
81
123
@scenario (
@@ -84,6 +126,7 @@ def test_verifying_a_simple_http_request_via_a_pact_broker() -> None:
84
126
)
85
127
def test_verifying_a_simple_http_request_via_a_pact_broker_with_publishing () -> None :
86
128
"""Verifying a simple HTTP request via a Pact broker with publishing."""
129
+ reset_broker_var .set (True ) # noqa: FBT003
87
130
88
131
89
132
@scenario (
@@ -92,6 +135,7 @@ def test_verifying_a_simple_http_request_via_a_pact_broker_with_publishing() ->
92
135
)
93
136
def test_verifying_multiple_pact_files_via_a_pact_broker () -> None :
94
137
"""Verifying multiple Pact files via a Pact broker."""
138
+ reset_broker_var .set (True ) # noqa: FBT003
95
139
96
140
97
141
@scenario (
@@ -100,6 +144,7 @@ def test_verifying_multiple_pact_files_via_a_pact_broker() -> None:
100
144
)
101
145
def test_incorrect_request_is_made_to_provider_via_a_pact_broker () -> None :
102
146
"""Incorrect request is made to provider via a Pact broker."""
147
+ reset_broker_var .set (True ) # noqa: FBT003
103
148
104
149
105
150
@scenario (
@@ -475,6 +520,7 @@ def a_pact_file_for_interaction_is_to_be_verified(
475
520
)
476
521
def a_pact_file_for_interaction_is_to_be_verified_from_a_pact_broker (
477
522
interaction_definitions : dict [int , InteractionDefinition ],
523
+ broker_url : URL ,
478
524
verifier : Verifier ,
479
525
interaction : int ,
480
526
temp_dir : Path ,
@@ -491,18 +537,18 @@ def a_pact_file_for_interaction_is_to_be_verified_from_a_pact_broker(
491
537
defn .add_to_pact (pact , f"interaction { interaction } " )
492
538
493
539
pacts_dir = temp_dir / "pacts"
540
+ if pacts_dir .exists ():
541
+ shutil .rmtree (pacts_dir )
494
542
pacts_dir .mkdir (exist_ok = True , parents = True )
495
543
pact .write_file (pacts_dir )
496
544
497
- with DockerCompose (
498
- Path (__file__ ).parent / "util" ,
499
- compose_file_name = "pact-broker.yml" ,
500
- pull = True ,
501
- ) as _ :
502
- pact_broker = PactBroker (URL ("http://pactbroker:pactbroker@localhost:9292" ))
503
- pact_broker .publish (pacts_dir )
504
- verifier .broker_source (pact_broker .url )
505
- yield pact_broker
545
+ pact_broker = PactBroker (broker_url )
546
+ if reset_broker_var .get ():
547
+ pact_broker .reset ()
548
+ reset_broker_var .set (False ) # noqa: FBT003
549
+ pact_broker .publish (pacts_dir )
550
+ verifier .broker_source (pact_broker .url )
551
+ yield pact_broker
506
552
507
553
508
554
@given ("publishing of verification results is enabled" )
0 commit comments