Feedback on Ruby / ChefSpec coding style
Asked Answered
W

1

6

I'm rather new to Ruby, but I have been doing a lot of research on Chef testing for the past two weeks. This test uses ChefSpec & Fauxhai, but it doesn't look very "ruby-ish" and I was hoping the community could give me some pointers on coding style. Is there a better way to write a nested loop like this?

cookbooks/foo/recipes/default.rb

package "foo" do
  action :install
end

cookbooks/foo/spec/default_spec.rb

require 'chefspec'

describe 'foo::default' do
  platforms = { 
    "debian"   => ['6.0.5'],
    "ubuntu"   => ['12.04', '10.04'],
    "centos"   => ['5.8', '6.0', '6.3'],
    "redhat"   => ['5.8', '6.3'],
    "mac_os_x" => ['10.6.8', '10.7.4', '10.8.2'],
    "windows"  => ['2008R2']
  }

  platforms.each do |platform,versions|
    versions.each do |version|
      context "on #{platform} #{version}" do
        before do
          Fauxhai.mock(platform: platform, version: version)
        end

        it 'should install foo' do
          @runner = ChefSpec::ChefRunner.new.converge('foo::default')
          @runner.should install_package 'foo'
        end
      end
    end
  end
end

Any and all feedback is welcome. Thank you!

Witchcraft answered 5/3, 2013 at 13:42 Comment(2)
github.com/bbatsov/ruby-style-guide is a good general resource for suggested Ruby coding guidelines. I don't think there's much you could do to clean up those nested loops while maintaining readability.Trix
I've read through the guide but I don't see much I can improve either. Thanks for the feedback!Witchcraft
A
6

First, a common practice is to extract ChefRunner instantiation to let helper. You can also include all Fauxhai configuration there:

let(:chef_run) do
  ChefSpec::ChefRunner.new(platform: platform, version: version) do |node|
    node.set['foo']['bar'] = 'baz'
    # ....
  end.converge('foo::default')
end

it "installs foo" do
  expect(chef_run).to install_package 'foo'
end

The expect syntax seems to be recommended over should. But in this example I would use a one-liner:

subject do
  ChefSpec::ChefRunner.new(platform: platform, version: version).converge('foo::default')
end
it { should install_package 'foo' }

To clean up the looping a bit you can use RSpec's shared examples. A bit more extended example:

require 'chefspec'

shared_examples 'foo' do |platform, version|
  context "on #{platform} #{version}" do
    let(:users) { %w[user1 user2] }
    let(:chef_run) do
      ChefSpec::ChefRunner.new(platform: platform, version: version) do |node|
        node.set['foo']['users'] = users
      end.converge('foo::default')
    end
    subject { chef_run }

    it { should install_package 'foo' }

    it "creates specified users" do
      users.each { |u| expect(chef_run).to create_user u }
    end
  end
end

describe 'foo::default' do
  platforms = {
    'debian'   => ['6.0.5'],
    'ubuntu'   => ['12.04', '10.04'],
    'centos'   => ['5.8', '6.0', '6.3'],
    'redhat'   => ['5.8', '6.3'],
    'mac_os_x' => ['10.6.8', '10.7.4', '10.8.2'],
    'windows'  => ['2008R2']
  }

  platforms.each do |platform, versions|
    versions.each do |version|
      include_examples 'foo', platform, version
    end
  end
end
Auriferous answered 2/7, 2013 at 17:28 Comment(1)
Another way might be to use RSpec tags (describe 'foo', platforms: [...] do ...) and loop the shared examples based on that.Haunt

© 2022 - 2024 — McMap. All rights reserved.