[chef] Re: Re: Re: Using node attributes in Kitchen ServerSpec tests


Chronological Thread 
  • From: Fabien Delpierre < >
  • To: chef < >
  • Subject: [chef] Re: Re: Re: Using node attributes in Kitchen ServerSpec tests
  • Date: Thu, 14 May 2015 09:01:40 -0400

Thanks for finding that!

On Tue, May 12, 2015 at 10:10 PM, John Osborne < " target="_blank"> > wrote:
Fabien-

I implemented exactly what you’re looking for by following the test fixture cookbook approach discussed in this blog article [1]. Using this, I can reference the node attributes within my ServerSpec tests.


From: Cassiano Leal
Reply-To: " " target="_blank"> "
Date: Tuesday, May 12, 2015 at 7:03 PM
To: Fabien Delpierre, " " target="_blank"> "
Subject: [chef] Re: Using node attributes in Kitchen ServerSpec tests

The “weird pseudocodey language used by RSpec” is called Ruby, and it’s definitely not pseudo in any sense. :)

It’s true that RSpec implements a DSL on top of Ruby, very much like Chef itself. It’s still 100% Ruby in both cases though.

Now, about your specific problem, I don’t think there is a way to do what you want.

In any case, ask yourself if you really need to test that the packages are installed. If the package provides a service, you can make ServerSpec verify that the service is running (it’s got a resource for that), or that a certain port has a listening TCP socket or something else. Think about the actual functionality you want to assert instead of the method used to put it there.

If you still see value in testing that specific packages were installed, then I guess you’ll have to be explicit in your tests as well as on the code.

-- 
Cassiano Leal


On 12 May 2015 at 22:18:51, Fabien Delpierre ( " target="_blank"> ) wrote:

Hi folks,
I'm new to Test Kitchen and trying to make sense of it all. I'm also new to TDD (and programming in general) so the weird pseudocodey language used by RSpec messes with my head because I'm expecting code and it reads more like English.
Anyway.
Right now I'd like to see if I can use one of my nodes' attributes to run tests.
Specifically, I have something like this:

attributes/default.rb:
case node['platform']
when 'centos'
  default['myrecipe']['packages'] = %w(package1 package2)
when 'ubuntu'
  default['myrecipe']['packages'] = %w(package3 package4)
end

recipes/default.rb:
node['myrecipe']['packages'].each do |pkg|
  package pkg
end

Now, in my tests, what I'd like is to do something like:

node['myrecipe']['packages'].each do |pkg|
  describe package(pkg) do
    it { should be_installed }
  end
end

I can do something like this (note I haven't tested it because I'm done for today but I looked at other people's code on GitHub and found similar things, so I imagine that it works):
case os['family']
when 'centos'
  packages = %w(package1 package2)
when 'ubuntu'
  packages = %w(package3 package4)
end

packages.each do |pkg|
  describe package(pkg) do
    it { should be_installed }
  end
end

So assuming this works, that's nice, but it requires maintaining on top of the list of attributes in the attributes/default.rb file, hence my wondering whether there is a way for Kitchen to grab those values off the cookbook. Because that would be more efficient.

Is that possible? Or am I doomed?





Archive powered by MHonArc 2.6.16.

§