LoginSignup
1
0

More than 5 years have passed since last update.

自作したAnsibleモジュールのsanityテストをTravis CIでやってみた

Last updated at Posted at 2019-03-24

自作したAnsibleモジュールのsanityテストをTravis CIでテストした時のメモです。

Travis CI

プライベートリポジトリを使う場合はProへ

Travis CIドキュメント

モジュールテストについて

テスト自体は、以下のようにGitLabでやっていたので、それをベースとしてやってみます。

使うリポジトリ

今回は以下のリポジトリを使ってテストをやってみます。

テスト対象

テスト対象のモジュールは上記リポジトリにある modules ディレクトリ配下にある全てに対してsanityテストをやってみます。

CI準備

Travis CIでリポジトリの有効化

Travis CIでGitHubアカウントでログインし連携した後に対象のリポジトリを有効化します。

TravisCI01.png

Dockerファイル

Ansibleモジュールのテストは、あらかじめ用意されているテスト用コンテナイメージを使ってテストします。
以下のようなDockerファイルを作成しました。

Dockerfile
FROM quay.io/ansible/default-test-container:latest
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
RUN cd /opt && \
    git clone https://github.com/ansible/ansible.git && \
    cd ansible && \
    mkdir lib/ansible/modules/salf_made/ && \
    mkdir test/units/modules/salf_made
ADD ansible_module_test.sh /opt
ADD modules /opt/ansible/lib/ansible/modules/salf_made/
RUN chmod +x /opt/ansible_module_test.sh
RUN apt-get -y install man-db

テスト用スクリプト

コンテナ内で実行するテスト用スクリプトです。

ansible_module_test.sh
#!/bin/sh
version=$1
module_dir='lib/ansible/modules/salf_made/'

cd /opt/ansible
. hacking/env-setup
for module in $(find $module_dir | grep "\.py$" | awk -F / '{print $(NF)}' | sed -e "s/\.py$//g" | grep -v "__init__" | sort) ; do
    if [ $version = "2.6" ] ; then
        ansible-test sanity --python $version --skip-test botmeta $module
    elif [ $version = "3.8" ] ; then
        ansible-test sanity --python $version --skip-test pylint $module
    else
        ansible-test sanity --python $version $module
    fi

    if [ $? -ne 0 ] ; then
      exit 1
    fi
done

python 2.6, 3.8では、一部のテスト用モジュールが対応していないためかエラーが出るためスキップしています。

CIファイル

Travis CIのCIファイルは以下のようにしました。
リポジトリにあるDockerファイルを元にイメージをビルドしてコンテナを起動し、コンテナ内に配置したスクリプトを実行します。

.travis.yml
language: python
services:
  - docker

before_install:
  - docker build -t ansible-ci:latest .
  - docker run -d -it --name ansible-ci ansible-ci:latest

jobs:
  include:
    - stage: sanity test
      name: python 2.6
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 2.6'
    - name: python 2.7
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 2.7'
    - name: python 3.5
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 3.5'
    - name: python 3.6
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 3.6'
    - name: python 3.7
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 3.7'
    - name: python 3.8
      script: docker exec ansible-ci /bin/sh -c '/opt/ansible_module_test.sh 3.8'

CI実行

後は、リポジトリにpushすればCIが実行されます。
結果例は以下の通りです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0