2

我在 serverspec 配方中进行了以下测试 - 哈希只是 Chef 中描述的整个资源(我希望在某个时候通过管道输入)

# Test Folder Permissons
# hash taken from attributes
share = {
    "name" => "example",
    "sharepath" => "d:/example",
    "fullshareaccess" => "everyone",
    "ntfsfullcontrol" => "u-dom1/s37374",
    "ntfsmodifyaccess" => "",
    "ntfsreadaccess" => "everyone"
  }

# Explicitly test individual permissions (base on NTFSSecurity module)
describe command ('get-ntfsaccess d:/example -account u-dom1\s37374') do
    its(:stdout) { should include "FullControl" }
end

我遇到的问题是将变量放入命令资源中 - 我是 ruby​​ 新手,想知道我是否遗漏了一些东西。

我希望命令资源调用接受变量而不是硬编码。

例如

describe command ('get-ntfsaccess d:/example -account "#{ntfsfullcontrol}"') do
    its(:stdout) { should include "FullControl" }
end

我已经设法在:stdout测试中使用变量,但无法让它们在命令行中工作。

非常感谢任何帮助

4

1 回答 1

1

您可以像这样在 Serverspec 测试中使用哈希中的变量(使用现代 RSpec 3):

describe command ("get-ntfsaccess #{share['sharepath']} -account #{share['ntfsfullcontrol']") do
  its(:stdout) { is_expected.to include "FullControl" }
end

"#{}"语法会将您的变量插入到字符串中,并且该hash[key]语法将从您的哈希中获取值。

您还可以遍历哈希以执行更多检查,如下所示:

share.each |key, value| do
  describe command("test with #{key} #{value}") do
    # first loop: test with name example
    its(:stdout) { is_expected.to match(/foo/) }
  end
end
于 2017-02-20T15:11:14.380 回答