Code coverage collection method, device and equipment
By injecting initialized containers into the deployment files of K8s application services, non-invasive code coverage tool solves the problems of high risks and low efficiency in the code coverage acquisition process in the existing technology, and achieves more efficient and secure code Coverage collection.
Patent Information
- Application Number
- CN202210297289.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-24
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2042-03-24
AI Technical Summary
In K8s-based application services, the prior art can easily increase the risk of mirroring instructions in the code coverage acquisition process and requires manual intervention, resulting in low processing efficiency and high learning cost.
By adding initial container settings information for injecting code coverage tools in the deployment file of the target application service, the initial container is started before the POD container is started, and the commands for injecting code coverage tools are executed to make it non-in the image Invasive injection.
It reduces the risk of problems introduced by code coverage acquisition, improves collection efficiency, avoids the risks brought by mirror modification, and reduces labor investment costs.
Smart Images

Figure CN114706767B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of testing technology, and in particular to a code coverage acquisition method, device and equipment. Background Art
[0002] Code coverage is one of the important indicators to measure the adequacy and completeness of software testing work. The so-called code coverage refers to the proportion and degree of the code that has been executed during the test process to the total amount of code to be tested. Code coverage focuses on which code has been executed and which code has not been executed. At present, more and more application services are managed and deployed using the Kubernets (i.e. K8s) platform. Therefore, it is very important to collect the code coverage of application services in K8s to evaluate their software quality.
[0003] In the prior art, code coverage is collected for K8s-based application services through a code coverage collection tool. When starting the coverage collection tool, it is generally necessary to modify the application image, add the coverage collection tool to the image, and modify the application process startup command. After the coverage collection test is completed, it is necessary to restore the modified process startup command and repackage an image before the official launch. Therefore, it is easy to increase the risk of image instructions and introduce online problems, and the processing efficiency is low. In addition, commands are executed through the kubectl command line tool of K8s to generate and collect code coverage, so manual intervention is required, which brings learning costs and labor costs.
[0004] Therefore, how to reduce risks in code coverage collection of K8s-based application services is a problem that needs to be solved. Summary of the invention
[0005] The code coverage collection method provided in the embodiment of the present application provides a more reasonable solution for code coverage collection of K8s-based application services, reduces the risk of problems introduced by code coverage collection, and improves the efficiency of code coverage collection.
[0006] An embodiment of the present application provides a code coverage collection method, including: obtaining a deployment file for deploying a target application service in a cluster; the deployment file is a resource list file for creating a POD container corresponding to the target application service; adding setting information of an initialization container for injecting a code coverage tool in the deployment file; the initialization container is started before the POD container is started, and the startup command of the initialization container is a command for injecting the code coverage tool; making the deployment file after adding the setting information effective to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service; executing test cases for the target application service, and collecting the code coverage corresponding to the target application service based on the injected code coverage tool.
[0007] Optionally, it also includes: adding information of a first storage resource and information of a second storage resource to the deployment file; wherein the first storage resource is used to store the code coverage tool; and the second storage resource is used for sharing the POD container corresponding to the target application service.
[0008] Optionally, the startup command of the initialization container is an executable script containing a command for injecting the code coverage tool, and the executable script performs the following steps when the initialization container is started to inject the code coverage tool: determine the environment directory of the first executable file corresponding to the formal operation of the target application service, and copy the environment directory and its contents to the second storage resource; copy the second executable file with the code coverage tool stored in the first storage resource to the environment directory of the second storage resource, so that the process in the POD container providing the target application service is replaced by the second executable file process from the first executable file process at startup.
[0009] Optionally, it also includes: adding the information of the environment directory in the second storage resource to the starting position of the path environment variable of the container resource corresponding to the target application service in the deployment file, so that after the POD container created according to the container resource is started, the search for the POD container startup command can be directed to the second storage resource through the environment variable; the POD container startup command uses the second executable file in the second storage material when the POD container is started.
[0010] Optionally, the method further includes: in the deployment file, setting the initialization container to use the same image as the target application service, so that the environment directory of the first executable file can be determined after the initialization container is started.
[0011] Optionally, determining the environment directory of the first executable file corresponding to the formal operation of the target application service, and copying the environment directory and its contents to a second storage resource includes: if the first executable file is a Python executable file, adding a script command in the executable script to traverse the real Python environment directory path pointed to by the Python executable file soft link, and copying each real Python environment directory and its contents to the second storage resource.
[0012] Optionally, if the first executable file is a Python executable file, it also includes: adding a script command in the executable script to execute the installation command of the code coverage tool based on the Python executable file.
[0013] Optionally, determining the environment directory of the first executable file corresponding to the formal running of the target application service, and copying the environment directory and its contents to a second storage resource includes: if the first executable file is a Java executable file, adding a script command in the executable script to obtain the JAVA_HOME path, and copying the contents in the JAVA_HOME path to the second storage resource.
[0014] Optionally, obtaining the deployment file for deploying the target application service in the cluster includes: detecting a partition identifier and an application service identifier input by a user, determining information of the target application service based on the partition identifier and the application service identifier; and obtaining the deployment file of the target application service based on the information of the target application service.
[0015] Optionally, it also includes: in response to detecting that an interface element for triggering code coverage collection is triggered, adding setting information for injecting an initialization container for a code coverage tool into the deployment file, providing the deployment file with the setting information added to the cluster management platform, and calling the API of the cluster management platform to make the deployment file effective.
[0016] Optionally, the injection-based code coverage tool collects the code coverage corresponding to the target application service, including: before the POD container is updated and / or when the POD container version ends, instructing the injected code coverage tool to collect the code coverage of the POD container; obtaining the code coverage and displaying it as the code coverage of the target application service.
[0017] Optionally, there are multiple POD containers corresponding to the target application service; the injection-based code coverage tool collects the code coverage corresponding to the target application service, including: before each POD container is updated and / or at the end of the version, instructing the execution of the code coverage injected in the second executable file of each POD container to collect the code coverage of the corresponding POD container, and generate a code coverage file for each POD container; merging each code coverage file to generate the code coverage of the target application service.
[0018] An embodiment of the present application also provides a code coverage collection device, including: a deployment file acquisition unit, used to obtain a deployment file for deploying a target application service in a cluster; the deployment file is a resource list file for creating a POD container corresponding to the target application service; a coverage tool injection unit, used to add setting information of an initialization container for injecting a code coverage tool to the deployment file; the initialization container is started before the POD container is started, and the startup command of the initialization container is a command for injecting the code coverage tool; a deployment unit, used to make the deployment file after adding the setting information effective, so as to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service; a coverage collection unit, used to execute test cases for the target application service, and collect the code coverage corresponding to the target application service based on the injected code coverage tool.
[0019] An embodiment of the present application further provides an electronic device, comprising: a memory, and a processor; the memory is used to store a computer program, and after the computer program is run by the processor, the method provided in the embodiment of the present application is executed.
[0020] An embodiment of the present application also provides a storage device storing a computer program. After the computer program is run by a processor, the method provided in the embodiment of the present application is executed.
[0021] Compared with the prior art, this application has the following advantages:
[0022] The embodiment of the present application provides a code coverage collection method, device, electronic device and storage device, by adding the setting information of the initialization container for injecting the code coverage tool in the deployment file of the target application service; the initialization container is started before the POD container is started, and the startup command of the initialization container is a command for injecting the code coverage tool; the deployment file after adding the setting information is effective to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service; the test case is executed for the target application service, and the code coverage corresponding to the target application service is collected based on the injected code coverage tool. Injecting the code coverage tool in the initialization container does not require changing the image corresponding to the target application service, so the code coverage can be collected without intrusion on the tested image, which can ensure the consistency of the tested image from testing to formal operation on the line, and reduce the risk of artificial modification of the image. Further, an interface for triggering code coverage collection is provided to trigger the automatic injection of the coverage tool and collect and display the code coverage, so that the tester is not required to understand the deployment, image and application service code of the application service, reducing the manpower investment cost. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 is a processing flow chart of a code coverage collection method provided in an embodiment of the present application;
[0024] Figure 2 This is a schematic diagram of the POD life cycle provided by an embodiment of the present application;
[0025] Figure 3 This is a code coverage injection processing flow chart provided by an embodiment of the present application;
[0026] Figure 4 This is a schematic diagram of a code coverage quality measurement system provided by an embodiment of the present application;
[0027] Figure 5 This is a schematic diagram of a code coverage quality measurement platform interface provided by an embodiment of the present application;
[0028] Figure 6 This is a schematic diagram of displaying code coverage results provided by an embodiment of the present application;
[0029] Figure 7 This is a schematic diagram of a code coverage collection device provided by an embodiment of the present application;
[0030] Figure 8 It is a schematic diagram of an electronic device provided by this application. DETAILED DESCRIPTION
[0031] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0032] The embodiments of the present application provide a code coverage acquisition method, device, electronic device and storage device, which are described one by one in the following embodiments.
[0033] To facilitate understanding, several concepts in this application are first explained.
[0034] Kubernetes (K8s for short) is a portable, scalable open source platform for managing containerized workloads and services. Kubectl is a command-line tool for managing Kubernetes clusters.
[0035] An image is a lightweight, executable, independent software package that packages the software runtime environment and the software developed based on the runtime environment. It contains everything needed to run a piece of software, including code, runtime, libraries, environment variables, and configuration files.
[0036] Volumes is a data storage solution for Kubernetes that defines file directories for storing disk data in containers and data sharing among multiple containers in a Pod.
[0037] Pod is the smallest deployable computing unit created and managed in Kubernetes.
[0038] Deployment is a YAML-based object in Kubernetes that provides declarative definitions for Pods resources and ReplicaSets. ReplicaSets are replica sets, or replica controllers, that create one or more PODs running on nodes in the cluster based on the resource list of the POD container defined in the Deployment.
[0039] InitContainer (initialization container) is a special container that runs before the container providing application services in the Pod is started.
[0040] Code coverage is the ratio and degree of the executed code in the software testing process to the total code to be tested. It focuses on which code is executed and which is not executed when executing the test case. Code coverage analysis can judge the code quality to a certain extent. Generally, the probability of error in code with high coverage is relatively low.
[0041] Jacoco is an open source code coverage tool for the Java language.
[0042] CoveragePy is an open source code coverage tool for Python.
[0043] The code coverage collection method provided in the embodiment of the present application injects the code coverage tool into the initialization container before the POD container corresponding to the tested application service is started, thereby realizing non-invasive injection of the image corresponding to the tested application service and avoiding the problem of modifying the image and the corresponding dockerfile. Since the image is not modified, it can be ensured that the image from testing to official launch is the same, reducing the risk of modifying the image. Furthermore, based on this, a code coverage collection solution for general python or java applications is implemented, reducing manual operations, and reducing the learning cost of learning Kubernetes and Kubectl, and gaining an in-depth understanding of the image and startup scripts of application services.
[0044] The following combination Figures 1 to 6 The code coverage collection method provided in one embodiment of the present application is described in detail. Figure 1 The code coverage collection method shown includes: step S101 to step S104.
[0045] Step S101, obtaining a deployment file for deploying a target application service in a cluster; the deployment file is a resource list file for creating a POD container corresponding to the target application service.
[0046] In this embodiment, the so-called POD container refers to one or more business containers that are generated after the image is deployed in the cluster and run on the cluster nodes to provide application services. The cluster is preferably a test cluster used for software testing. The target application service is an application service that needs to collect the coverage of the tested code, and the tested code or the compiled code corresponding to the tested code is included in the image corresponding to the target application service. In order to collect the code coverage of the target application service, it is necessary to start the code coverage tool and obtain the coverage data collected by the tool. In this embodiment, in terms of starting the code coverage tool, the code coverage tool is non-invasively injected into the image of the target application service to collect the code coverage of the target application service, specifically by adding an initialization container to the deployment file.
[0047] This step is to obtain the deployment file. Specifically, obtaining the deployment file includes: generating a new deployment file for the target application service, declaring the resources of the POD container in the deployment file; or obtaining an existing deployment file and editing it.
[0048] This embodiment also includes providing a general code coverage collection solution for Python or Java-based application services, which enables the tester to input the service identifier of the target application service through an interactive interface, and obtain the deployment file based on the service identifier. An application name or an application service name is an example of a service identifier. Specifically, obtaining the deployment file for deploying the target application service in the cluster includes: detecting the partition identifier and the application service identifier input by the user, determining the information of the target application service based on the partition identifier and the application service identifier; and obtaining the deployment file of the target application service based on the information of the target application service.
[0049] Step S102, adding setting information of an initialization container for injecting a code coverage tool to the deployment file; the initialization container is started before the POD container is started, and the startup command of the initialization container is a command for injecting the code coverage tool.
[0050] This step adds the setting information of the initialization container to the deployment file, that is, declares the initialization container. After the initialization container runs, the code coverage tool is injected into the execution file of the Python or Java-based application service. Furthermore, the initialization container has a separate image separated from the POD container. The initialization container running phase must precede the application startup phase. Therefore, the coverage collection tool is injected during the initContainers phase of the POD life cycle when kubernetes creates it, without any interference with other phases of the POD life cycle, thereby ensuring that the collection process is non-invasive and non-perceptible to the image being tested. Please refer to Figure 2 , which is a schematic diagram of the POD life cycle, including: initialization container 201, application service container 202. Among them, the initialization container (initContainers) can be one or more, which are started before the application service container is started. If there are multiple initContainers, one initContainer can be started after another one succeeds, until all initialization containers are started successfully. Then, the application service container is started. The states in the life cycle of the application service container shown in the figure include: start, readiness, liveness, and stop. This is an exemplary state and is not a limitation on the POD life cycle.
[0051] In this embodiment, information about the first storage resource and the second storage resource are added to the deployment file; wherein the first storage resource is used to store the code coverage tool; and the second storage resource is used for sharing by the POD container corresponding to the target application service. The first storage resource may be an NFS folder, and the second storage resource may be a shared folder of the POD container. Preferably, the startup command of the initialization container is an executable script containing commands for injecting the code coverage tool, and the executable script executes the following steps when the initialization container is started to inject the code coverage tool: determine the environment directory of the first executable file corresponding to the formal operation of the target application service, and copy the environment directory and its contents to the second storage resource; copy the second executable file with the code coverage tool stored in the first storage resource to the environment directory of the second storage resource, so that the process in the POD container providing the target application service is replaced by the second executable file process from the first executable file process at startup. Further, it also includes: in the deployment file, adding the information of the environment directory in the second storage resource to the starting position of the path environment variable of the container resource corresponding to the target application service, so that after the POD container created according to the container resource is started, the search of the POD container startup command can be directed to the second storage resource through the environment variable; the POD container startup command uses the second executable file in the second storage data when the POD container is started. In the deployment file, the initialization container is set to use the same image as the target application service, so that after the initialization container is started, the environment directory of the first executable file can be determined. Wherein, the determination of the environment directory of the first executable file corresponding to the formal operation of the target application service, and the copying of the environment directory and its content to the second storage resource includes: if the first executable file is a Python executable file, then adding a script command in the executable script to traverse the real Python environment directory path pointed to by the Python executable file soft link, and copying each real Python environment directory and its content to the second storage resource; adding a script command in the executable script to execute the installation command of the code coverage tool based on the Python executable file. If the first executable file is a Java executable file, then determining the environment directory of the first executable file corresponding to the formal operation of the target application service, and copying the environment directory and its contents to the second storage resource includes: adding a script command in the executable script to obtain the JAVA_HOME path, and copying the contents in the JAVA_HOME path to the second storage resource.
[0052] During implementation, the code coverage tool can be injected by performing the following processing on the deployment file deployment according to the specified syntax of the deployment: adding two folders (volumes) in the deployment, one of which is an NFS folder that pre-stores the code coverage tool and related scripts to be injected, and the NFS folder can be understood as the first storage resource; the other is a shared directory shared by multiple POD containers, and the shared directory can be understood as the second storage resource; adding initContainer (initialization container) in the deployment, the image of initContainer uses the same image address as the target application service, so that initContainer When the container is running, it can determine the environment directory of the Python or Java executable file (which can be understood as the first executable file) in the target application service image to be tested, so as to obtain the environment directory, which is used to inject the execution environment information represented by it into the execution environment for collecting code coverage; when declaring the startup command information of initContainer, it is set as the installation injection script of the code coverage tool, so that when initContainer starts, the command in the installation injection script can be executed to generate a Python or Java executable file with the code coverage tool and store it in a shared folder. The executable file can be understood as the second executable file, which executes the POD container to provide the target application service, thereby realizing the injection of the code coverage tool. Please refer to Figure 3 , the figure shows the installation injection steps, wherein the commands corresponding to the installation injection steps are included in the installation injection script. The figure includes:
[0053] S301, obtain the environment directory of the first executable file, that is, find the environment directory of the Python or Java executable file of the target application service and copy it to the shared directory in the subsequent steps; among them, Java is the Java_HOME directory; Python is the PYTHONPATH directory or, obtain the real environment directory path pointed to by the soft links of all Python executable files, and process all Python executable files to achieve a universal effect.
[0054] S302, installation of code coverage tool. For Python, the installation command of the coverage tool is executed through all Python executable files, such as: "${python}-m pip install coverage==5.5"; for Java, Jacoco has been pre-stored in the first storage resource, that is, the NFS folder.
[0055] S303, copy the execution environment directory to the shared folder, so that the POD container of the target application service has the execution environment and execution files injected with the code coverage tool. For Python, copy all Python environment directory paths obtained in S301 to the shared folder. For Java, copy JAVA_HOME to the shared folder.
[0056] S304, injection of Python or Java executable files with code coverage tools. That is, the Python or Java executable files with code coverage collection tools pre-stored in the NFS folder are placed in the shared directory, and the native Python or Java executable files are replaced in the language environment directory. It can be understood that the second executable file is used to replace the first executable file to start the POD container after the initialization container runs. For Python, the name of the original python executable file (i.e., the first executable file) is changed to rawpython, and the coverage executable file is modified to use python in the shared folder, and the gunicorn executable file (first executable file) is modified to use python in the shared folder (i.e., the second executable file), thereby adding a python executable file with CoveragePy. For Java, the first executable file java is changed to rawjava, thereby adding a Java executable file with Jacoco, i.e., the second executable file. The second executable file Python or Java executable file with the code coverage tool is stored in a shared folder, i.e., the second storage resource.
[0057] S305, adding a shared folder to the very beginning of the PATH declaration of the container resource of the target application service, that is, adding the address of the environment directory in the shared directory to the very beginning of the PATH environment variable, so that when the target application service is started, the startup command can point to the Python or Java executable file with the code coverage collection tool.
[0058] Step S103, making the deployment file after adding the setting information effective, so as to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service.
[0059] In this embodiment, the modified deployment is issued through the Kubernetes API to make it effective. After it takes effect, when the target application service container is started, the Python or Java executable file in the application container has been replaced with the executable file injected with the coverage collection tool to start. The code coverage tool injection is applicable to both Python and Java applications, without any modification to the tested image, and is supported regardless of whether the startup command is included in the image or in the deployment or startup script.
[0060] Step S104: executing the test case for the target application service, and collecting the code coverage corresponding to the target application service based on the injected code coverage tool.
[0061] After the code coverage tool is injected in the above steps, the Python or Java executable file of the node running the POD container in the test cluster is the second executable file that represents the code coverage tool. The method provided in this embodiment is not limited to the nodes in the cluster. Any node in the cluster where the POD container is deployed can run the POD container based on the second executable file injected with the code coverage tool, and collect code coverage. This step is to collect code coverage. Specifically, a coverage collection instruction is sent to the POD container, instructing the injected code coverage tool to collect the code coverage of the POD container before the POD container is updated and / or when the POD container version ends, and the collected result data is saved in the first storage resource, that is, the NFS folder where the code coverage tool and related scripts are stored.
[0062] During implementation, coverage collection can be integrated into the test process, and the process of injecting from the code coverage tool to code coverage collection can be automated. The automated process can be integrated into a platform, such as a quality testing platform. Since the code coverage tool injects processing that is common to Python and Java, it is convenient to provide a relatively concise code coverage collection interface, so that when the tester performs coverage collection, he does not need to know kubenertes and coverage-related technical knowledge, and does not need to know the specific startup commands and codes of the tested application service, and can configure code coverage collection for any Java or Python application. For example, a coverage collection function start button is provided. When the button is turned on, the code coverage tool is automatically injected into the tested application, and the code coverage data is automatically collected before the application is updated and at the end of the version, and the result is displayed on the coverage result display interface of the quality testing platform, thereby simplifying the tester's operation. Users do not need to manually perform coverage data collection, but automatically collect and display data, reducing manpower investment and learning costs. Moreover, the coverage tool injection and collection function is applicable to continuous deployment tasks. For example, coverage data is automatically generated and collected when the application is updated in the continuous deployment task, without human intervention. In addition, based on the non-invasive coverage tool injection of the image of the tested application, code coverage data can be collected without any changes to the image. This ensures the consistency of the image from testing to launch, reducing the risks brought by human operation.
[0063] Preferably, the method of obtaining the deployment file for deploying the target application service in the cluster further includes: detecting a partition identifier and an application service identifier input by a user, determining the information of the target application service according to the partition identifier and the application service identifier; and obtaining the deployment file of the target application service according to the information of the target application service. Further, the method further includes: in response to detecting that an interface element for triggering code coverage acquisition is triggered, adding setting information for injecting the initialization container of the code coverage tool into the deployment file, providing the deployment file with the setting information added to the cluster management platform, and calling the API of the cluster management platform to make the deployment file effective.
[0064] In this step, the code coverage tool based on injection collects the code coverage corresponding to the target application service, including: before the POD container is updated and / or when the POD container version ends, instruct the injected code coverage tool to collect the code coverage of the POD container; obtain the code coverage and display it as the code coverage of the target application service. When there are multiple POD containers corresponding to the target application service, it also includes merging the code coverage files of each POD container. Specifically, the code coverage tool based on injection collects the code coverage corresponding to the target application service, including: before each POD container is updated and / or when the version ends, instruct the execution of the code coverage injected in the second executable file of each POD container to collect the code coverage of the corresponding POD container and generate the code coverage file of each POD container; merge each code coverage file to generate the code coverage of the target application service. Further, the code coverage collection results are displayed on the quality testing platform, so that the development users and test users who can access the platform can view the results in real time to analyze the code quality of the application service under test.
[0065] Please refer to Figure 4 , the figure shows a schematic diagram of a code coverage quality measurement system, including: a quality measurement platform 401, an application service POD 402, and a test task POD 403; wherein, the application service corresponds to multiple PODs, that is, a deployment object of an application service creates a replica set (ReplicaSet), and the number of replicas in the replica set is set to be plural, so when the deployment object is executed, multiple PODs are created according to the number of replicas, and each POD is distributed to the nodes of the cluster according to the scheduling strategy or the pre-specified POD binding strategy. The quality measurement platform sends a command to generate a coverage file to each application service POD, and the execution process of each application service POD injected with the code coverage tool (i.e., the process based on the second executable file) schedules the coverage tool to execute the command, and generates a coverage file in the path corresponding to the coverage tool (i.e., the first storage resource). The quality measurement platform sends a command to execute and generate coverage to the test task POD, and the POD merges the coverage files of each application service POD, generates the coverage result of the application service, and reports it to the quality measurement platform. The quality measurement platform displays the coverage data of the application service.
[0066] Please refer to Figure 5, the figure shows a display of a code coverage quality testing platform interface, including: an application input area 501 under test and a test trigger area 502; wherein, the application input area under test is used to input relevant information of the target application service under test, and the input area, as shown in the figure, indicates the information that needs to be entered in the corresponding text controls, respectively. Of course, a selection control can also be used so that the tester can select relevant information of a predetermined application service. Of course, controls for entering other information, such as the location of the image repository, the name of the cluster, etc., can also be included. The present application does not limit the specific requirements for the input items in the input area. The test trigger area is used to trigger the automatic modification of the deployment file of the target application service and make it effective, as well as to automatically execute the collection command, obtain and display the collected coverage data. If the target application service corresponds to multiple POD containers, the coverage collection files of each POD container are merged to obtain and display the coverage collection results of the target application service.
[0067] Please refer to Figure 6 , the figure shows the code coverage result display, including: the collection result display area 601, which displays: the image repository name, cluster name, partition name, application name, collected code coverage data and a "View" button corresponding to the target application service under test. Trigger the "View" button to view the code coverage details. The collection results of different applications can be displayed in this area. If the content exceeds the display area on one screen, it will be displayed in multiple pages. Only one collection result is shown in the figure, so the page number in the display area is 1.
[0068] Understandably, Figure 5 and Figure 6 The layout, appearance, size, position and other information of each element in the middle interface are schematic and do not limit the method provided in this application.
[0069] It should be noted that, in the absence of conflict, the features given in this embodiment and other embodiments of the present application can be combined with each other, and steps S101 and S102 or similar terms do not limit the steps to be executed sequentially.
[0070] So far, the method provided by this embodiment has been described. In the method, the code coverage tool is injected into the initialization container, and the image corresponding to the target application service does not need to be changed. Therefore, the code coverage can be collected without intrusion into the tested image, which can ensure the consistency of the tested image from testing to formal operation online, and reduce the risk of artificial modification of the image. Furthermore, an interface for triggering code coverage collection is provided to trigger the automatic injection of the coverage tool and the collection and display of code coverage, so that the tester is not required to understand the deployment, image and application service code of the application service, reducing manpower investment.
[0071] Corresponding to the above embodiment, an embodiment of the present application provides a code coverage collection device. Figure 7 , Figure 7 The code coverage collection device shown includes:
[0072] The deployment file obtaining unit 701 is used to obtain a deployment file for deploying a target application service in a cluster; the deployment file is a resource list file for creating a POD container corresponding to the target application service;
[0073] A coverage tool injection unit 702 is used to add setting information of an initialization container for injecting a code coverage tool in the deployment file; the initialization container is started before the POD container is started, and the startup command of the initialization container is a command for injecting the code coverage tool;
[0074] A deployment unit 703 is used to make the deployment file after adding the setting information effective, so as to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service;
[0075] The coverage collection unit 704 is used to execute test cases for the target application service and collect the code coverage corresponding to the target application service based on the injected code coverage tool.
[0076] Optionally, the coverage tool injection unit 702 is specifically used to: add information about a first storage resource and information about a second storage resource in the deployment file; wherein the first storage resource is used to store the code coverage tool; and the second storage resource is used for sharing by a POD container corresponding to the target application service.
[0077] Optionally, the startup command of the initialization container is an executable script containing a command for injecting the code coverage tool, and the deployment unit 703 is specifically used to: execute the executable script to determine the environment directory of the first executable file corresponding to the formal operation of the target application service, and copy the environment directory and its contents to the second storage resource; copy the second executable file with the code coverage tool stored in the first storage resource to the environment directory of the second storage resource, so that the process in the POD container providing the target application service is replaced by the second executable file process from the first executable file process at startup.
[0078] Optionally, the coverage tool injection unit 702 is specifically used to: add the information of the environment directory in the second storage resource to the starting position of the path environment variable of the container resource corresponding to the target application service in the deployment file, so that after the POD container created according to the container resource is started, the search for the POD container startup command can be directed to the second storage resource through the environment variable; the POD container startup command uses the second executable file in the second storage material when the POD container is started.
[0079] Optionally, the coverage tool injection unit 702 is specifically used to: in the deployment file, set the initialization container to use the same image as the target application service, so that the environment directory of the first executable file can be determined after the initialization container is started.
[0080] Optionally, the deployment unit 703 is specifically used for: if the first executable file is a Python executable file, adding a script command in the executable script to traverse the real Python environment directory path pointed to by the Python executable file soft link, and copying each real Python environment directory and its contents to the second storage resource.
[0081] Optionally, the deployment unit 703 is specifically used to: if the first executable file is a Python executable file, add a script command in the executable script to execute the installation command of the code coverage tool based on the Python executable file.
[0082] Optionally, the deployment unit 703 is specifically used for: if the first executable file is a Java executable file, adding a script command in the executable script for obtaining a JAVA_HOME path, and copying the content in the JAVA_HOME path to the second storage resource.
[0083] Optionally, the coverage acquisition unit 704 is specifically used to: detect a partition identifier and an application service identifier input by a user, determine information of the target application service according to the partition identifier and the application service identifier; and obtain a deployment file of the target application service according to the information of the target application service.
[0084] Optionally, the coverage collection unit 704 is specifically used to: in response to detecting that an interface element for triggering code coverage collection is triggered, add setting information for injecting an initialization container for a code coverage tool into the deployment file, provide the deployment file with the setting information added to the cluster management platform, and call the API of the cluster management platform to make the deployment file effective.
[0085] Optionally, the coverage collection unit 704 is specifically used to: before the POD container is updated and / or when the POD container version ends, instruct the injected code coverage tool to collect the code coverage of the POD container; obtain the code coverage and display it as the code coverage of the target application service.
[0086] Optionally, there are multiple POD containers corresponding to the target application service; the coverage collection unit 704 is specifically used to: before each POD container is updated and / or at the end of the version, instruct the execution of the code coverage injected into the second executable file of each POD container to collect the code coverage of the corresponding POD container, and generate a code coverage file for each POD container; merge each code coverage file to generate the code coverage of the target application service.
[0087] Based on the above embodiments, an embodiment of the present application provides an electronic device. For the relevant parts, please refer to the corresponding description of the above embodiments. Figure 8 The electronic device shown in the figure includes: a memory 801 and a processor 802; the memory is used to store a computer program, and after the computer program is run by the processor, the method provided in the embodiment of the present application is executed.
[0088] Based on the above embodiment, an embodiment of the present application provides a storage device. For the relevant parts, please refer to the corresponding description of the above embodiment. Figure 8 The storage device stores a computer program, and after the computer program is run by the processor, the method provided in the embodiment of the present application is executed.
[0089] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0090] Although the present application is disclosed as above in the form of a preferred embodiment, it is not intended to limit the present application. Any technical personnel in this field may make possible changes and modifications without departing from the spirit and scope of the present application. Therefore, the scope of protection of the present application shall be based on the scope defined by the claims of the present application.
Claims
1. A code coverage collection method, characterized in that: include: Obtain a deployment file for deploying a target application service in a cluster; The deployment file is a resource list file used to create a POD container corresponding to the target application service; wherein the deployment file contains information about a first storage resource and information about a second storage resource; the first storage resource is used to store a code coverage tool; and the second storage resource is used for sharing with the POD container corresponding to the target application service; Adding setting information of an initialization container for injecting a code coverage tool to the deployment file; the initialization container has a separate image separated from the POD container, is started before the POD container is started, and the startup command of the initialization container is an executable script for injecting the code coverage tool, and the executable script is executed when the initialization container is started to inject the code coverage tool, including: determining and obtaining an environment directory of a first executable file corresponding to the formal operation of the target application service, installing the code coverage tool stored in the first storage resource to generate an executable file with the code coverage tool as a second executable file; installing The execution environment directory after the code coverage tool is installed and its content are copied to the second storage resource, so that the execution environment and executable file injected with the code coverage tool exist in the POD container of the target application service; the generated second executable file is copied to the environment directory of the second storage resource, and the second executable file is used to replace the first executable file, and the information of the environment directory in the second storage resource is added to the starting position of the environment variable declaration of the container resource corresponding to the target application service, so that when the target application service is started, the startup command can point to the second executable file with the code coverage collection tool, thereby realizing the injection of the code coverage tool; Making the deployment file after adding the setting information effective, so as to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service; A test case is executed for the target application service, and the code coverage corresponding to the target application service is collected based on the injected code coverage tool.
2. The method according to claim 1, characterized in that Also includes: In the deployment file, the initialization container is set to use the same image address as the target application service, so that the environment directory of the first executable file can be determined after the initialization container is started.
3. The method according to claim 1, characterized in that The step of determining and obtaining the environment directory of the first executable file corresponding to the formal operation of the target application service includes: If the first executable file is a Python executable file, a script command is added to the executable script to traverse the real Python environment directory path pointed to by the Python executable file soft link, and copy each real Python environment directory and its content to the second storage resource.
4. The method according to claim 3, characterized in that If the first executable file is a Python executable file, it also includes: A script command is added to the executable script to execute the installation command of the code coverage tool based on the Python executable file.
5. The method according to claim 1, characterized in that The step of determining and obtaining the environment directory of the first executable file corresponding to the formal operation of the target application service includes: If the first executable file is a Java executable file, a script command is added to the executable script to obtain the JAVA_HOME path and copy the content in the JAVA_HOME path to the second storage resource.
6. The method according to claim 1, characterized in that The step of obtaining a deployment file for deploying a target application service in a cluster includes: Detecting a partition identifier and an application service identifier input by a user, and determining information of the target application service according to the partition identifier and the application service identifier; The deployment file of the target application service is acquired according to the information of the target application service.
7. The method according to claim 6, characterized in that Also includes: In response to detecting that an interface element for triggering code coverage collection is triggered, setting information for injecting an initialization container for a code coverage tool is added to the deployment file, the deployment file with the setting information added is provided to the cluster management platform, and the API of the cluster management platform is called to make the deployment file effective.
8. The method according to claim 6, characterized in that: The injection-based code coverage tool collects the code coverage corresponding to the target application service, including: Before the POD container is updated and / or when the POD container version ends, instruct the injected code coverage tool to collect the code coverage of the POD container; The code coverage is obtained and displayed as the code coverage of the target application service.
9. The method according to claim 8, characterized in that There are multiple POD containers corresponding to the target application service; The injection-based code coverage tool collects the code coverage corresponding to the target application service, including: Before each POD container is updated and / or at the end of the version, instruct the execution of the code coverage injected into the second executable file of each POD container to collect the code coverage of the corresponding POD container, and generate a code coverage file for each POD container; Each code coverage file is merged to generate the code coverage of the target application service.
10. A code coverage collection device, characterized in that: include: A deployment file obtaining unit, used to obtain a deployment file for deploying a target application service in a cluster; The deployment file is a resource list file used to create a POD container corresponding to the target application service; wherein the deployment file contains information about a first storage resource and information about a second storage resource; the first storage resource is used to store a code coverage tool; and the second storage resource is used for sharing with the POD container corresponding to the target application service; The coverage tool injection unit is used to add setting information of an initialization container for injecting a code coverage tool in the deployment file; the initialization container has a separate image separated from the POD container, is started before the POD container is started, and the startup command of the initialization container is an executable script for injecting the code coverage tool, and the executable script is executed when the initialization container is started to inject the code coverage tool, including: determining and obtaining an environment directory of a first executable file corresponding to the formal operation of the target application service, installing the code coverage tool stored in the first storage resource, so as to generate an executable file with the code coverage tool as a second executable line file; copy the execution environment directory and its content after installing the code coverage tool to the second storage resource, so that the execution environment and execution file injected with the code coverage tool exist in the POD container of the target application service; copy the generated second executable file to the environment directory of the second storage resource, and use the second executable file to replace the first executable file, and add the information of the environment directory in the second storage resource to the starting position of the environment variable declaration of the container resource corresponding to the target application service, so that when the target application service is started, the startup command can point to the second executable file with the code coverage collection tool, thereby realizing the injection of the code coverage tool; A deployment unit, configured to make the deployment file after adding the setting information effective, so as to create the POD container and make it run on at least some nodes in the cluster, and the POD container running on the node provides the target application service; The coverage collection unit is used to execute test cases for the target application service and collect the code coverage corresponding to the target application service based on the injected code coverage tool.
11. An electronic device, characterized in that: include: A memory and a processor; the memory is used to store a computer program, and after the computer program is run by the processor, the method described in any one of claims 1 to 9 is executed.
Citation Information
Patent Citations
Method and system for automatically testing coverage rate in distributed system
CN104391795A
Application package deployment system based on K8s cluster
CN110753119A
Method and device for counting program code coverage rate in cloud container
CN111538660A