mardi 28 juillet 2015

chefspec/chef-rewind - How does one unit test a chef-rewind 'unwind' action?

I have written a wrapper cookbook which successfully unwinds part of an upstream recipe (ceph::mon). The original recipe was failing to converge, because it was attempting to carry out the now unwound block prior to the required services being up and running. The original block was as follows:

if node['ceph']['user_pools']
  # Create user-defined pools
  node['ceph']['user_pools'].each do |pool|
    ceph_pool pool['name'] do
      pg_num pool['pg_num']
      create_options pool['create_options'] if pool['create_options']
    end
  end

I unwound this block with chef-rewind and placed this part in a new wrapper recipe which now executes further down the runlist, when the aforementioned services are up.

The unwind block in my wrapper recipe is as follows:

 if node['ceph']['user_pools']
   # Create user-defined pools
   node['ceph']['user_pools'].each do |pool|
     # unwind user-defined pools
     unwind "ceph_pool[#{pool['name']}]"
   end
 end

This works as expected. My issue occurs when I attempt to test the unwind action with chefspec. The chefspec test is as follows:

# encoding UTF-8
require_relative '../spec_helper'

describe 'ceph_lwrp::mon' do
  let(:chef_run) do

    # Sets node attributes
    ChefSpec::Runner.new do |node|
      node.set['ceph']['user_pools'] = [{name: "volumes", pg_num: 128},{name: "images",pg_num: 128}]
    end.converge(described_recipe)
  end

  # Tests the chef-rewind 'unwind' action from the parent recipe
  it 'unwinds ceph user pools create' do
    expect(chef_run).to execute("unwind ceph_pool[#{pool['name']}]")
  end
end

Originally the chefspec test would not recognise the reference to chef/rewind in the working target recipe:

LoadError
---------
cannot load such file -- chef/rewind

I got around this by:
- adding gem 'chef-rewind' to the Gemfile of the wrapper cookbook
- Running 'bundle install' to update the cache for chefspec.

This brought me to the a new error, where it attempts attemps to connect to a non-existent chefserver:

2015-07-28T14:27:34+01:00] WARN: We are genereting a new uuid for fsid
[2015-07-28T14:27:34+01:00] ERROR: Connection refused connecting to https://localhost/nodes/fauxhai.local, retry 1/5
[2015-07-28T14:27:39+01:00] ERROR: Connection refused connecting to https://localhost/nodes/fauxhai.local, retry 2/5
[2015-07-28T14:27:44+01:00] ERROR: Connection refused connecting to https://localhost/nodes/fauxhai.local, retry 3/5
[2015-07-28T14:27:49+01:00] ERROR: Connection refused connecting to https://localhost/nodes/fauxhai.local, retry 4/5
[2015-07-28T14:27:54+01:00] ERROR: Connection refused connecting to https://localhost/nodes/fauxhai.local, retry 5/5

================================================================================
Recipe Compile Error in /Users/mick.mccarthy/workspace/cookbooks/ceph_lwrp/vendor/cookbooks/ceph_lwrp/recipes/mon.rb
================================================================================

Errno::ECONNREFUSED
-------------------
Connection refused - Connection refused connecting to https://localhost/nodes/fauxhai.local, giving up

I've been searching online and cannot find any examples of a scenario where someone has unit tested (through Chefspec or any other means) a chef-rewind action. I'm wondering if this is even possible. Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire