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

Read Chef attributes within the tests ? #541

Closed
scalp42 opened this issue Mar 15, 2016 · 14 comments
Closed

Read Chef attributes within the tests ? #541

scalp42 opened this issue Mar 15, 2016 · 14 comments
Labels
Type: Enhancement Improves an existing feature

Comments

@scalp42
Copy link

scalp42 commented Mar 15, 2016

I'm curious if there's a way to parse a JSON (from test-kitchen) and be able to use it within inspec ?

See this blog post: http://www.hurryupandwait.io/blog/accessing-chef-node-attributes-from-kitchen-tests

TLDR: read test-kitchen data JSON, store it in a variable with Rspec and leverage it later, or pretty much can you use Rspec DSL I guess?

Thanks in advance!

@arlimus
Copy link
Contributor

arlimus commented Mar 16, 2016

We will have all that data exposed to everyone who needs it (and much more), but with a cleaner attributes interface in the very near future.

However, in the meantime, Matt's method will still work, with tiny adjustments. When test-kitchen does the converge step and all the data is written to the file, you can access it via inspec. Instead of reading via IO.read and JSON.parse just use native inspec resources.

Using kitchen-inspec repo, in the test/cookbooks/os_prepare/recipes/default.rb add:

ruby_block "Save node attributes" do
  block do
    File.write("/tmp/kitchen_chef_node.json", node.to_json)
  end
end

and then for the test in test/integration/default/os_spec.rb you can now access that:

node = json("/tmp/kitchen_chef_node.json").params

@arlimus arlimus closed this as completed Mar 16, 2016
@arlimus
Copy link
Contributor

arlimus commented Mar 16, 2016

@mwrock your awesome trick still works 😄

@arlimus arlimus added the Type: Enhancement Improves an existing feature label Mar 16, 2016
@mwrock
Copy link
Contributor

mwrock commented Mar 16, 2016

👍 sweet!

@spuranam
Copy link
Contributor

spuranam commented Jun 26, 2016

@mwrock @arlimus @andy-dufour Test-kitchen v1.10.x included in ChefDK 0.15.15 broke this work around. It used works under test-kitchen v1.8.0 included in ChefDK v0.14.25.

I have posted a reproduce able minimal project at https://github.com/spuranam/hello. Any insight how best to solve would be greatly appreciated.

My Chef Environment:
ChefDK 0.15.15
OS: Windows 10
VirtualBox: 5.0.22
Vagrant: 1.8.4

@scalp42
Copy link
Author

scalp42 commented Jun 27, 2016

@spuranam have you looked in /tmp see if the JSON is here and what's the content ?

@spuranam
Copy link
Contributor

@scalp42 i have posted the content of json file here. Below is error i am seeing:

-----> Starting Kitchen (v1.10.0)
-----> Verifying ...
Use C:/Users/spuranam/chef-repo/cookbooks/hello/test/integration/default for testing

------Exception-------
Class: Kitchen::ActionFailed
Message: 1 actions failed.

Failed to complete #verify action: [undefined method `[]' for nil:NilClass] on default-leap

Please see .kitchen/logs/kitchen.log for more details
Also try running kitchen diagnose --all for configuration

@jeremymv2
Copy link
Contributor

This can be accomplished via an inspec profile. See #813

@mwrock
Copy link
Contributor

mwrock commented Jul 17, 2016

I think this is fixed @spuranam . I just ran verify against your repo and it succeeded. Checking my gem bundle I had:

inspec 0.27.1
kitchen-inspec 0.15.0

I then altered my bundle to use the same versions packaged with 0.15.15:

inspec 0.26.0
kitchen-inspec 0.14.0

This yielded the same failure as yours.

Not sure what the exact fix was but I'd recommend updating those gems and they should be included in the soon to be released chefdk 0.16.

@SolverDude
Copy link

@arlimus, you reference above (from March 16) about a 'cleaner attributes interface'...can you give an update on how that's coming, or a reference to the plan?

@dushyant28
Copy link

Hi,

I am not able to use this approach.

Fixture cookbook (export_node)

ruby_block "Save node attributes" do
block do
require 'json'
if Dir::exist?('/tmp/kitchen')
IO.write("/tmp/kitchen/chef_node.json", node.to_json)
end
end
end

default_spec.rb:

require 'json'
node = json("/tmp/kitchen/chef_node.json").params

describe file("#{Chef::Config[:file_cache_path]}/asset.json") do
it { should exist }
it { should be_file }
it { should_not be_directory }
its('content') { should match "#{node['a']['b']}" }
end

I am getting an error: "Detected alternative framework tests for inspec
Loaded"

Chef Development Kit Version: 1.1.16

Kindly help

@adbejpauli
Copy link

@arlimus

We will have all that data exposed to everyone who needs it (and much more), but with a cleaner attributes interface in the very near future.

I'm excited for this. Any updates here?

Would an inspec chef_node resource type be an option?
I'm a little surprised I haven't seen one yet, but maybe there's a reason for that.

@chris-rock
Copy link
Contributor

Since I've just discovered your responses. Please open a new issue if the json loading is not working

@yangpeng-chn
Copy link

Does anyone know how to solve the encoding issue below?

chef-client OS: windows 2016

[2020-08-17T14:24:40+09:00] FATAL: Chef::Exceptions::JSON::EncodeError: ruby_block[save node attributes] (mlj_save_attributes_cb::default line 22) had an error: Chef::Exceptions::JSON::EncodeError: Invalid UTF-8 string '・ス・ス・ス・ス (・スW・ス・ス・ス・ス)': cannot encode to UTF-8

Code used:

ruby_block "Save node attributes" do
  block do
    File.write("/tmp/kitchen_chef_node.json", node.to_json)
  end
end

@Schwad
Copy link
Contributor

Schwad commented Aug 17, 2020

Hey @yangpeng-chn - great question! Unfortunately we do not track closed issues - could you file your question in a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improves an existing feature
Projects
None yet
Development

No branches or pull requests