0

我在我的食谱中编写了一个库方法,该方法将读取 /etc/fstab 文件并在缺少某些挂载选项时对其进行修改。

当我尝试编写 Chefspec 测试时,它们都无法返回存根信息,而是读取我计算机上的本地 /etc/fstab。

我想知道在生产服务器上存根示例 fstab 并在运行 Chefspec 时将该信息传递给库方法的正确语法是什么。

库方法在 library/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
4

1 回答 1

0

存根 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)
于 2015-03-17T19:47:32.890 回答