1+ import re
12import shutil
23import subprocess
34import tempfile
@@ -28,6 +29,24 @@ def __init__(
2829 self .schema = "http://"
2930 if tls :
3031 self .schema = "https://"
32+ self .compat_args = self .check_compat_args ()
33+
34+ def check_compat_args (self ):
35+ version_re = re .compile (r"^etcd version:\s+(\d)\.(\d)" , re .I )
36+ version_data = subprocess .check_output (
37+ [self .proc_name , "--version" ]
38+ ).decode ("utf-8" )
39+ match = version_re .match (version_data )
40+ if match is not None :
41+ etcd_version = (int (match .group (1 )), int (match .group (2 )))
42+ else :
43+ etcd_version = (0 , 0 )
44+ if etcd_version [0 ] < 3 or (
45+ etcd_version [0 ] == 3 and etcd_version [1 ] < 4
46+ ):
47+ return []
48+ else :
49+ return ["--enable-v2=true" ]
3150
3251 def run (self , number = 1 , proc_args = []):
3352 if number > 1 :
@@ -40,7 +59,12 @@ def run(self, number=1, proc_args=[]):
4059 ]
4160 )
4261 proc_args .extend (
43- ["-initial-cluster" , initial_cluster , "-initial-cluster-state" , "new" ]
62+ [
63+ "-initial-cluster" ,
64+ initial_cluster ,
65+ "-initial-cluster-state" ,
66+ "new" ,
67+ ]
4468 )
4569 else :
4670 proc_args .extend (
@@ -70,7 +94,10 @@ def add_one(self, slot, proc_args=None):
7094
7195 log .debug ("Created directory %s" % directory )
7296 client = "%s127.0.0.1:%d" % (self .schema , self .port_range_start + slot )
73- peer = "%s127.0.0.1:%d" % ("http://" , self .internal_port_range_start + slot )
97+ peer = "%s127.0.0.1:%d" % (
98+ "http://" ,
99+ self .internal_port_range_start + slot ,
100+ )
74101 daemon_args = [
75102 self .proc_name ,
76103 "-data-dir" ,
@@ -86,7 +113,7 @@ def add_one(self, slot, proc_args=None):
86113 "-listen-client-urls" ,
87114 client ,
88115 ]
89-
116+ daemon_args . extend ( self . compat_args )
90117 if proc_args :
91118 daemon_args .extend (proc_args )
92119
@@ -134,7 +161,9 @@ def create_test_ca_certificate(cls, cert_path, key_path, cn=None):
134161 cert .add_extensions (
135162 [
136163 crypto .X509Extension (
137- "basicConstraints" .encode ("ascii" ), False , "CA:TRUE" .encode ("ascii" )
164+ "basicConstraints" .encode ("ascii" ),
165+ False ,
166+ "CA:TRUE" .encode ("ascii" ),
138167 ),
139168 crypto .X509Extension (
140169 "keyUsage" .encode ("ascii" ),
@@ -164,10 +193,16 @@ def create_test_ca_certificate(cls, cert_path, key_path, cn=None):
164193 cert .sign (k , "sha1" )
165194
166195 with open (cert_path , "w" ) as f :
167- f .write (crypto .dump_certificate (crypto .FILETYPE_PEM , cert ).decode ("utf-8" ))
196+ f .write (
197+ crypto .dump_certificate (crypto .FILETYPE_PEM , cert ).decode (
198+ "utf-8"
199+ )
200+ )
168201
169202 with open (key_path , "w" ) as f :
170- f .write (crypto .dump_privatekey (crypto .FILETYPE_PEM , k ).decode ("utf-8" ))
203+ f .write (
204+ crypto .dump_privatekey (crypto .FILETYPE_PEM , k ).decode ("utf-8" )
205+ )
171206
172207 return cert , k
173208
@@ -196,7 +231,9 @@ def create_test_certificate(cls, ca, ca_key, cert_path, key_path, cn=None):
196231 crypto .X509Extension (
197232 "keyUsage" .encode ("ascii" ),
198233 False ,
199- "nonRepudiation,digitalSignature,keyEncipherment" .encode ("ascii" ),
234+ "nonRepudiation,digitalSignature,keyEncipherment" .encode (
235+ "ascii"
236+ ),
200237 ),
201238 crypto .X509Extension (
202239 "extendedKeyUsage" .encode ("ascii" ),
@@ -220,7 +257,13 @@ def create_test_certificate(cls, ca, ca_key, cert_path, key_path, cn=None):
220257 cert .sign (ca_key , "sha1" )
221258
222259 with open (cert_path , "w" ) as f :
223- f .write (crypto .dump_certificate (crypto .FILETYPE_PEM , cert ).decode ("utf-8" ))
260+ f .write (
261+ crypto .dump_certificate (crypto .FILETYPE_PEM , cert ).decode (
262+ "utf-8"
263+ )
264+ )
224265
225266 with open (key_path , "w" ) as f :
226- f .write (crypto .dump_privatekey (crypto .FILETYPE_PEM , k ).decode ("utf-8" ))
267+ f .write (
268+ crypto .dump_privatekey (crypto .FILETYPE_PEM , k ).decode ("utf-8" )
269+ )
0 commit comments