Revision 4d5fc803
Added by Dmitri Dolguikh almost 9 years ago
test/puppet/puppet_cache_test.rb | ||
---|---|---|
require 'test_helper'
|
||
require 'puppet_proxy/puppet_plugin'
|
||
require 'puppet_proxy/puppet_class'
|
||
require 'puppet_proxy/class_scanner_base'
|
||
require 'puppet_proxy/puppet_cache'
|
||
require 'tmpdir'
|
||
|
||
class PuppetCacheTest < Test::Unit::TestCase
|
||
|
||
def setup
|
||
@scanner = Proxy::Puppet::ClassScannerBase.new
|
||
@scanner.stubs(:scan_manifest).returns([Proxy::Puppet::PuppetClass.new('testinclude')]).then.
|
||
returns([Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')])
|
||
Proxy::Puppet::PuppetCache.stubs(:write_to_cache)
|
||
returns([Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')])
|
||
end
|
||
|
||
def test_should_refresh_cache_when_dir_is_not_in_cache
|
||
Proxy::Puppet::PuppetCache.stubs(:read_from_cache).returns({})
|
||
Proxy::Puppet::PuppetCache.expects(:read_from_cache).returns({})
|
||
Proxy::Puppet::PuppetCache.expects(:write_to_cache).with(
|
||
{"./test/fixtures/modules_include" => {
|
||
"testinclude" => {
|
||
'timestamp' => Time.now.to_i,
|
||
'manifest' => [
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude')],
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')]]}
|
||
}}, "example_env")
|
||
|
||
cache = Proxy::Puppet::PuppetCache.scan_directory_with_cache('./test/fixtures/modules_include', 'example_env', @scanner)
|
||
|
||
assert_kind_of Array, cache
|
||
... | ... | |
end
|
||
|
||
def test_should_refresh_cache_when_dir_is_changed
|
||
mtime = File.mtime(Dir.glob('./test/fixtures/modules_include/*')[0])
|
||
mtime = File.mtime(Dir.glob('./test/fixtures/modules_include/*')[0]).to_i
|
||
|
||
Proxy::Puppet::PuppetCache.stubs(:read_from_cache).returns('./test/fixtures/modules_include' =>
|
||
{ 'testinclude' => { :timestamp => mtime - 1000,
|
||
:manifest => [[Proxy::Puppet::PuppetClass.new('test')],
|
||
[Proxy::Puppet::PuppetClass.new('test::sub::foo')]] }})
|
||
{ 'testinclude' => { 'timestamp' => (mtime - 1000),
|
||
'manifest' => [[Proxy::Puppet::PuppetClass.new('test')],
|
||
[Proxy::Puppet::PuppetClass.new('test::sub::foo')]] }})
|
||
Proxy::Puppet::PuppetCache.expects(:write_to_cache).with(
|
||
{"./test/fixtures/modules_include" => {
|
||
"testinclude" => {
|
||
'timestamp' => Time.now.to_i,
|
||
'manifest' => [
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude')],
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')]]}
|
||
}}, "example_env")
|
||
|
||
cache = Proxy::Puppet::PuppetCache.scan_directory_with_cache('./test/fixtures/modules_include', 'example_env', @scanner)
|
||
|
||
assert_kind_of Array, cache
|
||
... | ... | |
assert klass
|
||
end
|
||
|
||
def test_should_not_refresh_cache_when_cache_is_more_recent
|
||
def test_cache_file_should_contain_version
|
||
Dir.mktmpdir do |cache_dir|
|
||
Proxy::Puppet::Plugin.load_test_settings(:cache_location => cache_dir)
|
||
Proxy::Puppet::PuppetCache.write_to_cache({'./test/fixtures/modules_include' => {}}, 'production')
|
||
assert File.read(File.expand_path("cache_production.json", cache_dir)).include?("\"version\":\"#{Proxy::Puppet::Plugin.version}\"")
|
||
end
|
||
end
|
||
|
||
def test_should_return_empty_hash_when_cache_version_is_mismatched
|
||
Dir.mktmpdir do |cache_dir|
|
||
Proxy::Puppet::Plugin.load_test_settings(:cache_location => cache_dir)
|
||
Proxy::Puppet::PuppetCache.write_to_cache({'./test/fixtures/modules_include' =>
|
||
{ 'testinclude' => { 'timestamp' => Time.now.to_i,
|
||
'manifest' => [[Proxy::Puppet::PuppetClass.new('test')]] }}}, 'production')
|
||
|
||
Proxy::Puppet::Plugin.expects(:version).returns("0.0.0.0.1")
|
||
assert_equal({}, Proxy::Puppet::PuppetCache.read_from_cache('production'))
|
||
end
|
||
end
|
||
|
||
def test_should_not_refresh_cache_when_cache_is_more_recent
|
||
Proxy::Puppet::PuppetCache.stubs(:read_from_cache).returns('./test/fixtures/modules_include' =>
|
||
{ 'testinclude' => { :timestamp => Time.now,
|
||
:manifest => [[Proxy::Puppet::PuppetClass.new('test')],
|
||
[Proxy::Puppet::PuppetClass.new('test::sub::foo')]] }})
|
||
{ 'testinclude' => { 'timestamp' => Time.now.to_i,
|
||
'manifest' => [[Proxy::Puppet::PuppetClass.new('test')],
|
||
[Proxy::Puppet::PuppetClass.new('test::sub::foo')]] }})
|
||
Proxy::Puppet::PuppetCache.expects(:write_to_cache).never
|
||
cache = Proxy::Puppet::PuppetCache.scan_directory_with_cache('./test/fixtures/modules_include', 'example_env', @scanner)
|
||
|
||
assert_kind_of Array, cache
|
||
... | ... | |
klass = cache.find { |k| k.name == "test" }
|
||
assert klass
|
||
end
|
||
end
|
||
|
||
def test_read_write_cache_idempotency
|
||
Dir.mktmpdir do |cache_dir|
|
||
Proxy::Puppet::Plugin.load_test_settings(:cache_location => cache_dir)
|
||
|
||
expected = {'./test/fixtures/modules_include' =>
|
||
{'testinclude' => { 'timestamp' => Time.now.to_i,
|
||
'manifest' => [[Proxy::Puppet::PuppetClass.new('test')],
|
||
[Proxy::Puppet::PuppetClass.new('test::sub::foo')]] }}}
|
||
Proxy::Puppet::PuppetCache.write_to_cache(expected, 'production')
|
||
assert_equal expected, Proxy::Puppet::PuppetCache.read_from_cache('production')
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #10941: fixed puppet class caching