Chef_environment

I am looking for a way to override the node.chef_environment either with a kitchen.yml or a policy file.

The backstory is we have cookbooks written for the old role base instead of policy files. These cookbooks have items in them looking for certain chef_environment and branch code off of them. In lieu of changing the code, I was looking for a way to test them in test kitchen. It appears test kitchen, when using a policy file sets the chef_environment to “local” which is fine, we can code to handle that but were seeing if there was a more elegant way to set it to say “dev” at the kitchen or policy level for testing.

You can set the client.rb to use a specific environment as described here Chef Infra . otherwise the node.environment should be set to the policy_group in test-kitchen environment or policy_group both get set to local.

I saw that setting, but it says it requires an environments_path, but I haven’t found documentation on what that requires.

When I try to configure it with the client_rb I get the following error:

Running handlers:
[2025-09-02T19:18:10+00:00] ERROR: Running exception handlers
Running handlers complete
[2025-09-02T19:18:10+00:00] ERROR: Exception handlers complete
Infra Phase failed. 0 resources updated in 00 seconds
[2025-09-02T19:18:10+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
[2025-09-02T19:18:10+00:00] FATAL: ---------------------------------------------------------------------------------------
[2025-09-02T19:18:10+00:00] FATAL: PLEASE PROVIDE THE CONTENTS OF THE stacktrace.out FILE (above) IF YOU FILE A BUG REPORT
[2025-09-02T19:18:10+00:00] FATAL: ---------------------------------------------------------------------------------------
[2025-09-02T19:18:10+00:00] FATAL: Chef::PolicyBuilder::Policyfile::UnsupportedFeature: Policyfile does not work with an Environment configured.

Set the provisioner.policy_group property in the kitchen.yml file, like so:

provisioner:
  policy_group: kitchen

That will set both node.policy_group and node.chef_environment to kitchen during your Test Kitchen converge.

For example, the following Test Kitchen recipe:

# recipes/default.rb

log "node.policy_group: #{node.policy_group}"
log "node.chef_environment: #{node.chef_environment}"

Will log:

[2025-09-03T16:50:25+00:00] INFO: node.policy_group: kitchen
[2025-09-03T16:50:25+00:00] INFO: node.chef_environment: kitchen
1 Like

@decoyjoe Thank you, that is what I needed!