如何 mock 函数?
解读
在 Puppet 生态里,“函数”既包括 Puppet DSL 自定义函数(written in Ruby 或 the Puppet language),也包括 Bolt 计划里调用的 Ruby 方法。面试官问“如何 mock 函数”,想确认三件事:
- 你是否理解 Puppet 的编译期与运行期边界;
- 你是否能在不触碰真实资源的前提下,让 rspec-puppet、rspec-puppet-facts、bolt-spec 等测试框架把函数返回值“伪造”出来;
- 你是否知道国内 CI 环境(GitLab-EE、Jenkins on K8s、阿里云效、腾讯蓝盾)里做单元测试的最佳实践。
回答时要区分“Puppet 函数”与“Bolt 计划函数”,并给出可落地的代码片段,否则会被追问“你这段代码在 CI 里跑过吗?”
知识点
- 编译期 vs. 运行期:Puppet 函数在 catalog 编译阶段执行,不在 agent 端。
- 测试金字塔:unit(函数级)→ component(class 级)→ integration(agent 跑真实系统)。mock 只出现在 unit 层。
- 国内常用 gem:
– rspec-puppet(≥2.11)
– rspec-puppet-facts(自动注入 fact mocks)
– puppetlabs-spec-helper(提供Rakefile骨架)
– mocha(Ruby mock 库,和 rspec 无缝集成) - 三类函数入口:
–lib/puppet/functions/<namespace>/<name>.rb(现代 namespaced 函数)
–lib/puppet/parser/functions/<name>.rb(老版 3.x 函数)
–plans/目录下的 Bolt Ruby 计划(Puppet::Pal::ScriptCompiler加载)。 - 国内合规痛点:金融、运营商要求“测试必须可重复、离线运行”,因此 mock 必须零外部依赖,不能访问
puppet.com或apt.postgresql.org。
答案
下面给出国内流水线可直接落地的三段代码,分别覆盖“现代 Puppet 函数”“老版 parser 函数”“Bolt 计划函数”。
- 现代 namespaced 函数 mock(推荐新项目) 目录结构:
mod/
├── functions/
│ └── profile/mysql/root_pw.rb
├── spec/
│ └── functions/
│ └── profile_mysql_root_pw_spec.rb
spec/functions/profile_mysql_root_pw_spec.rb:
require 'spec_helper'
describe 'profile::mysql::root_pw' do
# 伪造函数内部调用的 vault::get
before(:each) do
Puppet::Functions.create_function(:'vault::get') do
dispatch :mock_get do
param 'String', :path
end
def mock_get(path); 'MOCK_SECRET'; end
end
end
it 'returns mocked vault secret' do
is_expected.to run.with_params('mysql/creds/root').and_return('MOCK_SECRET')
end
end
关键点:
- 用
Puppet::Functions.create_function在测试进程里动态注册同名函数,覆盖真实实现。 - 无需修改
Rakefile,bundle exec rake spec即可跑。 - 在
.gitlab-ci.yml里加:
unit:
image: registry.cn-hangzhou.aliyuncs.com/puppet/pdk:3.0.1
script:
- bundle install --deployment --path vendor
- bundle exec rake spec
cache:
paths: [vendor/]
- 老版 parser 函数 mock(存量 3.x 代码) spec/functions/parser_function_mock_spec.rb:
require 'spec_helper'
describe 'get_mysql_pw' do
before(:each) do
Puppet::Parser::Functions.newfunction(:get_mysql_pw, type: :rvalue) do |args|
'LEGACY_MOCK_PW'
end
end
it { is_expected.to run.with_params.and_return('LEGACY_MOCK_PW') }
end
注意:老函数是全局命名空间,mock 后需 Puppet::Parser::Functions.reset 防止交叉污染,可在 after(:each) 里调用。
- Bolt 计划函数 mock(DevOps 流水线常见) plans/deploy_db.pp:
plan myapp::deploy_db(
TargetSpec $nodes,
) {
$pw = run_function('vault::get', 'mysql/creds/root')
run_task('mysql::set_root_pw', $nodes, password => $pw)
}
spec/plans/deploy_db_spec.rb:
require 'spec_helper'
require 'bolt_spec/plans'
describe 'myapp::deploy_db' do
include BoltSpec::Plans
let(:nodes) { ['node1.example.com'] }
it 'mocks vault::get and executes plan' do
allow_call_function('vault::get').with_args('mysql/creds/root').and_return('BOLT_MOCK_PW')
expect_task('mysql::set_root_pw').with_params(password: 'BOLT_MOCK_PW')
run_plan('myapp::deploy_db', nodes: nodes)
end
end
在蓝盾流水线里:
- name: unit-test
image: registry.cn-hangzhou.aliyuncs.com/puppet/bolt:3.27
commands:
- bundle install
- bundle exec rspec spec/plans
总结回答话术:
“国内金融客户要求离线、可重复,因此我优先用 rspec-puppet 动态创建同名函数覆盖真实实现;对 Bolt 计划则用 bolt-spec 的 allow_call_function。整个 mock 过程零外部依赖,可在阿里云效私有 runner 里 30 秒内跑完。”
拓展思考
- 如果函数内部用
Puppet::Util::Execution.execute调了系统命令,如何 mock?
答:用 Ruby 的Open3桩,或rspec-mocks的allow(Puppet::Util::Execution).to receive(:execute),返回double(exitstatus: 0, stdout: 'mock_output')。 - 当函数依赖 Hiera 数据,如何一起 mock?
答:在spec/spec_helper.rb里加let(:hiera_data) { { 'profile::mysql::root_pw' => 'hiera_mock' } },rspec-puppet 会自动注入。 - 国内信创环境(鲲鹏 + 麒麟)下,Ruby 2.7 性能差,mock 跑不动怎么办?
答:把vendor/bundle提前在 x86 上bundle package --all-platforms,再拷贝到鲲鹏 runner;或者把函数拆成纯 Puppet 语言,减少 Ruby 调用。 - 如果团队使用自研“Puppet 函数市场”,如何确保 mock 与真实实现同步?
答:在函数仓库里加examples/mock/目录,随版本发布;消费方用git submodule锁定,CI 里跑rspec时自动引用同版本 mock。