嵌套 json 文件的 chef inspec 验证

chef inspec validation of the nested json file

我正在使用 chef inspec 在下面的 json 文件中验证注释是否等于测试。

"imdata": [
    {
        "aaaPwdStrengthProfile": {
            "attributes": {
                "annotation": "Test",
            }
        }
    }
]

尝试使用以下脚本但出现错误

describe json('C:/output.json') do
  its(['imdata','aaaPwdStrengthProfile','attributes','annotation']) { should eq 'Test' }
end

在您尝试测试的 JSON 文件中,imdata 是一个数组 []。嵌套字典 { .. } 是该数组的第一个元素 (0)。

我根据你的问题创建了 JSON 数据 /tmp/sample.json。所以inspec测试应该参考'imdata', 0,,如下:

describe json('/tmp/sample.json') do
  its(['imdata', 0, 'aaaPwdStrengthProfile', 'attributes', 'annotation']) { should eq 'Test' }
end