Merge pull request #87 from andeemarks/add-browser-config-test
Add browser config test
This commit is contained in:
commit
11df896e2b
4 changed files with 166 additions and 96 deletions
|
@ -1,7 +1,4 @@
|
|||
module Riemann::Dash::BrowserConfig
|
||||
require 'multi_json'
|
||||
require 'fileutils'
|
||||
require 'pp'
|
||||
|
||||
def self.backend
|
||||
@backend
|
||||
|
@ -11,50 +8,6 @@ module Riemann::Dash::BrowserConfig
|
|||
@backend = backend
|
||||
end
|
||||
|
||||
# TODO: this is gonna take significant restructuring of the dashboard itself,
|
||||
# but we should move to http://arxiv.org/abs/1201.1784 or equivalent CRDTs.
|
||||
|
||||
# Given a function to extract a key from an element, and a list of elements,
|
||||
# returns a map of keys to elements. Keys are assumed unique.
|
||||
def self.index_by(keyfn, list)
|
||||
list.reduce({}) do |index, element|
|
||||
index[keyfn.call(element)] = element
|
||||
index
|
||||
end
|
||||
end
|
||||
|
||||
# Merges two lists, given a key function which determines equivalent
|
||||
# elements, and a merge function to combine equivalent elements.
|
||||
def self.merge_lists(keyfn, mergefn, as, bs)
|
||||
asi = index_by keyfn, as
|
||||
bsi = index_by keyfn, bs
|
||||
ids = (as + bs).map(&keyfn).uniq.map do |key|
|
||||
mergefn.call asi[key], bsi[key]
|
||||
end
|
||||
end
|
||||
|
||||
# Merge two workspaces together
|
||||
def self.merge_workspace(a, b)
|
||||
# TODO: workspace versions
|
||||
return a unless b
|
||||
return b unless a
|
||||
if (a['view']['version'] || 0) < (b['view']['version'] || 0)
|
||||
b
|
||||
else
|
||||
a
|
||||
end
|
||||
end
|
||||
|
||||
# Merge a list of workspaces together
|
||||
def self.merge_workspaces(as, bs)
|
||||
return as unless bs
|
||||
return bs unless as
|
||||
merge_lists(lambda { |x| x['name'] },
|
||||
method(:merge_workspace),
|
||||
as,
|
||||
bs)
|
||||
end
|
||||
|
||||
# Merge two configs together
|
||||
def self.merge_configs(a, b)
|
||||
a.merge 'server' => (a['server'] or b['server']),
|
||||
|
@ -69,4 +22,49 @@ module Riemann::Dash::BrowserConfig
|
|||
def self.update(update)
|
||||
backend.update(update)
|
||||
end
|
||||
|
||||
# TODO: this is gonna take significant restructuring of the dashboard itself,
|
||||
# but we should move to http://arxiv.org/abs/1201.1784 or equivalent CRDTs.
|
||||
|
||||
# Given a function to extract a key from an element, and a list of elements,
|
||||
# returns a map of keys to elements. Keys are assumed unique.
|
||||
def self.index_by(keyfn, list)
|
||||
list.reduce({}) do |index, element|
|
||||
index[keyfn.call(element)] = element
|
||||
index
|
||||
end
|
||||
end
|
||||
|
||||
# Merges two lists, given a key function which determines equivalent
|
||||
# elements, and a merge function to combine equivalent elements.
|
||||
def self.merge_lists(keyfn, mergefn, as, bs)
|
||||
asi = index_by keyfn, as
|
||||
bsi = index_by keyfn, bs
|
||||
ids = (as + bs).map(&keyfn).uniq.map do |key|
|
||||
mergefn.call asi[key], bsi[key]
|
||||
end
|
||||
end
|
||||
|
||||
# Merge two workspaces together
|
||||
def self.merge_workspace(a, b)
|
||||
# TODO: workspace versions
|
||||
return a unless b
|
||||
return b unless a
|
||||
if (a['view']['version'] || 0) < (b['view']['version'] || 0)
|
||||
b
|
||||
else
|
||||
a
|
||||
end
|
||||
end
|
||||
|
||||
# Merge a list of workspaces together
|
||||
def self.merge_workspaces(as, bs)
|
||||
return as unless bs
|
||||
return bs unless as
|
||||
|
||||
merge_lists(lambda { |x| x['name'] },
|
||||
method(:merge_workspace),
|
||||
as,
|
||||
bs)
|
||||
end
|
||||
end
|
||||
|
|
120
test/browser_config_test.rb
Normal file
120
test/browser_config_test.rb
Normal file
|
@ -0,0 +1,120 @@
|
|||
require './test/test_helper'
|
||||
require 'pp'
|
||||
|
||||
describe "Riemann::Dash::BrowserConfig" do
|
||||
|
||||
before do
|
||||
@mock_backend = Minitest::Mock.new
|
||||
Riemann::Dash::BrowserConfig.backend = @mock_backend
|
||||
end
|
||||
|
||||
describe :read do
|
||||
it "delegates to its backend" do
|
||||
@mock_backend.expect :read, :return_value
|
||||
|
||||
Riemann::Dash::BrowserConfig.read
|
||||
|
||||
@mock_backend.verify
|
||||
end
|
||||
end
|
||||
|
||||
describe :update do
|
||||
it "delegates to its backend" do
|
||||
@mock_backend.expect :update, :return_value, [String]
|
||||
|
||||
Riemann::Dash::BrowserConfig.update("stuff to update")
|
||||
|
||||
@mock_backend.verify
|
||||
end
|
||||
end
|
||||
|
||||
describe :merge_configs do
|
||||
before do
|
||||
@first_config = {'server' => 'first_server', 'server_type' => 'first_type'}
|
||||
@second_config = {'server' => 'second_server', 'server_type' => 'second_type'}
|
||||
end
|
||||
|
||||
describe "when merging the server value" do
|
||||
it "prioritises the value from the first config" do
|
||||
merged_configs = Riemann::Dash::BrowserConfig.merge_configs(@first_config, @second_config)
|
||||
|
||||
assert_equal @first_config['server'], merged_configs['server']
|
||||
end
|
||||
|
||||
it "uses the value from the second config if no other exists" do
|
||||
merged_configs = Riemann::Dash::BrowserConfig.merge_configs({}, @second_config)
|
||||
|
||||
assert_equal @second_config['server'], merged_configs['server']
|
||||
end
|
||||
end
|
||||
|
||||
describe "when merging the server_type value" do
|
||||
it "prioritises the value from the first config" do
|
||||
merged_configs = Riemann::Dash::BrowserConfig.merge_configs(@first_config, @second_config)
|
||||
|
||||
assert_equal @first_config['server_type'], merged_configs['server_type']
|
||||
end
|
||||
|
||||
it "uses the value from the second config if no other exists" do
|
||||
merged_configs = Riemann::Dash::BrowserConfig.merge_configs({}, @second_config)
|
||||
|
||||
assert_equal @second_config['server_type'], merged_configs['server_type']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe :index_by do
|
||||
before do
|
||||
@list = [{'name' => 'a'}, {'name' => 'b'}, {'name' => 'c'}]
|
||||
end
|
||||
|
||||
it "returns the list of key/value pairs as a map indexed by the specified key/value" do
|
||||
indexed_config = Riemann::Dash::BrowserConfig.index_by(lambda { |x| x['name'] }, @list)
|
||||
|
||||
assert_equal({'name' => 'a'}, indexed_config['a'])
|
||||
assert_equal({'name' => 'b'}, indexed_config['b'])
|
||||
assert_equal({'name' => 'c'}, indexed_config['c'])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe :merge_workspace do
|
||||
before do
|
||||
@first_ws = {"view" => {"version" => 2}, "name" => "first"}
|
||||
@second_ws = {"view" => {"version" => 3}, "name" => "second"}
|
||||
end
|
||||
|
||||
it "prioritises the workspace with the higher version" do
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(@first_ws, @second_ws)
|
||||
assert_equal @second_ws, merged_workspace
|
||||
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(@second_ws, @first_ws)
|
||||
assert_equal @second_ws, merged_workspace
|
||||
end
|
||||
|
||||
it "prioritises any workspace over a nil workspace" do
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(@first_ws, nil)
|
||||
assert_equal @first_ws, merged_workspace
|
||||
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(nil, @first_ws)
|
||||
assert_equal @first_ws, merged_workspace
|
||||
end
|
||||
|
||||
it "prioritises any workspace with a version over a workspace without a version" do
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(@first_ws, {"view" => {}})
|
||||
assert_equal @first_ws, merged_workspace
|
||||
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace({"view" => {}}, @first_ws)
|
||||
assert_equal @first_ws, merged_workspace
|
||||
end
|
||||
|
||||
it "prioritises the first workspace if both versions are equal" do
|
||||
@second_ws['view']['version'] = @first_ws['view']['version']
|
||||
merged_workspace = Riemann::Dash::BrowserConfig.merge_workspace(@first_ws, @second_ws)
|
||||
assert_equal @first_ws, merged_workspace
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -62,53 +62,4 @@ describe "Riemann::Dash::Config" do
|
|||
end
|
||||
end
|
||||
|
||||
describe "workspace config" do
|
||||
before do
|
||||
FileUtils.rm_rf "test/tmp/"
|
||||
end
|
||||
|
||||
|
||||
describe :read_ws_config do
|
||||
=begin
|
||||
# this test fails if the config living at config/config.json has
|
||||
# been overwritten with user content -- this happens for people
|
||||
# who had previously run their riemann-dash instance via simply
|
||||
# cd riemann-dash && bin/riemann-dash -- it would also fail once
|
||||
# you save your config in the default location
|
||||
it "retuns hash for empty configs" do
|
||||
@config.read_ws_config.must_equal "{}"
|
||||
end
|
||||
=end
|
||||
|
||||
it "reads the file, if present" do
|
||||
@config.load_config("test/fixtures/config/ws_config.rb").must_equal true
|
||||
@config.store[:ws_config].must_equal "test/fixtures/ws_config/dummy_config.json"
|
||||
@config.read_ws_config.must_equal %Q{{hey: \"ho\"}}
|
||||
end
|
||||
end
|
||||
|
||||
describe :update_ws_config do
|
||||
it "works" do
|
||||
@config.store[:ws_config] = "test/tmp/config.json"
|
||||
@config.update_ws_config("{\"server\":\"10.10.10.10\",\"workspaces\":[]}")
|
||||
end
|
||||
|
||||
it "pretty-prints the config" do
|
||||
@config.store[:ws_config] = "test/tmp/config.json"
|
||||
@config.update_ws_config("{\"server\":\"10.10.10.10\",\"workspaces\":[]}")
|
||||
File.read("test/tmp/config.json").must_equal File.read("test/fixtures/ws_config/pretty_printed_config.json")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "backwards compatible :[] and :[]= forwarders to `store` variable" do
|
||||
it "reading works" do
|
||||
@config[:ws_config].must_match %r{config/config.json}
|
||||
end
|
||||
|
||||
it "writing works" do
|
||||
@config[:ws_config] = "something"
|
||||
@config[:ws_config].must_match %r{something}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ Bundler.setup(:default, :test)
|
|||
|
||||
require 'minitest/autorun'
|
||||
require 'minitest/spec'
|
||||
require 'minitest/mock'
|
||||
#require "mocha/setup"
|
||||
|
||||
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
||||
|
|
Loading…
Reference in a new issue