searchef 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in searchef.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Fletcher Nichol
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ # Searchef
2
+
3
+ Stub your Chef searches with pre-canned responses. Good for use when unit
4
+ testing or dummying out a Chef run that uses search calls.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'searchef'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install searchef
19
+
20
+ ## Usage
21
+
22
+ Require the library and include the `Searchef::API` module for convenience:
23
+
24
+ ```ruby
25
+ require 'searchef'
26
+
27
+ include Searchef::API
28
+ ```
29
+
30
+ ```ruby
31
+ # stub search for all nodes with the `web_node` role in their run list
32
+ stub_search(:node, "roles:web_node").to_return([
33
+ node_stub("web1.example.com"),
34
+ node_stub("web2.example.com")
35
+ ])
36
+
37
+ require 'chef/search/query'
38
+
39
+ # if running in pry, let's set a node name for signing requests
40
+ Chef::Config[:node_name] = "durr"
41
+
42
+ # run the search
43
+ query = Chef::Search::Query.new
44
+ query.search(:node, "roles:web_node")
45
+
46
+ # => [[node[web1.example.com], node[web2.example.com]], 0, 2]
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
4
+ #
5
+ # Copyright (C) 2013, Fletcher Nichol
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'searchef/api'
20
+ require 'searchef/node_stub_creator'
21
+ require 'searchef/search_stub'
22
+ require "searchef/version"
23
+
24
+ module Searchef
25
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
4
+ #
5
+ # Copyright (C) 2013, Fletcher Nichol
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ module Searchef
20
+
21
+ module API
22
+
23
+ extend self
24
+
25
+ def stub_search(type, query = nil, sort = nil, start = nil, rows = nil, &block)
26
+ SearchStub.new(type, query, sort, start, rows)
27
+ end
28
+
29
+ def node_stub(name, options = {}, &block)
30
+ NodeStubCreator.new(name, options, &block).create
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
4
+ #
5
+ # Copyright (C) 2013, Fletcher Nichol
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'chef/platform'
20
+ require 'chef/node'
21
+ require 'fauxhai'
22
+
23
+ module Searchef
24
+
25
+ class NodeStubCreator
26
+
27
+ def initialize(name, options = {}, &block)
28
+ @name = name
29
+ @attrs = options[:attrs] || Hash.new
30
+ @ohai = options[:ohai] || Hash.new
31
+ @platform = options[:platform] || "ubuntu"
32
+ @version = options[:version] || "12.04"
33
+ @node_block = block if block_given?
34
+ end
35
+
36
+ def create
37
+ node = Chef::Node.new
38
+ node.name(name)
39
+ node.consume_external_attrs(ohai_data, attr_data)
40
+ node.instance_eval(&node_block) unless node_block.nil?
41
+ node
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :name, :attrs, :ohai, :platform, :version, :node_block
47
+
48
+ def ohai_data
49
+ fauxhai = Fauxhai.mock(:platform => platform, :version => version).data
50
+ Mash.new(fauxhai.merge(ohai))
51
+ end
52
+
53
+ def attr_data
54
+ Mash.new(attrs)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
4
+ #
5
+ # Copyright (C) 2013, Fletcher Nichol
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'webmock'
20
+ require 'uri'
21
+
22
+ module Searchef
23
+
24
+ class SearchStub
25
+
26
+ include WebMock::API
27
+
28
+ def initialize(type, query, sort, start, rows)
29
+ @type = type
30
+ @params = Hash.new
31
+ @params["q"] = query if query
32
+ @params["sort"] = sort if sort
33
+ @params["start"] = start.to_s if start
34
+ @params["rows"] = rows.to_s if rows
35
+ end
36
+
37
+ def to_return(objects)
38
+ stub_request(:get, %r{/search/#{type}}).
39
+ with(:query => hash_including(params)).
40
+ to_return(:headers => response_headers, :body => response_body(objects))
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :type, :params
46
+
47
+ def escape(string)
48
+ string && URI.escape(string.to_s)
49
+ end
50
+
51
+ def response_headers
52
+ { 'Content-Type' => 'application/json; charset=utf-8' }
53
+ end
54
+
55
+ def response_body(objects)
56
+ # calling Arrray(objects) converts Chef::Node to an Array of attributes
57
+ # which is not what we want
58
+ objects = [objects] unless objects.is_a?(Array)
59
+
60
+ response = Hash.new
61
+ response["rows"] = objects
62
+ response["total"] = response["rows"].length
63
+ response["start"] = 0
64
+ response.to_json
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
4
+ #
5
+ # Copyright (C) 2013, Fletcher Nichol
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ module Searchef
20
+ VERSION = "0.1.0"
21
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'searchef/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "searchef"
8
+ gem.version = Searchef::VERSION
9
+ gem.authors = ["Fletcher Nichol"]
10
+ gem.email = ["fnichol@nichol.ca"]
11
+ gem.description = %q{Stub Chef Search!}
12
+ gem.summary = gem.description
13
+ gem.homepage = "https://github.com/fnichol/searchef"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = []
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'chef'
21
+ gem.add_runtime_dependency 'webmock'
22
+ gem.add_runtime_dependency 'fauxhai'
23
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: searchef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fletcher Nichol
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: webmock
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: fauxhai
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Stub Chef Search!
63
+ email:
64
+ - fnichol@nichol.ca
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/searchef.rb
75
+ - lib/searchef/api.rb
76
+ - lib/searchef/node_stub_creator.rb
77
+ - lib/searchef/search_stub.rb
78
+ - lib/searchef/version.rb
79
+ - searchef.gemspec
80
+ homepage: https://github.com/fnichol/searchef
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: 2900810078172785449
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 2900810078172785449
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.24
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Stub Chef Search!
110
+ test_files: []