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

Custom inspec resources not loaded: 'undefined method' #120

Closed
rauchrob opened this issue Nov 23, 2016 · 5 comments
Closed

Custom inspec resources not loaded: 'undefined method' #120

rauchrob opened this issue Nov 23, 2016 · 5 comments

Comments

@rauchrob
Copy link

rauchrob commented Nov 23, 2016

I'm trying to implement some kitchen-inspec tests using a custom inspec resource in the spirit of this blog post. Thus I have created a file named test/integration/my-suite/libraries/query.rb with the following content:

module Inspec::Resources
  class QueryResource < Inspec.resource(1)
    name 'query'
    desc 'dummy inspec resource'

    def initialize(args)
      @args = args
    end

    def bar?
      @args == 'foo'
    end
  end
end

My test case test/integration/my-suite/default_spec.rb is

describe query('foo') do
  it { should be_bar }
end

When running kitchen via kitchen verify my-suite the new resource doesn't seem to be available:

$ kitchen verify my-suite
Ignoring unf_ext-0.0.7.2 because its extensions are not built.  Try: gem pristine unf_ext --version 0.0.7.2
-----> Starting Kitchen (v1.13.2)
-----> Verifying <zabbix-resources-ubuntu-1404>...
       Use `/Users/robert/work/westernacher/chef-repo/cookbooks/zabbix_wps/test/integration/zabbix-resources` for testing
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>>     Failed to complete #verify action: [undefined method `query' for #<#<Class:0x007ffd8ad6a058>:0x007ffd8ad697c0>] on zabbix-resources-ubuntu-1404
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration

I also tried putting the inspec resource definition directly in my test-case with the same result.

Thanks in advance!

@nibroc
Copy link

nibroc commented Feb 7, 2017

I'm running into this issue as well.

Whenever I put a custom resource into test/integration/default/libraries/blah.rb, the file is simply not loaded. When I put the custom resource into the actual spec file, it is actually registered into the inspec resource registry, but by that point, the inspec context has already been created (see here where it defines the methods for the resources).

I don't understand how to get it to load my files before create_dsl is called. I've tried putting them in a million places. I also wish that attempting to register a resource after create_dsl had already been called would output a warning so this had been easier to track down, but I'm guessing it's sometimes called more than once per registry.

In short, I'm stumped on this, and I feel like I'm going crazy. As far as I can tell, the official documentation is still in line with that article, so I'm not quite sure why it doesn't work.

kitchen 1.14.2
kitchen-inspec 0.17.0
inspec 1.12.0

@dee-kryvenko
Copy link

It works for me using the following directory structure https://github.com/chef/kitchen-inspec#directory-structure-with-complete-profile
Seems like if you need to create custome resources, you need to use full profile format and not just tests.

@nibroc
Copy link

nibroc commented Feb 8, 2017

I tried that but was getting errors of an invalid format:

Failed to complete #verify action: [Don't understand inspec profile in removed/test/integration/apps, it doesn't look like a supported profile structure.]

Turns out I simply needed the inspec.yml file. I assumed since I didn't actually care to put any configuration in there I could just omit it, but I should have realized that's why it wasn't finding a supported structure. Thanks!

@adamleff
Copy link
Contributor

Custom resources in profiles cannot be in the Inspec::Resources namespace. If you move your class up one level and remove the module Inspec::Resources line, you should be fine.

@FilBot3
Copy link

FilBot3 commented Mar 28, 2019

@adamleff what do you mean by move your class up one level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants