diff options
-rw-r--r-- | spec/unit/plugins/digital_ocean_spec.rb | 166 |
1 files changed, 35 insertions, 131 deletions
diff --git a/spec/unit/plugins/digital_ocean_spec.rb b/spec/unit/plugins/digital_ocean_spec.rb index ad50cbd3..71500266 100644 --- a/spec/unit/plugins/digital_ocean_spec.rb +++ b/spec/unit/plugins/digital_ocean_spec.rb @@ -1,4 +1,5 @@ # +# Author:: Dylan Page (<dpage@digitalocean.com>) # Author:: Stafford Brunk (<stafford.brunk@gmail.com>) # License:: Apache License, Version 2.0 # @@ -15,12 +16,10 @@ # limitations under the License. # -require "ipaddress" -require_relative "../../spec_helper.rb" +require "spec_helper" describe Ohai::System, "plugin digital_ocean" do let(:plugin) { get_plugin("digital_ocean") } - let(:digitalocean_path) { "/etc/digitalocean" } let(:hint) do { "droplet_id" => 12345678, @@ -35,158 +34,63 @@ describe Ohai::System, "plugin digital_ocean" do } end - before do - plugin[:network] = { - "interfaces" => { - "eth0" => { - "addresses" => { - "00:D3:AD:B3:3F:00" => { - "family" => "lladdr", - }, - "1.2.3.4" => { - "netmask" => "255.255.255.0", - }, - "2400:6180:0000:00d0:0000:0000:0009:7001" => {}, - }, - }, - }, - } - - allow(plugin).to receive(:hint?).with("digital_ocean").and_return(hint) + before(:each) do + allow(plugin).to receive(:hint?).with("digital_ocean").and_return(false) end shared_examples_for "!digital_ocean" do - before(:each) do - plugin.run - end - - it "does not create the digital_ocean mash" do + it "should NOT attempt to fetch the digital_ocean metadata" do + expect(plugin).not_to receive(:http_client) expect(plugin[:digital_ocean]).to be_nil + plugin.run end end - shared_examples_for "digital_ocean_networking" do - it "creates the networks attribute" do - expect(plugin[:digital_ocean][:networks]).not_to be_nil + shared_examples_for "digital_ocean" do + before(:each) do + @http_client = double("Net::HTTP client") + allow(plugin).to receive(:http_client).and_return(@http_client) + 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) + allow(Socket).to receive(:pack_sockaddr_in).and_return(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 }]) + let(:body) do + '{"droplet_id":2756924,"hostname":"sample-droplet","vendor_data":"#cloud-config\ndisable_root: false\nmanage_etc_hosts: true\n\n# The modules that run in the \'init\' stage\ncloud_init_modules:\n - migrator\n - ubuntu-init-switch\n - seed_random\n - bootcmd\n - write-files\n - growpart\n - resizefs\n - set_hostname\n - update_hostname\n - [ update_etc_hosts, once-per-instance ]\n - ca-certs\n - rsyslog\n - users-groups\n - ssh\n\n# The modules that run in the \'config\' stage\ncloud_config_modules:\n - disk_setup\n - mounts\n - ssh-import-id\n - locale\n - set-passwords\n - grub-dpkg\n - apt-pipelining\n - apt-configure\n - package-update-upgrade-install\n - landscape\n - timezone\n - puppet\n - chef\n - salt-minion\n - mcollective\n - disable-ec2-metadata\n - runcmd\n - byobu\n\n# The modules that run in the \'final\' stage\ncloud_final_modules:\n - rightscale_userdata\n - scripts-vendor\n - scripts-per-once\n - scripts-per-boot\n - scripts-per-instance\n - scripts-user\n - ssh-authkey-fingerprints\n - keys-to-console\n - phone-home\n - final-message\n - power-state-change\n","public_keys":["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDAkMD3PYKHaH0KbDiXrRE6KCBo/OKcFqhM+fmnnb0+LUh4RalJWX4edeJmnT5bxLeqmLV/Yggjlpfq73R+Dy7JB4pbBLuM959mSM9ohBCSnByAGoT2iUPev4aZFZZ/ahUzTCylNxXrhZV/bopD399CvYREt7Q+FlauBv0O8MMuMGR8aC69Z3jNL+r+fGWNq98JVHGFO/UgoNL15wGCaidMhzfRqkt1u+m1nY77SFM5qWJz2R0CEC4fMlOiCg8mWBklnryV4yDEPgiXp2I8Rli1Eu2GHwuY1YX9elMeQS7n3Pzq7l6aIQmSgvcEWx6TgMD2V7nQUWpfcud/8dpp/t7z9UyfzLmNwnULHNmUeEp52sejcH5lYzISnkkWa1LzlKSeIrhF3y45m9AyxIfjEqyh/mlKQtUaW3NVXXLPwrNitxHtMIZPU5b16BODn0wb8bqPxpDNpUYrQd/BS7mWDxNpICP2ObLPhd9LW9KIYRNTzryE+uKwxm9NkMlhRku2fu415fH0G0+7aURsHviNN9SO4zct3Pj6QE5rnbVHqxt3biplUTOScdWxSk2Nv3V2dGdt/lBfu6iRPAV9IAS31s7Po3qK1t2jpEPCJwstaCBOM80kmoi3zAgotiAW50X8CelaWsHNrq5jBBgeHUZWgn/c8BkcI61pUE9l34Q6gsiEMQ== tsmith84@gmail.com"],"region":"nyc3","interfaces":{"public":[{"ipv4":{"ip_address":"159.203.92.161","netmask":"255.255.240.0","gateway":"159.203.80.1"},"ipv6":{"ip_address":"2604:A880:0800:00A1:0000:0000:0201:0001","cidr":64,"gateway":"2604:A880:0800:00A1:0000:0000:0000:0001"},"anchor_ipv4":{"ip_address":"10.17.0.5","netmask":"255.255.0.0","gateway":"10.17.0.1"},"mac":"04:01:e5:14:03:01","type":"public"}]},"floating_ip":{"ipv4":{"active":false}},"dns":{"nameservers":["2001:4860:4860::8844","2001:4860:4860::8888","8.8.8.8"]}}' end - end - shared_examples_for "digital_ocean" do - before(:each) do + it "should fetch and properly parse json metadata" do + expect(@http_client).to receive(:get). + with("/metadata/v1.json"). + and_return(double("Net::HTTP Response", :body => body, :code => "200")) plugin.run - end - it "creates a digital_ocean mash" do expect(plugin[:digital_ocean]).not_to be_nil + expect(plugin[:digital_ocean]["droplet_id"]).to eq(2756924) + expect(plugin[:digital_ocean]["hostname"]).to eq("sample-droplet") 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 - end - - it "skips the ip_addresses hint attribute" do - expect(plugin[:digital_ocean][:ip_addresses]).to be_nil - end + it "should complete the run despite unavailable metadata" do + expect(@http_client).to receive(:get). + with("/metadata/v1.json"). + and_return(double("Net::HTTP Response", :body => "", :code => "404")) + plugin.run - 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[:digitalocean]).to be_nil end - - include_examples "digital_ocean_networking" end - describe "with digital_ocean hint file" do - before do - allow(plugin).to receive(:hint?).with("digital_ocean").and_return(hint) - end - - context "without private networking enabled" do - it_behaves_like "digital_ocean" - end - - context "with private networking enabled" do - before do - plugin[:network][:interfaces][:eth1] = { - "addresses" => { - "10.128.142.89" => { - "netmask" => "255.255.255.0", - }, - "fdf8:f53b:82e4:0000:0000:0000:0000:0053" => {}, - }, - } - - plugin.run - end - - 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 }, - { "ip_address" => "fdf8:f53b:82e4:0000:0000:0000:0000:0053", - "type" => "private", - "cidr" => 128 }]) - end - end + describe "without hint or dmi data" do + it_should_behave_like "!digital_ocean" end - describe "without digital_ocean hint file" do - before do - allow(plugin).to receive(:hint?).with("digital_ocean").and_return(false) - end - -<<<<<<< HEAD - describe "with the /etc/digitalocean file" do - before do - allow(File).to receive(:exist?).with(digitalocean_path).and_return(true) - plugin.run - end - 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_behaves_like "!digital_ocean" -======= - yaml_example = <<-EOF - datasource_list: [ DigitalOcean, None ] - datasource: - DigitalOcean: - retries: 5 - timeout: 10 - - vendor_data: - enabled: True - EOF + describe "with digital_ocean hint file" do + it_should_behave_like "digital_ocean" before(:each) do - expect(File).to receive(:exist?).with("/etc/cloud/cloud.cfg").and_return(true) - allow(File).to receive(:read).with("/etc/cloud/cloud.cfg").and_return(yaml_example) ->>>>>>> Remove debug code/add proper has_do_init check. Attempt to fix failing tests + allow(plugin).to receive(:hint?).with("digital_ocean").and_return(true) end end |