概要
serverspec-runnerというgemの使い方です。
serverspecを拡張されたツールです。
関連記事
環境構築
dockerで環境を構築します。
From centos:7
ARG RUBY_VER_PREFIX=2.5
ARG RUBY_VER=2.5.1
RUN yum install -y make \
gcc-c++ \
glibc-headers \
openssl-devel \
readline \
libyaml-devel \
readline-devel \
zlib \
zlib-devel \
bzip2 \
libxslt-devel
WORKDIR /usr/local/src
RUN curl -O https://cache.ruby-lang.org/pub/ruby/${RUBY_VER_PREFIX}/ruby-${RUBY_VER}.tar.gz
RUN tar zxfv ruby-${RUBY_VER}.tar.gz
WORKDIR /usr/local/src/ruby-${RUBY_VER}
RUN ./configure
RUN make
RUN make install
RUN gem install bundle
インストール
$ bundle init
Gemfile
gem 'serverspec-runner'
gem 'highline'
$ bundle install --path vendor/bundle
nokogiriがどうのこうのとエラーが出る場合は、以下を実行してから、bundle install
を実行してください。
$ bundle config --local build.nokogiri --use-system-libraries
serverspec-runner初期設定
以下のコマンドで初期設定を行います。
$ bundle exec serverspec-runner -r ./
want to create spec-tree to /code? (y/n): y
Please edit "/code/scenario.yml" and change directory to "/code" and exec "serverspec-runner" command !!
以下のようなディレクトリ構成になります。
Gemfile
Gemfile.lock
lib
scenario.yml
spec
ssh_options_default.yml
vendor
scenario.ymlを作成
テスト対象のホストと、テストケースを定義します。
書式: https://github.com/hiracy/serverspec-runner#usage
test_top_dir: # test directory top
: # test hierarchy directories
test_bottom_dir: # test directory bottom
- servername # ssh-accessible ip address or fqdn. or alias
- :
- :
:
---
servername: # alias name(not required)
host: 192.168.0.11 # ssh-accessible ip address or fqdn(required if alias exist)
ssh_opts: # ssh options(not required)
port: 22 # ssh port option(not required)
user: "anyone" # ssh user option(not required)
keys: ["~/.ssh/id_rsa"] # ssh private keys option(not required)
: # any other Net::SSH Options(not required)
any_attribute: "aaa" # host attributes. this example available to get "property[:any_attribute]" from your codes(not required)
:
:
e.g.)
spec:
- server01
- server02
---
server01:
host: hostname01
ssh_opts:
user: 'myname'
server02:
host: hostname02
ssh_opts:
user: 'myname'
こちらのファイルで、sshの共通設定をすることもできます。
ssh_options_default.yml
:port: 22
:paranoid: false
# :user: "youre_name"
# :keys: ["/path/to/private_key"]
# :passphrase: "yourpassphrase"
テストケース作成
テストの作成方法は、serverspecと同じです。
spec/ディレクトリの下に作成してください。
テスト実行
-t mkd
はマークダウンで結果を出力するオプションです。
$ cd /home/vagrant/appname/serverspec-runner
$ bundle exec serverspec-runner -t mkd
sudoが必要な場合は、以下の様にASK_SUDO_PASSWORD=1
を付けるか、環境変数にSUDO_PASSWORD
をセットします。
$ ASK_SUDO_PASSWORD=1 bundle exec serverspec-runner -t mkd
scenario.ymlを別の場所、名前で保存した場合
$ bundle exec serverspec-runner -r ./ -s scenario/production.yml -t mkd
-r
トップディレクトリの指定
-s
シナリオファイルのパスの指定