2011-08-04 23:59:32 +00:00
|
|
|
from boto.s3.connection import S3Connection
|
2011-08-04 00:00:02 +00:00
|
|
|
from optparse import OptionParser
|
2011-08-05 18:42:33 +00:00
|
|
|
from boto import UserAgent
|
2011-08-04 00:00:02 +00:00
|
|
|
from . import common
|
|
|
|
|
|
|
|
import traceback
|
2011-08-10 18:27:06 +00:00
|
|
|
import itertools
|
2011-08-04 00:00:02 +00:00
|
|
|
import random
|
|
|
|
import string
|
2011-08-10 21:39:25 +00:00
|
|
|
import struct
|
2011-08-08 23:51:10 +00:00
|
|
|
import yaml
|
2011-08-04 00:00:02 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
def assemble_decision(decision_graph, prng):
|
|
|
|
""" Take in a graph describing the possible decision space and a random
|
|
|
|
number generator and traverse the graph to build a decision
|
2011-08-04 23:59:32 +00:00
|
|
|
"""
|
2011-08-09 18:56:38 +00:00
|
|
|
return descend_graph(decision_graph, 'start', prng)
|
|
|
|
|
|
|
|
|
|
|
|
def descend_graph(decision_graph, node_name, prng):
|
|
|
|
""" Given a graph and a particular node in that graph, set the values in
|
|
|
|
the node's "set" list, pick a choice from the "choice" list, and
|
|
|
|
recurse. Finally, return dictionary of values
|
|
|
|
"""
|
2011-08-09 22:44:25 +00:00
|
|
|
node = decision_graph[node_name]
|
|
|
|
|
2011-08-09 18:56:38 +00:00
|
|
|
try:
|
2011-08-10 20:26:00 +00:00
|
|
|
choice = make_choice(node['choices'], prng)
|
2011-08-09 18:56:38 +00:00
|
|
|
decision = descend_graph(decision_graph, choice, prng)
|
|
|
|
except IndexError:
|
|
|
|
decision = {}
|
|
|
|
|
2011-08-09 22:44:25 +00:00
|
|
|
#TODO: Add in headers
|
2011-08-09 18:56:38 +00:00
|
|
|
for key in node['set']:
|
|
|
|
if decision.has_key(key):
|
|
|
|
raise KeyError("Node %s tried to set '%s', but that key was already set by a lower node!" %(node_name, key))
|
2011-08-10 22:10:24 +00:00
|
|
|
decision[key] = make_choice(node['set'][key], prng)
|
2011-08-09 18:56:38 +00:00
|
|
|
return decision
|
2011-08-04 23:59:32 +00:00
|
|
|
|
|
|
|
|
2011-08-10 20:26:00 +00:00
|
|
|
def make_choice(choices, prng):
|
2011-08-10 22:10:24 +00:00
|
|
|
if isinstance(choices, str):
|
|
|
|
return choices
|
2011-08-10 20:26:00 +00:00
|
|
|
weighted_choices = []
|
|
|
|
for option in choices:
|
|
|
|
fields = option.split(None, 1)
|
|
|
|
if len(fields) == 1:
|
|
|
|
weight = 1
|
|
|
|
value = fields[0]
|
|
|
|
else:
|
|
|
|
weight = int(fields[0])
|
|
|
|
value = fields[1]
|
|
|
|
for _ in xrange(weight):
|
|
|
|
weighted_choices.append(value)
|
|
|
|
|
|
|
|
return prng.choice(weighted_choices)
|
|
|
|
|
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
def expand_decision(decision, prng):
|
|
|
|
""" Take in a decision and a random number generator. Expand variables in
|
|
|
|
decision's values and headers until all values are fully expanded and
|
|
|
|
build a request out of the information
|
|
|
|
"""
|
2011-08-10 18:27:06 +00:00
|
|
|
special_decision = SpecialVariables(decision, prng)
|
|
|
|
for key in special_decision:
|
|
|
|
decision[key] = expand_key(special_decision, key)
|
|
|
|
|
|
|
|
return decision
|
|
|
|
|
|
|
|
|
|
|
|
def expand_key(decision, key):
|
|
|
|
c = itertools.count()
|
|
|
|
fmt = string.Formatter()
|
|
|
|
old = decision[key]
|
|
|
|
while True:
|
|
|
|
new = fmt.vformat(old, [], decision)
|
|
|
|
if new == old:
|
|
|
|
return old
|
|
|
|
if next(c) > 5:
|
|
|
|
raise RuntimeError
|
|
|
|
old = new
|
2011-08-05 18:42:33 +00:00
|
|
|
|
|
|
|
|
2011-08-09 22:44:25 +00:00
|
|
|
class SpecialVariables(dict):
|
|
|
|
charsets = {
|
2011-08-10 21:39:25 +00:00
|
|
|
'binary': 'binary',
|
2011-08-09 22:44:25 +00:00
|
|
|
'printable': string.printable,
|
|
|
|
'punctuation': string.punctuation,
|
|
|
|
'whitespace': string.whitespace
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, orig_dict, prng):
|
|
|
|
self.update(orig_dict)
|
|
|
|
self.prng = prng
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
fields = key.split(None, 1)
|
|
|
|
fn = getattr(self, 'special_{name}'.format(name=fields[0]), None)
|
|
|
|
if fn is None:
|
|
|
|
return super(SpecialVariables, self).__getitem__(key)
|
|
|
|
|
|
|
|
if len(fields) == 1:
|
|
|
|
fields.apppend('')
|
|
|
|
return fn(fields[1])
|
|
|
|
|
|
|
|
|
|
|
|
def special_random(self, args):
|
|
|
|
arg_list = args.split()
|
|
|
|
try:
|
|
|
|
size_min, size_max = [int(x) for x in arg_list[0].split('-')]
|
|
|
|
except IndexError:
|
|
|
|
size_min = 0
|
|
|
|
size_max = 1000
|
|
|
|
try:
|
|
|
|
charset = self.charsets[arg_list[1]]
|
|
|
|
except IndexError:
|
|
|
|
charset = self.charsets['printable']
|
|
|
|
|
|
|
|
length = self.prng.randint(size_min, size_max)
|
2011-08-10 21:39:25 +00:00
|
|
|
if charset is 'binary':
|
|
|
|
num_bytes = length + 8
|
|
|
|
tmplist = [self.prng.getrandbits(64) for _ in xrange(num_bytes / 8)]
|
|
|
|
tmpstring = struct.pack((num_bytes / 8) * 'Q', *tmplist)
|
|
|
|
return tmpstring[0:length]
|
|
|
|
else:
|
|
|
|
return ''.join([self.prng.choice(charset) for _ in xrange(length)]) # Won't scale nicely; won't do binary
|
2011-08-09 22:44:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 00:00:02 +00:00
|
|
|
def parse_options():
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option('-O', '--outfile', help='write output to FILE. Defaults to STDOUT', metavar='FILE')
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.add_option('--seed', dest='seed', type='int', help='initial seed for the random number generator', metavar='SEED')
|
2011-08-04 00:00:02 +00:00
|
|
|
parser.add_option('--seed-file', dest='seedfile', help='read seeds for specific requests from FILE', metavar='FILE')
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.add_option('-n', dest='num_requests', type='int', help='issue NUM requests before stopping', metavar='NUM')
|
2011-08-08 23:51:10 +00:00
|
|
|
parser.add_option('--decision-graph', dest='graph_filename', help='file in which to find the request decision graph', metavar='NUM')
|
2011-08-04 00:00:02 +00:00
|
|
|
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.set_defaults(num_requests=5)
|
2011-08-08 23:51:10 +00:00
|
|
|
parser.set_defaults(graph_filename='request_decision_graph.yml')
|
2011-08-04 00:00:02 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def randomlist(n, seed=None):
|
|
|
|
""" Returns a generator function that spits out a list of random numbers n elements long.
|
|
|
|
"""
|
|
|
|
rng = random.Random()
|
|
|
|
rng.seed(seed if seed else None)
|
|
|
|
for _ in xrange(n):
|
|
|
|
yield rng.random()
|
|
|
|
|
|
|
|
|
|
|
|
def _main():
|
|
|
|
""" The main script
|
|
|
|
"""
|
|
|
|
(options, args) = parse_options()
|
|
|
|
random.seed(options.seed if options.seed else None)
|
2011-08-04 23:59:32 +00:00
|
|
|
s3_connection = common.s3.main
|
2011-08-04 00:00:02 +00:00
|
|
|
|
2011-08-04 23:59:32 +00:00
|
|
|
request_seeds = None
|
2011-08-04 00:00:02 +00:00
|
|
|
if options.seedfile:
|
|
|
|
FH = open(options.seedfile, 'r')
|
|
|
|
request_seeds = FH.readlines()
|
|
|
|
else:
|
|
|
|
request_seeds = randomlist(options.num_requests, options.seed)
|
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
graph_file = open(options.graph_filename, 'r')
|
|
|
|
decision_graph = yaml.safe_load(graph_file)
|
|
|
|
|
|
|
|
constants = {
|
|
|
|
'bucket_readable': 'TODO',
|
|
|
|
'bucket_writable' : 'TODO',
|
|
|
|
'bucket_nonexistant' : 'TODO',
|
|
|
|
'object_readable' : 'TODO',
|
|
|
|
'object_writable' : 'TODO',
|
|
|
|
'object_nonexistant' : 'TODO'
|
|
|
|
}
|
|
|
|
|
2011-08-04 23:59:32 +00:00
|
|
|
for request_seed in request_seeds:
|
2011-08-08 23:51:10 +00:00
|
|
|
prng = random.Random(request_seed)
|
|
|
|
decision = assemble_decision(decision_graph, prng)
|
|
|
|
decision.update(constants)
|
|
|
|
request = expand_decision(decision, prng)
|
|
|
|
|
|
|
|
response = s3_connection.make_request(request['method'], request['path'], data=request['body'], headers=request['headers'], override_num_retries=0)
|
|
|
|
|
|
|
|
if response.status == 500 or response.status == 503:
|
|
|
|
print 'Request generated with seed %d failed:\n%s' % (request_seed, request)
|
|
|
|
pass
|
2011-08-04 00:00:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
common.setup()
|
|
|
|
try:
|
|
|
|
_main()
|
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
common.teardown()
|
|
|
|
|