Docker CI: 基于 Dockerfile 构建 WebTestbed 新镜像

Docker CI: 基于 Dockerfile 构建 WebTestbed 新镜像

      • 一、概述
        • 架构图如下:
      • 二、Docker 平台(Ubuntu 14.04):基于 Dockerfile 构建 WebTestbed 新镜像
        • 文件清单
        • dockerfile
        • passwd
        • pip.conf
        • xstartup
        • vnc.sh
        • supervisord.conf
        • start.sh
      • 三、查看并运行 WebTestbed 镜像
      • 四、VNC Viewer 打开容器 webtest

一、概述

基于 Docker 集成 CI 环境。涉及技术:Linux(Ubuntu 14.04), Docker, Jenkins, Git/Gitlab, Web/Httpbin, Python/Pytest, UI/Selenium, Robotframework, Grid Server, Appium 等。

架构图如下:

在这里插入图片描述

二、Docker 平台(Ubuntu 14.04):基于 Dockerfile 构建 WebTestbed 新镜像

# cd /vm/docker/robotframework
# sudo docker build -t rf .

文件清单

dockerfile

FROM ubuntu:14.04

MAINTAINER Allan 

ENV DEBIAN_FRONTEND noninteractive
ENV USER root

# VNC 配置
RUN apt-get update && \
    apt-get install python-pip -y && \
    apt-get install -y --no-install-recommends ubuntu-desktop && \
    apt-get install -y gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal && \
    apt-get install wget && \
    mkdir /root/.vnc
RUN wget https://bintray.com/artifact/download/tigervnc/stable/ubuntu-14.04LTS/amd64/tigervncserver_1.4.3-3ubuntu1_amd64.deb
RUN apt-get install libgnutls28 libtasn1-3-bin -y
RUN dpkg -i tigervncserver_1.4.3-3ubuntu1_amd64.deb
ADD xstartup /root/.vnc/xstartup
ADD passwd /root/.vnc/passwd
RUN chmod 600 /root/.vnc/passwd

# SSH 配置
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
# RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# 编译器
RUN apt-get update && \
    apt-get install vim -y && \
    apt-get install gedit -y

#安装基本字体
RUN apt-get -qqy --no-install-recommends install \
fonts-ipafont-gothic \
xfonts-100dpi \
xfonts-75dpi \
xfonts-cyrillic \
xfonts-scalable

#安装文泉驿微米黑字体
RUN apt-get -qqy install ttf-wqy-microhei \
&& ln -snf /etc/fonts/conf.d/65-wqy-microhei.conf /etc/fonts/conf.d/69-language-selector-zh-cn.conf

# 时区:中国上海
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 配置中文
RUN apt-get update && apt-get install -y locales
RUN locale-gen zh_CN.UTF-8 &&\
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales
RUN locale-gen zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN:zh
ENV LC_ALL zh_CN.UTF-8

RUN sed -i '$a\set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936' /etc/vim/vimrc
RUN sed -i '$a\set encoding=utf-8' /etc/vim/vimrc

# 安装 Git
RUN apt-get install git -y

# 设置 Java(Jenkins CI) 
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get install openjdk-8-jdk -y
RUN apt-get install oracle-java8-set-default


# 配置 pip.conf
ADD pip.conf /root/.pip/pip.conf

# 搭建 robotframework 框架
ADD setuptools-19.2.zip setuptools-19.2.zip
RUN unzip setuptools-19.2.zip
RUN python setuptools-19.2/setup.py install
RUN apt-get install build-essential libssl-dev libffi-dev python-dev -y
RUN apt-get install python-wxgtk2.8 -y
RUN pip install requests
RUN pip install xlrd
RUN pip install xlwt
RUN pip install natsort
RUN pip install selenium==3.13.0
RUN pip install --default-timeout=10000 robotframework
RUN pip install --default-timeout=10000 robotframework-ride
RUN pip install --default-timeout=10000 robotframework-selenium2library
RUN pip install --default-timeout=10000 robotframework-databaselibrary
RUN pip install robotfixml
# python -m robotfixml example.xml fixed.xml
RUN pip --default-timeout=100000 install pymysql
RUN pip install enum


# 设置 chrome & chromedriver
RUN apt-get install libxss1 libappindicator1 libindicator7 fonts-liberation xdg-utils -y
ADD google-chrome_amd64.deb google-chrome_amd64.deb
RUN apt list google-chrome*.deb
RUN dpkg -i google-chrome*.deb
RUN sed -i '$s/.*/& --no-sandbox --start-maximized/' /usr/bin/google-chrome
RUN ln -s /opt/google/chrome/chrome /usr/bin/chrome

ADD chromedriver chromedriver
RUN chmod +x chromedriver
RUN mv -f chromedriver /usr/local/share/chromedriver
RUN ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
RUN ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

# 设置工程
RUN mkdir /var/project
ADD testrunner.py testrunner.py
ADD unic.py unic.py
ADD element.py element.py
RUN mv -f testrunner.py /usr/local/lib/python2.7/dist-packages/robotide/contrib/testrunner/testrunner.py
RUN mv -f unic.py /usr/local/lib/python2.7/dist-packages/robot/utils/unic.py
RUN mv -f element.py /usr/local/lib/python2.7/dist-packages/SeleniumLibrary/keywords/element.py

# 启动 SSH 和 vnc
RUN apt-get install supervisor -y
ADD vnc.sh /root/.vnc/vnc.sh
RUN chmod a+x /root/.vnc/vnc.sh
ADD supervisord.conf /etc/supervisord.conf
ADD start.sh /opt
RUN chmod a+x /opt/start.sh
ENTRYPOINT ["/opt/start.sh"]

EXPOSE 22
EXPOSE 5901

passwd

配置密码

pip.conf

[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

xstartup

#!/bin/sh
export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &

gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &

vnc.sh

#!/bin/bash
while [ 1 ]
do
  ps -fe|grep Xvnc |grep -v grep
  if [ $? -ne 0 ]
  then
  echo "start vnc ..."
  rm -f /tmp/.X1-lock
  rm -f /tmp/.X11-unix/X1
  /usr/bin/vncserver :1 -geometry 1280x800 -depth 24
  else
  echo "vnc is running"
  fi
  sleep 30
 done

supervisord.conf

[supervisord]
nodaemon=true

[program:vnc]
command=/root/.vnc/vnc.sh
autorestart=true
priority=200

start.sh

#!/bin/bash
/usr/sbin/sshd -D &
/usr/bin/supervisord -c /etc/supervisord.conf

三、查看并运行 WebTestbed 镜像

--name:容器名
--restart:自动启动
--shm-size:内存大小,1g 防止浏览器 crashed
-d:daemon 守护进程
-p:publlish 端口,5901是 webtest VNC 端口,1022 是 SSH 端口

# sudo docker images
# sudo docker run --name webtest --restart always --shm-size=1g -d -p 5901:5901 -p 1022:22 rf
# sudo docker ps

四、VNC Viewer 打开容器 webtest

Docker CI: 基于 Dockerfile 构建 WebTestbed 新镜像_第1张图片
Docker CI: 基于 Dockerfile 构建 WebTestbed 新镜像_第2张图片

你可能感兴趣的:(Robotframework,Python,docker,Docker,CI:持续集成)