From 3cd4e7afdedffdfeec1235d29c00c27a1633e088 Mon Sep 17 00:00:00 2001 From: fabios Date: Tue, 9 Dec 2014 11:20:09 -0500 Subject: [PATCH] enable/disable cache --- Rakefile | 2 +- lib/hiera/backend/ic_yaml_backend.rb | 19 ++- lib/hiera/filenocache.rb | 30 +++++ spec/unit/backend/ic_yaml_backend_spec.rb | 18 +++ spec/unit/filenocache_spec.rb | 143 ++++++++++++++++++++++ 5 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 lib/hiera/filenocache.rb create mode 100644 spec/unit/filenocache_spec.rb diff --git a/Rakefile b/Rakefile index 008ad3b..c793762 100644 --- a/Rakefile +++ b/Rakefile @@ -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" diff --git a/lib/hiera/backend/ic_yaml_backend.rb b/lib/hiera/backend/ic_yaml_backend.rb index a761011..d09c636 100644 --- a/lib/hiera/backend/ic_yaml_backend.rb +++ b/lib/hiera/backend/ic_yaml_backend.rb @@ -1,3 +1,6 @@ +require 'hiera/filenocache' +require 'hiera/filecache' + class Hiera module Backend class Ic_yaml_backend @@ -5,7 +8,7 @@ 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() @@ -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() diff --git a/lib/hiera/filenocache.rb b/lib/hiera/filenocache.rb new file mode 100644 index 0000000..09e0c37 --- /dev/null +++ b/lib/hiera/filenocache.rb @@ -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 \ No newline at end of file diff --git a/spec/unit/backend/ic_yaml_backend_spec.rb b/spec/unit/backend/ic_yaml_backend_spec.rb index 0e4f4f8..66adceb 100644 --- a/spec/unit/backend/ic_yaml_backend_spec.rb +++ b/spec/unit/backend/ic_yaml_backend_spec.rb @@ -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 diff --git a/spec/unit/filenocache_spec.rb b/spec/unit/filenocache_spec.rb new file mode 100644 index 0000000..a3b8d39 --- /dev/null +++ b/spec/unit/filenocache_spec.rb @@ -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 \ No newline at end of file