Sha256: 76c79a47ca093155733409c5f01a6486607be8c450910a524c52de6794632915

Contents?: true

Size: 1.98 KB

Versions: 13

Compression:

Stored size: 1.98 KB

Contents

require 'spec_helper'

describe LitmusPaper::Metric::Script do
  describe "#result" do
    it "is 1 when the script outputs 1" do
      check = LitmusPaper::Metric::Script.new("echo 1", 1)
      check.current_health.should == 1
    end

    it "is true when the script returns a lot of data" do
      check = LitmusPaper::Metric::Script.new("dd if=/dev/urandom bs=1M count=1|base64 >&2 && echo 1", 1)
      check.current_health.should == 1
    end

    it "logs stdout and stderr when it fails" do
      check = LitmusPaper::Metric::Script.new("echo Hello && echo Goodbye 1>&2 && false", 1)
      count = 0
      logs = [
        "Available check to echo Hello && echo Goodbye 1>&2 && false failed with status 1",
        "Failed stdout: Hello\n",
        "Failed stderr: Goodbye\n",
      ]
      LitmusPaper.logger.should_receive(:info).exactly(3).times do |log|
        log.should == logs[count]
        count += 1
      end
      check.current_health.should == 0
    end

    it "is zero when the script exits 1" do
      check = LitmusPaper::Metric::Script.new("false", 1)
      check.current_health.should == 0
    end

    it "is zero when the script exceeds the timeout" do
      check = LitmusPaper::Metric::Script.new("sleep 10", 1, :timeout => 1)
      check.current_health.should == 0
    end

    it "kills the child process when script check exceeds timeout" do
      check = LitmusPaper::Metric::Script.new("sleep 50", 1, :timeout => 1)
      check.current_health.should == 0
      expect { Process.kill(0, check.script_pid) }.to raise_error(Errno::ESRCH)
    end

    it "can handle pipes" do
      check = LitmusPaper::Metric::Script.new("echo 'a' | tr a 1", 1)
      check.current_health.should == 1

      check = LitmusPaper::Metric::Script.new("echo 'a' | tr a 0", 0)
      check.current_health.should == 0
    end
  end

  describe "to_s" do
    it "returns the command" do
      check = LitmusPaper::Metric::Script.new("sleep 10", 1)
      check.to_s.should == "Metric::Script(sleep 10, 1)"
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
litmus_paper-1.6.2 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.6.1 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.6.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.5.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.4.2 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.4.1 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.3.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.2.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.1.1 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.1.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-1.0.0 spec/litmus_paper/metric/script_spec.rb
litmus_paper-0.9.9 spec/litmus_paper/metric/script_spec.rb
litmus_paper-0.9.7 spec/litmus_paper/metric/script_spec.rb