1

I'm trying to check my spec test with bundle exec rspec for the following test

require 'spec_helper'

describe 'my_recipe::default' do
  windows_platforms = {
    windows: %w(2008R2 2012 2012R2 2016 2019)
  }

  windows_platforms.each do |platform, versions|
    versions.each do |version|
      context "When all attributes are default, on #{platform} #{version}" do
        let(:chef_run) do
          runner = ChefSpec::ServerRunner.new(platform: platform.to_s, version: version) do |node|
            node.override['domain'] = 'mydomain.com'
          end
          runner.converge(described_recipe)
        end

        it 'converges successfully' do
          expect { chef_run }.to_not raise_error
        end
      end
    end
  end
end

Here's example of my default attribute file:

default['wsus'] =
  case node['domain']
  when 'mydomain.com'
    "do something"
  else
    raise 'Domain cannot be determined.'
  end

From what I understand 'mydomain.com' should be assigned as default['domain'] but seems like this isn't the case.

Here's the error I get:

expected no Exception, got #<RuntimeError: Domain cannot be determined.> with backtrace:

Does anyone have suggestion why the test won't take the overridden attribute?

p.s. If I'm not making sense, please excuse me. This is my first post on stackoverflow :(

pmamueng
  • 11
  • 1

1 Answers1

0

Your code is well, and it should be working as expected with many other attributes, but not domain. Point is - domain is special - it is automatic attribute set by Ohai (or in test case by Fauxhai) and it has maximum priority, which cannot be overwritten by override attribute. To mock automatic attributes you need automatic key.

runner = ChefSpec::ServerRunner.new(platform: platform.to_s, version: version) do |node|
  node.automatic['domain'] = 'mydomain.com'
end

The whole list of automatic attributes is available in Chef docs on Ohai.

Draco Ater
  • 20,820
  • 8
  • 62
  • 86