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

Not able to stub library cookbook method #549

Closed
haad opened this Issue Jan 7, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@haad

haad commented Jan 7, 2015

In my cookbook I'm using code like this

::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
node.set['ssl_keystore_pass'] = secure_password

I would like to stub secure_password method in my spec files to make it work with my tests.

  before do
    allow_any_instance_of(Chef::Recipe).to receive(:secure_password).and_return('VerySecurePasswordForTest')
  end

  it 'should include required java/ark recipes' do
    expect(chef_run).to include_recipe('java::default')
    expect(chef_run).to include_recipe('ark::default')
  end

with these settings in place it still doesn't work and I'm getting same error

  1) rsdtomcat::default should include required java/ark recipes
     Failure/Error: end.converge(described_recipe)
     NoMethodError:
       undefined method `secure_password' for Opscode::OpenSSL::Password:Module
     # /var/folders/yq/_h9bmb6x1qxb7p4_w5xhkgx80000gn/T/d20150107-31232-9gzxp7/cookbooks/rsdtomcat/recipes/default.rb:30:in `block in from_file'
     # /var/folders/yq/_h9bmb6x1qxb7p4_w5xhkgx80000gn/T/d20150107-31232-9gzxp7/cookbooks/rsdtomcat/recipes/default.rb:24:in `from_file'
     # ./spec/unit/recipes/default.rb:8:in `block (2 levels) in <top (required)>'
     # ./spec/unit/recipes/default.rb:19:in `block (2 levels) in <top (required)>'
@sethvargo

This comment has been minimized.

Show comment
Hide comment
@sethvargo

sethvargo Jan 7, 2015

Collaborator

@haad does this recipe actually converge? It looks like you have a bug in that module.

For Chef, I don't recommend every mixing a module into a recipe as I think it's horrible practice and makes things like this more difficult to test. Why not make Opscode::OpenSSL::Password a self-extending module or class?

module Opscode
  module OpenSSL
    class Password
      def self.secure_password
        # ...
      end
    end
  end
end

Then invoke it like Opscode::OpenSSL::Password.secure_password and stub it like allow(Opscode::OpenSSL::Password).to receive(:secure_password).

Collaborator

sethvargo commented Jan 7, 2015

@haad does this recipe actually converge? It looks like you have a bug in that module.

For Chef, I don't recommend every mixing a module into a recipe as I think it's horrible practice and makes things like this more difficult to test. Why not make Opscode::OpenSSL::Password a self-extending module or class?

module Opscode
  module OpenSSL
    class Password
      def self.secure_password
        # ...
      end
    end
  end
end

Then invoke it like Opscode::OpenSSL::Password.secure_password and stub it like allow(Opscode::OpenSSL::Password).to receive(:secure_password).

@sethvargo sethvargo closed this Jan 7, 2015

@haad

This comment has been minimized.

Show comment
Hide comment
@haad

haad Jan 7, 2015

This would require that I would fork community openssl cookbook which is not what I want if it's not necessary. Is there any other way to get this working ?

haad commented Jan 7, 2015

This would require that I would fork community openssl cookbook which is not what I want if it's not necessary. Is there any other way to get this working ?

@sethvargo

This comment has been minimized.

Show comment
Hide comment
@sethvargo

sethvargo Jan 7, 2015

Collaborator

@haad blah omg that code is so horrible inefficient! (sorry I digress). Do this instead:

require "securerandom"
node.set['ssl_keystore_pass'] = SecureRandom.hex(20) # or SecureRandom.base64(20)

Then you can stub SecureRandom pretty easily:

allow(SecureRandom).to receive(:hex).and_return("whatever")

SecureRandom is also a far-better implementation (and more secure) than what is current in the OpenSSL cookbook.

Collaborator

sethvargo commented Jan 7, 2015

@haad blah omg that code is so horrible inefficient! (sorry I digress). Do this instead:

require "securerandom"
node.set['ssl_keystore_pass'] = SecureRandom.hex(20) # or SecureRandom.base64(20)

Then you can stub SecureRandom pretty easily:

allow(SecureRandom).to receive(:hex).and_return("whatever")

SecureRandom is also a far-better implementation (and more secure) than what is current in the OpenSSL cookbook.

@sethvargo

This comment has been minimized.

Show comment
Hide comment
@sethvargo

sethvargo Jan 7, 2015

Collaborator

It bugged me so much that I even submitted a PR to make it better: chef-cookbooks/openssl#18

Collaborator

sethvargo commented Jan 7, 2015

It bugged me so much that I even submitted a PR to make it better: chef-cookbooks/openssl#18

@lock lock bot locked as resolved and limited conversation to collaborators Aug 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.