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

Already on GitHub? Sign in to your account

How to override `ohai_time`? #363

Closed
JeanMertz opened this Issue Feb 27, 2014 · 9 comments

Comments

Projects
None yet
2 participants

I'm trynig to set the ohai_time when using ChefZero, without much luck:

stub_node('db', ohai_time: Time.now.to_i) # => 1344011611.7613227
stub_node('db', ohai: { ohai_time: Time.now.to_i }) # => 1344011611.7613227
stub_node('db', ohai: { time: Time.now.to_i }) # => 1344011611.7613227
stub_node('db') do |node|
  node.automatic['ohai_time'] = Time.now.to_i
end # => 1344011611.7613227

the returned time is the default one for Fauxhai using ubuntu 12.10.

I solved it for now, using mocks, but I would still like to know if/how this can be achieved without mocking...

before do
  allow_any_instance_of(Chef::Node).to receive(:ohai_time)
    .and_return(Time.now.to_i)
end
Collaborator

sethvargo commented Feb 27, 2014

[1] pry(main)> require 'chefspec'
=> true
[2] pry(main)> include ChefSpec::Macros
=> Object
[3] pry(main)> x = stub_node('db') do |node|
[3] pry(main)*   node.automatic['ohai_time'] = Time.now
[3] pry(main)* end
=> #<Chef::Node:0x007fa4c5647708
 @attributes=
  {"os"=>"chefspec",
   "os_version"=>"0.6.1",
   "fqdn"=>"chefspec.local",
   "domain"=>"local",
   "ipaddress"=>"127.0.0.1",
   "hostname"=>"chefspec",
   "languages"=>{"ruby"=>"/usr/somewhere"},
   "kernel"=>{"machine"=>"i386"},
   "ohai_time"=>2014-02-27 18:15:56 -0500},
 @chef_environment="_default",
 @name="db",
 @run_list=#<Chef::RunList:0x007fa4c56475a0 @run_list_items=[]>,
 @run_state={}>
[4] pry(main)> x['ohai_time']
=> 2014-02-27 18:15:56 -0500

@sethvargo sethvargo closed this Feb 27, 2014

Collaborator

sethvargo commented Feb 27, 2014

Are you saving the result of stub_node? I just did this in IRB and it worked fine...

@sethvargo your example still uses the local node. I just tried again, in my case the node is saved to ChefZero, and then returned again through a search:

context 'and last check-in in the last hour' do
  let(:db) do
    stub_node('db') do |node|
      node.run_list.add('helpers::db')
      node.automatic['ohai_time'] = Time.now.to_i
    end
  end

  before { ChefSpec::Server.create_node(db) }

  it 'finds current node' do
    expect(cb.find_self(relevant: true)).to be_present
  end
end
def find_self(options = {})
  return node if chef_solo?

  query = Chef::Search::Query.new
  me = query.search(:node, "ipaddress:#{node['ipaddress']}").first.first

  if options[:relevant] && me.present?
    (Time.now.to_i - Time.at(me.ohai_time).to_i < 3600) ? me : nil
  else
    me
  end
end
[1] pry(#<Kabisa::Cookbook>)> me.ohai_time
=> 1344011611.7613227
[2] pry(#<Kabisa::Cookbook>)> Time.at(me['ohai_time'])
=> 2012-08-03 18:33:31 +0200
Collaborator

sethvargo commented Feb 27, 2014

... again, I can't reproduce:

require 'spec_helper'

describe 'test::default' do
  let(:db) do
    stub_node('db') do |node|
      node.run_list.add('helpers::db')
      node.automatic['ohai_time'] = Time.now.to_i
    end
  end

  before { ChefSpec::Server.create_node(db) }

  it 'finds current node' do
    node = ChefSpec::Server.node('db')
    STDOUT.puts node
    STDOUT.puts Time.at(node['ohai_time'])
  end
end

Outputs:

node[db]
2014-02-27 18:27:34 -0500

@sethvargo You are correct. Your example worked for me as well. Investigating further, I noticed that there are two nodes saved on the server. Since I did search.first.first, I got the wrong one back.

I guess ChefSpec always saves a single node to the server by default?

Collaborator

sethvargo commented Feb 27, 2014

@JeanMertz ChefSpec saves the currently running node to the server as well as any ones that you create.

Thank you for that. I didn't really get that from the documentation.

Also, I just tried removing all but my own-created node using ChefSpec::Server.reset! in a before block, to no avail.

Collaborator

sethvargo commented Feb 27, 2014

The node is added during converge, so that's not possible.

Okay, thank you for your explanation. I opted to not create a new node, and instead override the ohai_time attribute of the original node being uploaded by ChefSpec during converge.

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