Disclosure of Invention
The invention provides an API testing method and system for a service open platform, and aims to solve the problem of how to quickly, simply and conveniently test an API.
In order to solve the above problem, according to an aspect of the present invention, there is provided an API testing method for a service open platform, the method including:
isolating the production environment gateway from the sandbox environment gateway based on a Kubernetes naming space, determining the minimum resource requirement of the sandbox environment gateway, and realizing the rapid deployment of the sandbox environment gateway service based on a yaml file according to the minimum resource requirement;
the publisher synchronizes the API established in the production environment to the sandbox environment through the service open platform;
when a publisher tests an API, an anonymous user is created in a sandbox environment gateway, so that the publisher initiates a service calling request by modifying a parameter value by utilizing the anonymous role, the service calling request is forwarded to a back-end service through the sandbox environment gateway, and service calling response information is obtained, so that the publisher determines an API debugging result according to the response information;
when a caller carries out API test, a temporary user is established in a sandbox environment gateway, API access authority is given to the temporary user, so that calling can initiate a service calling request by modifying a parameter value by using the role of the temporary user, the service calling request is forwarded to a back-end service through the sandbox environment gateway, service calling response information is obtained, and the caller determines an API test result according to the response information.
Preferably, wherein said determining the minimum resource requirement of the sandbox environment gateway comprises:
and determining the number of CPUs, memories and instances of the sandbox environment gateway based on the resource use condition of the production environment, the historical calling statistical data and the maximum concurrency limit of the sandbox environment service calling.
Preferably, the method synchronizes the API that has been created in the production environment to the sandbox environment by:
acquiring an API name, a rear-end address, a front-end access address and an authentication mode from a database, creating target, upstream, service and route required by the API in a sandbox environment gateway based on corresponding API information, and binding a plug-in corresponding to the API authentication mode and an access flow limiting plug-in;
and when the API has a test environment back-end access address, setting the back-end address as the test environment back-end address when synchronizing the API with the sandbox environment.
Preferably, wherein the response information includes: response code information, response delay information, response Header information, and response body information.
Preferably, wherein the method further comprises:
creating a corresponding flow restriction plug-in for the anonymous user;
and creating a corresponding authentication plug-in and a flow limiting plug-in for the temporary user.
Preferably, wherein the method further comprises:
and setting an access address, a parameter value and authentication information of the sandbox environment API corresponding to the service request address, automatically generating a sandbox environment service access sdk in a preset language, and simultaneously changing the service request address into the access address of the production environment API and the authentication information into the authentication information acquired after platform authorization by a caller, so that the production environment API is called by using sdk.
Preferably, wherein the method further comprises:
the sandbox environment gateway imports the access log of the API test function into an elastic search through logstack to monitor the access log in real time, and gives an alarm when the peak access amount or the access flow exceeds the capacity of the sandbox environment gateway, so that an administrator can perform elastic expansion example on the sandbox environment gateway in time.
According to another aspect of the present invention, there is provided an API testing system for a service open platform, the system including:
the sandbox environment gateway building unit is used for isolating the production environment gateway from the sandbox environment gateway based on a Kubernetes naming space, determining the minimum resource requirement of the sandbox environment gateway, and realizing the rapid deployment of the sandbox environment gateway service based on a yaml file according to the minimum resource requirement;
the API synchronization unit is used for enabling the publisher to synchronize the API which is created in the production environment to the sandbox environment through the service open platform;
the API debugging unit is used for creating an anonymous user in the sandbox environment gateway when a publisher tests the API, so that the publisher initiates a service calling request by modifying the parameter value by utilizing the anonymous role, forwards the service calling request to a back-end service through the sandbox environment gateway, and obtains service calling response information so that the publisher determines an API debugging result according to the response information;
and the API testing unit is used for creating a temporary user in the sandbox environment gateway when the caller performs API testing, endowing API access authority to the temporary user, enabling the call to use the temporary user role to initiate a service call request by modifying the parameter value, forwarding the service call request to a back-end service through the sandbox environment gateway, and acquiring service call response information so that the caller can determine an API testing result according to the response information.
Preferably, the determining the minimum resource requirement of the sandbox environment gateway by the sandbox environment gateway building unit includes:
and determining the number of CPUs, memories and instances of the sandbox environment gateway based on the resource use condition of the production environment, the historical calling statistical data and the maximum concurrency limit of the sandbox environment service calling.
Preferably, the API synchronization unit synchronizes an API that has been created in a production environment to a sandbox environment by:
acquiring an API name, a rear-end address, a front-end access address and an authentication mode from a database, creating target, upstream, service and route required by the API in a sandbox environment gateway based on corresponding API information, and binding a plug-in corresponding to the API authentication mode and an access flow limiting plug-in;
and when the API has a test environment back-end access address, setting the back-end address as the test environment back-end address when synchronizing the API with the sandbox environment.
Preferably, wherein the response information includes: response code information, response delay information, response Header information, and response body information.
Preferably, wherein the system further comprises:
the plug-in creating unit is used for creating a corresponding flow limiting plug-in for the anonymous user; and the system is used for creating a corresponding authentication plug-in and a flow limiting plug-in for the temporary user.
Preferably, wherein the system further comprises:
the sandbox environment service sdk automatic generation unit is used for setting an access address, a parameter value and authentication information of the sandbox environment API corresponding to the service request address and automatically generating a sandbox environment service access sdk in a preset language, and meanwhile, a caller changes the service request address into the access address of the production environment API and the authentication information into the authentication information acquired after platform authorization, so that the production environment API is called by sdk.
Preferably, wherein the system further comprises:
and the access log processing unit is used for leading the access log of the API test function into the ElasticSearch through logstack by the sandbox environment gateway so as to monitor the access log in real time, and giving an alarm when the peak access amount or the access flow exceeds the capacity of the sandbox environment gateway, so that an administrator can perform an elastic expansion example on the sandbox environment gateway in time.
The invention provides an API testing method and system for a service open platform, wherein a sandbox environment gateway is isolated through a Kubernetes naming space, API information is synchronized in the sandbox environment gateway, API access control and log management are independently carried out, and sandbox environment deployment is not required for actual back-end service; simultaneously, providing two API test schemes of anonymous user access without authentication and temporary user access with access authority aiming at users with different roles; the scheme of the invention can realize rapid, simple and visual debugging before service release and testing before service call, and ensure the safety and stability of the online service.
Detailed Description
The exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, however, the present invention may be embodied in many different forms and is not limited to the embodiments described herein, which are provided for complete and complete disclosure of the present invention and to fully convey the scope of the present invention to those skilled in the art. The terminology used in the exemplary embodiments illustrated in the accompanying drawings is not intended to be limiting of the invention. In the drawings, the same units/elements are denoted by the same reference numerals.
Unless otherwise defined, terms (including technical and scientific terms) used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs. Further, it will be understood that terms, such as those defined in commonly used dictionaries, should be interpreted as having a meaning that is consistent with their meaning in the context of the relevant art and will not be interpreted in an idealized or overly formal sense.
Fig. 1 is a flowchart of an API testing method 100 for a service open platform according to an embodiment of the present invention. As shown in fig. 1, in the API testing method for a service open platform according to the embodiment of the present invention, a sandbox environment gateway is isolated through a Kubernetes namespace, API information is synchronized at the sandbox environment gateway, access control and log management of an API are independently performed, and deployment of a sandbox environment is not required for actual back-end services; simultaneously, providing two API test schemes of anonymous user access without authentication and temporary user access with access authority aiming at users with different roles; the scheme of the invention can realize rapid, simple and visual debugging before service release and testing before service call, and ensure the safety and stability of the online service. The API testing method 100 for the service open platform provided by the embodiment of the present invention, starting at step 101, isolates the production environment gateway from the sandbox environment gateway in step 101 based on the kubernets namespace, determines the minimum resource requirement of the sandbox environment gateway, and implements rapid deployment of the sandbox environment gateway service based on the yaml file according to the minimum resource requirement.
Preferably, wherein said determining the minimum resource requirement of the sandbox environment gateway comprises:
and determining the number of CPUs, memories and instances of the sandbox environment gateway based on the resource use condition of the production environment, the historical calling statistical data and the maximum concurrency limit of the sandbox environment service calling.
The method is a method for debugging the API opened by the service open platform before release and testing the API before use, which is oriented to users with two roles of API issuers and API callers, and utilizes limited resources to quickly build the sandbox environment gateway, so that the users can conveniently and visually know the API calling mode and the API performance through the sandbox environment gateway.
In the embodiment of the invention, a kubernets namespace is utilized to realize the resource isolation of a production environment (namely an online environment) and a test environment (namely a sandbox environment), the minimum resource requirement of a sandbox environment gateway is determined based on the resource use condition of the production environment, historical calling statistical data and the maximum concurrency limit of sandbox environment service calling, and the rapid deployment of the gateway service of the test environment is realized in a yaml file mode. Specifically, by utilizing Kubernets namespace isolation and adopting a production environment gateway yaml file, only the namespace and the resource configuration are modified so as to quickly pull up the sandbox environment gateway service. Then, physical resource configuration and the number of instances such as a Central Processing Unit (CPU) and a memory of the sandbox environment gateway are set by analyzing log statistical data called by the gateway service of the production environment, resource utilization conditions and maximum concurrency limit called by the gateway service of the sandbox environment. Meanwhile, the invention monitors the resource utilization rate of the sandbox environment gateway in real time and performs elastic expansion and contraction on the deployment of the sandbox environment gateway. The production environment gateway and the sandbox environment gateway independently perform API management, access authority management and access log management.
At step 102, the publisher synchronizes the APIs that have been created in the production environment to the sandbox environment through the service opening platform.
Preferably, the method synchronizes the API that has been created in the production environment to the sandbox environment by:
acquiring an API name, a rear-end address, a front-end access address and an authentication mode from a database, creating target, upstream, service and route required by the API in a sandbox environment gateway based on corresponding API information, and binding a plug-in corresponding to the API authentication mode and an access flow limiting plug-in;
and when the API has a test environment back-end access address, setting the back-end address as the test environment back-end address when synchronizing the API with the sandbox environment.
In an embodiment of the present invention, when synchronizing an API to a sandbox environment, a publisher may synchronize an API that has been created in a production environment to the sandbox environment through a "synchronize to sandbox environment" button set by a service open platform interface. As shown in fig. 2, the system obtains information such as an API name, a back-end address, a front-end access address, an authentication method, and the like from a database, creates a target, an upstream, a service, and a route required by the API in a sandbox environment gateway based on corresponding API information, and binds a plug-in corresponding to the API authentication method and an access traffic restriction plug-in. In addition, if the API has a test environment back-end access address, the publisher may set the back-end address as the test environment back-end address when synchronizing the API with the sandbox environment.
In step 103, when the publisher performs API testing, an anonymous user is created in the sandbox environment gateway, so that the publisher initiates a service call request by modifying the parameter value using the anonymous role, forwards the service call request to the back-end service through the sandbox environment gateway, and obtains service call response information, so that the publisher determines an API debugging result according to the response information.
In step 104, when the caller performs API testing, a temporary user is created in the sandbox environment gateway, and an API access right is given to the temporary user, so that the call can initiate a service call request by modifying a parameter value using the role of the temporary user, the service call request is forwarded to a back-end service through the sandbox environment gateway, and service call response information is obtained, so that the caller determines an API testing result according to the response information.
Preferably, wherein the method further comprises:
creating a corresponding flow restriction plug-in for the anonymous user;
and creating a corresponding authentication plug-in and a flow limiting plug-in for the temporary user.
In the embodiment of the invention, different test schemes are respectively provided for users with two roles of API publisher and caller: 1) the publisher can rapidly detect the service configuration and performance through the anonymous user authentication-free access; 2) and a temporary access authority is created for the caller, the caller does not need to be authorized by a background, and only accesses the caller through the temporary user authority and the authentication information, so that the service calling test is conveniently and visually carried out.
Wherein the publisher conducts API debugging through anonymous users. Specifically, the system creates an anonymous user at the sandbox environment gateway, which can access existing APIs of the sandbox environment gateway without authentication. When the anonymous user is created, a flow limiting plug-in is created for the anonymous user, the access times of the anonymous user to the API are limited, and the abuse and direct production of the anonymous access are avoided. As shown in fig. 3, a publisher may initiate a service request by modifying a required parameter value only through an API debug function interface of the service open platform using an anonymous user role, forward the request to a back-end service through a sandbox environment gateway, and display service call response codes, response delay, response Header, and response body information on the interface. The publisher can confirm whether the API information set through the service open platform is accurate and the performance of the API call through the returned result.
Wherein the caller performs the API test by the temporary user. Specifically, the system creates a temporary user for a caller to be subjected to API testing at a sandbox environment gateway, gives corresponding API access authority to the user, and creates a corresponding authentication plug-in and a corresponding flow limiting plug-in. As shown in fig. 4, the API test function interface of the service open platform displays API call related information, which includes directly obtaining a service request address, a request parameter, authentication information, and the like from the database, where the service request address corresponds to an API access address of the sandbox environment gateway, and the authentication information is access authentication information of the temporary user to the API. And the caller modifies the parameter values, initiates a service calling request, forwards the service calling request to the back-end service through the sandbox environment gateway, and finally displays the returned result on the platform interface.
Preferably, wherein the method further comprises:
and setting an access address, a parameter value and authentication information of the sandbox environment API corresponding to the service request address, automatically generating a sandbox environment service access sdk in a preset language, and simultaneously changing the service request address into the access address of the production environment API and the authentication information into the authentication information acquired after platform authorization by a caller, so that the production environment API is called by using sdk.
Preferably, wherein the method further comprises:
the sandbox environment gateway imports the access log of the API test function into an elastic search through logstack to monitor the access log in real time, and gives an alarm when the peak access amount or the access flow exceeds the capacity of the sandbox environment gateway, so that an administrator can perform elastic expansion example on the sandbox environment gateway in time.
The present invention also enables automatic generation of test sdk. Wherein, an access address, a parameter value and authentication information of the sandbox environment API corresponding to the service request address are set in sdk, wherein the parameter value is a default value set when the API is created, and the authentication information is temporary user authentication information. The caller changes the service request address in the code into the access address of the production environment API, and changes the authentication information into the authentication information acquired after the platform is authorized, so that the production environment API can be called by using sdk.
In the invention, the access log of the API test function is independently managed by the sandbox environment gateway and is imported into the ElasticSearch through logstack. And monitoring the access log in real time, and when the peak access amount or the access flow exceeds the capacity of the sandbox environment gateway, carrying out mail and short message early warning so as to ensure that an administrator can carry out an elastic expansion example on the sandbox environment gateway in time.
The method realizes the on-line and test environment gateway isolation by utilizing a Kubernetes naming space, deploys test environment service by using the yaml file, determines the minimum physical and container resource requirements of the sandbox environment gateway according to the production environment gateway resource use condition and the maximum concurrent access limit of the test environment, and realizes the resource isolation and the rapid deployment of the service by utilizing the limited resources. Through two sets of independent service gateways of the online environment and the testing environment, access control and call log statistics are respectively carried out on service calls of the online environment and the testing environment, flow limitation is carried out on the service calls of the testing environment, and the use of the production environment cannot be influenced by the testing function. According to user requirements, different test schemes are provided for platform users with two roles of a service publisher and a service caller at the same time, the service publisher can access the service created by the service publisher without authentication by using an anonymous user of a service gateway, and interface debugging and performance testing before service publishing are completed conveniently and rapidly; when the service caller carries out API test, complicated service subscription and authorization processes of a service open platform are not needed, the gateway automatically allocates temporary authority, the platform automatically fills in temporary authentication information, and the user can visually and simply carry out service call test. The invention can provide a rapid, simple and visual debugging before service release and testing before service call for the service open platform user, and simultaneously ensures the safety and stability of the online service.
Fig. 5 is a schematic structural diagram of an API testing system 500 for a service open platform according to an embodiment of the present invention. As shown in fig. 5, an API testing system 500 for a service open platform according to an embodiment of the present invention includes: the sandbox environment gateway building unit 501, the API synchronizing unit 502, the API debugging unit 503 and the API testing unit 504.
Preferably, the sandbox environment gateway building unit 501 is configured to isolate the production environment gateway from the sandbox environment gateway based on a kubernets namespace, determine a minimum resource requirement of the sandbox environment gateway, and implement rapid deployment of the sandbox environment gateway service based on a yaml file according to the minimum resource requirement.
Preferably, the sandbox environment gateway building unit 501 determines the minimum resource requirement of the sandbox environment gateway, and includes:
and determining the number of CPUs, memories and instances of the sandbox environment gateway based on the resource use condition of the production environment, the historical calling statistical data and the maximum concurrency limit of the sandbox environment service calling.
Preferably, the API synchronization unit 502 is configured to enable the publisher to synchronize the API that has been created in the production environment to the sandbox environment through the service opening platform.
Preferably, the API synchronization unit 502 synchronizes the API that has been created in the production environment to the sandbox environment by the following method, including:
acquiring an API name, a rear-end address, a front-end access address and an authentication mode from a database, creating target, upstream, service and route required by the API in a sandbox environment gateway based on corresponding API information, and binding a plug-in corresponding to the API authentication mode and an access flow limiting plug-in;
and when the API has a test environment back-end access address, setting the back-end address as the test environment back-end address when synchronizing the API with the sandbox environment.
Preferably, the API debugging unit 503 is configured to create an anonymous user at the sandbox environment gateway when the publisher performs API testing, so that the publisher initiates a service call request by modifying the parameter value using the anonymous role, forwards the service call request to the back-end service through the sandbox environment gateway, and obtains service call response information, so that the publisher determines an API debugging result according to the response information.
Preferably, wherein the response information includes: response code information, response delay information, response Header information, and response body information.
Preferably, the API testing unit 504 is configured to, when a caller performs an API test, create a temporary user in a sandbox environment gateway, and give an API access right to the temporary user, so that a call can initiate a service call request by modifying a parameter value using a role of the temporary user, forward the service call request to a back-end service through the sandbox environment gateway, and obtain service call response information, so that the caller determines an API test result according to the response information.
Preferably, wherein the system further comprises:
the plug-in creating unit is used for creating a corresponding flow limiting plug-in for the anonymous user; and the system is used for creating a corresponding authentication plug-in and a flow limiting plug-in for the temporary user.
Preferably, wherein the system further comprises:
the sandbox environment service sdk automatic generation unit is used for setting an access address, a parameter value and authentication information of the sandbox environment API corresponding to the service request address and automatically generating a sandbox environment service access sdk in a preset language, and meanwhile, a caller changes the service request address into the access address of the production environment API and the authentication information into the authentication information acquired after platform authorization, so that the production environment API is called by sdk.
Preferably, wherein the system further comprises:
and the access log processing unit is used for leading the access log of the API test function into the ElasticSearch through logstack by the sandbox environment gateway so as to monitor the access log in real time, and giving an alarm when the peak access amount or the access flow exceeds the capacity of the sandbox environment gateway, so that an administrator can perform an elastic expansion example on the sandbox environment gateway in time.
The API testing system 500 for the service open platform according to the embodiment of the present invention corresponds to the API testing method 100 for the service open platform according to another embodiment of the present invention, and is not described herein again.
The invention has been described with reference to a few embodiments. However, other embodiments of the invention than the one disclosed above are equally possible within the scope of the invention, as would be apparent to a person skilled in the art from the appended patent claims.
Generally, all terms used in the claims are to be interpreted according to their ordinary meaning in the technical field, unless explicitly defined otherwise herein. All references to "a/an/the [ device, component, etc ]" are to be interpreted openly as referring to at least one instance of said device, component, etc., unless explicitly stated otherwise. The steps of any method disclosed herein do not have to be performed in the exact order disclosed, unless explicitly stated.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solutions of the present invention and not for limiting the same, and although the present invention is described in detail with reference to the above embodiments, those of ordinary skill in the art should understand that: modifications and equivalents may be made to the embodiments of the invention without departing from the spirit and scope of the invention, which is to be covered by the claims.