]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/rgw/test_multi.py
11 import ConfigParser
as configparser
15 from rgw_multi
import multisite
16 # make tests from rgw_multi.tests available to nose
17 from rgw_multi
.tests
import *
19 mstart_path
= os
.getenv('MSTART_PATH')
20 if mstart_path
is None:
21 mstart_path
= os
.path
.normpath(os
.path
.dirname(os
.path
.realpath(__file__
)) + '/../..') + '/'
23 test_path
= os
.path
.normpath(os
.path
.dirname(os
.path
.realpath(__file__
))) + '/'
25 # configure logging for the tests module
26 log
= logging
.getLogger('rgw_multi.tests')
28 def bash(cmd
, **kwargs
):
29 log
.debug('running cmd: %s', ' '.join(cmd
))
30 check_retcode
= kwargs
.pop('check_retcode', True)
31 kwargs
['stdout'] = subprocess
.PIPE
32 process
= subprocess
.Popen(cmd
, **kwargs
)
33 s
= process
.communicate()[0]
34 log
.debug('command returned status=%d stdout=%s', process
.returncode
, s
.decode('utf-8'))
36 assert(process
.returncode
== 0)
37 return (s
, process
.returncode
)
39 class Cluster(multisite
.Cluster
):
40 """ cluster implementation based on mstart/mrun scripts """
41 def __init__(self
, cluster_id
):
42 super(Cluster
, self
).__init
__()
43 self
.cluster_id
= cluster_id
44 self
.needs_reset
= True
46 def admin(self
, args
= None, **kwargs
):
47 """ radosgw-admin command """
48 cmd
= [test_path
+ 'test-rgw-call.sh', 'call_rgw_admin', self
.cluster_id
]
51 if kwargs
.pop('read_only', False):
52 cmd
+= ['--rgw-cache-enabled', 'false']
53 return bash(cmd
, **kwargs
)
56 cmd
= [mstart_path
+ 'mstart.sh', self
.cluster_id
]
58 cmd
+= ['-n', '--mds_num', '0']
60 self
.needs_reset
= False
63 cmd
= [mstart_path
+ 'mstop.sh', self
.cluster_id
]
66 class Gateway(multisite
.Gateway
):
67 """ gateway implementation based on mrgw/mstop scripts """
68 def __init__(self
, client_id
= None, *args
, **kwargs
):
69 super(Gateway
, self
).__init
__(*args
, **kwargs
)
72 def start(self
, args
= None):
73 """ start the gateway """
75 cmd
= [mstart_path
+ 'mrgw.sh', self
.cluster
.cluster_id
, str(self
.port
)]
77 cmd
+= ['-i', self
.id]
78 cmd
+= ['--debug-rgw=20', '--debug-ms=1']
84 """ stop the gateway """
86 cmd
= [mstart_path
+ 'mstop.sh', self
.cluster
.cluster_id
, 'radosgw', self
.id]
90 return ''.join(random
.choice(string
.ascii_uppercase
+ string
.digits
) for _
in range(16))
93 return ''.join(random
.choice(string
.ascii_uppercase
+ string
.ascii_lowercase
+ string
.digits
) for _
in range(32))
95 def gen_credentials():
96 return multisite
.Credentials(gen_access_key(), gen_secret())
98 def cluster_name(cluster_num
):
99 return 'c' + str(cluster_num
)
101 def zonegroup_name(zonegroup_num
):
102 return string
.ascii_lowercase
[zonegroup_num
]
104 def zone_name(zonegroup_num
, zone_num
):
105 return zonegroup_name(zonegroup_num
) + str(zone_num
+ 1)
107 def gateway_port(zonegroup_num
, gateway_num
):
108 return 8000 + 100 * zonegroup_num
+ gateway_num
110 def gateway_name(zonegroup_num
, zone_num
, gateway_num
):
111 return zone_name(zonegroup_num
, zone_num
) + '-' + str(gateway_num
+ 1)
113 def zone_endpoints(zonegroup_num
, zone_num
, gateways_per_zone
):
115 base
= gateway_port(zonegroup_num
, zone_num
* gateways_per_zone
)
116 for i
in range(0, gateways_per_zone
):
117 endpoints
.append('http://localhost:' + str(base
+ i
))
120 def get_log_level(log_level
):
129 return logging
.CRITICAL
131 def setup_logging(log_level_console
, log_file
, log_level_file
):
133 formatter
= logging
.Formatter('%(asctime)s %(levelname)s %(message)s')
134 fh
= logging
.FileHandler(log_file
)
135 fh
.setFormatter(formatter
)
136 fh
.setLevel(get_log_level(log_level_file
))
139 formatter
= logging
.Formatter('%(levelname)s %(message)s')
140 ch
= logging
.StreamHandler()
141 ch
.setFormatter(formatter
)
142 ch
.setLevel(get_log_level(log_level_console
))
145 def init(parse_args
):
146 cfg
= configparser
.RawConfigParser({
149 'gateways_per_zone': 2,
150 'no_bootstrap': 'false',
153 'file_log_level': 20,
157 path
= os
.environ
['RGW_MULTI_TEST_CONF']
159 path
= tpath('test_multi.conf')
162 with
open(path
) as f
:
165 print('WARNING: error reading test config. Path can be set through the RGW_MULTI_TEST_CONF env variable')
168 parser
= argparse
.ArgumentParser(
169 description
='Run rgw multi-site tests',
170 usage
='test_multi [--num-zonegroups <num>] [--num-zones <num>] [--no-bootstrap]')
173 parser
.add_argument('--num-zonegroups', type=int, default
=cfg
.getint(section
, 'num_zonegroups'))
174 parser
.add_argument('--num-zones', type=int, default
=cfg
.getint(section
, 'num_zones'))
175 parser
.add_argument('--gateways-per-zone', type=int, default
=cfg
.getint(section
, 'gateways_per_zone'))
176 parser
.add_argument('--no-bootstrap', action
='store_true', default
=cfg
.getboolean(section
, 'no_bootstrap'))
177 parser
.add_argument('--log-level', type=int, default
=cfg
.getint(section
, 'log_level'))
178 parser
.add_argument('--log-file', type=str, default
=cfg
.get(section
, 'log_file'))
179 parser
.add_argument('--file-log-level', type=int, default
=cfg
.getint(section
, 'file_log_level'))
180 parser
.add_argument('--tenant', type=str, default
=cfg
.get(section
, 'tenant'))
187 args
= parser
.parse_args(argv
)
188 bootstrap
= not args
.no_bootstrap
190 setup_logging(args
.log_level
, args
.log_file
, args
.file_log_level
)
192 # start first cluster
193 c1
= Cluster(cluster_name(1))
199 admin_creds
= gen_credentials()
200 admin_user
= multisite
.User('zone.user')
202 user_creds
= gen_credentials()
203 user
= multisite
.User('tester')
205 realm
= multisite
.Realm('r')
207 # create the realm on c1
211 period
= multisite
.Period(realm
=realm
)
212 realm
.current_period
= period
214 for zg
in range(0, args
.num_zonegroups
):
215 zonegroup
= multisite
.ZoneGroup(zonegroup_name(zg
), period
)
216 period
.zonegroups
.append(zonegroup
)
218 is_master_zg
= zg
== 0
220 period
.master_zonegroup
= zonegroup
222 for z
in range(0, args
.num_zones
):
224 # start a cluster, or use c1 for first zone
226 if is_master_zg
and is_master
:
229 cluster
= Cluster(cluster_name(len(clusters
) + 1))
230 clusters
.append(cluster
)
233 # pull realm configuration from the master's gateway
234 gateway
= realm
.meta_master_zone().gateways
[0]
235 realm
.pull(cluster
, gateway
, admin_creds
)
237 endpoints
= zone_endpoints(zg
, z
, args
.gateways_per_zone
)
240 # create the zonegroup on its first zone's cluster
244 if len(endpoints
): # use master zone's endpoints
245 arg
+= ['--endpoints', ','.join(endpoints
)]
246 zonegroup
.create(cluster
, arg
)
248 zonegroup
.get(cluster
)
250 # create the zone in its zonegroup
251 zone
= multisite
.Zone(zone_name(zg
, z
), zonegroup
, cluster
)
253 arg
= admin_creds
.credential_args()
257 arg
+= ['--endpoints', ','.join(endpoints
)]
258 zone
.create(cluster
, arg
)
261 zonegroup
.zones
.append(zone
)
263 zonegroup
.master_zone
= zone
265 # update/commit the period
267 period
.update(zone
, commit
=True)
270 for g
in range(0, args
.gateways_per_zone
):
271 port
= gateway_port(zg
, g
+ z
* args
.gateways_per_zone
)
272 client_id
= gateway_name(zg
, z
, g
)
273 gateway
= Gateway(client_id
, 'localhost', port
, cluster
, zone
)
276 zone
.gateways
.append(gateway
)
278 if is_master_zg
and is_master
:
281 arg
= ['--display-name', '"Zone User"', '--system']
282 arg
+= admin_creds
.credential_args()
283 admin_user
.create(zone
, arg
)
285 arg
= ['--display-name', '"Test User"']
286 arg
+= user_creds
.credential_args()
288 cmd
+= ['--tenant', args
.tenant
]
289 user
.create(zone
, arg
)
291 # read users and update keys
292 admin_user
.info(zone
)
293 admin_creds
= admin_user
.credentials
[0]
295 user_creds
= user
.credentials
[0]
300 init_multi(realm
, user
)
305 if __name__
== "__main__":