<s> import os <EOL> import json <EOL> import mock <EOL> import unittest <EOL> from StringIO import StringIO <EOL> from test . unit import with_tempdir <EOL> from swift . cli . ring_builder_analyzer import parse_scenario , run_scenario <EOL> class TestRunScenario ( unittest . TestCase ) : <EOL> @ with_tempdir <EOL> def test_it_runs ( self , tempdir ) : <EOL> builder_path = os . path . join ( tempdir , '<STR_LIT>' ) <EOL> scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:200> ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:200> ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:200> ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT> ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:1> ] ] , <EOL> [ [ '<STR_LIT>' , builder_path ] ] ] } <EOL> parsed = parse_scenario ( json . dumps ( scenario ) ) <EOL> fake_stdout = StringIO ( ) <EOL> with mock . patch ( '<STR_LIT>' , fake_stdout ) : <EOL> run_scenario ( parsed ) <EOL> self . assertTrue ( '<STR_LIT>' in fake_stdout . getvalue ( ) ) <EOL> self . assertTrue ( os . path . exists ( builder_path ) ) <EOL> class TestParseScenario ( unittest . TestCase ) : <EOL> def test_good ( self ) : <EOL> scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:200> ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT> ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:1> ] ] ] } <EOL> parsed = parse_scenario ( json . dumps ( scenario ) ) <EOL> self . assertEqual ( parsed [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( parsed [ '<STR_LIT>' ] , <NUM_LIT:8> ) <EOL> self . assertEqual ( parsed [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( parsed [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( parsed [ '<STR_LIT>' ] , [ <EOL> [ [ '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:2> } ] , <EOL> [ '<STR_LIT>' , { '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:2> } ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT> ] ] , <EOL> [ [ '<STR_LIT>' , <NUM_LIT:1> ] ] ] ) <EOL> def test_invalid_json ( self ) : <EOL> self . assertRaises ( ValueError , parse_scenario , "<STR_LIT:{>" ) <EOL> def test_json_not_object ( self ) : <EOL> self . assertRaises ( ValueError , parse_scenario , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , parse_scenario , "<STR_LIT>" ) <EOL> def test_bad_replicas ( self ) : <EOL> working_scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] ] ] } <EOL> busted = dict ( working_scenario ) <EOL> del busted [ '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , replicas = '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , replicas = - <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_part_power ( self ) : <EOL> working_scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] ] ] } <EOL> busted = dict ( working_scenario ) <EOL> del busted [ '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , part_power = '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , part_power = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , part_power = <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_random_seed ( self ) : <EOL> working_scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] ] ] } <EOL> busted = dict ( working_scenario ) <EOL> del busted [ '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , random_seed = '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_overload ( self ) : <EOL> working_scenario = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] ] ] } <EOL> busted = dict ( working_scenario ) <EOL> del busted [ '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , overload = '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( working_scenario , overload = - <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_rounds ( self ) : <EOL> base = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( base ) ) <EOL> busted = dict ( base , rounds = { } ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ { } ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_add ( self ) : <EOL> base = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:2> ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ] ] ] ) <EOL> expected = "<STR_LIT>" "<STR_LIT>" <EOL> try : <EOL> parse_scenario ( json . dumps ( busted ) ) <EOL> except ValueError as err : <EOL> self . assertEqual ( str ( err ) , expected ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' , - <NUM_LIT:1> ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_remove ( self ) : <EOL> base = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:2> ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_set_weight ( self ) : <EOL> base = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , <NUM_LIT:0> ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> ] ] ] ) <EOL> expected = "<STR_LIT>" "<STR_LIT>" <EOL> try : <EOL> parse_scenario ( json . dumps ( busted ) ) <EOL> except ValueError as e : <EOL> self . assertEqual ( str ( e ) , expected ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , <NUM_LIT:1> , - <NUM_LIT:1> ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) <EOL> def test_bad_save ( self ) : <EOL> base = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> busted = dict ( base , rounds = [ [ [ '<STR_LIT>' ] ] ] ) <EOL> self . assertRaises ( ValueError , parse_scenario , json . dumps ( busted ) ) </s>
