13
13
import subprocess
14
14
import sys
15
15
import time
16
+ from contextvars import ContextVar
16
17
from pathlib import Path
17
18
from threading import Thread
18
- from typing import Any , Generator , NoReturn
19
+ from typing import Any , Generator , NoReturn , Union
19
20
20
21
import pytest
21
22
import requests
34
35
35
36
logger = logging .getLogger (__name__ )
36
37
38
+ reset_broker_var = ContextVar ("reset_broker" , default = True )
39
+ """
40
+ This context variable is used to determine whether the Pact broker should be
41
+ cleaned up. It is used to ensure that the broker is only cleaned up once, even
42
+ if a step is run multiple times.
43
+
44
+ All scenarios which make use of the Pact broker should set this to `True` at the
45
+ start of the scenario.
46
+ """
47
+
37
48
38
49
@pytest .fixture ()
39
50
def verifier () -> Verifier :
40
51
"""Return a new Verifier."""
41
52
return Verifier ()
42
53
43
54
55
+ @pytest .fixture (scope = "session" )
56
+ def broker_url (request : pytest .FixtureRequest ) -> Generator [URL , Any , None ]:
57
+ """
58
+ Fixture to run the Pact broker.
59
+
60
+ This inspects whether the `--broker-url` option has been given. If it has,
61
+ it is assumed that the broker is already running and simply returns the
62
+ given URL.
63
+
64
+ Otherwise, the Pact broker is started in a container. The URL of the
65
+ containerised broker is then returned.
66
+ """
67
+ broker_url : Union [str , None ] = request .config .getoption ("--broker-url" )
68
+
69
+ # If we have been given a broker URL, there's nothing more to do here and we
70
+ # can return early.
71
+ if broker_url :
72
+ yield URL (broker_url )
73
+ return
74
+
75
+ with DockerCompose (
76
+ Path (__file__ ).parent / "util" ,
77
+ compose_file_name = "pact-broker.yml" ,
78
+ pull = True ,
79
+ ) as _ :
80
+ yield URL ("http://pactbroker:pactbroker@localhost:9292" )
81
+ return
82
+
83
+
44
84
################################################################################
45
85
## Scenario
46
86
################################################################################
@@ -70,36 +110,44 @@ def test_incorrect_request_is_made_to_provider() -> None:
70
110
"""Incorrect request is made to provider."""
71
111
72
112
113
+ @pytest .mark .container ()
73
114
@scenario (
74
115
"definition/features/V1/http_provider.feature" ,
75
116
"Verifying a simple HTTP request via a Pact broker" ,
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
123
+ @pytest .mark .container ()
81
124
@scenario (
82
125
"definition/features/V1/http_provider.feature" ,
83
126
"Verifying a simple HTTP request via a Pact broker with publishing results enabled" ,
84
127
)
85
128
def test_verifying_a_simple_http_request_via_a_pact_broker_with_publishing () -> None :
86
129
"""Verifying a simple HTTP request via a Pact broker with publishing."""
130
+ reset_broker_var .set (True ) # noqa: FBT003
87
131
88
132
133
+ @pytest .mark .container ()
89
134
@scenario (
90
135
"definition/features/V1/http_provider.feature" ,
91
136
"Verifying multiple Pact files via a Pact broker" ,
92
137
)
93
138
def test_verifying_multiple_pact_files_via_a_pact_broker () -> None :
94
139
"""Verifying multiple Pact files via a Pact broker."""
140
+ reset_broker_var .set (True ) # noqa: FBT003
95
141
96
142
143
+ @pytest .mark .container ()
97
144
@scenario (
98
145
"definition/features/V1/http_provider.feature" ,
99
146
"Incorrect request is made to provider via a Pact broker" ,
100
147
)
101
148
def test_incorrect_request_is_made_to_provider_via_a_pact_broker () -> None :
102
149
"""Incorrect request is made to provider via a Pact broker."""
150
+ reset_broker_var .set (True ) # noqa: FBT003
103
151
104
152
105
153
@scenario (
@@ -475,6 +523,7 @@ def a_pact_file_for_interaction_is_to_be_verified(
475
523
)
476
524
def a_pact_file_for_interaction_is_to_be_verified_from_a_pact_broker (
477
525
interaction_definitions : dict [int , InteractionDefinition ],
526
+ broker_url : URL ,
478
527
verifier : Verifier ,
479
528
interaction : int ,
480
529
temp_dir : Path ,
@@ -494,15 +543,14 @@ def a_pact_file_for_interaction_is_to_be_verified_from_a_pact_broker(
494
543
pacts_dir .mkdir (exist_ok = True , parents = True )
495
544
pact .write_file (pacts_dir )
496
545
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
546
+ pact_broker = PactBroker (broker_url )
547
+ if reset_broker_var .get ():
548
+ logger .debug ("Resetting Pact broker" )
549
+ pact_broker .reset ()
550
+ reset_broker_var .set (False ) # noqa: FBT003
551
+ pact_broker .publish (pacts_dir )
552
+ verifier .broker_source (pact_broker .url )
553
+ yield pact_broker
506
554
507
555
508
556
@given ("publishing of verification results is enabled" )
0 commit comments