示例#1
0
def it_contain(step, condition, something):
    if hasattr(step.context.stash,
               'resource_list') and not step.context.stash.resource_list:
        return

    if step.context.stash.__class__ is TerraformPropertyList:
        for property in step.context.stash.properties:
            assert property.property_value == something, \
                '{} property in {} can not be found in {} ({}). It is set to {} instead'.format(something,
                                                                                                property.property_name,
                                                                                                property.resource_name,
                                                                                                property.resource_type,
                                                                                                property.property_value)

    elif step.context.stash.__class__ is TerraformResourceList:
        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)
示例#2
0
def define_a_resource(step, resource):
    if (resource in resource_name.keys()):
        resource = resource_name[resource]

    step.context.resource_type = resource
    step.context.defined_resource = resource
    step.context.stash = world.config.terraform.resources(resource)
示例#3
0
def i_have_name_section_configured(step, name, type, radish_world=None):
    if radish_world is None:
        radish_world = world

    step.context.type = type
    step.context.name = name

    if type == "resource":
        if (name in resource_name.keys()):
            name = resource_name[name]

        found_resource = radish_world.config.terraform.resources(name)

        if hasattr(found_resource,
                   'resource_list') and found_resource.resource_list:
            step.context.resource_type = name
            step.context.defined_resource = name
            step.context.stash = radish_world.config.terraform.resources(name)
        else:
            skip_step(step, name)
    else:
        if type in radish_world.config.terraform.terraform_config:
            if name in radish_world.config.terraform.terraform_config[type]:
                step.context.stash = radish_world.config.terraform.terraform_config[
                    type][name]
            else:
                step.context.stash = radish_world.config.terraform.terraform_config[
                    type]

        else:
            skip_step(step, type)
示例#4
0
def i_have_resource_defined(step, resource, radish_world=None):
    if radish_world is None:
        radish_world = world

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

    step.context.resource_type = resource
    step.context.defined_resource = resource
    step.context.stash = radish_world.config.terraform.resources(resource)
示例#5
0
文件: helper.py 项目: solaegis/cli
def convert_resource_type(resource_type):
    '''
    Searchs given resource_type within resource_name array and returns the value if it is found

    :param resource_type: String of resource_type
    :return: converted or original resource_type
    '''
    if resource_type in resource_name.keys():
        resource_type = resource_name[resource_type]

    return resource_type
示例#6
0
def i_have_resource_defined(step, resource, radish_world=None):
    if radish_world is None:
        radish_world = world

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

    found_resource = radish_world.config.terraform.resources(resource)

    if found_resource.resource_list:
        step.context.resource_type = resource
        step.context.defined_resource = resource
        step.context.stash = radish_world.config.terraform.resources(resource)
    else:
        skip_step(step, '{} resource'.format(resource))
示例#7
0
def i_have_name_section_configured(step, name, type, radish_world=None):
    if radish_world is None:
        radish_world = world

    step.context.type = type
    step.context.name = name

    if type == "resource":
        if (name in resource_name.keys()):
            name = resource_name[name]

        step.context.resource_type = name
        step.context.defined_resource = name
        step.context.stash = radish_world.config.terraform.resources(name)
    else:
        if name in radish_world.config.terraform.terraform_config[type]:
            step.context.stash = radish_world.config.terraform.terraform_config[
                type][name]
        else:
            step.context.stash = radish_world.config.terraform.terraform_config[
                type]
示例#8
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)
示例#9
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)