Jenkinsメモ¶
OSXへのインストール¶
http://qiita.com/makoto_kw/items/cbe93d4ebbc35f3b43fd を参考にした。
brew install jenkins
cp -p /usr/local/opt/jenkins/*.plist ~/Library/LaunchAgents
vi ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
launchctl start homebrew.mxcl.jenkins
# launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
# launchctl stop homebrew.mxcl.jenkins
APIテストを作成してみる¶
概要¶
ここではWeb API開発を前提としてテストを作成する。
http://tech.albert2005.co.jp/blog/2014/07/10/notify-hipchat-from-jenkins/
をみるのが良さそう。
WebAPIの作成¶
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4from flask import Flask, request
5from urllib import urlencode
6import json
7
8"""
9テストは以下のように実施します。
10正常な例:
11curl -H "Content-Type:application/json" -X POST -d '{"val1": 1, "val2": 2}' http://localhost:5000/add
12失敗する例
13curl -H "Content-Type:application/json" -X POST -d '{"val1": 1, "val2": "2"}' http://localhost:5000/add
14
15"""
16
17app = Flask(__name__)
18
19@app.route('/', methods=['GET'])
20def index():
21 return('index')
22
23
24@app.route('/add', methods=['POST'])
25def hello():
26 res = proc_add(request.data)
27 c = ""
28 c += str(res)
29 return(c)
30
31@app.route('/hello/<name>', methods=['GET'])
32def hello_withname(name):
33 return("hi! {0}".format(name))
34
35def proc_add(stJson):
36 diInput = json.loads(stJson)
37 if not "val1" in diInput:
38 raise Excetion("noval1")
39 if not "val2" in diInput:
40 raise Excetion("noval2")
41
42 inRes = diInput["val1"] + diInput["val2"]
43 diRet = {}
44 diRet['res'] = inRes
45 return(json.dumps(diRet, indent = 2))
46
47if __name__ == '__main__':
48 app.run(debug=True, host="0.0.0.0")
みたいなコードを作成した。
# curl -H "Content-Type:application/json" -X POST -d '{"val1": 1, "val2": 2}' http://localhost:5000/add
<hr>3<hr>
のようにテストできる。
で、Flaskアプリ化したいので、
1# -*- coding:utf-8 -*-
2
3import sys, os
4sys.path.append(os.path.dirname(__file__))
5from json_add import app as application
を書いて、
WSGIDaemonProcess user=nobody group=nogroup threads=5
WSGIScriptReloading On
WSGIScriptAlias /calc /home/kanai/git/PythonJunkTest/Flask/json_add.wsgi
みたいに書いてあげる。 すると、
curl -H "Content-Type:application/json" -X POST -d '{"val1": 1, "val2": 1}' http://www.hogetan.net/calc/add
みたいに、apacheで提供できる。やったね。
テストを作る¶
APIができました!が目的ではなくて、テストの
Jenkinsに設定¶
http://tech.albert2005.co.jp/blog/2014/07/10/notify-hipchat-from-jenkins/ を最大限に使う。
easy_install unittest-xml-reporting
jenkins
フリースタイルプロジェクト
ソースコード管理「なし」
シェルスクリプトにして、
cd /Users/kanai/git/PythonJunkTest/Flask/Test_json_add
/usr/local/bin/nosetests --with-xunit -v
という感じで作る。これで上記のディレクトリに nosetests.xmlが生成されて、その結果を基にテストが行われる。