Access attributes in serverspec tests excuted in a report handler

Hello,

I wrote a custom chef handler to execute some rspec tests:
class Handler < ::Chef::Handler
def report

result = RSpec::Core::Runner.run(existing_test_files)

end

end

Is it possible to access the attributes of the current chef run in these
rspec-tests? And when, how?
And if its not possible: Can I get the node or environment of the current chef
run in these rspec-tests? Then I would parse it’s jsonfile… .

Regards
iceweasel

I don't recommend this approach, because you're conflating the
variables of the system under test with the test vectors, so how do
you know if you have a valid test?

However, that said, this is the approach that the Chef Minitest
Handler takes, so you might want to check that out.

  • Julian

On Mon, Jun 23, 2014 at 9:40 AM, iceweasel.iceweasel@web.de wrote:

Hello,

I wrote a custom chef handler to execute some rspec tests:
class Handler < ::Chef::Handler
def report
...
result = RSpec::Core::Runner.run(existing_test_files)
...
end
...
end

Is it possible to access the attributes of the current chef run in these
rspec-tests? And when, how?
And if its not possible: Can I get the node or environment of the current chef
run in these rspec-tests? Then I would parse it's jsonfile.. .

Regards
iceweasel

--
[ Julian C. Dunn jdunn@aquezada.com * Sorry, I'm ]
[ WWW: Julian Dunn's Blog - Commentary on media, technology, and everything in between. * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]

iceweasel.iceweasel@web.de wrote:

Is it possible to access the attributes of the current chef run in
these rspec-tests? And when, how?

Julian C. Dunn responded:

I don't recommend this approach, because you're conflating the
variables of the system under test with the test vectors, so how do
you know if you have a valid test?

What do others recommend doing to verify, for example, that use of a
recipe results in a file existing in a location described in a node
attribute? The recipe I want to test includes golang::default,
downloads some source code, then creates files in a subdirectory of
node['go']['gopath'].

I'm considering these options:

  • Hard-coding the default values of applicable node attributes
    in my serverspec tests
  • Overriding the attributes in .kitchen.yml and hard-coding the
    override values in my tests
  • Dumping attributes with a helper cookbook, then loading
    the result in my tests
  • Figuring out where the "property" hash in the apache2
    cookbook's tests
    comes from and doing something similar

I would appreciate any related suggestions.

References:

--
Phil Mocek
https://mocek.org

I prefer this method:

  • Overriding the attributes in .kitchen.yml and hard-coding the
    override values in my tests

This provides a more 'stable' approach of stating with some determinism
that when providing some attributes at an input, the cookbook behaves in a
known manner.

If you need to test multiple outcomes, add more suites with different
attributes.

-M

On Thu, Feb 12, 2015 at 8:00 PM, Phil Mocek phil-lists@mocek.org wrote:

iceweasel.iceweasel@web.de wrote:

Is it possible to access the attributes of the current chef run in
these rspec-tests? And when, how?

Julian C. Dunn responded:

I don't recommend this approach, because you're conflating the
variables of the system under test with the test vectors, so how do
you know if you have a valid test?

What do others recommend doing to verify, for example, that use of a
recipe results in a file existing in a location described in a node
attribute? The recipe I want to test includes golang::default,
downloads some source code, then creates files in a subdirectory of
node['go']['gopath'].

I'm considering these options:

  • Hard-coding the default values of applicable node attributes
    in my serverspec tests
  • Overriding the attributes in .kitchen.yml and hard-coding the
    override values in my tests
  • [Dumping attributes with a helper cookbook][1], then loading
    the result in my tests
  • Figuring out where the "property" hash in the [apache2
    cookbook's tests][2] comes from and doing something similar

I would appreciate any related suggestions.

References:

[1]:
<
http://jakshi.com/blog/2014/05/12/accessing-chef-attributes-in-serverspec-tests/

[2]:
<
https://github.com/svanzoest/apache2-cookbook/blob/master/test/integration/default/serverspec/localhost/default_spec.rb

--
Phil Mocek
https://mocek.org

On Thu, Feb 12, 2015 at 5:00 PM, Phil Mocek phil-lists@mocek.org wrote:

iceweasel.iceweasel@web.de wrote:

Is it possible to access the attributes of the current chef run in
these rspec-tests? And when, how?

Julian C. Dunn responded:

I don't recommend this approach, because you're conflating the
variables of the system under test with the test vectors, so how do
you know if you have a valid test?

What do others recommend doing to verify, for example, that use of a
recipe results in a file existing in a location described in a node
attribute? The recipe I want to test includes golang::default,
downloads some source code, then creates files in a subdirectory of
node['go']['gopath'].

I'm considering these options:

  • Hard-coding the default values of applicable node attributes
    in my serverspec tests
  • Overriding the attributes in .kitchen.yml and hard-coding the
    override values in my tests
  • [Dumping attributes with a helper cookbook][1], then loading
    the result in my tests
  • Figuring out where the "property" hash in the [apache2
    cookbook's tests][2] comes from and doing something similar

The property hash comes from the per platform fixtures
at test/fixtures/platforms. They are per platform static attributes.
I would have just used the ones in .kitchen.yml, but with needing to test
permutations on each of the platforms, having
several suites would make it way to complex. The idea came from serverspec (
Serverspec - Advanced Tips),
I just applied it to chefspec as well.

See

See

-- Sander