def test_getatt_from_yaml(self): """ Test that we correctly convert the short form of GetAtt into the correct JSON format from YAML """ source = """ - !GetAtt foo.bar - Fn::GetAtt: [foo, bar] """ expected = [ { "Fn::GetAtt": ["foo", "bar"] }, { "Fn::GetAtt": ["foo", "bar"] }, ] # No clean actual = cfn_flip.to_json(source, clean_up=False) self.assertEqual(expected, json.loads(actual)) # With clean actual = cfn_flip.to_json(source, clean_up=True) self.assertEqual(expected, json.loads(actual))
def test_flip_to_json_with_condition(self): """ Test that the Condition key is correctly converted """ source = """ MyAndCondition: !And - !Equals ["sg-mysggroup", !Ref "ASecurityGroup"] - !Condition SomeOtherCondition """ expected = { "MyAndCondition": { "Fn::And": [{ "Fn::Equals": ["sg-mysggroup", { "Ref": "ASecurityGroup" }] }, { "Condition": "SomeOtherCondition" }] } } actual = cfn_flip.to_json(source, clean_up=True) self.assertEqual(expected, json.loads(actual))
def test_to_json_with_yaml(self): """ Test that to_json performs correctly """ actual = cfn_flip.to_json(self.input_yaml) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_to_json_with_json(self): """ Test that to_json still works when passed json (All json is valid yaml) """ actual = cfn_flip.to_json(self.input_json) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_flip_to_json_with_multi_level_getatt(self): """ Test that we correctly convert multi-level Fn::GetAtt from YAML to JSON format """ data = "!GetAtt 'First.Second.Third'\n" expected = {"Fn::GetAtt": ["First", "Second", "Third"]} actual = cfn_flip.to_json(data, clean_up=True) self.assertEqual(expected, json.loads(actual))
def test_flip_to_json_with_datetimes(self): """ Test that the json encoder correctly handles dates and datetimes """ from datetime import date, datetime, time tricky_data = """ a date: 2017-03-02 a datetime: 2017-03-02 19:52:00 """ actual = cfn_flip.to_json(tricky_data) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, { "a date": "2017-03-02", "a datetime": "2017-03-02T19:52:00", })
def strip_shorthand_from_yaml(yaml): return to_json(StringIO(yaml))