2016-09-22 12:43:57 +00:00
---
title: About the os_env Resource
2018-02-16 00:28:15 +00:00
platform: os
2016-09-22 12:43:57 +00:00
---
# os_env
2019-04-26 18:24:29 +00:00
Use the `os_env` Chef InSpec audit resource to test the environment variables for the platform on which the system is running.
2016-09-22 12:43:57 +00:00
2017-10-03 21:35:10 +00:00
<br>
2018-08-09 12:34:49 +00:00
## Availability
### Installation
2019-04-26 18:24:29 +00:00
This resource is distributed along with Chef InSpec itself. You can use it automatically.
2018-08-09 12:34:49 +00:00
### Version
This resource first became available in v1.0.0 of InSpec.
2016-09-27 19:03:23 +00:00
## Syntax
2016-09-22 12:43:57 +00:00
A `os_env` resource block declares an environment variable, and then declares its value:
describe os_env('VARIABLE') do
2018-02-01 10:51:12 +00:00
its('property') { should eq 1 }
2016-09-22 12:43:57 +00:00
end
where
* `('VARIABLE')` must specify an environment variable, such as `PATH`
* `matcher` is a valid matcher for this resource
2017-10-03 21:35:10 +00:00
<br>
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
## Examples
2016-09-22 12:43:57 +00:00
2019-04-26 18:24:29 +00:00
The following examples show how to use this Chef InSpec audit resource.
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
### Test the PATH environment variable
2016-09-22 12:43:57 +00:00
describe os_env('PATH') do
its('split') { should_not include('') }
its('split') { should_not include('.') }
end
2018-04-26 19:10:14 +00:00
### Test the Path environment variable by specifying the target Environment (Windows)
On windows a User's environment variable may obscure the local machine (system) environment variable. The correct environment variable may be tested as follows:
describe os_env('PATH', 'target') do
its('split') { should_not include('') }
its('split') { should_not include('.') }
end
where
2019-04-26 18:24:29 +00:00
* `'target'` may be either `system` or `user`
2018-04-26 19:10:14 +00:00
2019-04-26 18:24:29 +00:00
### Test Chef Habitat environment variables
2016-09-22 12:43:57 +00:00
2019-04-26 18:24:29 +00:00
Chef Habitat uses the `os_env` resource to test environment variables. The environment variables are first defined in a whitespace array, after which each environment variable is tested:
2016-09-22 12:43:57 +00:00
hab_env_vars = %w(HAB_AUTH_TOKEN
HAB_CACHE_KEY_PATH
HAB_DEPOT_URL
HAB_ORG
HAB_ORIGIN
HAB_ORIGIN_KEYS
HAB_RING
HAB_RING_KEY
HAB_STUDIOS_HOME
HAB_STUDIO_ROOT
HAB_USER)
hab_env_vars.each do |e|
describe os_env(e) do
its('content') { should eq nil }
end
end
2017-10-03 21:35:10 +00:00
<br>
## Matchers
2018-02-16 03:07:18 +00:00
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
2017-10-03 21:35:10 +00:00
### content
The `content` matcher return the value of the environment variable:
its('content') { should eq '/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin' }
### split
2017-10-12 06:45:37 +00:00
The `split` matcher splits the value of the environment variable with the `:` deliminator (use the `;` deliminator if Windows):
2017-10-03 21:35:10 +00:00
2017-10-12 06:45:37 +00:00
its('split') { should include ('/usr/bin') }
2017-10-03 21:35:10 +00:00
2017-10-12 06:45:37 +00:00
Note: the `split` matcher returns an array including `""` for cases where there is a trailing colon (`:`), such as `dir1::dir2:`