Skip to content

Commit

Permalink
Add Manipulator specs
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Apr 29, 2013
1 parent b1a538c commit 0e1752f
Show file tree
Hide file tree
Showing 8 changed files with 677 additions and 173 deletions.
11 changes: 7 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
source 'https://rubygems.org'

gem 'chef', ENV['CHEF_VERSION'] || '~> 11.0.0'
gem 'yard', '~> 0.8.6'
gem 'yard-chef', '~> 1.0.0'

group :test do
gem 'chef', '~> 11.0.0'
gem 'chefspec', :github => 'acrmp/chefspec'
gem 'fauxhai', '~> 0.1.1'
end
gem 'chefspec', '~> 1.0.0'
gem 'strainer', '~> 2.0.0'
end
121 changes: 90 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
PATH
remote: ~/Development/chefspec
specs:
chefspec (1.0.0.rc1)
chef (>= 10.0)
erubis
fauxhai (~> 0.1)
minitest-chef-handler (~> 0.6.0)
moneta (< 0.7.0)
rspec (~> 2.12.0)

GEM
remote: https://rubygems.org/
specs:
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
addressable (2.3.4)
berkshelf (1.4.0)
activesupport (>= 3.2.0)
addressable
celluloid (>= 0.13.0)
chozo (>= 0.6.1)
faraday (>= 0.8.5)
hashie (>= 2.0.2)
json (>= 1.5.0)
minitar
mixlib-config (~> 1.1)
mixlib-shellout (~> 1.1)
multi_json (~> 1.5)
retryable
ridley (~> 0.9.0)
solve (>= 0.4.2)
thor (~> 0.18.0)
yajl-ruby
builder (3.2.0)
celluloid (0.13.0)
timers (>= 1.0.0)
chef (11.0.0)
erubis
highline (>= 1.6.9)
Expand All @@ -27,35 +39,51 @@ GEM
ohai (>= 0.6.0)
rest-client (>= 1.0.4, < 1.7.0)
yajl-ruby (~> 1.1)
chefspec (1.0.0)
chef (>= 10.0)
erubis
fauxhai (~> 0.1)
minitest-chef-handler (>= 0.6.0)
rspec (~> 2.0)
chozo (0.6.1)
activesupport (>= 3.2.0)
hashie (>= 2.0.2)
multi_json (>= 1.3.0)
ci_reporter (1.8.4)
builder (>= 2.1.2)
diff-lcs (1.1.3)
diff-lcs (1.2.4)
erubis (2.7.0)
faraday (0.8.7)
multipart-post (~> 1.1)
fauxhai (0.1.1)
chef
httparty
net-ssh
highline (1.6.15)
httparty (0.10.2)
hashie (2.0.4)
highline (1.6.18)
httparty (0.11.0)
multi_json (~> 1.0)
multi_xml (>= 0.5.2)
i18n (0.6.1)
ipaddress (0.8.0)
json (1.6.1)
mime-types (1.21)
minitest (4.6.2)
minitest-chef-handler (0.6.8)
mime-types (1.23)
minitar (0.5.4)
minitest (4.7.3)
minitest-chef-handler (1.0.1)
chef
ci_reporter
minitest
minitest (~> 4.7.3)
mixlib-authentication (1.3.0)
mixlib-log
mixlib-cli (1.3.0)
mixlib-config (1.1.2)
mixlib-log (1.4.1)
mixlib-log (1.6.0)
mixlib-shellout (1.1.0)
moneta (0.6.0)
multi_json (1.6.1)
multi_json (1.7.2)
multi_xml (0.5.3)
multipart-post (1.2.0)
net-http-persistent (2.8)
net-ssh (2.2.2)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
Expand All @@ -70,23 +98,54 @@ GEM
mixlib-shellout
systemu
yajl-ruby
redcarpet (2.1.1)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)
retryable (1.3.2)
ridley (0.9.1)
activesupport (>= 3.2.0)
addressable
celluloid (~> 0.13.0)
chozo (>= 0.6.0)
erubis
faraday (>= 0.8.4)
json (>= 1.5.0)
mixlib-authentication (>= 1.3.0)
mixlib-config (>= 1.1.0)
mixlib-log (>= 1.3.0)
mixlib-shellout (>= 1.1.0)
multi_json (>= 1.0.4)
net-http-persistent (>= 2.8)
net-ssh
retryable
solve (>= 0.4.1)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
solve (0.4.2)
json
strainer (2.0.0)
berkshelf (~> 1.3)
systemu (2.5.2)
thor (0.18.1)
timers (1.1.0)
yajl-ruby (1.1.0)
yard (0.8.6.1)
yard-chef (1.0.0)
redcarpet (~> 2.1.1)
yard (~> 0.8)

PLATFORMS
ruby

DEPENDENCIES
chef (~> 11.0.0)
chefspec!
fauxhai (~> 0.1.1)
chefspec (~> 1.0.0)
strainer (~> 2.0.0)
yard (~> 0.8.6)
yard-chef (~> 1.0.0)
2 changes: 2 additions & 0 deletions Strainerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
knife: bundle exec knife cookbook test $COOKBOOK
spec: bundle exec rspec --color --format documentation
112 changes: 81 additions & 31 deletions libraries/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,20 @@

require 'ipaddr'

# An object representation of a single line in a hostsfile.
#
# @author Seth Vargo <sethvargo@gmail.com>
class Entry
attr_accessor :ip_address, :hostname, :aliases, :comment, :priority

def initialize(options = {})
raise ArgumentError, ':ip_address and :hostname are both required options' if options[:ip_address].nil? || options[:hostname].nil?

@ip_address = IPAddr.new(options[:ip_address])
@hostname = options[:hostname]
@aliases = [options[:aliases]].flatten.compact
@comment = options[:comment]
@priority = options[:priority] || calculate_priority(options[:ip_address])
end

def priority=(new_priority)
@calculated_priority = false
@priority = new_priority
end

class << self
# Creates a new Hostsfile::Entry object by parsing a text line. The
# `line` attribute will be in the following format:
#
# 1.2.3.4 hostname [alias[, alias[, alias]]] [# comment [@priority]]
#
# This returns a new Entry object...
# @param [String] line
# the line to parse
# @return [Entry]
# a new entry object
def parse(line)
entry_part, comment_part = line.split('#', 2).collect { |part| part.strip.empty? ? nil : part.strip }

Expand All @@ -69,8 +58,50 @@ def parse(line)
end
end

# Write out the entry as it appears in the hostsfile
def to_s
# @return [String]
attr_accessor :ip_address, :hostname, :aliases, :comment, :priority

# Creates a new entry from the given options.
#
# @param [Hash] options
# a list of options to create the entry with
# @option options [String] :ip_address
# the IP Address for this entry
# @option options [String] :hostname
# the hostname for this entry
# @option options [String, Array<String>] :aliases
# a alias or array of aliases for this entry
# @option options[String] :comment
# an optional comment for this entry
# @option options [Fixnum] :priority
# the relative priority of this entry (compared to others)
#
# @raise [ArgumentError]
# if neither :ip_address nor :hostname are supplied
def initialize(options = {})
raise ArgumentError, ':ip_address and :hostname are both required options' if options[:ip_address].nil? || options[:hostname].nil?

@ip_address = IPAddr.new(options[:ip_address])
@hostname = options[:hostname]
@aliases = [options[:aliases]].flatten.compact
@comment = options[:comment]
@priority = options[:priority] || calculated_priority
end

# Set a the new priority for an entry.
#
# @param [Fixnum] new_priority
# the new priority to set
def priority=(new_priority)
@calculated_priority = false
@priority = new_priority
end

# The line representation of this entry.
#
# @return [String]
# the string representation of this entry
def to_line
hosts = [hostname, aliases].flatten.join(' ')

comments = "# #{comment.to_s}".strip
Expand All @@ -81,16 +112,35 @@ def to_s
[ip_address, hosts, comments].compact.join("\t").strip
end

private
# Attempt to calculate the relative priority of each entry
def calculate_priority(ip_address)
@calculated_priority = true
ip_address = IPAddr.new(ip_address)

return 81 if ip_address == IPAddr.new('127.0.0.1')
return 80 if IPAddr.new('127.0.0.0/8').include?(ip_address) # local
return 60 if ip_address.ipv4? # ipv4
return 20 if ip_address.ipv6? # ipv6
return 00
# The string representation of this Entry
#
# @return [String]
# the string representation of this entry
def to_s
"#<Entry ip_address: '#{ip_address}', hostname: '#{hostname}'>"
end

# The object representation of this Entry
#
# @return [String]
# the object representation of this entry
def inspect
"#<Entry ip_address: '#{ip_address}', hostname: '#{hostname}', aliases: #{aliases.inspect}, comment: '#{comment}', priority: #{priority}, calculated_priority?: #{@calculated_priority}>"
end

private

# Calculates the relative priority of this entry.
#
# @return [Fixnum]
# the relative priority of this item
def calculated_priority
@calculated_priority = true

return 81 if ip_address == IPAddr.new('127.0.0.1')
return 80 if IPAddr.new('127.0.0.0/8').include?(ip_address) # local
return 60 if ip_address.ipv4? # ipv4
return 20 if ip_address.ipv6? # ipv6
return 00
end
end
Loading

0 comments on commit 0e1752f

Please sign in to comment.