首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用InSpec创建上下文?

如何使用InSpec创建上下文?
EN

Stack Overflow用户
提问于 2016-12-05 22:15:04
回答 2查看 883关注 0票数 0

我正在使用InSpec创建我的测试。这是我对Apache的测试:

代码语言:javascript
复制
require 'inspec'

if os[:family] == 'redhat'

  describe package 'httpd' do
    it { should be_installed }
  end

  describe service 'httpd' do
    it { should be_enabled }
    it { should be_running }
  end

  describe file '/etc/httpd/conf/httpd.conf' do
    it { should be_file }
  end
end
describe port(80) do
  it { should_not be_listening }
end

describe port(9000) do
  it { should be_listening }
end

我的问题与上下文相关。在使用InSpec之前,我使用了ChefSpec,我喜欢它创建上下文和输出显示上下文的方式。对于上面的示例,输出如下:

代码语言:javascript
复制
System Package
     ✔  httpd should be installed
  Service httpd
     ✔  should be enabled
     ✔  should be running
  File /etc/httpd/conf/httpd.conf
     ✔  should be file
  Port 80
     ✔  should not be listening
  Port 9000
     ✔  should be listening

我希望在输出中包含家庭风格或版本或arch,以便了解并获得更清晰的测试输出。

有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2016-12-06 00:01:04

首先,ChefSpec和InSpec做的是完全不同的事情,所以两者是可以比较的。其次,虽然InSpec支持一定程度的RSpec语法兼容性,但它远不如ChefSpec或ServerSpec那么完整,这两个库都是完整的RSpec帮助器库。正如@Tensibai提到的,InSpec提供了自己的custom syntax for more complex tests。如果您特别想使用RSpec describecontext block系统或自定义RSpec格式化程序,我建议您改用ServerSpec。

票数 3
EN

Stack Overflow用户

发布于 2019-01-29 01:27:26

我认为您正在寻找一种更好地控制输出的方法。如果是这样,Chef建议您使用expect语法。尽管他们说它的可读性不如should语法,但它是定制输出的唯一方法。

例如,如果运行此控件:

代码语言:javascript
复制
control 'Services' do
  title 'Check that the required services are installed and running.'
  if os['family'] == 'darwin'
    describe 'For Mac OS, the Apache service' do
      it 'should be installed and running.' do
        expect(service('httpd')).to(be_installed)
        expect(service('httpd')).to(be_running)
      end
    end
    describe 'For Mac OS, the Docker service' do
      it 'should be installed and running.' do
        expect(service('docker')).to(be_installed)
        expect(service('docker')).to(be_running)
      end
    end
  end
end

您将获得以下输出:

代码语言:javascript
复制
  ×  Services: Check that the required services are installed and running. (1 failed)
     ×  For Mac OS, the Apache service should be installed and running.
     expected that `Service httpd` is installed
     ✔  For Mac OS, the Docker service should be installed and running.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40976305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档