Trying to adopt the Poise/Halite pattern, having testing issues


#1

I’ve been banging my head against the proverbial brick wall here for a bit
and I think it’s time to ask for some help.

The Code

I present: https://github.com/vancluever/ssh_keygen_halite

Basically a library-based “HWRP” designed to make it easy to generate SSH keys.

A couple of things I am aware of:

  • I might remove the dependency on sshkey as this stuff should be easy
    enough to do with openssl and net/ssh (are these both standard library?
    I can see openssl but didn’t see net/ssh on ruby-doc). I am aware of
    some issues with using chef_gem in library resources as well which gives
    me another reason to de-couple it.
  • The code needs some cleanup (I have butchered it a bit trying to fix this)
  • I might not be doing ChefSpec right in a way that’s unrelated to
    Poise/Halite.
  • My greater knowledge of Ruby project management is quite novice. In fact,
    this cookbook is an effort to try and teach myself how to use these tools.

My intentions here:

  • Write a resource using the Poise pattern.
  • Build it with the Halite pattern.
  • Use the Halite helpers to simply my tests (ie: inline recipes)
  • Do all of this using as few dependencies as possible (I’m deliberately
    not using poise-boiler right now as it seems unrelated to the pattern itself
    and more related to the poise-* cookbooks).

Ultimately, I’d like to get this to a point where I can re-use the skeleton of
the code for other Poise-based resources, and maybe tool its generation (ie:
a la berks cookbook).

The Problem

The generated resources seem to be empty or completely missing!

$bundle exec rspec test/spec/
...
1) SSHKeygen::Resource
   Failure/Error: it { is_expected.to create_file('/root/.ssh/id_rsa') }
   NoMethodError:
     ssh_keygen[/root/.ssh/id_rsa] (/.../ssh_keygen_halite/test/spec/resources/ssh_keygen_spec.rb line 7) had an error: NoMethodError: undefined method `owner' for nil:NilClass
     # ./lib/ssh_keygen/provider.rb:45:in `block (2 levels) in save_private_key'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/resource_builder.rb:77:in `instance_eval'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/resource_builder.rb:77:in `build'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/dsl/recipe.rb:108:in `build_resource'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/dsl/recipe.rb:69:in `declare_resource'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/dsl/resources.rb:15:in `file'
     # ./lib/ssh_keygen/provider.rb:43:in `block in save_private_key'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/mixin/why_run.rb:52:in `call'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/mixin/why_run.rb:52:in `add_action'
     # ./vendor/ruby/2.2.0/gems/chef-12.5.1/lib/chef/provider.rb:175:in `converge_by'
     # ./lib/ssh_keygen/provider.rb:42:in `save_private_key'
     # ./lib/ssh_keygen.rb:50:in `block in action_create'
     [truncated]

I’ve pored over examples (namely some in poise-service) but still can’t seem
to figure out what I’m missing, without just copy-pasting. If I had to gander a
guess I’d say I’m doing ChefSpec wrong more than anything.

Any help would be appreciated!

PS: Noah, thanks for all you work on this. I’ve used a couple of the Poise
cookbooks now with success (especially poise-service to sensibly manage)
Upstart jobs.


#2

Do all of this using as few dependencies as possible (I’m deliberately
not using poise-boiler right now as it seems unrelated to the pattern itself
and more related to the poise-* cookbooks).

After just going over the docs for poise-boiler a bit more, I think I might be mistaken in this assessment. Looks like assisting in the building and testing process is exactly what it is designed for. And of course, I had it in as a dev dependency anyway. Hah.

Updated Rakefile and spec_helper.rb to reflect this and am using bundle exec rake spec now and am getting the same errors pretty much, but now with color. :wink:


#3

I found my problem. I was incorrectly referring to new_resource as an instance variable.

Removed that, and on to resolving other kinks. :slight_smile:

Shout out to pry-byebug, which allowed me to figure out that it was my owner attribute I needed to look at versus the file resource’s.