Пример #1
0
def happy_path_retrieve_service_admin_token_with_scope(context):
    """
    Retrieve a token with context.subservice scope using admin credentials available in context
    :param context: behave.runner.Context
    """
    happy_path_retrieve_service_admin_token(context)
    context.token_scope = ks_get_token_with_scope(context, context.token, context.service, context.subservice)
    print ("\n #>> Token to use: {} \n".format(context.token))
Пример #2
0
def happy_path_request_collector(context, INSTANCE, REQUEST, ACTION):
    """
    :type context behave.runner.Context
    :type INSTANCE str
    :type REQUEST str
    :type ACTION str
    """
    context.instance = INSTANCE

    # Setup default headers
    context.headers = {}
    context.headers = {"Accept": "application/json", "Content-type": "application/json"}

    # recover the data to send the request
    context.instance_ip = context.config['components'][INSTANCE]['instance']
    context.instance_port = context.config['components'][INSTANCE]['port']
    context.instance_protocol = context.config['components'][INSTANCE]['protocol']

    context.url_component = get_endpoint(context.instance_protocol,
                                         context.instance_ip,
                                         context.instance_port)

    if INSTANCE == "ORC" and REQUEST == "SERVICE_ENTITY" and ACTION == "CREATE":
        url = '{}/v1.0/service/{}/subservice/{}/register_service'.format(context.url_component,
                                                                         context.service_id,
                                                                         context.subservice_id)

        context.headers.update({"Fiware-Service": "{}".format(context.service)})
        context.headers.update({"Fiware-ServicePath": "/{}".format(context.servicepath)})

        payload_table = dict(context.table)

        # Depends on Orquestator version, in old version is needed:
        # payload_table['ATT_TIMEOUT'] = int(payload_table['ATT_TIMEOUT'])

        # Properties replacement Var environment
        tp_url = payload_table['ATT_ENDPOINT']
        if "TP" in tp_url:
            tp_endpoint = "{}://{}:{}".format(context.config['components']["TP"]['protocol'],
                                              context.config['components']["TP"]['instance'],
                                              context.config['components']["TP"]['port'])

            payload_table['ATT_ENDPOINT'] = tp_url.replace("TP", tp_endpoint)

        json_payload = json.dumps(payload_table)
        print (json_payload)
        __logger__.debug("Create service: {}, \n url: {}".format(json_payload, url))

        try:
            context.r = requests.post(url=url,
                                      headers=context.headers,
                                      data=json_payload)

            eq_(context.r.status_code, 201,
                "[ERROR] when calling {} responsed a HTTP {}".format(url, context.r.status_code))
            context.create_service_entity = context.r.content
            print (context.create_service_entity)
        except:
            eq_(True, False, "# Exception # Error Creating SERVICE_ENTITY")

    if INSTANCE == "ORC" and REQUEST == "SERVICE" and ACTION == "CREATE":
        url = str("{0}/v1.0/service".format(context.url_component))

        payload = dict(context.table) if context.table else context.table_create_service
        json_payload = json.dumps(payload)

        print ("\n JSON TABLE SERVICE: \n {}\n".format(json_payload))
        __logger__.debug("Create service: {}, \n url: {}".format(json_payload, url))
        context.r = requests.post(url=url,
                                  headers=context.headers,
                                  data=json_payload)

        __logger__.debug(context.r.content)
        __logger__.debug(context.r.status_code)
        eq_(context.r.status_code, 201,
            "[ERROR] when calling {} responsed a HTTP {}".format(url, context.r.status_code))
        context.create_service = context.r.content
        jsobject = json.loads(context.create_service)

        context.service_id = jsobject["id"]
        context.token_service = jsobject["token"]
        context.service_admin = payload["NEW_SERVICE_ADMIN_USER"]
        context.service_admin_pass = payload["NEW_SERVICE_ADMIN_PASSWORD"]

        print ("\n --->>  ID service: {} <<--- \n".format(context.service_id))
        print ("TOKEN service: {} \n".format(context.token_service))

    if INSTANCE == "ORC" and REQUEST == "SERVICE" and ACTION == "DELETE":
        # Get list of services if needed
        if "service_id" not in context:
            for service in context.services:
                if context.service == service["name"]:
                    print ("#>> Service Targeted: {} {}".format(service["name"], service["id"]))
                    context.service_id = service["id"]
                    break

        # Get config test env credentials
        context.user_admin = context.config["env_data"]["users"]["user_1"]["user_name"]
        context.password_admin = context.config["env_data"]["users"]["user_1"]["user_password"]
        context.domain_admin = context.config["env_data"]["users"]["user_1"]["user_service"]

        # In case delete using token is needed
        # domain_token = ks_get_token(context,
        #                             service=context.domain_admin,
        #                             user=context.user_admin,
        #                             password=context.password_admin)

        if "service_id" in context:
            delete_response = orc_delete_service(context, context.service_id)
            eq_(204, delete_response,
                "[ERROR] Deleting Service {} \n Orch responded: {}".format(context.service_id, delete_response))
        else:
            eq_(True, False, "[Error] Service to delete ({}) not found".format(context.service))

    if INSTANCE == "ORC" and REQUEST == "SUBSERVICE" and ACTION == "DELETE":
        # Get service id if needed
        if "service_id" not in context:
            for service in context.services:
                if context.service == service["name"]:
                    print ("service retrieved: {} {}".format(service["name"], service["id"]))
                    context.service_id = service["id"]
                    break

        # Get subservice id
        for subservice in context.subservices:
            if "/" + context.subservice == subservice["name"]:
                print ("#>> Subservice Targeted: {} {}".format(subservice["name"], subservice["id"]))
                context.subservice_id = subservice["id"]
                break
            else:
                print ("#>> Subservice Targeted: {} {}".format(subservice["name"], subservice["id"]))

        context.user_admin = context.config["env_data"]["users"]["user_3"]["user_name"]
        context.password_admin = context.config["env_data"]["users"]["user_3"]["user_password"]

        context.token_scope = ks_get_token_with_scope(context, context.token, context.service, context.subservice)

        delete_response = orc_delete_subservice(context,
                                                context.service_id,
                                                context.subservice_id,
                                                context.token_scope)

        eq_(204, delete_response,
            "[ERROR] Deleting Service {} responsed a HTTP {}".format(context.service_id, delete_response))

    if INSTANCE == "IOTA_MQTT" and REQUEST == "DEVICE" and ACTION == "CREATE":
        url = str("{0}/iot/devices".format(context.url_component))

        dictio = dict(context.table)
        print ("\n{}\n".format(dictio))

        # create dict to contain the payload
        payload = dict({})
        payload["devices"] = [dictio]

        context.headers.update({"Fiware-Service": "{}".format(context.service)})
        context.headers.update({"Fiware-ServicePath": "/{}".format(context.servicepath)})

        __logger__.debug("[MQTT] Create Device request: {}, \n url: {}".format(payload, url))
        context.mqtt_create_request = payload
        context.mqtt_create_url = url