Skip to content

Commit

Permalink
add lots'o specs to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Ryan committed Jan 10, 2011
1 parent 1a0a14d commit 3006bd3
Show file tree
Hide file tree
Showing 27 changed files with 686 additions and 45 deletions.
64 changes: 61 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
PATH
remote: .
specs:
spice (0.0.1.alpha.2)
spice (0.0.1.beta1)
mixlib-authentication
rest-client
yajl-ruby

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
addressable (2.2.2)
builder (3.0.0)
bunny (0.6.0)
chef (0.9.12)
bunny (>= 0.6.0)
erubis
extlib
highline
json (>= 1.4.4, <= 1.4.6)
mixlib-authentication (>= 1.1.0)
mixlib-cli (>= 1.1.0)
mixlib-config (>= 1.1.2)
mixlib-log (>= 1.2.0)
moneta
ohai (>= 0.5.7)
rest-client (>= 1.0.4, < 1.7.0)
uuidtools
crack (0.1.8)
cucumber (0.10.0)
builder (>= 2.1.2)
diff-lcs (~> 1.1.2)
gherkin (~> 2.3.2)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
diff-lcs (1.1.2)
fakeweb (1.3.0)
erubis (2.6.6)
abstract (>= 1.0.0)
extlib (0.9.15)
forgery (0.3.6)
gherkin (2.3.2)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
highline (1.6.1)
json (1.4.6)
mime-types (1.16)
mixlib-authentication (1.1.4)
mixlib-log
mixlib-cli (1.2.0)
mixlib-config (1.1.2)
mixlib-log (1.2.0)
moneta (0.6.0)
ohai (0.5.8)
extlib
json (>= 1.4.4, <= 1.4.6)
mixlib-cli
mixlib-config
mixlib-log
systemu
rest-client (1.6.1)
mime-types (>= 1.16)
rspec (2.4.0)
Expand All @@ -25,15 +68,30 @@ GEM
rspec-expectations (2.4.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.4.0)
systemu (1.2.0)
term-ansicolor (1.0.5)
timecop (0.3.5)
uuidtools (2.1.1)
vcr (1.4.0)
webmock (1.6.1)
addressable (>= 2.2.2)
crack (>= 0.1.7)
yajl-ruby (0.7.8)
yard (0.6.4)

PLATFORMS
ruby

DEPENDENCIES
fakeweb (~> 1.3.0)
chef (= 0.9.12)
cucumber (~> 0.10.0)
forgery (~> 0.3.6)
mixlib-authentication
rest-client
rspec (~> 2.4.0)
spice!
timecop (~> 0.3.5)
vcr (~> 1.4.0)
webmock (~> 1.6.1)
yajl-ruby
yard (~> 0.6.4)
19 changes: 19 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:cucumber)

task :default => :spec

require 'yard'
YARD::Rake::YardocTask.new
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Allow configurable variables instead of relying on class variables
* Refactor classes that inherity from Spice::Chef to take config values
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
require 'spice'

require 'rspec/expectations'
require 'webmock/cucumber'
79 changes: 79 additions & 0 deletions lib/spice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,83 @@
require 'spice/connection'

module Spice
class << self
attr_writer :host, :port, :scheme, :client_name, :connection, :key_file, :raw_key

def default_host
@default_host || "localhost"
end

def default_port
@default_port || "4000"
end

def default_scheme
@default_scheme || "http"
end

def host
@host || default_host
end

def port
@port || default_port
end

def scheme
@scheme || default_scheme
end

def client_name
@client_name
end

def key_file
@key_file
end

def raw_key
@raw_key
end

def key_file=(new_key_file)
raw_key = File.read(new_key_file)
assert_valid_key_format!(raw_key)
@key_file = new_key_file
@raw_key = raw_key
end

def connection
@connection
end

def connect!
@connection = Connection.new(
:url => "#{scheme}://#{host}:#{port}",
:client_name => client_name,
:key_file => key_file
)
end


def reset!
@host = default_host
@port = default_port
@scheme = default_scheme
@key_file = nil
@client_name = nil
@connection = nil
end

private

def assert_valid_key_format!(raw_key)
unless (raw_key =~ /\A-----BEGIN RSA PRIVATE KEY-----$/) &&
(raw_key =~ /^-----END RSA PRIVATE KEY-----\Z/)
msg = "The file #{key_file} does not contain a correctly formatted private key.\n"
msg << "The key file should begin with '-----BEGIN RSA PRIVATE KEY-----' and end with '-----END RSA PRIVATE KEY-----'"
raise ArgumentError, msg
end
end
end
end
2 changes: 1 addition & 1 deletion lib/spice/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def signature_headers(request_params={})

def load_signing_key
begin
@raw_key = IO.read(key_file, :mode => "r").strip
@raw_key = File.read(key_file).strip
rescue SystemCallError, IOError => e
raise IOError, "Unable to read #{key_file}"
end
Expand Down
45 changes: 24 additions & 21 deletions lib/spice/chef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,42 @@ class Chef


def initialize(options={})
@@client_name ||= @client_name = options[:client_name]
@@key_file ||= @key_file = options[:key_file]
@@host ||= @host = options[:host] || 'localhost'
@@port ||= @port = options[:port] || 4000
@@path ||= @path = options[:path] || '/'
@@scheme ||= @scheme = options[:scheme] || 'http'
@@connection ||= @connection = Connection.new(
:url => "#{@scheme}://#{@host}:#{@port}#{@path}",
:client_name => options[:client_name],
:key_file => options[:key_file]
)
@client_name = options[:client_name] || Spice.client_name
@key_file = options[:key_file] || Spice.key_file
@host = options[:host] || Spice.host
@port = options[:port] || Spice.port
@scheme = options[:scheme] || Spice.scheme
@connection = Connection.new(
:url => "#{@scheme}://#{@host}:#{@port}",
:client_name => options[:client_name],
:key_file => options[:key_file]
) || Spice.connection


@default_headers = options[:headers] || {}
@sign_on_redirect, @sign_request = true, true
end

def connection
@@connection
@connection || Spice.connection
end

def self.connection
@@connection
@connection || Spice.connection
end

def clients
Client.all
end
class << self
def clients
Client.list
end

def nodes
Node.all
end
def nodes
Node.list
end

def data_bags
DataBag.all
def data_bags
DataBag.list
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/spice/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spice
class Client < Spice::Chef
def self.all(options={})
def self.list(options={})
if options[:complete]
results = []
connection.get("/clients").map { |c| c[0] }.each do |client|
Expand Down Expand Up @@ -32,7 +32,7 @@ def self.update(options={})

def self.delete(options={})
name = options.delete(:name)
connection.delete("/clients/#{name}")
connection.delete("/clients/#{name}", options)
end
end
end
4 changes: 2 additions & 2 deletions lib/spice/connection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rest-client'
require 'yajl/json_gem'

module Spice
class Connection
attr_accessor :client_name, :key_file, :auth_credentials, :url, :path, :port, :scheme
attr_accessor :client_name, :key_file, :auth_credentials, :url, :path, :port, :scheme, :host

def initialize(options={})
endpoint = URI.parse(options[:url])
Expand Down
4 changes: 2 additions & 2 deletions lib/spice/cookbook.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spice
class Cookbook < Spice::Chef
def self.all(options={})
def self.list(options={})
connection.get("/cookbooks").map { |c| c[0] }
end

Expand All @@ -24,7 +24,7 @@ def self.update(options={})

def self.delete(options={})
name = options.delete(:name)
connection.delete("/cookbooks/#{name}")
connection.delete("/cookbooks/#{name}", options)
end
end
end
4 changes: 2 additions & 2 deletions lib/spice/data_bag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spice
class DataBag < Spice::Chef
def self.all(options={})
def self.list(options={})
connection.get("/data")
end

Expand All @@ -24,7 +24,7 @@ def self.update(options={})

def self.delete(options={})
name = options.delete(:name)
connection.delete("/data/#{name}")
connection.delete("/data/#{name}", options)
end
end
end
4 changes: 2 additions & 2 deletions lib/spice/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spice
class Node < Spice::Chef
def self.all(options={})
def self.list(options={})
connection.get("/nodes")
end

Expand All @@ -24,7 +24,7 @@ def self.update(options={})

def self.delete(options={})
name = options.delete(:name)
connection.delete("/nodes/#{name}")
connection.delete("/nodes/#{name}", options)
end
end
end
4 changes: 2 additions & 2 deletions lib/spice/role.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spice
class Role < Spice::Chef
def self.all(options={})
def self.list(options={})
connection.get("/roles")
end

Expand All @@ -24,7 +24,7 @@ def self.update(options={})

def self.delete(options={})
name = options.delete(:name)
connection.delete("/roles/#{name}")
connection.delete("/roles/#{name}", options)
end
end
end
2 changes: 1 addition & 1 deletion lib/spice/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Spice
VERSION = "0.0.1.alpha.2"
VERSION = "0.0.1.beta1"
end
Loading

0 comments on commit 3006bd3

Please sign in to comment.