0

I have a problem with the testing the Sensu Plugin. Everytime when I start rspec to test plugin it test it, but anyway at the end of test, the original plugin is started automatically. So I have in my console:

Finished in 0 seconds (files took 0.1513 seconds to load) 
1 example, 0 failures 
CheckDisk OK:       # This comes from the plugin

Short explanation how my system works: Plugin call system 'wmic' command, processes it, checks the conditions about the disk parameters and returns the exit statuses (ok, critical, etc) Rspec mocks the response from system and sets into the input of plugin. At the end rspec checks the plugin exit status when the mocked input is given.

My plugin looks like that:

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

Here is my test in 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

I know that I can just put "exit 0" after the last example, but this is not a solution because when I will try to start many spec files it will exit after the first one. How to start only test, without running the plugin? Maybe someone can help me and show how to handle with such problem? Thank you.

Piotr
  • 1

1 Answers1

0

You can stub the original plugin call and optionally return a dummy object:

allow(SomeObject).to receive(:method) # .and_return(double)

you can put it in the before block to make sure that all assertions will share the code.

Another thing is that you are using rescue blocks to catch the situation when your code aborts with an error. You should use raise_error matcher instead:

expect { run }.to raise_error(SystemExit)
Maciej Małecki
  • 2,725
  • 19
  • 29
  • Thanks for the reply. But the plugin code is still executed after the last step... BTW: How to use raise_error matcher to check the exit code (whether plugin returns 0,1 or 2) ? – Piotr Oct 27 '16 at 14:18
  • It means that you didn't mock the right method. You can also allow the code to execute and just mock standard output (`allow(STDOUT).to receive(:write)`). It depends on what you want to really test. As for `SystemExit`, it is not possible to check the exit code. – Maciej Małecki Oct 27 '16 at 14:28