Skip to content

Commit

Permalink
enable/disable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fabios committed Dec 9, 2014
1 parent fbab777 commit 3cd4e7a
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'rspec/core/rake_task'

spec = Gem::Specification.new do |gem|
gem.name = "hiera-ic-yaml"
gem.version = "1.2.1"
gem.version = "1.3.0"
gem.summary = "ic yaml backend"
gem.email = "fabio.bat.silva@gmail.com"
gem.author = "Fabio B. Silva"
Expand Down
19 changes: 18 additions & 1 deletion lib/hiera/backend/ic_yaml_backend.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'hiera/filenocache'
require 'hiera/filecache'

class Hiera
module Backend
class Ic_yaml_backend
def initialize(cache=nil)
require 'yaml'
Hiera.debug("Hiera IC YAML backend starting")

@cache = cache || Filecache.new
@cache = cache || create_filecache()
end

def ic_yaml_config()
Expand Down Expand Up @@ -62,6 +65,20 @@ def imports_key()
return '__imports__'
end

def create_filecache()
config = ic_yaml_config()

if !config.has_key?('cacheable') && !config.has_key?(:cacheable)
return Hiera::Filecache.new
end

if config[:cacheable] || config[:cacheable]
return Hiera::Filecache.new
end

return Hiera::Filenocache.new
end

def parameters_key()
config = ic_yaml_config()

Expand Down
30 changes: 30 additions & 0 deletions lib/hiera/filenocache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Hiera
class Filenocache

def read(path, expected_type = Object, default=nil, &block)
read_file(path, expected_type, &block)
rescue TypeError => detail
Hiera.debug("#{detail.message}, setting defaults")
default
rescue => detail
error = "Reading data from #{path} failed: #{detail.class}: #{detail}"
if default.nil?
raise detail
else
Hiera.debug(error)
default
end
end

def read_file(path, expected_type = Object)
data = File.read(path)
result = block_given? ? yield(data) : data

if !result.is_a?(expected_type)
raise TypeError, "Data retrieved from #{path} is #{data.class} not #{expected_type}"
end

result
end
end
end
18 changes: 18 additions & 0 deletions spec/unit/backend/ic_yaml_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ def read_file(path, expected_type, &block)
end
end

describe "#create_filecache" do
it "should create Filecache by default" do
@backend.create_filecache().kind_of?(Filecache).should == true
end

it "should create Filecache when cacheable" do
Config[:ic_yaml][:cacheable] = true

@backend.create_filecache().kind_of?(Filecache).should == true
end

it "should create Filenocache when not cacheable" do
Config[:ic_yaml][:cacheable] = false

@backend.create_filecache().kind_of?(Filenocache).should == true
end
end

describe "#load_yaml_file" do

it "should load files" do
Expand Down
143 changes: 143 additions & 0 deletions spec/unit/filenocache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
require 'hiera/filenocache'
require 'spec_helper'
require 'tmpdir'

class Hiera
describe Filenocache do
before do
@cache = Filenocache.new
end

def write_file(file, contents)
File.open(file, 'w') do |f|
f.write(contents)
end
end

describe "#read" do
it "reads data from a file" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

@cache.read(file).should == "my data"
end
end

it "rereads data" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
@cache.read(file).should == "my data"

write_file(file, "changed data")
@cache.read(file).should == "changed data"
end
end

it "uses the provided default when the type does not match the expected type" do
Hiera.expects(:debug).with(regexp_matches(/String.*not.*Hash, setting defaults/))
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
data = @cache.read(file, Hash, { :testing => "hash" }) do |data|
"a string"
end

data.should == { :testing => "hash" }
end
end

it "traps any errors from the block and uses the default value" do
Hiera.expects(:debug).with(regexp_matches(/Reading data.*failed:.*testing error/))
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
data = @cache.read(file, Hash, { :testing => "hash" }) do |data|
raise ArgumentError, "testing error"
end

data.should == { :testing => "hash" }
end
end

it "raises an error when there is no default given and there is a problem" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

expect do
@cache.read(file, Hash) do |data|
raise ArgumentError, "testing error"
end
end.to raise_error(ArgumentError, "testing error")
end
end
end

describe "#read_file" do
it "reads data from a file" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

@cache.read_file(file).should == "my data"
end
end

it "rereads data" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
@cache.read_file(file).should == "my data"

write_file(file, "changed data")
@cache.read_file(file).should == "changed data"
end
end

it "errors when the type does not match the expected type" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

expect do
@cache.read_file(file, Hash) do |data|
"a string"
end
end.to raise_error(TypeError)
end
end

it "converts the read data using the block" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

@cache.read_file(file, Hash) do |data|
{ :data => data }
end.should == { :data => "my data" }
end
end

it "errors when the file does not exist" do
expect do
@cache.read_file("/notexist")
end.to raise_error(Errno::ENOENT)
end

it "propogates any errors from the block" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")

expect do
@cache.read_file(file) do |data|
raise ArgumentError, "testing error"
end
end.to raise_error(ArgumentError, "testing error")
end
end
end
end
end

0 comments on commit 3cd4e7a

Please sign in to comment.