Sensuプラグインのテストに問題があります。 rspecをテストしてプラグインをテストするたびにテストしますが、テストの最後には元のプラグインが自動的に開始されます。だから、私は、コンソールにあります。Rspec。テスト後にテストされたコードが自動的に起動します
Finished in 0 seconds (files took 0.1513 seconds to load)
1 example, 0 failures
CheckDisk OK: # This comes from the plugin
短い説明は私のシステムがどのように機能するか: プラグインコールシステム「WMIC」コマンドを、それを処理し、ディスクパラメータに関する条件をチェックし、[OK](終了ステータスを返し、重要な、など) Rspecはシステムからの応答を模倣し、プラグインの入力に設定します。最後に、rspecはmocked入力が与えられたときにプラグインの終了ステータスをチェックします。
私のプラグインは以下のようになります。ここでは
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'
class CheckDisk < Sensu::Plugin::Check::CLI
def initialize
super
@crit_fs = []
end
def get_wmic
`wmic volume where DriveType=3 list brief`
end
def read_wmic
get_wmic
# do something, fill the class variables with system response
end
def run
severity = "ok"
msg = ""
read_wmic
unless @crit_fs.empty?
severity = "critical"
end
case severity
when /ok/
ok msg
when /warning/
warning msg
when /critical/
critical msg
end
end
end
はRSpecの中に私のテストで:
require_relative '../check-disk.rb'
require 'rspec'
def loadFile
#Load template of system output when ask 'wmic volume(...)
end
def fillParametersInTemplate (template, parameters)
#set mocked disk parameters in template
end
def initializeMocks (options)
mockedSysOutput = fillParametersInTemplate @loadedTemplate, options
po = String.new(mockedSysOutput)
allow(checker).to receive(:get_wmic).and_return(po) #mock system call here
end
describe CheckDisk do
let(:checker) { described_class.new }
before(:each) do
@loadedTemplate = loadFile
def checker.critical(*_args)
exit 2
end
end
context "When % of free disk space = 10 >" do
options = {:diskName => 'C:\\', :diskSize => 1000, :diskFreeSpace => 100}
it 'Returns ok exit status ' do
begin
initializeMocks options
checker.run
rescue SystemExit => e
exit_code = e.status
end
expect(exit_code).to eq 0
end
end
end
私はちょうど最後の例の後に「終了0」を置くことができることを知っているが、これはあります解決策ではありません。なぜなら、多くのspecファイルを起動しようとすると、最初のファイルの後に終了するからです。プラグインを実行しないで、テストだけを開始する方法は?多分誰かが私を助け、そのような問題に対処する方法を示すことができますか? ありがとうございます。
返信ありがとうございます。 プラグインコードは、最後のステップの後でも実行されています... BTW:raise_error matcherを使用して終了コードをチェックする方法(プラグインが0,1または2を返すかどうか) – Piotr
これはあなたが正しい方法を模擬しなかったことを意味します。また、コードの実行を許可し、標準出力( 'allow(STDOUT).to receive(:write)')を実行することもできます。それはあなたが本当にテストしたいものに依存します。 'SystemExit'に関しては、終了コードをチェックすることはできません。 – smefju