Build 自动验证程序按图 2 所示步骤循环执行。
图 2. Build 自动验证程序
验证结果发布服务器
Build 验证结果需要被持久化存储,所以我们引入验证结果发布服务器,开发人员和测试人员可以通过浏览器查看所有 Build 的验证结果。图 3 展示了验证结果发布服务器的具体实现,构建(Build)验证服务器和发布服务器通过数据库交互,并通过一个 Web 程序进行结果发布;图 4 则是一个典型的验证结果发布的实例。
图 3. 验证结果发布服务器的具体实现
图 4. 构建(Build)验证结果发布
验证结果发布程序的实现相对简单,我们对此不作展开。 Build 自动验证程序则是整个解决方案的核心,下面对它的所有步骤逐一说明。
轮询下载 Build
Build 的下载一般通过 FTP 协议完成,虽然 Windows 和 Unix 的命令脚本都提供相信的 FTP 支持,但用命令脚本不易实现各种错误(例如网络错误或者 Build 服务器不可访问)的处理逻辑,因此脚本运行的容错性和稳定性相对较差。而 Build 自动验证程序要求在无人干预的情况下 7 × 24 小时工作,所以我们选择 Java 实现 Build 轮询下载。图 5 是 Build 轮询下载的基本流程,BuildDownload.java 则是该流程的具体实现。
图 5. Build 轮询下载流程
清单 1. BuildDownload.java
public class BuildDownload { private String server; //Build Server's IP or host name private int port; //Build Server's FTP port private String user; //User ID used to log in the Build Server private String pwd; //Password private String remoteBuildDir; //The remote build directory on the Build Server private String localBuildDir; //The local directory to store the downloaded build private int interval; //The interval minutes to search the new build public static void main(String[] args) { //1. Check the input parameter: the full path of the configuration file if (args.length != 1) { System.out.println("Error - Invalid parameters."); System.exit(1); } BuildDownload dameon = new BuildDownload(args[0]); //2. Search and download the newest build boolean isSuccess = false; do { isSuccess = dameon.download(); } while(!isSuccess); } public BuildDownload(String configFile) { /*Load the specified property file and initialize the configurations*/ } public boolean download() { FtpClient client = new FtpClient(); InputStream input = null; BufferedInputStream buffer = null; BufferedReader bufferReader = null; FileOutputStream outfile = null; try { System.out.println("Info - Wait " + this.interval + " minutes."); Thread.sleep(this.interval*60000); //1. Connect to FTP server client.openServer(this.server, this.port); client.login(this.user, this.pwd); //2. Search the newest build on the Build Server System.out.println("Info - Search the newest build on the Build Server."); client.cd(this.remoteBuildDir); input = client.list(); bufferReader = new BufferedReader(new InputStreamReader(input)); String newest = "00000000000000.zip"; //The format of build name: yyyyMMddHHmmss.zip while ((temp=bufferReader.readLine())!=null) { String temp = temp.trim(); int start = temp.lastIndexOf(" "); temp = temp.substring(start+1); if ( temp.compareTo(newest) > 0 ) newest = temp; } bufferReader.close(); input.close(); System.out.println("Info - The newest build on the Build Server is " + newest); //3. Compare the remote newest build with the latest verified build String latestVerifiedBuild = getLatestVerifiedBuildID(); if (latestVerifiedBuild.compareTo(newest) >= 0) { client.closeServer(); System.out.println("Info - The build " + newest + " has been verified."); return false; } System.out.println("Info - A new build " + newest + " is ready."); //4. Download the newest build clearLocalDirectory(); System.out.println("Info - Start to download the build " + newest); client.binary(); input = (InputStream)client.get(this.remoteBuildDir + "/" + newest); buffer = new BufferedInputStream(input); outfile = new FileOutputStream(this.localBuildDir + "/" + newest, true); int content; while((content=buffer.read()) != -1) { outfile.write(content); } outfile.flush(); outfile.close(); buffer.close(); input.close(); client.closeServer(); System.out.println("Info - Finish to download the build " + newest); return true; } catch (Exception e) { e.printStackTrace(); System.out.println("Error - Unexpected exception happened."); return false; } } public String getLatestVerifiedBuildID() { /*Get the latest verified build from the database in the publish server*/ } public void clearLocalDirectory() { /*Clear the local directory in which the downloaded build was stored*/ }} |
文章来源于领测软件测试网 https://www.ltesting.net/