Skip to content

Commit

Permalink
Merge pull request #169 from mizzy/interface-type
Browse files Browse the repository at this point in the history
Add interface type
  • Loading branch information
mizzy committed Jun 28, 2013
2 parents f8970ab + 4eaf44a commit e3fa129
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/serverspec/commands/linux.rb
Expand Up @@ -23,6 +23,10 @@ def check_selinux(mode)
def check_kernel_module_loaded(name)
"lsmod | grep ^#{name}"
end

def get_interface_speed_of(name)
"ethtool #{name} | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\\/s/,\"\\\\1\",\"\")}'"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/serverspec/helper/type.rb
Expand Up @@ -3,7 +3,7 @@ module Helper
module Type
types = %w(
base yumrepo service package port file cron command linux_kernel_parameter iptables host
routing_table default_gateway selinux user group zfs ipnat ipfilter kernel_module
routing_table default_gateway selinux user group zfs ipnat ipfilter kernel_module interface
)

types.each {|type| require "serverspec/type/#{type}" }
Expand Down
12 changes: 12 additions & 0 deletions lib/serverspec/type/interface.rb
@@ -0,0 +1,12 @@
module Serverspec
module Type
class Interface < Base
def speed
ret = backend.run_command(commands.get_interface_speed_of(@name))
val = ret[:stdout].strip
val = val.to_i if val.match(/^\d+$/)
val
end
end
end
end
2 changes: 2 additions & 0 deletions spec/debian/commands_spec.rb
Expand Up @@ -61,6 +61,8 @@
it_behaves_like 'support command check_access_by_user'

it_behaves_like 'support command check_kernel_module_loaded', 'lp'

it_behaves_like 'support command get_interface_speed_of', 'eth0'
end

describe 'check_enabled' do
Expand Down
7 changes: 7 additions & 0 deletions spec/debian/interface_spec.rb
@@ -0,0 +1,7 @@
require 'spec_helper'

include Serverspec::Helper::Debian

describe 'Serverspec interface matchers of Debian family' do
it_behaves_like 'support interface matcher', 'eth0'
end
2 changes: 2 additions & 0 deletions spec/gentoo/commands_spec.rb
Expand Up @@ -61,6 +61,8 @@
it_behaves_like 'support command check_access_by_user'

it_behaves_like 'support command check_kernel_module_loaded', 'lp'

it_behaves_like 'support command get_interface_speed_of', 'eth0'
end

describe 'check_enabled' do
Expand Down
7 changes: 7 additions & 0 deletions spec/gentoo/interface_spec.rb
@@ -0,0 +1,7 @@
require 'spec_helper'

include Serverspec::Helper::Gentoo

describe 'Serverspec interface matchers of Gentoo family' do
it_behaves_like 'support interface matcher', 'eth0'
end
2 changes: 2 additions & 0 deletions spec/redhat/commands_spec.rb
Expand Up @@ -59,6 +59,8 @@
it_behaves_like 'support command get_mode'

it_behaves_like 'support command check_kernel_module_loaded', 'lp'

it_behaves_like 'support command get_interface_speed_of', 'eth0'
end

describe 'check_enabled' do
Expand Down
7 changes: 7 additions & 0 deletions spec/redhat/interface_spec.rb
@@ -0,0 +1,7 @@
require 'spec_helper'

include Serverspec::Helper::RedHat

describe 'Serverspec interface matchers of Red Hat family' do
it_behaves_like 'support interface matcher', 'eth0'
end
5 changes: 5 additions & 0 deletions spec/support/shared_commands_examples.rb
Expand Up @@ -270,3 +270,8 @@
subject { commands.check_kernel_module_loaded(name) }
it { should eq "lsmod | grep ^#{name}" }
end

shared_examples_for 'support command get_interface_speed_of' do |name|
subject { commands.get_interface_speed_of(name) }
it { should eq "ethtool eth0 | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\/s/,\"\\\\1\",\"\")}'" }
end
17 changes: 17 additions & 0 deletions spec/support/shared_interface_examples.rb
@@ -0,0 +1,17 @@
shared_examples_for 'support interface matcher' do |name|
describe 'interface' do
before :all do
RSpec.configure do |c|
c.stdout = "1000"
end
end

describe interface(name) do
its(:speed) { should eq 1000 }
end

describe interface('invalid-interface') do
its(:speed) { should_not eq 100 }
end
end
end

0 comments on commit e3fa129

Please sign in to comment.