move the loadAverage class into his own file and do the same for the specs

Fixes #6240
This commit is contained in:
Pier-Hugues Pellerin 2016-11-11 16:15:37 -05:00
parent 80c2019dcf
commit 169ba4636a
4 changed files with 139 additions and 134 deletions

View file

@ -1,5 +1,6 @@
# encoding: utf-8
require "logstash/instrument/periodic_poller/base"
require "logstash/instrument/periodic_poller/load_average"
require "logstash/environment"
require "jrmonitor"
require "set"
@ -33,50 +34,6 @@ module LogStash module Instrument module PeriodicPoller
end
end
class LoadAverage
class Windows
def self.get
nil
end
end
class Linux
LOAD_AVG_FILE = "/proc/loadavg"
TOKEN_SEPARATOR = " "
def self.get
load_average = ::File.read(LOAD_AVG_FILE).chomp.split(TOKEN_SEPARATOR)
{
:"1m" => load_average[0].to_f,
:"5m" => load_average[1].to_f,
:"15m" => load_average[2].to_f
}
end
end
class Other
def self.get()
load_average_1m = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()
return nil if load_average_1m.nil?
{
:"1m" => load_average_1m
}
end
end
def self.create
if LogStash::Environment.windows?
Windows
elsif LogStash::Environment.linux?
Linux
else
Other
end
end
end
attr_reader :metric

View file

@ -0,0 +1,47 @@
# encoding: utf-8
module LogStash module Instrument module PeriodicPoller
class LoadAverage
class Windows
def self.get
nil
end
end
class Linux
LOAD_AVG_FILE = "/proc/loadavg"
TOKEN_SEPARATOR = " "
def self.get
load_average = ::File.read(LOAD_AVG_FILE).chomp.split(TOKEN_SEPARATOR)
{
:"1m" => load_average[0].to_f,
:"5m" => load_average[1].to_f,
:"15m" => load_average[2].to_f
}
end
end
class Other
def self.get()
load_average_1m = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()
return nil if load_average_1m.nil?
{
:"1m" => load_average_1m
}
end
end
def self.create
if LogStash::Environment.windows?
Windows
elsif LogStash::Environment.linux?
Linux
else
Other
end
end
end
end end end

View file

@ -28,96 +28,6 @@ describe LogStash::Instrument::PeriodicPoller::JVM::GarbageCollectorName do
end
end
describe LogStash::Instrument::PeriodicPoller::JVM::LoadAverage do
subject { described_class.create }
context "on mocked system" do
context "on Linux" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(false)
expect(LogStash::Environment).to receive(:linux?).and_return(true)
end
context "when it can read the file" do
let(:proc_loadavg) { "0.00 0.01 0.05 3/180 29727" }
before do
expect(::File).to receive(:read).with("/proc/loadavg").and_return(proc_loadavg)
end
it "return the 3 load average from `/proc/loadavg`" do
avg_1m, avg_5m, avg_15m = proc_loadavg.chomp.split(" ")
expect(subject.get).to include(:"1m" => avg_1m.to_f, :"5m" => avg_5m.to_f, :"15m" => avg_15m.to_f)
end
end
end
context "on windows" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(true)
end
it "Xreturns nil" do
expect(subject.get).to be_nil
end
end
context "on other" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(false)
expect(LogStash::Environment).to receive(:linux?).and_return(false)
end
context "when 'OperatingSystemMXBean.getSystemLoadAverage' return something" do
let(:load_avg) { 5 }
before do
expect(ManagementFactory).to receive(:getOperatingSystemMXBean).and_return(double("OperatingSystemMXBean", :getSystemLoadAverage => load_avg))
end
it "returns the value" do
expect(subject.get).to include(:"1m" => 5)
end
end
context "when 'OperatingSystemMXBean.getSystemLoadAverage' doesn't return anything" do
before do
expect(ManagementFactory).to receive(:getOperatingSystemMXBean).and_return(double("OperatingSystemMXBean", :getSystemLoadAverage => nil))
end
it "returns nothing" do
expect(subject.get).to be_nil
end
end
end
end
# Since we are running this on macos and linux I think it make sense to have real test
# insteadof only mock
context "real system" do
if LogStash::Environment.linux?
context "Linux" do
it "returns the load avg" do
expect(subject.get).to include(:"1m" => a_kind_of(Numeric), :"5m" => a_kind_of(Numeric), :"15m" => a_kind_of(Numeric))
end
end
elsif LogStash::Environment.windows?
context "window" do
it "returns nothing" do
expect(subject.get).to be_nil
end
end
else
context "Other" do
it "returns 1m only" do
expect(subject.get).to include(:"1m" => a_kind_of(Numeric))
end
end
end
end
end
describe LogStash::Instrument::PeriodicPoller::JVM do
let(:metric) { LogStash::Instrument::Metric.new(LogStash::Instrument::Collector.new) }

View file

@ -0,0 +1,91 @@
# encoding: utf-8
require "logstash/instrument/periodic_poller/load_average"
describe LogStash::Instrument::PeriodicPoller::LoadAverage do
subject { described_class.create }
context "on mocked system" do
context "on Linux" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(false)
expect(LogStash::Environment).to receive(:linux?).and_return(true)
end
context "when it can read the file" do
let(:proc_loadavg) { "0.00 0.01 0.05 3/180 29727" }
before do
expect(::File).to receive(:read).with("/proc/loadavg").and_return(proc_loadavg)
end
it "return the 3 load average from `/proc/loadavg`" do
avg_1m, avg_5m, avg_15m = proc_loadavg.chomp.split(" ")
expect(subject.get).to include(:"1m" => avg_1m.to_f, :"5m" => avg_5m.to_f, :"15m" => avg_15m.to_f)
end
end
end
context "on windows" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(true)
end
it "Xreturns nil" do
expect(subject.get).to be_nil
end
end
context "on other" do
before do
expect(LogStash::Environment).to receive(:windows?).and_return(false)
expect(LogStash::Environment).to receive(:linux?).and_return(false)
end
context "when 'OperatingSystemMXBean.getSystemLoadAverage' return something" do
let(:load_avg) { 5 }
before do
expect(ManagementFactory).to receive(:getOperatingSystemMXBean).and_return(double("OperatingSystemMXBean", :getSystemLoadAverage => load_avg))
end
it "returns the value" do
expect(subject.get).to include(:"1m" => 5)
end
end
context "when 'OperatingSystemMXBean.getSystemLoadAverage' doesn't return anything" do
before do
expect(ManagementFactory).to receive(:getOperatingSystemMXBean).and_return(double("OperatingSystemMXBean", :getSystemLoadAverage => nil))
end
it "returns nothing" do
expect(subject.get).to be_nil
end
end
end
end
# Since we are running this on macos and linux I think it make sense to have real test
# insteadof only mock
context "real system" do
if LogStash::Environment.linux?
context "Linux" do
it "returns the load avg" do
expect(subject.get).to include(:"1m" => a_kind_of(Numeric), :"5m" => a_kind_of(Numeric), :"15m" => a_kind_of(Numeric))
end
end
elsif LogStash::Environment.windows?
context "window" do
it "returns nothing" do
expect(subject.get).to be_nil
end
end
else
context "Other" do
it "returns 1m only" do
expect(subject.get).to include(:"1m" => a_kind_of(Numeric))
end
end
end
end
end