Skip to content

Commit

Permalink
Add integration tests for vmdb/loggers.rb
Browse files Browse the repository at this point in the history
Adds specs for each of the global loggers around the multicast logger
based loggers.

Written in a way where we should be able to swap out what we are using
under the hood for Multicast logger, and the tests should be a litmus
test if we are doing everything correct.  Borrowed a decent number of
tests from lib/vmdb/loggers/multicast_logger_spec.rb since that has the
potential to go away, but we want to test the functionality from that
spec file on all of the existing loggers.

Hopefully most of the specs here are idempotent, but we will see.
  • Loading branch information
NickLaMuro committed Mar 28, 2018
1 parent 26ed45e commit 26d625a
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions spec/lib/vmdb/loggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,131 @@ def in_container_env(example)
ENV['CONTAINER'] = old_env
end

shared_examples "has basic logging functionality" do
let(:logger1) { subject.loggers.first }
let(:logger2) { subject.loggers.to_a.last }

around do |example|
has_old_container_log = true
unless subject.loggers.include?($container_log)
has_old_container_log = false
subject.loggers << $container_log
end
example.run
subject.loggers.delete($container_log) unless has_old_container_log
end

it "responds to #<<" do
expect(subject.respond_to?(:<<)).to be true
end

it "responds to #debug" do
expect(subject.respond_to?(:debug)).to be true
end

it "responds to #info" do
expect(subject.respond_to?(:warn)).to be true
end

it "responds to #error" do
expect(subject.respond_to?(:error)).to be true
end

it "responds to #fatal" do
expect(subject.respond_to?(:fatal)).to be true
end

it "responds to #unknown" do
expect(subject.respond_to?(:unknown)).to be true
end

describe "#datetime_format" do
it "works as expected" do
expect(subject.datetime_format).to be nil
end
end

# Ripped from lib/vmdb/loggers/multicast_logger_spec.rb, but adapted used
# in a integration spec fashion to test against all of the configured
# loggers, and be idempotent with any of the settings that affect the
# levels and number of loggers (always have the $container_log attached to
# each of the logs tested here).
context "#add" do
context "for info logger levels" do
around do |example|
old_subject_level, subject.level = subject.level, 1
example.run
subject.level = old_subject_level
end

it "forwards to the other loggers" do
expect(logger1).to receive(:add).with(1, nil, "test message")
expect(logger2).to receive(:add).with(1, nil, "test message")

subject.info("test message")
end
end

context "for higher logger levels" do
around do |example|
old_subject_level, subject.level = subject.level, 1
old_logger_1_level, logger1.level = logger1.level, 0
example.run
subject.level = old_subject_level
logger1.level = old_logger_1_level
end

it "only forwards the message if the severity is correct" do
[logger1, logger2].each { |l| expect(l).not_to receive(:add) }

subject.debug("test message")
end
end
end

context "#level=" do
around do |example|
old_subject_level, subject.level = subject.level, 0
example.run
subject.level = old_subject_level
end

it "updates the log level on all backing devices" do
expect(logger1.level).to eq(0)
expect(logger2.level).to eq(0)
expect(subject.level).to eq(0)
subject.level = 3
expect(logger1.level).to eq(3)
expect(logger2.level).to eq(0) # $container_log is always DEBUG
expect(subject.level).to eq(3)
end
end

context "#<<" do
it "forwards to the other loggers" do
expect(logger1).to receive(:<<).with("test message")
expect(logger2).to receive(:<<).with("test message")

subject << "test message"
end
end
end

context "for all multicast loggers" do
[
$log, $rails_log, $api_log, $miq_ae_logger, $aws_log, $azure_log,
$cn_monitoring_log, $datawarehouse_log, $fog_log, $kube_log, $lenovo_log,
$nuage_log, $policy_log, $rhevm_log, $scvmm_log, $vcloud_log, $vim_log,
$websocket_log
].each do |logger|
context "for the #{File.basename(logger.filename)}" do
subject { logger }

include_examples "has basic logging functionality"
end
end
end

describe "#create_multicast_logger (private)" do
it "defaults the lower level loggers to `DEBUG`" do
log = described_class.send(:create_multicast_logger, log_file)
Expand Down

0 comments on commit 26d625a

Please sign in to comment.