Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing notification of custom LWRPs does not behave correctly #546

Closed
ghost opened this issue Dec 30, 2014 · 11 comments
Closed

Testing notification of custom LWRPs does not behave correctly #546

ghost opened this issue Dec 30, 2014 · 11 comments

Comments

@ghost
Copy link

ghost commented Dec 30, 2014

Imaging a simple LWRP test.

actions :create
attribute :name, name_attribute: true
action(:create) do
  new_resource.updated_by_last_action(new_resource.name == 'bar')
end

This means, that only if I create the resource test[bar], it fires its notifications.

test 'bar' do
  action :create
  notifies :restart, 'service[name]'
end

test[foo], shouldn't fire a notification.

test 'foo' do
  action :create
  notifies :restart, 'service[name]'
end

The appropriate tests could look like this:

describe 'test::default' do

  cached(:chef_run) {
    ChefSpec::ServerRunner.new.converge(described_recipe)
  }


  it 'should notify service[name] when test is called bar' do
    bar = chef_run.test('bar')
    expect(bar).to notify('service[name]').to(:restart)
  end

  it 'should NOT notify service[name] when test is called foo' do
    foo = chef_run.test('foo')
    expect(foo).to_not notify('service[name]').to(:restart)
  end

end

And the matcher

ChefSpec.define_matcher :test

def test(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:test, :create, resource_name)
end

Now, when you run this test, it's saying:

     Failure/Error: expect(foo).to_not notify('service[name]').to(:restart)
       expected "test[foo]" to not notify "service[name]", but it did.
@sethvargo
Copy link
Contributor

You need to step_into the LWRP if you want to test this.

@ghost
Copy link
Author

ghost commented Dec 30, 2014

I tried to step_into the LWRP with no effect.

@sethvargo
Copy link
Contributor

@yves-vogl it should be notifies, not notify

@ghost
Copy link
Author

ghost commented Dec 30, 2014

Sorry - I fixed it in the example above. But error was just in the example.
How can I help to debug this?

@sethvargo
Copy link
Contributor

@yves-vogl can you put together a PR that shows the failing test? Please use the cucumber tests and follow the existing patterns in place.

@ghost
Copy link
Author

ghost commented Dec 30, 2014

Give a me a few days and I'll get back to you with a PR

@dblessing
Copy link

ChefSpec is testing the catalog. So, it is correct to say each of those resources does cause a notification to be set up in the catalog. You need an integration test to determine if a specific event causes a notification to actually be sent. This is my understanding of what you're describing here.

@ghost
Copy link
Author

ghost commented Jan 4, 2015

@sethvargo Could you please reopen this issue?

@sethvargo
Copy link
Contributor

Please see my response on your PR

@sethvargo
Copy link
Contributor

@yves-vogl also, @dblessing gave a much shorter explanation than mine, which is correct.

@till
Copy link

till commented Mar 19, 2016

@sethvargo I spent a good amount of time on the same problem before I found this Github issue.

I totally get the explanation, so thanks for that.

However, I am wondering if since ChefSpec will always assume that resources notify, maybe something like expect(resource).not_to notify('something[else]') should be added to the documentation so people do not try this and instead do an integration test to start with?

Maybe I am not sure where to draw the line between ChefSpec and actual integration tests.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants