puppet - 如何调试和测试你的模块是否正常工作
puppet - How to debug and test to see if your module is working properly
我写了一个简单的模块来在 Ubuntu 虚拟机上安装一个包 (BioPerl)。整个 init.pp 文件在这里:
https://gist.github.com/anonymous/17b4c31bf7309aff14dfdcd378e44f40
问题是它不起作用,它没有给我任何反馈让我知道它为什么不起作用。模块中有 3 个简单的步骤。我检查了一下,它没有做任何事情。这是前 2 个:
第 1 步:下载存档并将其保存到 /usr/local/lib
exec { 'bioperl-download':
command => "sudo /usr/bin/wget --no-check-certificate -O ${archive_path} ${package_uri}",
require => Package['wget']
}
第 2 步:提取存档
exec { 'bioperl-extract':
command => "sudo /usr/bin/tar zxvf ${archive_path} --directory ${install_path}; sudo rm ${archive_path}",
require => Exec['bioperl-download']
}
很简单。但我不知道问题出在哪里,因为我看不到它在做什么。配置器设置为详细模式,这是我的模块的输出行:
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-download]/returns: executed successfully
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-extract]/returns: executed successfully
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-path]/returns: executed successfully
所以我只知道它成功执行了这三个步骤。它没有告诉我有关这些步骤是否正确完成工作的任何信息。我知道它没有将存档下载到 /usr/local/lib
那个目录,也没有将环境变量文件添加到 /usr/profile.d
。也许问题是包含目录的变量是错误的。可能包含档案下载 URI 的变量是错误的。我怎样才能找出这些东西呢?
更新:
事实证明该模块确实有效。但是为了改进模块(因为我想将它上传到 forge.puppetlabs.com
,我尝试实施 Matt 建议的更改。这是新代码:
file { 'bioperl-download':
path => "${archive_path}",
source => "http://cpan.metacpan.org/authors/id/C/CJ/CJFIELDS/${archive_name}",
ensure => "present"
}
exec { 'bioperl-extract':
command => "sudo /bin/tar zxvf ${archive_name}",
cwd => "${bioperl_target_dir}",
require => File['bioperl-download']
}
一个问题:它给我一个错误,告诉我源不能是 http://
。我在文档中看到他们确实允许 http://
文件作为 file
资源的来源。也许我使用的是旧版本的木偶?
我想试用 puppet-archive
模块,但不确定如何将其设置为必需的依赖项。我的意思是我如何确保它首先安装。我是否需要让我的模块从 github 下载模块并将其保存到模块目录?或者有没有办法让puppet自动安装?我将它添加为 metadata.json
文件的依赖项,但并没有安装它。我知道我可以让我的模块下载包,但我想知道最好的做法是什么。
您描述的最初问题是验收测试。验证您编写的 Puppet 资源和代码是否确实达到了您想要的最终状态,通常使用 Serverspec 来完成:http://serverspec.org/。例如,您可以编写一个 Puppet 模块来部署应用程序,但您只知道 Puppet 执行了您告诉它的操作,而不知道应用程序是否真的成功部署了。注意 Serverspec 也是人们通常用来为 Ansible 和 Chef 解决此问题的工具。
您可以编写类似于以下内容的 Serverspec 测试来帮助测试您的模块的结束状态:
describe file('/usr/local/lib/bioperl.tar.gz') do
it { expect(subject).to be_file }
end
describe file('/usr/profile.d/env_file') do
it { expect_subject).to be_file }
its(:content) { is_expected.to match(/env stuff/) }
end
但是,您的问题似乎还与调试验收测试失败的原因有关。为此,您需要进行单元测试。这通常用 RSpec-Puppet: http://rspec-puppet.com/ 来解决。我会向您展示如何针对您的情况编写一些测试,但我认为您不应该按照您的方式编写 Puppet 模块,这样会使单元测试变得无关紧要。
相反,请考虑使用具有 source
属性的 file
资源和 HTTP URI 来获取 tarball,而不是使用 wget
的 exec
:https://docs.puppet.com/puppet/latest/type.html#file-attribute-source. Also, you might want to consider using the Puppet archive module to assist you: https://forge.puppet.com/puppet/archive.
如果您对如何使用这些工具提供单元和验收测试有疑问,或者对如何重构您的模块有疑问,请不要犹豫,在 Whosebug 上写后续问题,我们可以帮助您。
我写了一个简单的模块来在 Ubuntu 虚拟机上安装一个包 (BioPerl)。整个 init.pp 文件在这里: https://gist.github.com/anonymous/17b4c31bf7309aff14dfdcd378e44f40
问题是它不起作用,它没有给我任何反馈让我知道它为什么不起作用。模块中有 3 个简单的步骤。我检查了一下,它没有做任何事情。这是前 2 个:
第 1 步:下载存档并将其保存到 /usr/local/lib
exec { 'bioperl-download':
command => "sudo /usr/bin/wget --no-check-certificate -O ${archive_path} ${package_uri}",
require => Package['wget']
}
第 2 步:提取存档
exec { 'bioperl-extract':
command => "sudo /usr/bin/tar zxvf ${archive_path} --directory ${install_path}; sudo rm ${archive_path}",
require => Exec['bioperl-download']
}
很简单。但我不知道问题出在哪里,因为我看不到它在做什么。配置器设置为详细模式,这是我的模块的输出行:
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-download]/returns: executed successfully
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-extract]/returns: executed successfully
==> default: Notice: /Stage[main]/Bioperl/Exec[bioperl-path]/returns: executed successfully
所以我只知道它成功执行了这三个步骤。它没有告诉我有关这些步骤是否正确完成工作的任何信息。我知道它没有将存档下载到 /usr/local/lib
那个目录,也没有将环境变量文件添加到 /usr/profile.d
。也许问题是包含目录的变量是错误的。可能包含档案下载 URI 的变量是错误的。我怎样才能找出这些东西呢?
更新:
事实证明该模块确实有效。但是为了改进模块(因为我想将它上传到 forge.puppetlabs.com
,我尝试实施 Matt 建议的更改。这是新代码:
file { 'bioperl-download':
path => "${archive_path}",
source => "http://cpan.metacpan.org/authors/id/C/CJ/CJFIELDS/${archive_name}",
ensure => "present"
}
exec { 'bioperl-extract':
command => "sudo /bin/tar zxvf ${archive_name}",
cwd => "${bioperl_target_dir}",
require => File['bioperl-download']
}
一个问题:它给我一个错误,告诉我源不能是 http://
。我在文档中看到他们确实允许 http://
文件作为 file
资源的来源。也许我使用的是旧版本的木偶?
我想试用 puppet-archive
模块,但不确定如何将其设置为必需的依赖项。我的意思是我如何确保它首先安装。我是否需要让我的模块从 github 下载模块并将其保存到模块目录?或者有没有办法让puppet自动安装?我将它添加为 metadata.json
文件的依赖项,但并没有安装它。我知道我可以让我的模块下载包,但我想知道最好的做法是什么。
您描述的最初问题是验收测试。验证您编写的 Puppet 资源和代码是否确实达到了您想要的最终状态,通常使用 Serverspec 来完成:http://serverspec.org/。例如,您可以编写一个 Puppet 模块来部署应用程序,但您只知道 Puppet 执行了您告诉它的操作,而不知道应用程序是否真的成功部署了。注意 Serverspec 也是人们通常用来为 Ansible 和 Chef 解决此问题的工具。
您可以编写类似于以下内容的 Serverspec 测试来帮助测试您的模块的结束状态:
describe file('/usr/local/lib/bioperl.tar.gz') do
it { expect(subject).to be_file }
end
describe file('/usr/profile.d/env_file') do
it { expect_subject).to be_file }
its(:content) { is_expected.to match(/env stuff/) }
end
但是,您的问题似乎还与调试验收测试失败的原因有关。为此,您需要进行单元测试。这通常用 RSpec-Puppet: http://rspec-puppet.com/ 来解决。我会向您展示如何针对您的情况编写一些测试,但我认为您不应该按照您的方式编写 Puppet 模块,这样会使单元测试变得无关紧要。
相反,请考虑使用具有 source
属性的 file
资源和 HTTP URI 来获取 tarball,而不是使用 wget
的 exec
:https://docs.puppet.com/puppet/latest/type.html#file-attribute-source. Also, you might want to consider using the Puppet archive module to assist you: https://forge.puppet.com/puppet/archive.
如果您对如何使用这些工具提供单元和验收测试有疑问,或者对如何重构您的模块有疑问,请不要犹豫,在 Whosebug 上写后续问题,我们可以帮助您。