Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Docker Swarm in continuous Integration testing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Docker Swarm in continuous integration testing how to apply the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will read this Docker Swarm in continuous integration testing how to apply the article will have some gains, let's take a look at it together.

background

Agile patterns are widely used, and testing is especially important. Since new releases need to be released frequently, we need to execute test cases more frequently to ensure that no new bugs are introduced into the release.

The time and resources required for a complete test process cannot be ignored, including the analysis of test results. How to provide complete and comprehensive testing to ensure quality in a shorter time is a problem we are eager to solve and the key to ensure that agile development can proceed smoothly.

Jenkins implements an unattended testing process where downstream testing tasks are executed immediately after development and once deployed in a test environment.

The application of jenkins saves human resources to a certain extent, and docker technology can achieve rapid expansion of containers, thus saving a lot of equipment resources and time, and quickly completing tests. This is a very important link in the jenkins pipeline (code pipeline management), as shown in Figure 1:

Figure 1. jenkins pipeline

This article mainly introduces how to use docker swarm cluster function and selenium grid script distribution function to build a selenium automatic script execution environment that can be dynamically expanded. Compared to using a real machine as the selenium automation script execution environment, using this environment can greatly reduce the maintenance of the execution environment, such as the management of various browser types and versions. It can also greatly reduce the material input to the script execution environment and save various resources.

Build docker swarm

Swarm Introduction

Swarm is a cluster management tool provided by docker officially to manage docker clusters. It abstracts several docker hosts into a whole and manages various docker resources on these docker hosts through a single portal.

Swarm is just a scheduler and router. Swarm does not run containers itself. It only accepts requests from docker clients and schedules appropriate nodes to run containers. This means that even if swarm fails for some reason, the nodes in the cluster will still run as usual. When swarm resumes running, it will collect and rebuild cluster information.

Swarm is similar to kubernetes, but lighter and has fewer features than kubernetes.

Environmental preparation

To set up the docker swarm environment, I prepared two machines in the example. One acts as a manager node and also as a worker node, and the other acts as a worker node only.

Here assume that our two machine ip information is as follows:

m1:10.13.181.1

m2:10.13.181.2

docker engine started with v1.12.0 and integrates docker swarm natively, so as long as docker is installed on each machine, docker swarm can be used directly. Here, docker installation will not be described in detail, please follow the official docker swarm documentation to install. After installation, start the docker service on each machine.

Tip:

Note: It is best to turn off the firewall on the machine, otherwise there may be swarm cluster network connectivity problems.

Command to turn off firewall: systemctl stop firewalld.service

Disable firewall startup command: systemctl disable firewalld.service

step

1. Create an administrative node.

We use machine m1 as the manager node and execute commands on this machine to initialize the cluster environment. The order reads as follows:

sudo docker swarm init --advertise-addr 10.13.181.1

Executing this command returns a token to join the cluster so that other workers can join the cluster.

Listing 1. Join Cluster Token Example:

The copy code is as follows:

docker swarm join --token swmtkn-1-5p3kzxhsvlqonst5wr02hdo185kcpdajcu9omy4z5dpmlsyrzj-

3phtv1qkfdly2kchzxh0h1xft 10.13.181.1:2377

If you want to get the command to join the cluster again, you can get it by executing the following command:

sudo docker swarm join-token worker

Add machine m1 to the cluster as a worker node.

Running the command in Listing 1 on the manager node machine adds machine m1 to the swarm cluster as a worker.

Add another machine m2 to the cluster as a worker node.

Execute the command in Listing 1 above on machine m2 to enable m2 to join the cluster.

4. Run the following command to create a clustered network:

sudo docker network create -d overlay seleniumnet

Here, seleniumnet is the name of the cluster network we created.

Create a selenium grid service on the new cluster network.

a. Create a selenium grid hub service. Based on cluster network seleciumnet, map port 4444 to port 4444 of the cluster, and set timeout time to 120 seconds to increase or decrease timeout time, as shown in Listing 2.

Listing 2. Creating a selenium grid hub service:

The copy code is as follows:

sudo docker service create --name selenium-hub --network seleniumnet -p 4444:4444 -e

grid_timeout=120 selenium/hub

b. Create selenium grid firefox node service and connect to the hub service you just created. This is shown in Listing 3.

listing 3. Create selenium grid firefox node service:

sudo docker service create \--name node-firefox \--replicas 5 \-p 7900:5900 \--network seleniumnet \-e hub_port_4444_tcp_addr=selenium-hub \-e hub_port_4444_tcp_port=4444 \selenium/node-firefox-debug bash -c 'se_opts="-host $hostname" /opt/bin/entry_point.sh'

Parameter Description:

-p: 7900:5900 exposes docker internal vnc5900 to host port 7900, allowing users to monitor docker internal execution from the outside via vnc.

c. Create a selenium grid chrome node service and connect to the hub service you just created. This is shown in Listing 4.

listing 4. Create Node Services:

sudo docker service create \--name node-chrome \--replicas 3 \-p 7901:5900 \--network seleniumnet \-e hub_port_4444_tcp_addr=selenium-hub \-e hub_port_4444_tcp_port=4444 \selenium/node-chrome-debug bash -c 'se_opts="-host $hostname" /opt/bin/entry_point.sh'

Parameter Description:

-p: 7901:5900 exposes docker internal vnc5900 to host port 7901, allowing users to monitor docker internal execution from the outside via vnc.

6. Check whether the environment is successfully built. Execute the following command on machine m1 to see if each service starts successfully:

sudo docker service ls

You can see that the selenium hub, firefox node and chrome node have been successfully started. Firefox's node copy is 5 and chrome's node copy is 3, as shown in figure 2.

Figure 2. docker service list

We then open the selenium hub url via ip plus port 4444 on any machine to see if the started firefox and chrome nodes have been successfully mounted on the hub node, as shown in figure 3.

hub url: 10.13.181.1:4444

Figure 3. Selenium hub interface diagram

As you can see from Figure 3, five Firefox nodes and three Chrome nodes have been successfully mounted on the hub node. At this point, docker swarm provides 5 firefox nodes and 3 chrome nodes to execute selenium automation scripts.

expansion method

Users can dynamically expand the number of nodes at any time according to the number of script executions to improve the execution efficiency of automation scripts. For example, we need 10 containers that can run Firefox browser. The corresponding commands are as follows:

sudo docker service scale node-firefox=10

Run jenkins job in docker swarm

Users running jenkins jobs in docker swarm do not need to do extra configuration in jenkins, but need to call selenium hub in the corresponding automation script to invoke webdriver remotely. This enables you to run selenium scripts in docker containers.

For the scenario in this article, simply call the remote selenium hub in the automation script, as follows: 9.111.139.104:4444/wd/hub

Running automation scripts in selenium grid

basic concepts

Selenium grid, for distributed automated testing, that is, a set of selenium code can be run on different environments, which makes it easy to run applications in different containers provided by docker.

Selenium grid has two concepts:

Hub: Master node, you can think of it as a general dispatch center.

node: branch node, you can think of it as the worker who actually performs the task.

That is to say, there can only be one main hub in selenium grid, but multiple n branch nodes can be established locally or remotely. Test scripts point to the main hub, and the main hub distributes them to local/remote nodes to run test cases.

implementations

To run automation scripts in selenium grid, first we need to create a remote driver object, which can be implemented through the source code in Figure 4. The corresponding input parameter selhub in the screenshot is the url of selenium hub: 9.111.139.104:4444/wd/hub

Figure 4. Code screenshots of automation scripts

By calling the driver above, you can run the automation script in the docker container.

About "Docker Swarm in continuous integration testing how to apply" the content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "Docker Swarm in continuous integration testing how to apply" knowledge, if you want to learn more knowledge, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report