@@ -92,8 +92,8 @@ def __init__(self, code):
9292 def code (self ):
9393 return self ._code
9494
95- @pytest . fixture
96- def etcd (self ):
95+ @contextlib . contextmanager
96+ def get_clean_etcd (self ):
9797 endpoint = os .environ .get ('PYTHON_ETCD_HTTP_URL' )
9898 timeout = 5
9999 if endpoint :
@@ -115,66 +115,79 @@ def delete_keys_definitely():
115115
116116 delete_keys_definitely ()
117117
118+ @pytest .fixture
119+ def etcd (self ):
120+ with self .get_clean_etcd () as etcd :
121+ yield etcd
122+
118123 def test_get_unknown_key (self , etcd ):
119124 value , meta = etcd .get ('probably-invalid-key' )
120125 assert value is None
121126 assert meta is None
122127
123128 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
124- def test_get_key (self , etcd , string ):
125- etcdctl ('put' , '/doot/a_key' , string )
126- returned , _ = etcd .get ('/doot/a_key' )
127- assert returned == string .encode ('utf-8' )
129+ def test_get_key (self , string ):
130+ with self .get_clean_etcd () as etcd :
131+ etcdctl ('put' , '/doot/a_key' , string )
132+ returned , _ = etcd .get ('/doot/a_key' )
133+ assert returned == string .encode ('utf-8' )
128134
129135 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
130- def test_get_random_key (self , etcd , string ):
131- etcdctl ('put' , '/doot/' + string , 'dootdoot' )
132- returned , _ = etcd .get ('/doot/' + string )
133- assert returned == b'dootdoot'
136+ def test_get_random_key (self , string ):
137+ with self .get_clean_etcd () as etcd :
138+ etcdctl ('put' , '/doot/' + string , 'dootdoot' )
139+ returned , _ = etcd .get ('/doot/' + string )
140+ assert returned == b'dootdoot'
134141
135142 @given (
136143 characters (blacklist_categories = ['Cs' , 'Cc' ]),
137144 characters (blacklist_categories = ['Cs' , 'Cc' ]),
138145 )
139- def test_get_key_serializable (self , etcd , key , string ):
140- etcdctl ('put' , '/doot/' + key , string )
141- with _out_quorum ():
142- returned , _ = etcd .get ('/doot/' + key , serializable = True )
143- assert returned == string .encode ('utf-8' )
146+ def test_get_key_serializable (self , key , string ):
147+ with self .get_clean_etcd () as etcd :
148+ etcdctl ('put' , '/doot/' + key , string )
149+ with _out_quorum ():
150+ returned , _ = etcd .get ('/doot/' + key , serializable = True )
151+ assert returned == string .encode ('utf-8' )
144152
145153 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
146- def test_get_have_cluster_revision (self , etcd , string ):
147- etcdctl ('put' , '/doot/' + string , 'dootdoot' )
148- _ , md = etcd .get ('/doot/' + string )
149- assert md .response_header .revision > 0
154+ def test_get_have_cluster_revision (self , string ):
155+ with self .get_clean_etcd () as etcd :
156+ etcdctl ('put' , '/doot/' + string , 'dootdoot' )
157+ _ , md = etcd .get ('/doot/' + string )
158+ assert md .response_header .revision > 0
150159
151160 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
152- def test_put_key (self , etcd , string ):
153- etcd .put ('/doot/put_1' , string )
154- out = etcdctl ('get' , '/doot/put_1' )
155- assert base64 .b64decode (out ['kvs' ][0 ]['value' ]) == \
156- string .encode ('utf-8' )
161+ def test_put_key (self , string ):
162+ with self .get_clean_etcd () as etcd :
163+ etcd .put ('/doot/put_1' , string )
164+ out = etcdctl ('get' , '/doot/put_1' )
165+ assert base64 .b64decode (out ['kvs' ][0 ]['value' ]) == \
166+ string .encode ('utf-8' )
157167
158168 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
159- def test_put_has_cluster_revision (self , etcd , string ):
160- response = etcd .put ('/doot/put_1' , string )
161- assert response .header .revision > 0
169+ def test_put_has_cluster_revision (self , string ):
170+ with self .get_clean_etcd () as etcd :
171+ response = etcd .put ('/doot/put_1' , string )
172+ assert response .header .revision > 0
162173
163174 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
164- def test_put_has_prev_kv (self , etcd , string ):
165- etcdctl ('put' , '/doot/put_1' , 'old_value' )
166- response = etcd .put ('/doot/put_1' , string , prev_kv = True )
167- assert response .prev_kv .value == b'old_value'
175+ def test_put_has_prev_kv (self , string ):
176+ with self .get_clean_etcd () as etcd :
177+ etcdctl ('put' , '/doot/put_1' , 'old_value' )
178+ response = etcd .put ('/doot/put_1' , string , prev_kv = True )
179+ assert response .prev_kv .value == b'old_value'
168180
169181 @given (characters (blacklist_categories = ['Cs' , 'Cc' ]))
170- def test_put_if_not_exists (self , etcd , string ):
171- txn_status = etcd .put_if_not_exists ('/doot/put_1' , string )
172- assert txn_status is True
182+ def test_put_if_not_exists (self , string ):
183+ with self .get_clean_etcd () as etcd :
184+ txn_status = etcd .put_if_not_exists ('/doot/put_1' , string )
185+ assert txn_status is True
173186
174- txn_status = etcd .put_if_not_exists ('/doot/put_1' , string )
175- assert txn_status is False
187+ txn_status = etcd .put_if_not_exists ('/doot/put_1' , string )
188+ assert txn_status is False
176189
177- etcdctl ('del' , '/doot/put_1' )
190+ etcdctl ('del' , '/doot/put_1' )
178191
179192 def test_delete_key (self , etcd ):
180193 etcdctl ('put' , '/doot/delete_this' , 'delete pls' )
0 commit comments