使用Inspec迭代JSON,仅针对特定条目进行测试

发布于 2025-01-26 05:40:39 字数 843 浏览 4 评论 0原文

要测试我的服务器上是否安装了正确的领事版本,我正在尝试编写Inspec测试。这个想法是用Mutiple IP地址调用此测试,将其针对JSON(我从领事API中获得)对其进行解析,然后仅查看领事版本的服务器,以获取我列出的IPS的服务器。

现在,我正在努力迭代JSON,然后只检查正确的服务器。

它看起来应该像这样:

describe json(content: http('http://172.17.0.1:8500/v1/agent/members').body) do

for i in 0..2 do
 ip_addresses.each { |node| its ([i, 'Tags', 'build']) { should match consul_version }.where(['Addr'] == node) }
end

当然不起作用,但是我认为这解释了我要实现的目标。

是否有一种可以让我这样做的方法?

编辑: JSON归结为:

[
{
    "Addr": "192.100.100.100”,
    "Name": “machine0”,
    "Tags": {
        "build": "1.11.3",
    }
},
{
    "Addr": "192.100.100.101”,
    "Name": “machine1",
    "Tags": {
        "build": "1.11.3”,
    }
},
{
    "Addr": "192.100.100.102”,
    "Name": “machine2”,
    "Tags": {
        "build": "1.11.3",
    }
}
]

To test if the correct consul version is installed on my servers, I am trying to write an inspec test. The idea is to call this test with mutiple ip addresses, parse it against json (which I get from the consul api) and then check the consul version for only the servers that I have the IPs listed for.

Now I am struggling with iterating over the json and then only checking the correct servers.

It should look something like this:

describe json(content: http('http://172.17.0.1:8500/v1/agent/members').body) do

for i in 0..2 do
 ip_addresses.each { |node| its ([i, 'Tags', 'build']) { should match consul_version }.where(['Addr'] == node) }
end

It doesn't work of course, but I think it explains what I am trying to achieve.

Is there an approach that would allow me to do this?

Edit:
The json boils down to this:

[
{
    "Addr": "192.100.100.100”,
    "Name": “machine0”,
    "Tags": {
        "build": "1.11.3",
    }
},
{
    "Addr": "192.100.100.101”,
    "Name": “machine1",
    "Tags": {
        "build": "1.11.3”,
    }
},
{
    "Addr": "192.100.100.102”,
    "Name": “machine2”,
    "Tags": {
        "build": "1.11.3",
    }
}
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文