Chefspec 和存根 shell_out 命令
Chefspec and stubbing shell_out commands
我在我的食谱中写了一个库方法,它会读取 /etc/fstab 文件并在缺少某些挂载选项时修改它。
当我尝试编写 Chefspec 测试时,它们都无法 returns 存根信息,而是读取我计算机上的本地 /etc/fstab。
我想知道当 运行 Chefspec.
时,我希望在生产服务器上存根示例 fstab 并将该信息传递给库方法的正确语法是什么
库方法在libraries/fstab_quota.rb文件中:
module ApplicationQuota
module Fstab
def read_fstab
cmd = shell_out('cat /etc/fstab')
cmd.run_command
cmd.stdout.split("\n").each do |fs|
# Logic here
end
end
end
[Chef::Recipe, Chef::Resource].each do |l|
l.send :include, ::ApplicationQuota::Fstab
end
然后我会在食谱食谱中调用库方法:
# Fstab returns either an Array if there are modifications or false if
# no modifications have been made
fstab = read_fstab
file '/etc/fstab' do
content fstab.join("\n")
owner "root"
group "root"
mode "644"
action :create
only_if { fstab }
end
存根 shell_out 命令本身。
my_double = double('shellout_double')
allow(my_double).to receive(:run_command)
allow(my_double).to receive(:stdout).and_return(.....)
allow(Mixlib:ShellOut).to receive(:shell_out).with("your command").and_return(my_double)
我在我的食谱中写了一个库方法,它会读取 /etc/fstab 文件并在缺少某些挂载选项时修改它。
当我尝试编写 Chefspec 测试时,它们都无法 returns 存根信息,而是读取我计算机上的本地 /etc/fstab。
我想知道当 运行 Chefspec.
时,我希望在生产服务器上存根示例 fstab 并将该信息传递给库方法的正确语法是什么库方法在libraries/fstab_quota.rb文件中:
module ApplicationQuota
module Fstab
def read_fstab
cmd = shell_out('cat /etc/fstab')
cmd.run_command
cmd.stdout.split("\n").each do |fs|
# Logic here
end
end
end
[Chef::Recipe, Chef::Resource].each do |l|
l.send :include, ::ApplicationQuota::Fstab
end
然后我会在食谱食谱中调用库方法:
# Fstab returns either an Array if there are modifications or false if
# no modifications have been made
fstab = read_fstab
file '/etc/fstab' do
content fstab.join("\n")
owner "root"
group "root"
mode "644"
action :create
only_if { fstab }
end
存根 shell_out 命令本身。
my_double = double('shellout_double')
allow(my_double).to receive(:run_command)
allow(my_double).to receive(:stdout).and_return(.....)
allow(Mixlib:ShellOut).to receive(:shell_out).with("your command").and_return(my_double)