Skip to content

Commit

Permalink
Merge pull request #228 from ryotarai/add-ipaddress-matcher
Browse files Browse the repository at this point in the history
Add have_ipv4_address matcher to interface type.
  • Loading branch information
mizzy committed Aug 12, 2013
2 parents 9001071 + 61942c9 commit 1818e5f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/serverspec/commands/base.rb
Expand Up @@ -248,6 +248,10 @@ def check_access_by_user(file, user, access)
def check_kernel_module_loaded(name)
raise NotImplementedError.new
end

def check_ipv4_address(interface, ip_address)
raise NotImplementedError.new
end
end
end
end
11 changes: 11 additions & 0 deletions lib/serverspec/commands/linux.rb
Expand Up @@ -32,6 +32,17 @@ def check_kernel_module_loaded(name)
def get_interface_speed_of(name)
"ethtool #{name} | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\\/s/,\"\\\\1\",\"\")}'"
end

def check_ipv4_address(interface, ip_address)
ip_address = ip_address.dup
if ip_address =~ /\/\d+$/
ip_address << " "
else
ip_address << "/"
end
ip_address.gsub!(".", "\\.")
"ip addr show #{interface} | grep 'inet #{ip_address}'"
end
end
end
end
4 changes: 4 additions & 0 deletions lib/serverspec/type/interface.rb
Expand Up @@ -7,6 +7,10 @@ def speed
val = val.to_i if val.match(/^\d+$/)
val
end

def has_ipv4_address?(ip_address)
backend.check_ipv4_address(@name, ip_address)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/debian/interface_spec.rb
Expand Up @@ -8,6 +8,16 @@
its(:command) { should eq "ethtool eth0 | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\/s/,\"\\\\1\",\"\")}'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10/24") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/24 '" }
end

describe interface('invalid-interface') do
let(:stdout) { '1000' }
its(:speed) { should_not eq 100 }
Expand Down
10 changes: 10 additions & 0 deletions spec/gentoo/interface_spec.rb
Expand Up @@ -8,6 +8,16 @@
its(:command) { should eq "ethtool eth0 | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\/s/,\"\\\\1\",\"\")}'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10/24") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/24 '" }
end

describe interface('invalid-interface') do
let(:stdout) { '1000' }
its(:speed) { should_not eq 100 }
Expand Down
10 changes: 10 additions & 0 deletions spec/redhat/interface_spec.rb
Expand Up @@ -8,6 +8,16 @@
its(:command) { should eq "ethtool eth0 | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\/s/,\"\\\\1\",\"\")}'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/'" }
end

describe interface('eth0') do
it { should have_ipv4_address("192.168.10.10/24") }
its(:command) { should eq "ip addr show eth0 | grep 'inet 192\\.168\\.10\\.10/24 '" }
end

describe interface('invalid-interface') do
let(:stdout) { '1000' }
its(:speed) { should_not eq 100 }
Expand Down

0 comments on commit 1818e5f

Please sign in to comment.