Skip to content

Commit 8862692

Browse files
committed
added fetch functionality. no longer in alpha status.
1 parent afcf3e3 commit 8862692

File tree

9 files changed

+105
-17
lines changed

9 files changed

+105
-17
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2012 CustomInk
1+
Copyright (c) 2012 Seth Vargo and CustomInk, LCC
22

33
MIT License
44

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require 'chefspec'
2626

2727
describe 'awesome_cookbook::default' do
2828
before do
29-
Fauxhai.mock(url:'server01.example.com')
29+
Fauxhai.fetch(host:'server01.example.com')
3030
end
3131

3232
it 'should install awesome' do
@@ -78,12 +78,16 @@ The `node` block variable allows you to set any Ohai attribute on the mock that
7878
### Fetching
7979
Alternatively, if you do not want to mock the data, Fauxhai provides a `fetch` mechanism for collecting "real" ohai data from a remote server or local file. Maybe you want to test against the fully-replicated environment for a front-facing server in your pool. Just pass in the `url` option instead of a `platform`:
8080

81+
The `fetch` method supports all the same options as the Net-SSH command, such as `:user`, `:password`, `:key_file`, etc.
82+
83+
The `fetch` method will cache the JSON file in a temporary path on your local machine. Similar to gems like VCR, this allows fauxhai to use the cached copy, making your test suite run faster. You can optionally force a cache miss by passing the `:force_cache_miss => true` option to the `fetch` initializer. **Because this is real data, there may be a security concern. Secure your laptop accordingly.**
84+
8185
```ruby
8286
require 'chefspec'
8387

8488
describe 'awesome_cookbook::default' do
8589
before do
86-
Fauxhai.mock(url:'server01.example.com')
90+
Fauxhai.fetch(host:'server01.example.com')
8791
end
8892

8993
it 'should install awesome' do
@@ -103,7 +107,7 @@ require 'chefspec'
103107

104108
describe 'awesome_cookbook::default' do
105109
before do
106-
Fauxhai.mock(url:'server01.example.com') do |node|
110+
Fauxhai.fetch(host:'server01.example.com') do |node|
107111
node['languages']['ruby']['version'] = 'ree'
108112
end
109113
end

examples/chefspec.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ require 'chefspec'
4848

4949
describe 'awesome_cookbook::default' do
5050
before do
51-
Fauxhai.mock(url:'server01.example.com')
51+
Fauxhai.fetch(host:'server01.example.com')
5252
end
5353
end
5454
```
@@ -60,7 +60,7 @@ require 'chefspec'
6060

6161
describe 'awesome_cookbook::default' do
6262
before do
63-
Fauxhai.mock(url:'server01.example.com') do |node|
63+
Fauxhai.fetch(host:'server01.example.com') do |node|
6464
node['languages']['ruby']['version'] = 'ree'
6565
end
6666
end

examples/rspec-chef.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Fetching a Remote Box
4545
---------------------
4646
```ruby
4747
describe 'foo::bar' do
48-
let(:json_attributes) { Fauxhai.mock(url:'server01.example.com' }
48+
let(:json_attributes) { Fauxhai.fetch(host:'server01.example.com' }
4949
end
5050
```
5151

@@ -54,7 +54,7 @@ Fetching a Remote Box with custom Ruby Version
5454
```ruby
5555
describe 'foo::bar' do
5656
let(:json_attributes) do
57-
Fauxhai.mock(url:'server01.example.com') do |node|
57+
Fauxhai.fetch(host:'server01.example.com') do |node|
5858
node['languages']['ruby']['version'] = 'ree'
5959
end
6060
end

fauxhai.gemspec

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ lib = File.expand_path('../lib', __FILE__)
22
$:.unshift(lib) unless $:.include?(lib)
33

44
Gem::Specification.new do |gem|
5-
gem.version = '0.0.2.alpha'
6-
gem.authors = ["Seth Vargo"]
7-
gem.email = ["[email protected]"]
5+
gem.version = '0.0.1'
6+
gem.authors = ['Seth Vargo']
7+
gem.email = ['[email protected]']
88
gem.description = %q{Easily mock out ohai data}
99
gem.summary = %q{Fauxhai provides an easy way to mock out your ohai data for testing with chefspec!}
10-
gem.homepage = ""
10+
gem.homepage = 'https://github.com/customink/fauxhai'
1111

1212
gem.files = `git ls-files`.split($\)
1313
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
1414
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15-
gem.name = "fauxhai"
16-
gem.require_paths = ["lib"]
15+
gem.name = 'fauxhai'
16+
gem.require_paths = ['lib']
1717

1818
gem.add_runtime_dependency 'chef'
19+
gem.add_runtime_dependency 'net-ssh'
1920
end

lib/fauxhai.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
module Fauxhai
22
require 'fauxhai/exception'
3-
require 'fauxhai/mock'
3+
require 'fauxhai/fetcher'
4+
require 'fauxhai/mocker'
45

56
def self.root
67
@@root ||= File.expand_path('../../', __FILE__)
78
end
89

910
def self.mock(*args)
10-
Fauxhai::Mock.new(*args)
11+
Fauxhai::Mocker.new(*args)
12+
end
13+
14+
def self.fetch(*args)
15+
Fauxhai::Fetcher.new(*args)
1116
end
1217
end

lib/fauxhai/fetcher.rb

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'digest/sha1'
2+
require 'json'
3+
require 'net/ssh'
4+
5+
module Fauxhai
6+
class Fetcher
7+
def initialize(options = {}, &override_attributes)
8+
@options = options
9+
10+
if !force_cache_miss? && cached?
11+
@data = cache
12+
else
13+
Net::SSH.start(host, user, @options) do |ssh|
14+
@data = JSON.parse(ssh.exec!('ohai'))
15+
end
16+
17+
# cache this data so we do not have to SSH again
18+
File.open(cache_file, 'w+'){ |f| f.write(@data.to_json) }
19+
end
20+
21+
yield(@data) if block_given?
22+
23+
if defined?(ChefSpec)
24+
data = @data
25+
::ChefSpec::ChefRunner.send :define_method, :fake_ohai do |ohai|
26+
data.each_pair do |attribute,value|
27+
ohai[attribute] = value
28+
end
29+
end
30+
end
31+
32+
@data
33+
end
34+
35+
def cache
36+
@cache ||= JSON.parse(File.read(cache_file))
37+
end
38+
39+
def cached?
40+
File.exists?(cache_file)
41+
end
42+
43+
def cache_key
44+
Digest::SHA2.hexdigest("#{user}@#{host}")
45+
end
46+
47+
def cache_file
48+
File.expand_path( File.join(Fauxhai.root, 'tmp', cache_key) )
49+
end
50+
51+
def force_cache_miss?
52+
@force_cache_miss ||= @options.delete(:force_cache_miss) || false
53+
end
54+
55+
# Return the given `@data` attribute as a Ruby hash instead of a JSON object
56+
#
57+
# @return [Hash] the `@data` represented as a Ruby hash
58+
def to_hash(*args)
59+
@data.to_hash(*args)
60+
end
61+
62+
def to_s
63+
"#<Fauxhai::Fetcher @host=#{host}, @options=#{@options}>"
64+
end
65+
66+
private
67+
def host
68+
@host ||= begin
69+
raise ArgumentError, ':host is a required option for Fauxhai.fetch' unless @options[:host]
70+
@options.delete(:host)
71+
end
72+
end
73+
74+
def user
75+
@user ||= (@options.delete(:user) || ENV['USER'] || ENV['USERNAME']).chomp
76+
end
77+
end
78+
end

lib/fauxhai/mock.rb lib/fauxhai/mocker.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'json'
22

33
module Fauxhai
4-
class Mock
4+
class Mocker
55
def initialize(options = {}, &override_attributes)
66
@options = options
77

tmp/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)