Serverspec(Specinfra)にcheck_is_installed_by_pip2を実装しました
TL;DR
serverspecでは、pip2コマンドでモジュールをチェックする機能が実装されていないので、独自でリソースを実装し、以下のように利用できるようにする。
describe package(module_name) do
it { should be_installed.by('pip2') }
it { should be_installed.by('pip2').with_version(version) }
end
追記
check_is_installed_by_pip2
を実装して、SpecinfraのGitHubにPull requestを出したらマージされたので、独自実装する必要がなくなりました。
https://github.com/mizzy/specinfra/pull/688
経緯
serverspecで、pip2, pip3のモジュールをそれぞれチェックしたかった。
チェック対象のサーバーは、pip
、pip3
コマンドではpip3が呼ばれ、pip2
コマンドではpip2が呼ばれるようにしている。
しかし、serverspecでは、以下のように、pip、pip3コマンドでのチェックしかできない。
# check_is_installed_by_pip
it { should be_installed.by('pip') }
# check_is_installed_by_pip3
it { should be_installed.by('pip3') }
理由は、specinfraでは、check_is_installed_by_pip
とcheck_is_installed_by_pip3
しか用意されていないからだ。
これではpip2のチェックができない。
そこで、check_is_installed_by_pip2を独自で用意し、serverspecで以下のように定義できるようにする。
it { should be_installed.by('pip2') }
独自リソースを作成する
packageリソースを拡張するため、check_is_installed_by_pip2
を定義する。
参考:specinfra/lib/specinfra/command/base/package.rb
check_is_installed_by_pip2を定義
以下のファイルにcheck_is_installed_by_pip2
を定義する。
spec/lib/extension/specinfra/command/base/package.rb
class Specinfra::Command::Base::Package < Specinfra::Command::Base
class << self
def check_is_installed_by_pip2(name, version=nil)
regexp = "^#{name}"
cmd = "pip2 freeze | grep -iw -- #{escape(regexp)}"
cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
cmd
end
end
end
spec/lib以下のファイルがautoloadされるようにする
spec/libの*.rbのファイルがautoloadされるように、以下の一行をspec/spec_helper.rbに追加する。
Dir[File.expand_path "#{__dir__}/lib/**/*.rb"].each{|f| require(f)}
specファイルで呼び出す
be_installed.by('pip2')
とすることで、check_is_installed_by_pip2
が呼び出される。
require 'spec_helper'
describe package('boto3') do
it { should be_installed.by('pip2') }
it { should be_installed.by('pip2').with_version('1.9.156') }
end
追記
check_is_installed_by_pip2
を実装して、SpecinfraのGitHubにPull requestを出したらマージされたので、独自実装する必要がなくなりました。
https://github.com/mizzy/specinfra/pull/688
参考にしたserverspec, specinfraのファイル
- check_is_installed_by_pip(), check_is_installed_by_pip3()が定義されているファイル
- be_installedが定義されているファイル
- be_installedで呼んでいるinstalled?が定義されているファイル
その他参考記事
Author And Source
この問題について(Serverspec(Specinfra)にcheck_is_installed_by_pip2を実装しました), 我々は、より多くの情報をここで見つけました https://qiita.com/Esfahan/items/c2230006f7c791d4fb4b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .