Chef 14 Upgrade: Change in ValidationFailed error messages when setting required properties

Featured image for sharing metadata for article

While upgrading one of my cookbooks to Chef 14.5.33 (ChefDK 3.3.23), I noticed some test failures around the title of exceptions raised by Chef's validation on required properties within custom resources.

I had this issue where I had a caddy resource:

resource_name :caddy
provides :caddy

property :site_type, String, required: true

And the following spec:

describe '...' do
  context 'When site type is an empty string' do
    # ...

    it 'throws an error' do
      expect { chef_run.converge(described_recipe) }.to\
        raise_error(Chef::Exceptions::ValidationFailed, %r{site_type is required})
    end
  end
end

This would unfortunately fail the ChefSpec run, with the following error:

1) cookbook-spectat::_caddy_resource When no site type is set throws an error
   Failure/Error:
     expect { chef_run.converge(described_recipe) }.to\
       raise_error(Chef::Exceptions::ValidationFailed, %r{site_type is required})

     expected Chef::Exceptions::ValidationFailed with message matching /site_type is required/, got #<Chef::Exceptions::ValidationFailed: site_type is a required property>
       # /tmp/d20181112-30712-qz3b7a/cookbooks/cookbook-spectat/recipes/_caddy_resource.rb:39:in `block in from_file'
       # /tmp/d20181112-30712-qz3b7a/cookbooks/cookbook-spectat/recipes/_caddy_resource.rb:37:in `from_file'
       # ./spec/unit/resources/caddy_spec.rb:402:in `block (4 levels) in <top (required)>'
       # ./spec/unit/resources/caddy_spec.rb:402:in `block (3 levels) in <top (required)>'
   # ./spec/unit/resources/caddy_spec.rb:402:in `block (3 levels) in <top (required)>'

This was odd, as running in Chef 13.6.4 (ChefDK 2.4.17) worked successfully.

As the error suggests, it's due to a change in the error's message, which is resolved with the very straightforward change:

 describe 'cookbook-spectat::_caddy_resource' do
   context 'When site type is an empty string' do
     # ...

     it 'throws an error' do
       expect { chef_run.converge(described_recipe) }.to\
-         raise_error(Chef::Exceptions::ValidationFailed, %r{site_type is required})
+         raise_error(Chef::Exceptions::ValidationFailed, %r{site_type is a required property})
     end
   end
 end

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #chef-14-upgrade #chef #chefspec #chef-14.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.