diff options
author | Tim Smith <tsmith@chef.io> | 2016-04-13 14:20:08 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2016-04-13 14:20:08 -0700 |
commit | c049c4e450170cd14eb021c4481ff033b0cf97a3 (patch) | |
tree | ca86a1f431723c348878f1ee4bdba24e1f2e8dc3 | |
parent | 654b24f436fde12eb05e8d3725cd27c7c78e5d48 (diff) | |
parent | 772d3ba46a6403674a44b5f35796e1aadd4fbbc8 (diff) | |
download | ohai-c049c4e450170cd14eb021c4481ff033b0cf97a3.tar.gz |
Merge pull request #797 from chef/hint_logging
Add debug logging to hints and improve cloud specs
-rw-r--r-- | lib/ohai/hints.rb | 30 | ||||
-rw-r--r-- | spec/unit/plugins/azure_spec.rb | 94 | ||||
-rw-r--r-- | spec/unit/plugins/digital_ocean_spec.rb | 139 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 40 | ||||
-rw-r--r-- | spec/unit/plugins/eucalyptus_spec.rb | 63 | ||||
-rw-r--r-- | spec/unit/plugins/gce_spec.rb | 38 | ||||
-rw-r--r-- | spec/unit/plugins/linode_spec.rb | 77 | ||||
-rw-r--r-- | spec/unit/plugins/rackspace_spec.rb | 58 | ||||
-rw-r--r-- | spec/unit/plugins/softlayer_spec.rb | 2 |
9 files changed, 194 insertions, 347 deletions
diff --git a/lib/ohai/hints.rb b/lib/ohai/hints.rb index 20420856..f170ff45 100644 --- a/lib/ohai/hints.rb +++ b/lib/ohai/hints.rb @@ -22,28 +22,30 @@ require "ffi_yajl" module Ohai module Hints def self.refresh_hints - @hints = Hash.new + @hints = {} + end + + def self.parse_hint_file(filename) + json_parser = FFI_Yajl::Parser.new + hash = json_parser.parse(File.read(filename)) + hash || {} # hint + # should exist because the file did, even if it didn't + # contain anything + rescue FFI_Yajl::ParseError => e + Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}") end def self.hint?(name) - @hints ||= Hash.new + @hints ||= {} return @hints[name] if @hints[name] - Ohai.config[:hints_path].each do |path| filename = File.join(path, "#{name}.json") - if File.exist?(filename) - begin - json_parser = FFI_Yajl::Parser.new - hash = json_parser.parse(File.read(filename)) - @hints[name] = hash || Hash.new # hint - # should exist because the file did, even if it didn't - # contain anything - rescue FFI_Yajl::ParseError => e - Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}") - end - end + next unless File.exist?(filename) + Ohai::Log.debug("Found hint #{name}.json at #{filename}") + @hints[name] = parse_hint_file(filename) end + Ohai::Log.debug("Did not find hint #{name}.json in the hint path(s): #{Ohai.config[:hints_path].join(', ')} ") unless @hints.key?(name) @hints[name] end end diff --git a/spec/unit/plugins/azure_spec.rb b/spec/unit/plugins/azure_spec.rb index 0af7df24..dfa4f728 100644 --- a/spec/unit/plugins/azure_spec.rb +++ b/spec/unit/plugins/azure_spec.rb @@ -20,34 +20,50 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper.rb") require "open-uri" describe Ohai::System, "plugin azure" do - before(:each) do - @plugin = get_plugin("azure") + let(:plugin) { get_plugin("azure") } + let(:hint) { + { + "public_ip" => "137.135.46.202", + "vm_name" => "test-vm", + "public_fqdn" => "service.cloudapp.net", + "public_ssh_port" => "22", + "public_winrm_port" => "5985", + } + } + + shared_examples_for "!azure" do + it "does not set the azure attribute" do + plugin.run + expect(plugin[:azure]).to be_nil + end + end + + shared_examples_for "azure" do + it "sets the azure attribute" do + plugin.run + expect(plugin[:azure]).to be_truthy + end end describe "with azure hint file" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/azure.json").and_return('{"public_ip":"137.135.46.202","vm_name":"test-vm","public_fqdn":"service.cloudapp.net","public_ssh_port":"22", "public_winrm_port":"5985"}') - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/azure.json').and_return('{"public_ip":"137.135.46.202","vm_name":"test-vm","public_fqdn":"service.cloudapp.net","public_ssh_port":"22", "public_winrm_port":"5985"}') - @plugin.run + allow(plugin).to receive(:hint?).with("azure").and_return(hint) end - it "should set the azure cloud attributes" do - expect(@plugin[:azure]).not_to be_nil - expect(@plugin[:azure]["public_ip"]).to eq("137.135.46.202") - expect(@plugin[:azure]["vm_name"]).to eq("test-vm") - expect(@plugin[:azure]["public_fqdn"]).to eq("service.cloudapp.net") - expect(@plugin[:azure]["public_ssh_port"]).to eq("22") - expect(@plugin[:azure]["public_winrm_port"]).to eq("5985") + it "sets the azure cloud attributes" do + plugin.run + expect(plugin[:azure]["public_ip"]).to eq("137.135.46.202") + expect(plugin[:azure]["vm_name"]).to eq("test-vm") + expect(plugin[:azure]["public_fqdn"]).to eq("service.cloudapp.net") + expect(plugin[:azure]["public_ssh_port"]).to eq("22") + expect(plugin[:azure]["public_winrm_port"]).to eq("5985") end end describe "without azure hint file or agent or dhcp options" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(plugin).to receive(:hint?).with("azure").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(false) allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) allow(File).to receive(:exist?).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(true) @@ -73,66 +89,46 @@ describe Ohai::System, "plugin azure" do and_yield(" expire 2 2016/03/01 16:40:56;"). and_yield("}") allow(File).to receive(:open).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(@double_file) - @plugin.run end - it "should not behave like azure" do - expect(@plugin[:azure]).to be_nil - end + it_behaves_like "!azure" end - describe "with rackspace hint file no agent and no dhcp lease" do + describe "with rackspace hint file, no agent, and no dhcp lease" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return("") + allow(plugin).to receive(:hint?).with("rackspace").and_return(true) + allow(plugin).to receive(:hint?).with("azure").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(false) - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) allow(File).to receive(:exist?).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(false) - @plugin.run end - it "should not behave like azure" do - expect(@plugin[:azure]).to be_nil - end + it_behaves_like "!azure" end describe "without azure hint file but with agent on linux" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(plugin).to receive(:hint?).with("azure").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(true) allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) - @plugin.run end - it "should create empty azure mash" do - expect(@plugin[:azure]).to be_empty - end + it_behaves_like "azure" end describe "without azure hint file but with agent on windows" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(plugin).to receive(:hint?).with("azure").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(false) allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(true) - @plugin.run end - it "should create empty azure mash" do - puts "The dir exists?:" + "#{Dir.exist?('C:\WindowsAzure')}" - expect(@plugin[:azure]).to be_empty - end + it_behaves_like "azure" end describe "without azure hint or agent but with dhcp option" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/azure.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(plugin).to receive(:hint?).with("azure").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(false) allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) allow(File).to receive(:exist?).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(true) @@ -158,13 +154,9 @@ describe Ohai::System, "plugin azure" do and_yield(" expire 5 2152/03/10 09:03:39;"). and_yield("}") allow(File).to receive(:open).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(@double_file) - @plugin.run end - it "should create empty azure mash" do - puts "The dir exists?:" + "#{Dir.exist?('C:\WindowsAzure')}" - expect(@plugin[:azure]).to be_empty - end + it_behaves_like "azure" end end diff --git a/spec/unit/plugins/digital_ocean_spec.rb b/spec/unit/plugins/digital_ocean_spec.rb index a9b0c48e..64374755 100644 --- a/spec/unit/plugins/digital_ocean_spec.rb +++ b/spec/unit/plugins/digital_ocean_spec.rb @@ -19,26 +19,24 @@ require "ipaddress" require "spec_helper" describe Ohai::System, "plugin digital_ocean" do - let(:hint_path_nix) { "/etc/chef/ohai/hints/digital_ocean.json" } - let(:hint_path_win) { 'C:\chef\ohai\hints/digital_ocean.json' } + let(:plugin) { get_plugin("digital_ocean") } let(:digitalocean_path) { "/etc/digitalocean" } let(:hint) { - '{ - "droplet_id": 12345678, - "name": "example.com", - "image_id": 3240036, - "size_id": 66, - "region_id": 4, - "ip_addresses": { - "public": "1.2.3.4", - "private": "5.6.7.8" - } - }' + { + "droplet_id" => 12345678, + "name" => "example.com", + "image_id" => 3240036, + "size_id" => 66, + "region_id" => 4, + "ip_addresses" => { + "public" => "1.2.3.4", + "private" => "5.6.7.8", + }, + } } before do - @plugin = get_plugin("digital_ocean") - @plugin[:network] = { + plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { @@ -54,64 +52,61 @@ describe Ohai::System, "plugin digital_ocean" do }, } - allow(File).to receive(:exist?).with(hint_path_nix).and_return(true) - allow(File).to receive(:read).with(hint_path_nix).and_return(hint) - allow(File).to receive(:exist?).with(hint_path_win).and_return(true) - allow(File).to receive(:read).with(hint_path_win).and_return(hint) + allow(plugin).to receive(:hint?).with("digital_ocean").and_return(hint) end shared_examples_for "!digital_ocean" do before(:each) do - @plugin.run + plugin.run end it "does not create the digital_ocean mash" do - expect(@plugin[:digital_ocean]).to be_nil + expect(plugin[:digital_ocean]).to be_nil end end shared_examples_for "digital_ocean_networking" do it "creates the networks attribute" do - expect(@plugin[:digital_ocean][:networks]).not_to be_nil + expect(plugin[:digital_ocean][:networks]).not_to be_nil end it "pulls ip addresses from the network interfaces" do - expect(@plugin[:digital_ocean][:networks][:v4]).to eq([{ "ip_address" => "1.2.3.4", - "type" => "public", - "netmask" => "255.255.255.0" }]) - expect(@plugin[:digital_ocean][:networks][:v6]).to eq([{ "ip_address" => "2400:6180:0000:00d0:0000:0000:0009:7001", - "type" => "public", - "cidr" => 128 }]) + expect(plugin[:digital_ocean][:networks][:v4]).to eq([{ "ip_address" => "1.2.3.4", + "type" => "public", + "netmask" => "255.255.255.0" }]) + expect(plugin[:digital_ocean][:networks][:v6]).to eq([{ "ip_address" => "2400:6180:0000:00d0:0000:0000:0009:7001", + "type" => "public", + "cidr" => 128 }]) end end shared_examples_for "digital_ocean" do before(:each) do - @plugin.run + plugin.run end it "creates a digital_ocean mash" do - expect(@plugin[:digital_ocean]).not_to be_nil + expect(plugin[:digital_ocean]).not_to be_nil end it "has all hint attributes" do - expect(@plugin[:digital_ocean][:droplet_id]).not_to be_nil - expect(@plugin[:digital_ocean][:name]).not_to be_nil - expect(@plugin[:digital_ocean][:image_id]).not_to be_nil - expect(@plugin[:digital_ocean][:size_id]).not_to be_nil - expect(@plugin[:digital_ocean][:region_id]).not_to be_nil + expect(plugin[:digital_ocean][:droplet_id]).not_to be_nil + expect(plugin[:digital_ocean][:name]).not_to be_nil + expect(plugin[:digital_ocean][:image_id]).not_to be_nil + expect(plugin[:digital_ocean][:size_id]).not_to be_nil + expect(plugin[:digital_ocean][:region_id]).not_to be_nil end it "skips the ip_addresses hint attribute" do - expect(@plugin[:digital_ocean][:ip_addresses]).to be_nil + expect(plugin[:digital_ocean][:ip_addresses]).to be_nil end it "has correct values for all hint attributes" do - expect(@plugin[:digital_ocean][:droplet_id]).to eq(12345678) - expect(@plugin[:digital_ocean][:name]).to eq("example.com") - expect(@plugin[:digital_ocean][:image_id]).to eq(3240036) - expect(@plugin[:digital_ocean][:size_id]).to eq(66) - expect(@plugin[:digital_ocean][:region_id]).to eq(4) + expect(plugin[:digital_ocean][:droplet_id]).to eq(12345678) + expect(plugin[:digital_ocean][:name]).to eq("example.com") + expect(plugin[:digital_ocean][:image_id]).to eq(3240036) + expect(plugin[:digital_ocean][:size_id]).to eq(66) + expect(plugin[:digital_ocean][:region_id]).to eq(4) end include_examples "digital_ocean_networking" @@ -119,17 +114,16 @@ describe Ohai::System, "plugin digital_ocean" do describe "with digital_ocean hint file" do before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(true) - allow(File).to receive(:exist?).with(hint_path_win).and_return(true) + allow(plugin).to receive(:hint?).with("digital_ocean").and_return(hint) end context "without private networking enabled" do - it_should_behave_like "digital_ocean" + it_behaves_like "digital_ocean" end context "with private networking enabled" do before do - @plugin[:network][:interfaces][:eth1] = { + plugin[:network][:interfaces][:eth1] = { "addresses" => { "10.128.142.89" => { "netmask" => "255.255.255.0", @@ -138,19 +132,19 @@ describe Ohai::System, "plugin digital_ocean" do }, } - @plugin.run + plugin.run end - it "should extract the private networking ips" do - expect(@plugin[:digital_ocean][:networks][:v4]).to eq([{ "ip_address" => "1.2.3.4", - "type" => "public", - "netmask" => "255.255.255.0" }, + it "extracts the private networking ips" do + expect(plugin[:digital_ocean][:networks][:v4]).to eq([{ "ip_address" => "1.2.3.4", + "type" => "public", + "netmask" => "255.255.255.0" }, { "ip_address" => "10.128.142.89", "type" => "private", "netmask" => "255.255.255.0" }]) - expect(@plugin[:digital_ocean][:networks][:v6]).to eq([{ "ip_address" => "2400:6180:0000:00d0:0000:0000:0009:7001", - "type" => "public", - "cidr" => 128 }, + expect(plugin[:digital_ocean][:networks][:v6]).to eq([{ "ip_address" => "2400:6180:0000:00d0:0000:0000:0009:7001", + "type" => "public", + "cidr" => 128 }, { "ip_address" => "fdf8:f53b:82e4:0000:0000:0000:0000:0053", "type" => "private", "cidr" => 128 }]) @@ -160,53 +154,22 @@ describe Ohai::System, "plugin digital_ocean" do describe "without digital_ocean hint file" do before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(false) - allow(File).to receive(:exist?).with(hint_path_win).and_return(false) - end - - describe "with the /etc/digitalocean file" do - before do - allow(File).to receive(:exist?).with(digitalocean_path).and_return(true) - @plugin.run - end - it_should_behave_like "digital_ocean_networking" - end - - describe "without the /etc/digitalocean file" do - before do - allow(File).to receive(:exist?).with(digitalocean_path).and_return(false) - end - it_should_behave_like "!digital_ocean" - end - end - - context "with ec2 hint file" do - let(:ec2_hint_path_nix) { "/etc/chef/ohai/hints/ec2.json" } - let(:ec2_hint_path_win) { 'C:\chef\ohai\hints/ec2.json' } - - before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(false) - allow(File).to receive(:exist?).with(hint_path_win).and_return(false) - - allow(File).to receive(:exist?).with(ec2_hint_path_nix).and_return(true) - allow(File).to receive(:read).with(ec2_hint_path_nix).and_return("") - allow(File).to receive(:exist?).with(ec2_hint_path_win).and_return(true) - allow(File).to receive(:read).with(ec2_hint_path_win).and_return("") + allow(plugin).to receive(:hint?).with("digital_ocean").and_return(false) end describe "with the /etc/digitalocean file" do before do allow(File).to receive(:exist?).with(digitalocean_path).and_return(true) - @plugin.run + plugin.run end - it_should_behave_like "digital_ocean_networking" + it_behaves_like "digital_ocean_networking" end describe "without the /etc/digitalocean file" do before do allow(File).to receive(:exist?).with(digitalocean_path).and_return(false) end - it_should_behave_like "!digital_ocean" + it_behaves_like "!digital_ocean" end end end diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index 7ad3f5d8..df0f00d0 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -24,8 +24,7 @@ require "base64" describe Ohai::System, "plugin ec2" do before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) + allow(plugin).to receive(:hint?).with("ec2").and_return(false) allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(false) end @@ -132,13 +131,7 @@ describe Ohai::System, "plugin ec2" do context "with ec2_iam hint file" do before do - if windows? - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/iam.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/iam.json').and_return("") - else - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/iam.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/iam.json").and_return("") - end + allow(plugin).to receive(:hint?).with("iam").and_return(true) end it "parses ec2 iam/ directory and collect iam/security-credentials/" do @@ -168,11 +161,7 @@ describe Ohai::System, "plugin ec2" do context "without ec2_iam hint file" do before do - if windows? - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/iam.json').and_return(false) - else - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/iam.json").and_return(false) - end + allow(plugin).to receive(:hint?).with("iam").and_return(false) end it "parses ec2 iam/ directory and NOT collect iam/security-credentials/" do @@ -296,33 +285,14 @@ describe Ohai::System, "plugin ec2" do it_behaves_like "ec2" before(:each) do - if windows? - expect(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/ec2.json').and_return("") - else - expect(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/ec2.json").and_return("") - end - end - end - - describe "with rackspace hint file" do - it_behaves_like "!ec2" - - before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return("") + allow(plugin).to receive(:hint?).with("ec2").and_return({}) end end - describe "without any hints that it is an ec2 system" do it_behaves_like "!ec2" before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) + allow(plugin).to receive(:hint?).with("ec2").and_return(false) plugin[:dmi] = nil end end diff --git a/spec/unit/plugins/eucalyptus_spec.rb b/spec/unit/plugins/eucalyptus_spec.rb index 89a877d3..e834b430 100644 --- a/spec/unit/plugins/eucalyptus_spec.rb +++ b/spec/unit/plugins/eucalyptus_spec.rb @@ -21,21 +21,19 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper.rb") require "open-uri" describe Ohai::System, "plugin eucalyptus" do - before(:each) do - @plugin = get_plugin("eucalyptus") - end + let(:plugin) { get_plugin("eucalyptus") } shared_examples_for "!eucalyptus" do - it "should NOT attempt to fetch the eucalyptus metadata" do + it "does NOT attempt to fetch the eucalyptus metadata" do expect(OpenURI).not_to receive(:open) - @plugin.run + plugin.run end end shared_examples_for "eucalyptus" do before(:each) do @http_client = double("Net::HTTP client") - allow(@plugin).to receive(:http_client).and_return(@http_client) + allow(plugin).to receive(:http_client).and_return(@http_client) expect(@http_client).to receive(:get). with("/").twice. @@ -57,69 +55,50 @@ describe Ohai::System, "plugin eucalyptus" do and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) end - it "should recursively fetch all the eucalyptus metadata" do + it "recursively fetches all the eucalyptus metadata" do allow(IO).to receive(:select).and_return([[], [1], []]) t = double("connection") allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS) allow(Socket).to receive(:new).and_return(t) - @plugin.run - expect(@plugin[:eucalyptus]).not_to be_nil - expect(@plugin[:eucalyptus]["instance_type"]).to eq("c1.medium") - expect(@plugin[:eucalyptus]["ami_id"]).to eq("ami-5d2dc934") - expect(@plugin[:eucalyptus]["security_groups"]).to eql %w{group1 group2} + plugin.run + expect(plugin[:eucalyptus]).not_to be_nil + expect(plugin[:eucalyptus]["instance_type"]).to eq("c1.medium") + expect(plugin[:eucalyptus]["ami_id"]).to eq("ami-5d2dc934") + expect(plugin[:eucalyptus]["security_groups"]).to eql %w{group1 group2} end end describe "with eucalyptus mac and metadata address connected" do - it_should_behave_like "eucalyptus" + it_behaves_like "eucalyptus" before(:each) do allow(IO).to receive(:select).and_return([[], [1], []]) - @plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { "d0:0d:95:47:6E:ED" => { "family" => "lladdr" } } } } } + plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { "d0:0d:95:47:6E:ED" => { "family" => "lladdr" } } } } } end end describe "without eucalyptus mac and metadata address connected" do - it_should_behave_like "!eucalyptus" + it_behaves_like "!eucalyptus" before(:each) do - @plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { "ff:ff:95:47:6E:ED" => { "family" => "lladdr" } } } } } + plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { "ff:ff:95:47:6E:ED" => { "family" => "lladdr" } } } } } end end - describe "with eucalyptus cloud file" do - it_should_behave_like "eucalyptus" + describe "with eucalyptus hint file" do + it_behaves_like "eucalyptus" before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/eucalyptus.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/eucalyptus.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/eucalyptus.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/eucalyptus.json').and_return("") + allow(plugin).to receive(:hint?).with("eucalyptus").and_return(true) end end - describe "without cloud file" do - it_should_behave_like "!eucalyptus" + describe "without hint file" do + it_behaves_like "!eucalyptus" before(:each) do - @plugin[:network] = { :interfaces => {} } - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/eucalyptus.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/eucalyptus.json').and_return(false) - end - end - - describe "with ec2 cloud file" do - it_should_behave_like "!eucalyptus" - - before(:each) do - @plugin[:network] = { :interfaces => {} } - - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/eucalyptus.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/eucalyptus.json').and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(true) - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/ec2.json").and_return("") - allow(File).to receive(:read).with('C:\chef\ohai\hints/ec2.json').and_return("") + plugin[:network] = { :interfaces => {} } + allow(plugin).to receive(:hint?).with("eucalyptus").and_return(false) end end diff --git a/spec/unit/plugins/gce_spec.rb b/spec/unit/plugins/gce_spec.rb index 1d3ae838..a30b232d 100644 --- a/spec/unit/plugins/gce_spec.rb +++ b/spec/unit/plugins/gce_spec.rb @@ -19,26 +19,24 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper.rb") require "open-uri" describe Ohai::System, "plugin gce" do - before(:each) do - @plugin = get_plugin("gce") - end + let(:plugin) { get_plugin("gce") } shared_examples_for "!gce" do - it "should NOT attempt to fetch the gce metadata" do - expect(@plugin).not_to receive(:http_get) - @plugin.run + it "does NOT attempt to fetch the gce metadata" do + expect(plugin).not_to receive(:http_get) + plugin.run end - it "should NOT set gce attributes" do - expect(@plugin[:gce]).to be_nil - @plugin.run + it "does NOT set gce attributes" do + expect(plugin[:gce]).to be_nil + plugin.run end end shared_examples_for "gce" do before(:each) do @http_get = double("Net::HTTP client") - allow(@plugin).to receive(:http_get).and_return(double("Net::HTTP Response", :body => '{"instance":{"hostname":"test-host"}}', :code => "200")) + allow(plugin).to receive(:http_get).and_return(double("Net::HTTP Response", :body => '{"instance":{"hostname":"test-host"}}', :code => "200")) allow(IO).to receive(:select).and_return([[], [1], []]) t = double("connection") allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS) @@ -46,32 +44,28 @@ describe Ohai::System, "plugin gce" do allow(Socket).to receive(:pack_sockaddr_in).and_return(nil) end - it "should recursively fetch and properly parse json metadata" do - @plugin.run + it "recursively fetches and properly parses json metadata" do + plugin.run - expect(@plugin[:gce]).not_to be_nil - expect(@plugin[:gce]["instance"]).to eq("hostname" => "test-host") + expect(plugin[:gce]).not_to be_nil + expect(plugin[:gce]["instance"]).to eq("hostname" => "test-host") end end describe "with hint file and with metadata connection" do - it_should_behave_like "gce" + it_behaves_like "gce" before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/gce.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/gce.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/gce.json').and_return("") + allow(plugin).to receive(:hint?).with("gce").and_return({}) end end describe "without hint file and without metadata connection" do - it_should_behave_like "!gce" + it_behaves_like "!gce" before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/gce.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(false) + allow(plugin).to receive(:hint?).with("gce").and_return(false) # Raise Errno::ENOENT to simulate the scenario in which metadata server # can not be connected diff --git a/spec/unit/plugins/linode_spec.rb b/spec/unit/plugins/linode_spec.rb index 62a05967..43c43f39 100644 --- a/spec/unit/plugins/linode_spec.rb +++ b/spec/unit/plugins/linode_spec.rb @@ -18,12 +18,10 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper.rb") describe Ohai::System, "plugin linode" do - let(:hint_path_nix) { "/etc/chef/ohai/hints/linode.json" } - let(:hint_path_win) { 'C:\chef\ohai\hints/linode.json' } + let(:plugin) { get_plugin("linode") } before do - @plugin = get_plugin("linode") - @plugin[:network] = { + plugin[:network] = { "interfaces" => { "eth0" => { "addresses" => { @@ -48,49 +46,49 @@ describe Ohai::System, "plugin linode" do shared_examples_for "!linode" do it "does not create the linode mash" do - @plugin.run - expect(@plugin[:linode]).to be_nil + plugin.run + expect(plugin[:linode]).to be_nil end end shared_examples_for "linode" do - it "creates a linode mash" do - @plugin.run - expect(@plugin[:linode]).not_to be_nil + it "creates the linode mash" do + plugin.run + expect(plugin[:linode]).not_to be_nil end - it "has all required attributes" do - @plugin.run - expect(@plugin[:linode][:public_ip]).not_to be_nil + it "has public_ip attribute" do + plugin.run + expect(plugin[:linode][:public_ip]).not_to be_nil end - it "has correct values for all attributes" do - @plugin.run - expect(@plugin[:linode][:public_ip]).to eq("1.2.3.4") + it "has correct value for public_ip attribute" do + plugin.run + expect(plugin[:linode][:public_ip]).to eq("1.2.3.4") end end context "without linode kernel" do before do - @plugin[:kernel] = { "release" => "3.5.2-x86_64" } + plugin[:kernel] = { "release" => "3.5.2-x86_64" } end - it_should_behave_like "!linode" + it_behaves_like "!linode" end context "with linode kernel" do before do - @plugin[:kernel] = { "release" => "3.5.2-x86_64-linode24" } + plugin[:kernel] = { "release" => "3.5.2-x86_64-linode24" } end - it_should_behave_like "linode" + it_behaves_like "linode" # This test is an interface created according to this guide by Linode # http://library.linode.com/networking/configuring-static-ip-interfaces context "with configured private ip address as suggested by linode" do before do - @plugin[:network][:interfaces]["eth0:1"] = { + plugin[:network][:interfaces]["eth0:1"] = { "addresses" => { "5.6.7.8" => { "broadcast" => "10.176.191.255", @@ -110,49 +108,28 @@ describe Ohai::System, "plugin linode" do end it "detects and sets the private ip" do - @plugin.run - expect(@plugin[:linode][:private_ip]).not_to be_nil - expect(@plugin[:linode][:private_ip]).to eq("5.6.7.8") + plugin.run + expect(plugin[:linode][:private_ip]).not_to be_nil + expect(plugin[:linode][:private_ip]).to eq("5.6.7.8") end end end - describe "with linode cloud file" do + describe "with linode hint file" do before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(true) - allow(File).to receive(:read).with(hint_path_nix).and_return("") - allow(File).to receive(:exist?).with(hint_path_win).and_return(true) - allow(File).to receive(:read).with(hint_path_win).and_return("") + allow(plugin).to receive(:hint?).with("linode").and_return({}) end - it_should_behave_like "linode" + it_behaves_like "linode" end - describe "without cloud file" do + describe "without hint file" do before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(false) - allow(File).to receive(:exist?).with(hint_path_win).and_return(false) + allow(plugin).to receive(:hint?).with("linode").and_return(false) end - it_should_behave_like "!linode" - end - - context "with ec2 cloud file" do - let(:ec2_hint_path_nix) { "/etc/chef/ohai/hints/ec2.json" } - let(:ec2_hint_path_win) { 'C:\chef\ohai\hints/ec2.json' } - - before do - allow(File).to receive(:exist?).with(hint_path_nix).and_return(false) - allow(File).to receive(:exist?).with(hint_path_win).and_return(false) - - allow(File).to receive(:exist?).with(ec2_hint_path_nix).and_return(true) - allow(File).to receive(:read).with(ec2_hint_path_nix).and_return("") - allow(File).to receive(:exist?).with(ec2_hint_path_win).and_return(true) - allow(File).to receive(:read).with(ec2_hint_path_win).and_return("") - end - - it_should_behave_like "!linode" + it_behaves_like "!linode" end end diff --git a/spec/unit/plugins/rackspace_spec.rb b/spec/unit/plugins/rackspace_spec.rb index 5678dccb..76dc34bf 100644 --- a/spec/unit/plugins/rackspace_spec.rb +++ b/spec/unit/plugins/rackspace_spec.rb @@ -82,19 +82,19 @@ describe Ohai::System, "plugin rackspace" do end shared_examples_for "!rackspace" do - it "does not create rackspace" do + it "does not create rackspace attribute" do plugin.run expect(plugin[:rackspace]).to be_nil end end shared_examples_for "rackspace" do - it "creates rackspace" do + it "has rackspace attribute" do plugin.run expect(plugin[:rackspace]).not_to be_nil end - it "has all required attributes" do + it "has expected rackspace ip/hostname attributes" do plugin.run expect(plugin[:rackspace][:public_ip]).not_to be_nil expect(plugin[:rackspace][:private_ip]).not_to be_nil @@ -184,17 +184,14 @@ OUT end end - describe "with rackspace cloud file" do + describe "with rackspace hint file" do it_behaves_like "rackspace" before(:each) do allow(Resolv).to receive(:getname).and_raise(Resolv::ResolvError) - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return("") allow(File).to receive(:exist?).with("/etc/resolv.conf").and_return(true) allow(File).to receive(:read).with("/etc/resolv.conf").and_return("") + allow(plugin).to receive(:hint?).with("rackspace").and_return(true) end describe "with no public interfaces (empty eth0)" do @@ -203,7 +200,7 @@ OUT plugin[:network][:interfaces][:eth0]["addresses"] = {} end - it "has all required attributes" do + it "has correct rackspace attributes" do plugin.run # expliticly nil expect(plugin[:rackspace][:public_ip]).to be_nil @@ -211,14 +208,7 @@ OUT expect(plugin[:rackspace][:public_ipv6]).to be_nil expect(plugin[:rackspace][:public_hostname]).to be_nil # per normal - expect(plugin[:rackspace][:private_ip]).not_to be_nil - expect(plugin[:rackspace][:local_ipv4]).not_to be_nil expect(plugin[:rackspace][:local_ipv6]).to be_nil - expect(plugin[:rackspace][:local_hostname]).not_to be_nil - end - - it "has correct values for all attributes" do - plugin.run expect(plugin[:rackspace][:private_ip]).to eq("5.6.7.8") expect(plugin[:rackspace][:local_ipv4]).to eq("5.6.7.8") expect(plugin[:rackspace][:local_hostname]).to eq("katie") @@ -226,26 +216,11 @@ OUT end end - describe "without cloud file" do - it_behaves_like "!rackspace" - - before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(false) - end - end - - describe "with ec2 cloud file" do + describe "without hint file" do it_behaves_like "!rackspace" before(:each) do - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/ec2.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/ec2.json').and_return("") - - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(false) - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(false) + allow(plugin).to receive(:hint?).with("rackspace").and_return(false) end end @@ -254,6 +229,7 @@ OUT before(:each) do stdout = "Rackspace\n" + allow(plugin).to receive(:hint?).with("rackspace").and_return(false) allow(plugin).to receive(:shell_out).with("xenstore-read vm-data/provider_data/provider").and_return(mock_shell_out(0, stdout, "" )) end end @@ -262,6 +238,7 @@ OUT it_behaves_like "!rackspace" before(:each) do + allow(plugin).to receive(:hint?).with("rackspace").and_return(false) stdout = "cumulonimbus\n" allow(plugin).to receive(:shell_out).with("xenstore-read vm-data/provider_data/provider").and_return(mock_shell_out(0, stdout, "" )) end @@ -271,6 +248,7 @@ OUT it_behaves_like "!rackspace" before(:each) do + allow(plugin).to receive(:hint?).with("rackspace").and_return(false) allow(plugin). to receive(:shell_out). with("xenstore-read vm-data/provider_data/provider"). @@ -301,14 +279,10 @@ OUT describe "does not have private networks" do before do stdout = 'BC764E20422B = "{"label": "public"}"\n' + allow(plugin).to receive(:hint?).with("rackspace").and_return(true) allow(plugin).to receive(:shell_out).with("xenstore-ls vm-data/networking").and_return(mock_shell_out(0, stdout, "" )) stdout = '{"label": "public", "broadcast": "9.10.11.255", "ips": [{"ip": "9.10.11.12", "netmask": "255.255.255.0", "enabled": "1", "gateway": null}], "mac": "BC:76:4E:20:42:2B", "dns": ["69.20.0.164", "69.20.0.196"], "gateway": null}' allow(plugin).to receive(:shell_out).with("xenstore-read vm-data/networking/BC764E20422B").and_return(mock_shell_out(0, stdout, "" )) - - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return("") end it "does not have private_networks object" do @@ -340,11 +314,7 @@ OUT allow(plugin).to receive(:shell_out).with("xenstore-ls vm-data/networking").and_return(mock_shell_out(0, stdout, "" )) stdout = '{"label": "private-network", "broadcast": "9.10.11.255", "ips": [{"ip": "9.10.11.12", "netmask": "255.255.255.0", "enabled": "1", "gateway": null}], "mac": "BC:76:4E:20:42:2B", "dns": ["69.20.0.164", "69.20.0.196"], "gateway": null}' allow(plugin).to receive(:shell_out).with("xenstore-read vm-data/networking/BC764E20422B").and_return(mock_shell_out(0, stdout, "" )) - - allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true) - allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("") - allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) - allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return("") + allow(plugin).to receive(:hint?).with("rackspace").and_return(true) end it "has private_networks object" do @@ -352,7 +322,7 @@ OUT expect(plugin[:rackspace][:private_networks]).not_to be_nil end - it "has correct values for all attributes" do + it "has correct values for all rackspace attributes" do plugin.run expect(plugin[:rackspace][:private_networks][0][:label]).to eq("private-network") expect(plugin[:rackspace][:private_networks][0][:broadcast]).to eq("9.10.11.255") diff --git a/spec/unit/plugins/softlayer_spec.rb b/spec/unit/plugins/softlayer_spec.rb index a8370962..1734b83d 100644 --- a/spec/unit/plugins/softlayer_spec.rb +++ b/spec/unit/plugins/softlayer_spec.rb @@ -23,7 +23,7 @@ describe Ohai::System, "plugin softlayer" do let(:plugin) { get_plugin("softlayer") } it "not create softlayer if hint file doesn't exists" do - allow(@plugin).to receive(:hint?).with("softlayer").and_return(false) + allow(plugin).to receive(:hint?).with("softlayer").and_return(false) plugin.run expect(plugin[:softlayer]).to be_nil end |