# jenkinsでのunitテストは、dockerでクリーン環境を作って行おう
準備
docker
jenkins
jenkinsがdockerを利用するために、グループ追加
sudo gpasswd -a jenkins docker
CI用Dockerの環境定義
Dockerfileで環境定義
# cat Dockerfile FROM centos RUN yum install -y git man # NVM インストール RUN git clone --depth 1 https://github.com/creationix/nvm.git ~/.nvm # nodejs インストール RUN bash -c ". /.nvm/nvm.sh; nvm install 0.10" # スクリプト実行ユーザの作成 RUN useradd -u 45678 -s /bin/bash -m worker # ワークスペースのマウント先を作成 RUN mkdir /workspace ENTRYPOINT ["/bin/bash", "-c"]
jenkinsのjob作成
新規job作成
ジョブ名を今回は「docker_test」と入力して、フリースタイルを選択
jobの設定内容
ビルド->シェルの実行を選択して、以下を記述
#job名をIMAGEに保存 IMAGE=$JOB_NAME #dockerで作成するときのimage名定義 #ワークスペースをjenkinsのワークスペースに設定 docker build -t $IMAGE $WORKSPACE docker run -v $WORKSPACE:/workspace -w /workspace $IMAGE "$(cat <<EOL source ~/.nvm/nvm.sh nvm use 0.10 npm install jasmine-node -g jasmine-node --junitreport --output junit-reports/ spec EOL )" #nvmを読み込み #nvm0.10を使用 #jasmine-nodeをglobalでインストール #jasmineでjsのunitテストを実行 # - --junitreportでjunit形式のレポートを出力する # - --outputでレポート出力先をjunit-reportsに設定 # specディレクトリ以下のspecを実行
ビルド後の処理
JUnitテスト結果の集計
junit-reports/*xml
ワークスペースにサンプルでテストを作成する
テスト対象のjsを作成
# cat lib/stylist.js
var Stylist, exports;
Stylist = (function() {
function Stylist() {}
Stylist.prototype.styles = {};
Stylist.prototype.decorate = function(target, options) {
var key, value, _ref;
if (!target) {
return this.styles[options.style];
}
_ref = this.styles[options.style];
for (key in _ref) {
value = _ref[key];
if (options.override || !target[key]) {
target[key] = value;
}
}
return target;
};
return Stylist;
})();
if (!exports) {
exports = {};
}
exports.Stylist = Stylist;
specを作成
# cat spec/stylist_spec.js
var Stylist = require('lib/stylist').Stylist;
describe('Stylist', function() {
beforeEach(function() {
this.stylist = new Stylist;
this.stylist.styles["normal"] = {
width: '50',
height: '100'
};
this.stylist.styles["double height"] = {
width: '50',
height: '200'
};
});
describe('オブジェクトをデコレートした場合', function() {
it('オブジェクトに指定されたスタイルを付与すること', function() {
var obj = { name: 'マネキン' };
this.stylist.decorate(obj, {
style: "double height"
});
expect(obj.width).toEqual('51');
expect(obj.height).toEqual('200');
expect(obj.name).toEqual('マネキン');
});
describe('オブジェクトにスタイルと同名のプロパティがあった場合', function() {
it('プロパティを上書きしないこと', function() {
var obj = {
name: 'マネキン',
width: '1000'
};
this.stylist.decorate(obj, {
style: "double height"
});
expect(obj.width).toEqual('1000');
expect(obj.height).toEqual('200');
expect(obj.name).toEqual('マネキン');
});
it('上書きフラグが立っていればプロパティを上書きすること', function() {
var obj = {
name: 'マネキン',
width: '1000'
};
this.stylist.decorate(obj, {
style: "normal",
override: true
});
expect(obj.width).toEqual('50');
expect(obj.name).toEqual('マネキン');
});
});
});
});
これで、jenkins,docker,js,unitテストの準備が完了!
jenkinsでjobを実行
http://localhost:8080/job/docker_test/ を開いて、左カラムのビルド実行を押下 するとテストが走ります。 左下に結果が表示されます。
おわりに
今回は、ワークスペースにjs,unit testファイルを事前に準備しましたが gitrepositoryを定義して、jobが走るごとに、checkoutしてきて テストする。とすると なんかCIぽくなります
今回は、dockerを使って、テストをするまでだったので そういったところは、省きましたが次回、テスト部分をもっと書いていきたいです。