1

我正在我们的 puppet 项目中集成rspec-puppet测试,并且我正在尝试为所有主机(最终可能还有其他资源)自动生成“应该编译”规范。这是为了确保至少所有内容都能成功编译。

给定一个节点列表,我可以这样做:

hosts.each do |host|
  describe host, type: :host do
    it { should compile }
  end
end

问题是如何实际获取主机列表。我可以使用正则表达式来解析节点文件,但这显然会导致精神错乱......由于 puppet gem 已经存在并用于通过 rspec-puppet gem 加载目录,我可以使用它来获取主机列表吗?

编辑:

我最终设法使用puppet pops 系统做到了这一点,但我不确定这是否是最好的方法,或者是否有一些更容易使用的更高级别的抽象:

require 'spec_helper'
require 'puppet/pops'

code_dirs = [RSpec.configuration.module_path, RSpec.configuration.manifest_dir]
definitions =
    Dir["{#{code_dirs.join(',')}}/**/*.pp"].
        map {|file| Puppet::Pops::Parser::Parser.new.parse_file file}.
        flat_map {|parsed_manifest| parsed_manifest.definitions}

hosts = definitions.
    select {|definition| definition.is_a? Puppet::Pops::Model::NodeDefinition}.
    flat_map {|node_definition| node_definition.host_matches}.
    select {|host_match| host_match.is_a? Puppet::Pops::Model::LiteralString}.
    map {|string_host_match| string_host_match.value}

classes = definitions.
    select {|definition| definition.is_a? Puppet::Pops::Model::HostClassDefinition}.
    map {|host_class_definition| host_class_definition.name}

hosts.each do |host|
  describe host, type: :host do
    it {should compile}
  end
end

classes.each do |klass|
  describe klass, type: :class do
    it {should compile}
  end
end
4

1 回答 1

1

查看https://github.com/nwops/puppet-retrospec,它提供了对现有模块的解析清单和模板测试。

于 2017-10-02T10:31:39.543 回答