Exemplo n.º 1
0
def it_condition_contain_something(step,
                                   condition,
                                   something,
                                   propertylist=TerraformPropertyList,
                                   resourcelist=TerraformResourceList):

    if hasattr(step.context.stash,
               'resource_list') and not step.context.stash.resource_list:
        return

    if step.context.stash.__class__ is propertylist:
        for property in step.context.stash.properties:
            value = parse_hcl_value(property.property_value)

            assert (value == something or something.lower() in value), \
                '{} property in {} can not be found in {} ({}). It is set to {} instead'.format(something,
                                                                                                property.property_name,
                                                                                                property.resource_name,
                                                                                                property.resource_type,
                                                                                                value)

    elif step.context.stash.__class__ is resourcelist:
        if condition == 'must':
            step.context.stash.should_have_properties(something)

        if something in resource_name.keys():
            something = resource_name[something]

        step.context.stash = step.context.stash.property(something)

        if condition == 'must':
            assert step.context.stash.properties, \
                '{} doesnt have a property list.'.format(something)
    elif step.context.stash.__class__ is dict:
        if something in step.context.stash:
            step.context.stash = step.context.stash[something]
        else:
            if condition == 'must':
                assert False, '{} does not exist.'.format(something)
Exemplo n.º 2
0
def it_condition_contain_something(step_obj,
                                   something,
                                   propertylist=TerraformPropertyList,
                                   resourcelist=TerraformResourceList):

    if something in resource_name.keys():
        something = resource_name[something]

    step_can_skip = step_condition(step_obj) in ["given", "when"]

    if step_obj.context.stash.__class__ is propertylist:
        for prop in step_obj.context.stash.properties:
            value = parse_hcl_value(prop.property_value,
                                    world.config.terraform.terraform_config)

            if value is not None:
                assert (value == something or something.lower() in value), \
                    '{} property in {} can not be found in {} ({}). It is set to {} instead'.format(something,
                                                                                                    prop.property_name,
                                                                                                    prop.resource_name,
                                                                                                    prop.resource_type,
                                                                                                    value)
            else:
                write_stdout(
                    level='WARNING',
                    message='Can not get value of {} in {}/{}. '
                    'Might be set by an unknown source (module, etc. )\n'
                    'Value : {}'.format(something, prop.property_name,
                                        prop.resource_type,
                                        prop.property_value))
                step_obj.state = 'skipped'

    elif step_obj.context.stash.__class__ is resourcelist:
        if step_can_skip is False:
            step_obj.context.stash.should_have_properties(something)
            step_obj.context.stash = step_obj.context.stash.find_property(
                something)
            assert step_obj.context.stash.properties, \
                'No defined property/value found for {}.'.format(something)
            step_obj.context.stash = step_obj.context.stash.properties
        else:
            try:
                step_obj.context.stash.should_have_properties(something)
                number_of_resources = len(step_obj.context.stash.resource_list)
                step_obj.context.stash = step_obj.context.stash.find_property(
                    something)
                if step_obj.context.stash:
                    if number_of_resources > len(
                            step_obj.context.stash.properties):
                        write_stdout(
                            level='INFO',
                            message=
                            'Some of the resources does not have {} property defined within.\n'
                            'Removed {} resource (out of {}) from the test scope.\n'
                            '\n'.format(
                                something,
                                (number_of_resources -
                                 len(step_obj.context.stash.properties)),
                                number_of_resources,
                            ))
            except Exception as e:
                number_of_resources = len(step_obj.context.stash.resource_list)
                step_obj.context.stash = step_obj.context.stash.find_property(
                    something)
                if step_obj.context.stash:
                    write_stdout(
                        level='INFO',
                        message=
                        'Some of the resources does not have {} property defined within.\n'
                        'Removed {} resource (out of {}) from the test scope.\n\n'
                        'Due to : \n'
                        '{}'.format(something,
                                    (number_of_resources -
                                     len(step_obj.context.stash.properties)),
                                    number_of_resources, str(e)))
                else:
                    skip_step(
                        step_obj,
                        resource=something,
                        message=
                        'Can not find {resource} property in any resource.')

    elif step_obj.context.stash.__class__ is dict:
        if something in step_obj.context.stash:
            step_obj.context.stash = step_obj.context.stash[something]
        else:
            if step_can_skip:
                skip_step(
                    step_obj,
                    resource=something,
                    message=
                    'Can not find {resource} resource in terraform files.')
            else:
                assert False, '{} does not exist.'.format(something)
Exemplo n.º 3
0
 def test_parse_hcl_value_return_same(self):
     tf_conf = dict(variable=dict(key='value'))
     self.assertEqual(parse_hcl_value('some_string', tf_conf),
                      'some_string')
Exemplo n.º 4
0
 def test_parse_hcl_value_return_same(self):
     self.assertEqual(parse_hcl_value('some_string'), 'some_string')