Created
May 7, 2014 01:36
-
-
Save tas50/3fe17a6d9fbede0a7669 to your computer and use it in GitHub Desktop.
Help an rspec newb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WTF am I doing wrong here? | |
mdadm ohai plugin: | |
Ohai.plugin(:Mdadm) do | |
provides 'mdadm' | |
def create_raid_device_mash(stdout) | |
device_mash = Mash.new | |
device_mash[:device_counts] = Mash.new | |
stdout.each_line do |line| | |
case line | |
when /Version\s+: ([0-9.]+)/ | |
device_mash[:version] = Regexp.last_match[1].to_f | |
when /Raid Level\s+: raid([0-9]+)/ | |
device_mash[:level] = Regexp.last_match[1].to_i | |
when /Array Size.*\(([0-9.]+)/ | |
device_mash[:size] = Regexp.last_match[1].to_f | |
when /State\s+: ([a-z]+)/ | |
device_mash[:state] = Regexp.last_match[1] | |
when /Total Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:total] = Regexp.last_match[1].to_i | |
when /Raid Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:raid] = Regexp.last_match[1].to_i | |
when /Working Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:working] = Regexp.last_match[1].to_i | |
when /Failed Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:failed] = Regexp.last_match[1].to_i | |
when /Active Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:active] = Regexp.last_match[1].to_i | |
when /Spare Devices\s+: ([0-9]+)/ | |
device_mash[:device_counts][:spare] = Regexp.last_match[1].to_i | |
end | |
end | |
device_mash | |
end | |
collect_data(:linux) do | |
# gather a list of all raid arrays | |
if File.exist?('/proc/mdstat') | |
devices = [] | |
File.open('/proc/mdstat').each do |line| | |
devices << Regexp.last_match[1] if line =~ /(md[0-9]+)/ | |
end | |
# create the mdadm mash and gather individual information if devices are present | |
unless devices.empty? | |
mdadm Mash.new | |
devices.sort.each do |device| | |
mdadm[device] = Mash.new | |
# gather detailed information on the array | |
cmd = "mdadm --detail /dev/#{device}" | |
status, stdout, stderr = run_command(:command => cmd) | |
# if the mdadm command was sucessful pass stdout to create_raid_device_mash to grab the tidbits we want | |
mdadm[device] = create_raid_device_mash(stdout) if status == 0 | |
end | |
end | |
end | |
end | |
end | |
output: | |
"mdadm": { | |
"md0": { | |
"device_counts": { | |
"raid": 6, | |
"total": 6, | |
"active": 6, | |
"working": 6, | |
"failed": 0, | |
"spare": 0 | |
}, | |
"version": 1.2, | |
"level": 10, | |
"size": 2794.16, | |
"state": "clean" | |
} | |
} | |
spec: | |
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb') | |
@md0 = <<-MD | |
/dev/md0: | |
Version : 1.2 | |
Creation Time : Thu Jan 30 03:11:40 2014 | |
Raid Level : raid10 | |
Array Size : 2929893888 (2794.16 GiB 3000.21 GB) | |
Used Dev Size : 976631296 (931.39 GiB 1000.07 GB) | |
Raid Devices : 6 | |
Total Devices : 6 | |
Persistence : Superblock is persistent | |
Update Time : Tue May 6 23:30:32 2014 | |
State : clean | |
Active Devices : 6 | |
Working Devices : 6 | |
Failed Devices : 0 | |
Spare Devices : 0 | |
Layout : near=2 | |
Chunk Size : 256K | |
Name : host.therealtimsmith.com:3 (local to host host.therealtimsmith.com) | |
UUID : 5ed74d5b:70bfe21d:8cd57792:c1e13d65 | |
Events : 155 | |
Number Major Minor RaidDevice State | |
0 8 32 0 active sync /dev/sdc | |
1 8 48 1 active sync /dev/sdd | |
2 8 64 2 active sync /dev/sde | |
3 8 80 3 active sync /dev/sdf | |
4 8 96 4 active sync /dev/sdg | |
5 8 112 5 active sync /dev/sdh | |
MD | |
describe Ohai::System, "Linux Mdadm Plugin" do | |
before(:each) do | |
@plugin = get_plugin("linux/mdadm") | |
@plugin.stub(:collect_os).and_return(:linux) | |
@double_file = double("/proc/mdstat") | |
@double_file.stub(:each). | |
and_yield("Personalities : [raid1] [raid6] [raid5] [raid4] [linear] [multipath] [raid0] [raid10]"). | |
and_yield("md0 : active raid10 sdh[5] sdg[4] sdf[3] sde[2] sdd[1] sdc[0]"). | |
and_yield(" 2929893888 blocks super 1.2 256K chunks 2 near-copies [6/6] [UUUUUU]") | |
File.stub(:open).with("/proc/mdstat").and_return(@double_file) | |
File.stub(:exists?).with("/proc/mdstat").and_return(true) | |
@plugin.stub(:shell_out).with("mdadm --detail /dev/md0").and_return(mock_shell_out(0, @md0, "")) | |
end | |
describe "gathering Mdadm information via /proc/mdstat and mdadm --detail" do | |
it "should not raise an error" do | |
lambda { @plugin.run }.should_not raise_error | |
end | |
it "should have a device count of 6 on md0" do | |
@plugin.run | |
@plugin[:mdadm][:md0][:device_counts][:raid].should be 6 | |
end | |
end | |
end | |
Painful failure where I'm probably missing something horribly obvious: | |
1) Ohai::System Linux Mdadm Plugin gathering Mdadm information via /proc/mdstat and mdadm --detail should have a device count of 6 on md0 | |
Failure/Error: @plugin[:mdadm][:md0][:device_counts][:raid] == 6 | |
NoMethodError: | |
undefined method `[]' for nil:NilClass | |
# ./spec/unit/plugins/linux/mdadm_spec.rb:77:in `block (3 levels) in <top (required)>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the fork: https://gist.github.com/btm/5fb6b9011f7a3eb3a7b2
I think the initial error you're getting comes from using File.exist? but stubbing File.exists?
Then you're using run_command in the plugin but stubbing shell_out, just use shell_out in the plugin.