需求是每部署一台服务器均要反复测试docker可用性,通过python脚本完成容器的自动化测试
脚本
启动指定数量容器提供测试
#!/usr/bin/env python
# coding=utf-8
'用于测试docker创建容器'
__author__ = 'xiaocai.name'
import os
import pycurl
import StringIO
import time
create_container_num = 100
print 'STEP 1 : remove all container'
if( os.popen('docker ps -q').read() ):
print os.popen('docker kill $(docker ps -q)').read()
if( os.popen('docker ps -a -q').read() ):
print os.popen('docker rm $(docker ps -a -q)').read()
print 'STEP 2 : run container'
for i in range(0, create_container_num):
port = 8100 + i
cmd = 'docker run --name "TEST_'+str(port)+'" -i -d -p '+str(port)+':80 -v /data/wwwroot/testsite/:/data/wwwroot test_images /data/start.sh'
print os.popen(cmd).read()
print 'STEP 3 : sleep 5s'
time.sleep(5)
print 'STEP 4 : curl container'
for i in range(0, create_container_num):
port = 8100 + i
url = 'http://127.0.0.1:'+str(port)+'/_test.php'
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
rel = StringIO.StringIO()
curl.setopt(pycurl.WRITEFUNCTION, rel.write)
curl.perform()
if( rel.getvalue()!='T' ):
print 'ERROR: TEST_'+str(port)+' '+url
else:
print 'SUCCESS: TEST_'+str(port)