Revision e86db516
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')])
|
||
@classes_cache = ::Proxy::Puppet::MemoryStore.new
|
||
@timestamps = ::Proxy::Puppet::MemoryStore.new
|
||
@scanner = ::Proxy::Puppet::PuppetCache.new(::Proxy::Puppet::ClassScanner, @classes_cache, @timestamps)
|
||
end
|
||
|
||
def test_should_refresh_cache_when_dir_is_not_in_cache
|
||
timestamp = Time.new
|
||
Proxy::Puppet::PuppetCache.expects(:read_from_cache).returns({})
|
||
Proxy::Puppet::PuppetCache.expects(:write_to_cache).with(
|
||
{"./test/fixtures/modules_include" => {
|
||
"testinclude" => {
|
||
'timestamp' => timestamp.to_i,
|
||
'manifest' => [
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude')],
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')]]}
|
||
}}, "example_env")
|
||
Time.expects(:new).returns(timestamp)
|
||
|
||
cache = Proxy::Puppet::PuppetCache.scan_directory_with_cache('./test/fixtures/modules_include', 'example_env', @scanner)
|
||
def test_should_refresh_classes_cache_when_dir_is_not_in_cache
|
||
@scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
|
||
assert_kind_of Array, cache
|
||
assert_equal 2, cache.size
|
||
assert_equal Proxy::Puppet::PuppetClass.new('testinclude'),
|
||
@classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'].first
|
||
assert_equal Proxy::Puppet::PuppetClass.new('testinclude::sub::foo', 'param1' => 'first_parameter', 'param2' => 'second_parameter'),
|
||
@classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/sub/foo.pp'].first
|
||
assert_equal 2, @classes_cache.values('./test/fixtures/modules_include').size
|
||
end
|
||
|
||
klass = cache.find { |k| k.name == "sub::foo" }
|
||
assert cache
|
||
assert_equal "testinclude", klass.module
|
||
def test_should_refresh_timestamps_when_dir_is_not_in_cache
|
||
@scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
|
||
klass = cache.find { |k| k.name == "testinclude" }
|
||
assert klass
|
||
assert @timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/sub/foo.pp']
|
||
assert @timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp']
|
||
assert_equal 2, @timestamps.values('./test/fixtures/modules_include').size
|
||
end
|
||
|
||
def test_should_refresh_cache_when_dir_is_changed
|
||
mtime = File.mtime(Dir.glob('./test/fixtures/modules_include/*')[0]).to_i
|
||
timestamp = Time.new
|
||
|
||
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')]] }})
|
||
Proxy::Puppet::PuppetCache.expects(:write_to_cache).with(
|
||
{"./test/fixtures/modules_include" => {
|
||
"testinclude" => {
|
||
'timestamp' => timestamp.to_i,
|
||
'manifest' => [
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude')],
|
||
[::Proxy::Puppet::PuppetClass.new('testinclude::sub::foo')]]}
|
||
}}, "example_env")
|
||
Time.expects(:new).returns(timestamp)
|
||
|
||
cache = Proxy::Puppet::PuppetCache.scan_directory_with_cache('./test/fixtures/modules_include', 'example_env', @scanner)
|
||
def test_scan_directory_response
|
||
cache = @scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
|
||
assert_kind_of Array, cache
|
||
assert_equal 2, cache.size
|
||
|
||
klass = cache.find { |k| k.name == "sub::foo" }
|
||
assert cache
|
||
assert_equal "testinclude", klass.module
|
||
|
||
klass = cache.find { |k| k.name == "testinclude" }
|
||
assert klass
|
||
end
|
||
|
||
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
|
||
assert_equal "sub::foo", klass.name
|
||
assert_equal({'param1' => 'first_parameter', 'param2' => 'second_parameter'}, klass.params)
|
||
assert cache.find { |k| k.name == "testinclude" }
|
||
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
|
||
def test_should_refresh_cache_when_dir_is_changed
|
||
@classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] =
|
||
[Proxy::Puppet::PuppetClass.new('testinclude'),
|
||
Proxy::Puppet::PuppetClass.new('another_testinclude'),
|
||
Proxy::Puppet::PuppetClass.new('yet_another_testinclude')]
|
||
@timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] =
|
||
1000
|
||
assert_equal 3, @classes_cache.values('./test/fixtures/modules_include').size
|
||
|
||
@scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
|
||
assert_equal 2, @classes_cache.values('./test/fixtures/modules_include').size
|
||
assert_equal 2, @timestamps.values('./test/fixtures/modules_include').size
|
||
assert @timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] != 1000
|
||
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.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)
|
||
def test_should_detect_module_removals
|
||
@classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/removed_testinclude/manifests/init.pp'] =
|
||
[Proxy::Puppet::PuppetClass.new('testinclude')]
|
||
@timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/removed_testinclude/manifests/init.pp'] =
|
||
Time.now.to_i + 10_000
|
||
assert @classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/removed_testinclude/manifests/init.pp']
|
||
|
||
assert_kind_of Array, cache
|
||
# reading from the cache returns two puppet classes
|
||
assert_equal 2, cache.size
|
||
@scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
|
||
klass = cache.find { |k| k.name == "sub::foo" }
|
||
assert cache
|
||
assert_equal "test", klass.module
|
||
assert_nil @classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/removed_testinclude/manifests/init.pp']
|
||
assert_nil @timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/removed_testinclude/manifests/init.pp']
|
||
assert @classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp']
|
||
assert @classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/sub/foo.pp']
|
||
end
|
||
|
||
klass = cache.find { |k| k.name == "test" }
|
||
assert klass
|
||
def test_should_not_refresh_cache_when_cache_is_more_recent
|
||
@classes_cache['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] =
|
||
[Proxy::Puppet::PuppetClass.new('testinclude'),
|
||
Proxy::Puppet::PuppetClass.new('another_testinclude'),
|
||
Proxy::Puppet::PuppetClass.new('yet_another_testinclude')]
|
||
@timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] =
|
||
(current_time = Time.now.to_i + 10_000)
|
||
assert_equal 3, @classes_cache.values('./test/fixtures/modules_include').size
|
||
|
||
@scanner.scan_directory('./test/fixtures/modules_include', 'example_env')
|
||
assert_equal 4, @classes_cache.values('./test/fixtures/modules_include').size
|
||
assert_equal 2, @timestamps.values('./test/fixtures/modules_include').size
|
||
assert @timestamps['./test/fixtures/modules_include', './test/fixtures/modules_include/testinclude/manifests/init.pp'] == current_time
|
||
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
|
||
def test_should_return_no_puppet_classes_when_environment_has_no_modules
|
||
Dir.expects(:glob).with('empty_environment/*').returns([])
|
||
result = @scanner.scan_directory('empty_environment', 'example_env')
|
||
|
||
assert result.empty?
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #11229: changes in puppet modules are now being detected when listing available puppet classes