Skip to content

Commit

Permalink
Actually remove host entry duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Nov 5, 2013
1 parent c5bd0f4 commit cc785e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 76 deletions.
26 changes: 0 additions & 26 deletions libraries/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,6 @@ def to_line
[ip_address, hosts, comments].compact.join("\t").strip
end

# The string representation of this Entry
#
# @return [String]
# the string representation of this entry
def to_s
"#<#{self.class.to_s} " + [
"ip_address: '#{ip_address}'",
"hostname: '#{hostname}'",
].join(', ') + '>'
end

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

# Returns true if priority is calculated
#
# @return [Boolean]
Expand Down
4 changes: 3 additions & 1 deletion libraries/manipulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

class Manipulator
attr_reader :node
attr_reader :entries

# Create a new Manipulator object (aka an /etc/hosts manipulator). If a
# hostsfile is not found, a Chef::Application.fatal is risen, causing
Expand Down Expand Up @@ -249,7 +250,7 @@ def remove_existing_hostnames(entry)
@entries.delete(entry)
changed_hostnames = [entry.hostname, entry.aliases].flatten.uniq

@entries.collect do |entry|
@entries = @entries.collect do |entry|
entry.hostname = nil if changed_hostnames.include?(entry.hostname)
entry.aliases = entry.aliases - changed_hostnames

Expand All @@ -265,6 +266,7 @@ def remove_existing_hostnames(entry)
end
end.compact


@entries << entry

nil
Expand Down
46 changes: 0 additions & 46 deletions spec/unit/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,50 +160,4 @@
end
end
end

describe '#to_s' do
subject { Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com') }

it 'prints correctly' do
expect(subject.to_s).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com'>")
end

it 'prints without aliases' do
subject.aliases << 'foo'
expect(subject.to_s).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com'>")
end

it 'prints without a priority' do
subject.priority = 10
expect(subject.to_s).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com'>")
end

it 'prints without comments' do
subject.comment = "This is a comment"
expect(subject.to_s).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com'>")
end
end

describe '#inspect' do
subject { Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com') }

it 'prints correctly' do
expect(subject.inspect).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: [], comment: '', priority: 60, calculated_priority?: true>")
end

it 'prints with aliases' do
subject.aliases << 'foo'
expect(subject.inspect).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: [\"foo\"], comment: '', priority: 60, calculated_priority?: true>")
end

it 'prints with a priority' do
subject.priority = 10
expect(subject.inspect).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: [], comment: '', priority: 10, calculated_priority?: false>")
end

it 'prints with comments' do
subject.comment = "This is a comment"
expect(subject.inspect).to eq("#<Entry ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: [], comment: 'This is a comment', priority: 60, calculated_priority?: true>")
end
end
end
43 changes: 40 additions & 3 deletions spec/unit/manipulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

let(:entries) do
[
double('entry_1', ip_address: '127.0.0.1', hostname: 'localhost', to_line: '127.0.0.1 localhost', priority: 10),
double('entry_2', ip_address: '1.2.3.4', hostname: 'example.com', to_line: '1.2.3.4 example.com', priority: 20),
double('entry_3', ip_address: '4.5.6.7', hostname: 'foo.example.com', to_line: '4.5.6.7 foo.example.com', priority: 30)
Entry.new(ip_address: '127.0.0.1', hostname: 'localhost', to_line: '127.0.0.1 localhost', priority: 10),
Entry.new(ip_address: '1.2.3.4', hostname: 'example.com', to_line: '1.2.3.4 example.com', priority: 20),
Entry.new(ip_address: '4.5.6.7', hostname: 'foo.example.com', to_line: '4.5.6.7 foo.example.com', priority: 30)
]
end

Expand Down Expand Up @@ -204,4 +204,41 @@
expect(manipulator.find_entry_by_ip_address('77.77.77.77')).to be_nil
end
end

describe '#remove_exisitng_hostnames' do
before { manipulator.class.send(:public, :remove_existing_hostnames) }

context 'with no duplicates' do
it 'does not change anything' do
entry = Entry.new(ip_address: '7.8.9.10', hostname: 'new.example.com')
entries << entry

expect {
manipulator.remove_existing_hostnames(entry)
}.to_not change(manipulator, :entries)
end
end

context 'with duplicate hostnames' do
it 'removes the duplicate hostnames' do
entry = Entry.new(ip_address: '7.8.9.10', hostname: 'example.com')
entries << entry

manipulator.remove_existing_hostnames(entry)
expect(manipulator.entries).to_not include(entries[1])
end
end

context 'with duplicate aliases' do
it 'removes the duplicate aliases' do
entry = Entry.new(ip_address: '7.8.9.10', hostname: 'bar.example.com')
entries << entry
entries[1].aliases = ['bar.example.com']

manipulator.remove_existing_hostnames(entry)
expect(manipulator.entries).to include(entries[1])
expect(manipulator.entries[1].aliases).to be_empty
end
end
end
end

0 comments on commit cc785e4

Please sign in to comment.