InSpec os_env resource thinks environment variable is set when it is not


#1

I can't seem to get the os_env InSpec resource to work. It thinks an env var is set when it isn't. Can anybody see what I'm doing wrong here?

$ uname
Darwin

$ cat test.rb
# Confirm FOO is set and not the empty string
describe os_env('FOO') do
  its('content') { should_not eq '' }
end

$ unset FOO

$ inspec exec ./test.rb 

Profile: tests from ./test.rb (tests from ..test.rb)
Version: (not specified)
Target:  local://

  Environment variable FOO
     ✔  content should not eq ""

Test Summary: 1 successful, 0 failures, 0 skipped

I'm using InSpec 1.51.21 that comes with ChefDK 2.5.3.


#2

I think I understand now. An unset env var will pass the test not eq "" because it's nil.

Similarly, an env var that is set to the empty string will pass the test not eq nil.

So I think what I need is:

# Confirm FOO is set and not the empty string
describe os_env('FOO') do
  its('content') { should_not eq nil }
  its('content') { should_not eq '' }
end

Is there a simpler construct to test that an env var is set and not empty?
 


UPDATE #1

Argh. I thought I had it. But now this: this works on Linux:

$ cat test.rb
# Confirm GEM_HOME is set and not the empty string
describe os_env('GEM_HOME') do
  its('content') { should_not eq nil }
  its('content') { should_not eq '' }
end

e20103@apadmlsq0104d (e20103_HC-6494) controls
$ env | grep GEM_HOME
GEM_HOME=/home/e20103/.chefdk/gem/ruby/2.4.0

e20103@apadmlsq0104d (e20103_HC-6494) controls
$ inspec exec ./test.rb 

Profile: tests from ./test.rb (tests from ..test.rb)
Version: (not specified)
Target:  local://

  Environment variable GEM_HOME
     ✔  content should not eq nil
     ✔  content should not eq ""

Test Summary: 2 successful, 0 failures, 0 skipped

But not on Mac:

$ cat test.rb
# Confirm GEM_HOME is set and not the empty string
describe os_env('GEM_HOME') do
  its('content') { should_not eq nil }
  its('content') { should_not eq '' }
end

e20103@LCHI094369 (e20103_HC-6494:LC) controls
$ env | grep ^GEM_HOME
GEM_HOME=/Users/e20103/.chefdk/gem/ruby/2.4.0

e20103@LCHI094369 (e20103_HC-6494:LC) controls
$ inspec exec ./test.rb 

Profile: tests from ./test.rb (tests from ..test.rb)
Version: (not specified)
Target:  local://

  Environment variable GEM_HOME
     ∅  content should not eq nil
     
     expected: value != nil
          got: nil
     
     (compared using ==)

     ✔  content should not eq ""

Test Summary: 1 successful, 1 failure, 0 skipped

What might account for the different behavior on macOS (10.12.6) and RHEL 7.5?

 

UPDATE #2*

The problem seems to go away when I user a new version of InSpec (3.0.52).