【问题标题】:using variable in the the command resource in serverspec在 serverspec 的命令资源中使用变量
【发布时间】:2017-07-09 19:32:43
【问题描述】:

我在 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 测试中使用变量,但无法让它们在命令行中工作。

非常感谢任何帮助

【问题讨论】:

    标签: ruby hash rspec serverspec


    【解决方案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
    

    【讨论】:

    • 感谢 Matt 给了我一个款待 - 我有一些语法问题你已经很好地解决了 - 现在从厨师那里提取哈希。
    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2016-04-10
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多