0

我有一门课看起来像

class addon {
    case $::operatingsystem {
    'windows': {
        Dsc_xfirewall {
            dsc_ensure    => 'Present',
        }
     }
     'RedHat': {
     }
     default: { warning "OS : ${::operatingsystem} is not (yet) supported" }
  }
}

我希望我的测试看起来像

describe 'addon', :type => :class do
    os = 'windows'
    let(:facts) {{:operatingsystem => os}}
    describe os do
        it {
            is_expected.to contain_class('addon').with(
              {
                :Dsc_xfirewall => {
                  :dsc_ensure => 'Present',
                }
              }
            )
        }
    end
end

目录编译正确,为清楚起见删除了 is_expected.to 编译。但是我似乎无法让它工作:dsc_xfirewall 为零,如果我尝试 Dsc_xfirewall 相同的故事,如果尝试

contains_dsc_xfirewall

我收到 dsc_firewall 不是有效定义的错误。有谁知道如何更好地构建我的测试?在有人指出如果目录编译正确我不需要这个测试之前,我知道;这只是更复杂的东西的熟化版本。

因此,我的问题是:为了检查该类是否包含 dsc_xfirewall 以及所有参数是否设置正确,测试必须是什么样子?

4

1 回答 1

1

明显的问题是您的清单没有声明任何实际资源,而您的 Rspec 似乎期望清单实际上做了。

这段代码在这里:

    Dsc_xfirewall {
        dsc_ensure => 'Present',
    }

为自定义类型声明资源默认值( refdsc_xfirewall ) 。我的猜测是大写PPresent也是一个错字。

我还注意到您的let(:facts)语句放错了位置,并且您打开了另一个describe我认为您应该使用的块context

我按照我认为您正在尝试做的事情编写了一些代码,以说明清单和 Rspec 代码的外观:

(我改变了一些东西,所以我可以很容易地让它在我的 Mac 上编译。)

class foo {
  case $::operatingsystem {
    'Darwin': {
      file { '/tmp/foo':
        ensure => file,
      }
    }
    'RedHat': {
    }
    default: { fail("OS : ${::operatingsystem} is not (yet) supported") }
  }
}

规格:

describe 'foo', :type => :class do
  context 'Darwin' do
    let(:facts) {{:operatingsystem => 'Darwin'}}
    it {
      is_expected.to contain_class('foo')
    }
    it {
      is_expected.to contain_file('/tmp/foo').with(
        {
          :ensure => 'file',
        }
      )
    }

    # Write out the catalog for debugging purposes.
    it { File.write('myclass.json', PSON.pretty_generate(catalogue)) }
  end
end

显然,而不是contain_file你会使用contain_dsc_xfirewall.

于 2017-06-24T13:36:56.343 回答