| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sure  # noqa | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 13:34:47 -04:00
										 |  |  | from botocore.awsrequest import AWSPreparedRequest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from moto.core.responses import AWSServiceSpec, BaseResponse | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  | from moto.core.responses import flatten_json_request_body | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_flatten_json_request_body(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     spec = AWSServiceSpec("data/emr/2009-03-31/service-2.json").input_spec("RunJobFlow") | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     body = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Name": "cluster", | 
					
						
							|  |  |  |         "Instances": { | 
					
						
							|  |  |  |             "Ec2KeyName": "ec2key", | 
					
						
							|  |  |  |             "InstanceGroups": [ | 
					
						
							|  |  |  |                 {"InstanceRole": "MASTER", "InstanceType": "m1.small"}, | 
					
						
							|  |  |  |                 {"InstanceRole": "CORE", "InstanceType": "m1.medium"}, | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "Placement": {"AvailabilityZone": "us-east-1"}, | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Steps": [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "HadoopJarStep": { | 
					
						
							|  |  |  |                     "Properties": [ | 
					
						
							|  |  |  |                         {"Key": "k1", "Value": "v1"}, | 
					
						
							|  |  |  |                         {"Key": "k2", "Value": "v2"}, | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     "Args": ["arg1", "arg2"], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         "Configurations": [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "Classification": "class", | 
					
						
							|  |  |  |                 "Properties": {"propkey1": "propkey1", "propkey2": "propkey2"}, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             {"Classification": "anotherclass", "Properties": {"propkey3": "propkey3"}}, | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |         ], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     flat = flatten_json_request_body("", body, spec) | 
					
						
							|  |  |  |     flat["Name"].should.equal(body["Name"]) | 
					
						
							|  |  |  |     flat["Instances.Ec2KeyName"].should.equal(body["Instances"]["Ec2KeyName"]) | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |     for idx in range(2): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         flat[ | 
					
						
							|  |  |  |             "Instances.InstanceGroups.member." + str(idx + 1) + ".InstanceRole" | 
					
						
							|  |  |  |         ].should.equal(body["Instances"]["InstanceGroups"][idx]["InstanceRole"]) | 
					
						
							|  |  |  |         flat[ | 
					
						
							|  |  |  |             "Instances.InstanceGroups.member." + str(idx + 1) + ".InstanceType" | 
					
						
							|  |  |  |         ].should.equal(body["Instances"]["InstanceGroups"][idx]["InstanceType"]) | 
					
						
							|  |  |  |     flat["Instances.Placement.AvailabilityZone"].should.equal( | 
					
						
							|  |  |  |         body["Instances"]["Placement"]["AvailabilityZone"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for idx in range(1): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         prefix = "Steps.member." + str(idx + 1) + ".HadoopJarStep" | 
					
						
							|  |  |  |         step = body["Steps"][idx]["HadoopJarStep"] | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |         i = 0 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         while prefix + ".Properties.member." + str(i + 1) + ".Key" in flat: | 
					
						
							|  |  |  |             flat[prefix + ".Properties.member." + str(i + 1) + ".Key"].should.equal( | 
					
						
							|  |  |  |                 step["Properties"][i]["Key"] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             flat[prefix + ".Properties.member." + str(i + 1) + ".Value"].should.equal( | 
					
						
							|  |  |  |                 step["Properties"][i]["Value"] | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |             i += 1 | 
					
						
							|  |  |  |         i = 0 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         while prefix + ".Args.member." + str(i + 1) in flat: | 
					
						
							|  |  |  |             flat[prefix + ".Args.member." + str(i + 1)].should.equal(step["Args"][i]) | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |             i += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for idx in range(2): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         flat["Configurations.member." + str(idx + 1) + ".Classification"].should.equal( | 
					
						
							|  |  |  |             body["Configurations"][idx]["Classification"] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         props = {} | 
					
						
							|  |  |  |         i = 1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         keyfmt = "Configurations.member.{0}.Properties.entry.{1}" | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |         key = keyfmt.format(idx + 1, i) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         while key + ".key" in flat: | 
					
						
							|  |  |  |             props[flat[key + ".key"]] = flat[key + ".value"] | 
					
						
							| 
									
										
										
										
											2016-09-21 20:59:19 -07:00
										 |  |  |             i += 1 | 
					
						
							|  |  |  |             key = keyfmt.format(idx + 1, i) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         props.should.equal(body["Configurations"][idx]["Properties"]) | 
					
						
							| 
									
										
										
										
											2019-05-25 13:34:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_parse_qs_unicode_decode_error(): | 
					
						
							|  |  |  |     body = b'{"key": "%D0"}, "C": "#0 = :0"}' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     request = AWSPreparedRequest("GET", "http://request", {"foo": "bar"}, body, False) | 
					
						
							| 
									
										
										
										
											2019-05-25 13:34:47 -04:00
										 |  |  |     BaseResponse().setup_class(request, request.url, request.headers) |