knife-essentials 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +114 -0
  3. data/Rakefile +24 -0
  4. data/lib/chef/knife/diff.rb +28 -0
  5. data/lib/chef/knife/list.rb +107 -0
  6. data/lib/chef/knife/show.rb +30 -0
  7. data/lib/chef_fs.rb +9 -0
  8. data/lib/chef_fs/command_line.rb +91 -0
  9. data/lib/chef_fs/diff.rb +158 -0
  10. data/lib/chef_fs/file_pattern.rb +292 -0
  11. data/lib/chef_fs/file_system.rb +69 -0
  12. data/lib/chef_fs/file_system/base_fs_dir.rb +23 -0
  13. data/lib/chef_fs/file_system/base_fs_object.rb +52 -0
  14. data/lib/chef_fs/file_system/chef_server_root_dir.rb +48 -0
  15. data/lib/chef_fs/file_system/cookbook_dir.rb +80 -0
  16. data/lib/chef_fs/file_system/cookbook_file.rb +32 -0
  17. data/lib/chef_fs/file_system/cookbook_subdir.rb +25 -0
  18. data/lib/chef_fs/file_system/cookbooks_dir.rb +21 -0
  19. data/lib/chef_fs/file_system/data_bag_dir.rb +22 -0
  20. data/lib/chef_fs/file_system/data_bags_dir.rb +23 -0
  21. data/lib/chef_fs/file_system/file_system_entry.rb +36 -0
  22. data/lib/chef_fs/file_system/file_system_root_dir.rb +11 -0
  23. data/lib/chef_fs/file_system/nonexistent_fs_object.rb +23 -0
  24. data/lib/chef_fs/file_system/not_found_exception.rb +12 -0
  25. data/lib/chef_fs/file_system/rest_list_dir.rb +42 -0
  26. data/lib/chef_fs/file_system/rest_list_entry.rb +72 -0
  27. data/lib/chef_fs/knife.rb +47 -0
  28. data/lib/chef_fs/path_utils.rb +43 -0
  29. data/lib/chef_fs/version.rb +4 -0
  30. data/spec/chef_fs/diff_spec.rb +263 -0
  31. data/spec/chef_fs/file_pattern_spec.rb +507 -0
  32. data/spec/chef_fs/file_system_spec.rb +117 -0
  33. data/spec/support/file_system_support.rb +108 -0
  34. metadata +79 -0
@@ -0,0 +1,117 @@
1
+ require 'support/file_system_support'
2
+ require 'chef_fs/file_system'
3
+ require 'chef_fs/file_pattern'
4
+
5
+ describe ChefFS::FileSystem do
6
+ include FileSystemSupport
7
+
8
+ context 'with empty filesystem' do
9
+ let(:fs) { memory_fs('', {}) }
10
+
11
+ context 'list' do
12
+ it '/' do
13
+ list_should_yield_paths(fs, '/', '/')
14
+ end
15
+ it '/a' do
16
+ list_should_yield_paths(fs, '/a', '/a')
17
+ end
18
+ it '/a/b' do
19
+ list_should_yield_paths(fs, '/a/b')
20
+ end
21
+ it '/*' do
22
+ list_should_yield_paths(fs, '/*', '/')
23
+ end
24
+ end
25
+
26
+ context 'resolve_path' do
27
+ it '/' do
28
+ ChefFS::FileSystem.resolve_path(fs, '/').path.should == '/'
29
+ end
30
+ it 'nonexistent /a' do
31
+ ChefFS::FileSystem.resolve_path(fs, '/a').path.should == '/a'
32
+ end
33
+ it 'nonexistent /a/b' do
34
+ ChefFS::FileSystem.resolve_path(fs, '/a/b').path.should == '/a/b'
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'with a populated filesystem' do
40
+ let(:fs) {
41
+ memory_fs('', {
42
+ :a => {
43
+ :aa => {
44
+ :c => '',
45
+ :zz => ''
46
+ },
47
+ :ab => {
48
+ :c => '',
49
+ }
50
+ },
51
+ :x => ''
52
+ })
53
+ }
54
+ context 'list' do
55
+ it '/**' do
56
+ list_should_yield_paths(fs, '/**', '/', '/a', '/x', '/a/aa', '/a/aa/c', '/a/aa/zz', '/a/ab', '/a/ab/c')
57
+ end
58
+ it '/' do
59
+ list_should_yield_paths(fs, '/', '/')
60
+ end
61
+ it '/*' do
62
+ list_should_yield_paths(fs, '/*', '/', '/a', '/x')
63
+ end
64
+ it '/*/*' do
65
+ list_should_yield_paths(fs, '/*/*', '/a/aa', '/a/ab')
66
+ end
67
+ it '/*/*/*' do
68
+ list_should_yield_paths(fs, '/*/*/*', '/a/aa/c', '/a/aa/zz', '/a/ab/c')
69
+ end
70
+ it '/*/*/?' do
71
+ list_should_yield_paths(fs, '/*/*/?', '/a/aa/c', '/a/ab/c')
72
+ end
73
+ it '/a/*/c' do
74
+ list_should_yield_paths(fs, '/a/*/c', '/a/aa/c', '/a/ab/c')
75
+ end
76
+ it '/**b/c' do
77
+ list_should_yield_paths(fs, '/**b/c', '/a/ab/c')
78
+ end
79
+ it '/a/ab/c' do
80
+ no_blocking_calls_allowed
81
+ list_should_yield_paths(fs, '/a/ab/c', '/a/ab/c')
82
+ end
83
+ it 'nonexistent /a/ab/blah' do
84
+ no_blocking_calls_allowed
85
+ list_should_yield_paths(fs, '/a/ab/blah', '/a/ab/blah')
86
+ end
87
+ it 'nonexistent /a/ab/blah/bjork' do
88
+ no_blocking_calls_allowed
89
+ list_should_yield_paths(fs, '/a/ab/blah/bjork')
90
+ end
91
+ end
92
+
93
+ context 'resolve_path' do
94
+ before(:each) do
95
+ no_blocking_calls_allowed
96
+ end
97
+ it 'resolves /' do
98
+ ChefFS::FileSystem.resolve_path(fs, '/').path.should == '/'
99
+ end
100
+ it 'resolves /x' do
101
+ ChefFS::FileSystem.resolve_path(fs, '/x').path.should == '/x'
102
+ end
103
+ it 'resolves /a' do
104
+ ChefFS::FileSystem.resolve_path(fs, '/a').path.should == '/a'
105
+ end
106
+ it 'resolves /a/aa' do
107
+ ChefFS::FileSystem.resolve_path(fs, '/a/aa').path.should == '/a/aa'
108
+ end
109
+ it 'resolves /a/aa/zz' do
110
+ ChefFS::FileSystem.resolve_path(fs, '/a/aa/zz').path.should == '/a/aa/zz'
111
+ end
112
+ it 'resolves nonexistent /y/x/w' do
113
+ ChefFS::FileSystem.resolve_path(fs, '/y/x/w').path.should == '/y/x/w'
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,108 @@
1
+ require 'chef_fs/file_system'
2
+ require 'chef_fs/file_system/base_fs_dir'
3
+ require 'chef_fs/file_system/base_fs_object'
4
+
5
+ module FileSystemSupport
6
+ class MemoryFile < ChefFS::FileSystem::BaseFSObject
7
+ def initialize(name, parent, value)
8
+ super(name, parent)
9
+ @value = value
10
+ end
11
+ def read
12
+ return @value
13
+ end
14
+ end
15
+
16
+ class MemoryDir < ChefFS::FileSystem::BaseFSDir
17
+ def initialize(name, parent)
18
+ super(name, parent)
19
+ @children = []
20
+ end
21
+ attr_reader :children
22
+ def child(name)
23
+ @children.select { |child| child.name == name }.first || ChefFS::FileSystem::NonexistentFSObject.new(name, self)
24
+ end
25
+ def add_child(child)
26
+ @children.push(child)
27
+ end
28
+ end
29
+
30
+ class MemoryRoot < MemoryDir
31
+ def initialize(pretty_name)
32
+ super('', nil)
33
+ @pretty_name = pretty_name
34
+ end
35
+
36
+ def path_for_printing
37
+ @pretty_name
38
+ end
39
+ end
40
+
41
+ def memory_fs(pretty_name, value)
42
+ if !value.is_a?(Hash)
43
+ raise "memory_fs() must take a Hash"
44
+ end
45
+ dir = MemoryRoot.new(pretty_name)
46
+ value.each do |key, child|
47
+ dir.add_child(memory_fs_value(child, key.to_s, dir))
48
+ end
49
+ dir
50
+ end
51
+
52
+ def memory_fs_value(value, name = '', parent = nil)
53
+ if value.is_a?(Hash)
54
+ dir = MemoryDir.new(name, parent)
55
+ value.each do |key, child|
56
+ dir.add_child(memory_fs_value(child, key.to_s, dir))
57
+ end
58
+ dir
59
+ else
60
+ MemoryFile.new(name, parent, value || "#{name}\n")
61
+ end
62
+ end
63
+
64
+ def pattern(p)
65
+ ChefFS::FilePattern.new(p)
66
+ end
67
+
68
+ def return_paths(*expected)
69
+ ReturnPaths.new(expected)
70
+ end
71
+
72
+ def no_blocking_calls_allowed
73
+ [ MemoryFile, MemoryDir ].each do |c|
74
+ [ :children, :exists?, :read ].each do |m|
75
+ c.any_instance.stub(m).and_raise("#{m.to_s} should not be called")
76
+ end
77
+ end
78
+ end
79
+
80
+ def diffable_leaves_should_yield_paths(a_root, b_root, recurse_depth, expected_paths)
81
+ result_paths = []
82
+ ChefFS::Diff.diffable_leaves(a_root, b_root, recurse_depth) do |a,b|
83
+ a.root.should == a_root
84
+ b.root.should == b_root
85
+ a.path.should == b.path
86
+ result_paths << a.path
87
+ end
88
+ result_paths.should =~ expected_paths
89
+ end
90
+
91
+ def diffable_leaves_from_pattern_should_yield_paths(pattern, a_root, b_root, recurse_depth, expected_paths)
92
+ result_paths = []
93
+ ChefFS::Diff.diffable_leaves_from_pattern(pattern, a_root, b_root, recurse_depth) do |a,b|
94
+ a.root.should == a_root
95
+ b.root.should == b_root
96
+ a.path.should == b.path
97
+ result_paths << a.path
98
+ end
99
+ result_paths.should =~ expected_paths
100
+ end
101
+
102
+ def list_should_yield_paths(fs, pattern_str, *expected_paths)
103
+ result_paths = []
104
+ ChefFS::FileSystem.list(fs, pattern(pattern_str)) { |result| result_paths << result.path }
105
+ result_paths.should =~ expected_paths
106
+ end
107
+ end
108
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-essentials
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Keiser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-26 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A library that treats the Chef server as if it were a filesystem
15
+ email: jkeiser@opscode.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - LICENSE
21
+ files:
22
+ - LICENSE
23
+ - README.rdoc
24
+ - Rakefile
25
+ - lib/chef/knife/diff.rb
26
+ - lib/chef/knife/list.rb
27
+ - lib/chef/knife/show.rb
28
+ - lib/chef_fs/command_line.rb
29
+ - lib/chef_fs/diff.rb
30
+ - lib/chef_fs/file_pattern.rb
31
+ - lib/chef_fs/file_system/base_fs_dir.rb
32
+ - lib/chef_fs/file_system/base_fs_object.rb
33
+ - lib/chef_fs/file_system/chef_server_root_dir.rb
34
+ - lib/chef_fs/file_system/cookbook_dir.rb
35
+ - lib/chef_fs/file_system/cookbook_file.rb
36
+ - lib/chef_fs/file_system/cookbook_subdir.rb
37
+ - lib/chef_fs/file_system/cookbooks_dir.rb
38
+ - lib/chef_fs/file_system/data_bag_dir.rb
39
+ - lib/chef_fs/file_system/data_bags_dir.rb
40
+ - lib/chef_fs/file_system/file_system_entry.rb
41
+ - lib/chef_fs/file_system/file_system_root_dir.rb
42
+ - lib/chef_fs/file_system/nonexistent_fs_object.rb
43
+ - lib/chef_fs/file_system/not_found_exception.rb
44
+ - lib/chef_fs/file_system/rest_list_dir.rb
45
+ - lib/chef_fs/file_system/rest_list_entry.rb
46
+ - lib/chef_fs/file_system.rb
47
+ - lib/chef_fs/knife.rb
48
+ - lib/chef_fs/path_utils.rb
49
+ - lib/chef_fs/version.rb
50
+ - lib/chef_fs.rb
51
+ - spec/chef_fs/diff_spec.rb
52
+ - spec/chef_fs/file_pattern_spec.rb
53
+ - spec/chef_fs/file_system_spec.rb
54
+ - spec/support/file_system_support.rb
55
+ homepage: http://www.opscode.com
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A library that treats the Chef server as if it were a filesystem
79
+ test_files: []