In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use Python and Conu test containers, the content is very detailed, interested friends can refer to, hope to be helpful to you.
More and more developers are using containers to develop and deploy their applications. This means that it is also important that containers can be easily tested. Conu (short for container utilities) is a Python library that makes it easy to write container tests.
Let's go
First, you need a container program to test. To do this, the following command creates a folder containing the Dockerfile of a container and a Flask application that is serviced by the container.
$mkdir container_test$ cd container_test$ touch Dockerfile$ touch app.py
Copy the following code into the app.py file. This is the usual basic Flask application, which returns the string "Hello Container World!" .
From flask import Flaskapp = Flask (_ _ name__) @ app.route ('/') def hello_world (): return 'Hello Container Worldwide' If _ _ name__ = ='_ _ main__': app.run (debug=True,host='0.0.0.0') create and build test container
To build the test container, add the following directive to Dockerfile.
FROM registry.fedoraproject.org/fedora-minimal:latestRUN microdnf-y install python3-flask & & microdnf clean allADD. / app.py / srvCMD ["python3", "/ srv/app.py"]
Then use the Docker CLI tool to build the container.
$sudo dnf-y install docker$ sudo systemctl start docker$ sudo docker build. -t flaskapp_container
Tip: the first two commands are required only if Docker is not installed on the system.
Run the container after building using the following command.
$sudo docker run-p 5000 rm flaskapp_container* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat* Debugger is activebirds * Debugger PIN: 47350551
*, use curl to check whether the Flask application runs correctly in the container:
$curl http://127.0.0.1:5000Hello Container World!
Now that flaskapp_container is running and ready for testing, you can use Ctrl+C to stop it.
Create a test script
Conu must be installed before writing test scripts. In the container_test directory you created earlier, run the following command.
$python3-m venv .venv $source .venv / bin/activate (.venv) $pip install-- upgrade pip (.venv) $pip install conu$ touch test_container.py
Then copy and save the following script in the test_container.py file.
Import conu PORT = 5000 with conu.DockerBackend () as backend: image = backend.ImageClass ("flaskapp_container") options = ["- p", "5000 image"] container = image.run_via_binary (additional_opts=options) try: # Check that the container is running and wait for the flask application to start. Assert container.is_running () container.wait_for_port (PORT) # Run a GET request on / port 5000. Http_response = container.http_request (path= "/", port=PORT) # Check the response status code is 200 assert http_response.ok # Get the response content response_content = http_response.content.decode ("utf-8") # Check that the "Hello Container World!" String is served. Assert "Hello Container World!" In response_content # Get the logs from the container logs = [line for line in container.logs ()] # Check the the Flask application saw the GET request. Assert b' "GET / HTTP/1.1" 200 -'in logs [- 1] finally: container.stop () container.delete () Test Settings
The script first sets up conu to use Docker as the backend to run the container. It then sets up the container image to use the flaskapp_container you built in the * * section of this tutorial.
The next step is to configure the options required to run the container. In this example, the Flask application provides content on port 5000. So you need to expose this port and map it to the same port on the host.
Start the container with this script, and you can now test it.
Testing method
Before testing the container, check that the container is running and ready. The demonstration script uses container.is_running and container.wait_for_port. These methods ensure that the container is running and that the service is available on the default port.
Container.http_request is a wrapper around the request library that makes it easy to send HTTP requests during testing. This method returns requests.Responseobject, so you can easily access the contents of the response for testing.
Conu can also access the container log. Once again, this is very useful during testing. In the above example, the container.logs method returns the container log. You can use them to assert that specific logs are printed, or, for example, no exceptions are thrown during testing.
Conu provides many useful ways to interface with containers. A complete list of API is provided in the documentation. You can also refer to the examples provided on GitHub.
All the code and files needed to run this tutorial are also available on GitHub. For readers who want to take this example further, you can take a look at using pytest to run tests and build a container test suite.
On how to use Python and Conu test containers to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.