\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/ohai-8.24.0/spec/unit/

Viewing File: provides_map_spec.rb

#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright (c) 2013-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the license at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License
#

require File.expand_path(File.dirname(__FILE__) + "/../spec_helper.rb")

describe Ohai::ProvidesMap do

  let(:ohai_system) { Ohai::System.new }
  let(:provides_map) { Ohai::ProvidesMap.new }
  let(:plugin_1) { Ohai::DSL::Plugin.new(ohai_system.data) }
  let(:plugin_2) { Ohai::DSL::Plugin.new(ohai_system.data) }
  let(:plugin_3) { Ohai::DSL::Plugin.new(ohai_system.data) }
  let(:plugin_4) { Ohai::DSL::Plugin.new(ohai_system.data) }

  describe "when looking up providing plugins for a single attribute" do
    describe "when the attribute does not exist" do
      it "should raise Ohai::Exceptions::AttributeNotFound error" do
        expect { provides_map.find_providers_for(["single"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "No such attribute: 'single'")
      end
    end

    describe "when the attribute does not have a provider" do
      it "should raise Ohai::Exceptions::ProviderNotFound error" do
        provides_map.set_providers_for(plugin_1, ["first/second"])
        expect { provides_map.find_providers_for(["first"]) }.to raise_error(Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: 'first'")
      end
    end

    describe "when only one plugin provides the attribute" do
      before do
        provides_map.set_providers_for(plugin_1, ["single"])
      end

      it "should return the provider" do
        expect(provides_map.find_providers_for(["single"])).to eq([plugin_1])
      end
    end

    describe "when multiple plugins provide the attribute" do
      before do
        provides_map.set_providers_for(plugin_1, ["single"])
        provides_map.set_providers_for(plugin_2, ["single"])
      end

      it "should return all providers" do
        expect(provides_map.find_providers_for(["single"])).to eq([plugin_1, plugin_2])
      end
    end
  end

  describe "when looking up providing plugins for multiple attributes" do
    describe "when a different plugin provides each attribute" do

      before do
        provides_map.set_providers_for(plugin_1, ["one"])
        provides_map.set_providers_for(plugin_2, ["two"])
      end

      it "should return each provider" do
        expect(provides_map.find_providers_for(%w{one two})).to eq([plugin_1, plugin_2])
      end
    end

    describe "when one plugin provides both requested attributes" do

      before do
        provides_map.set_providers_for(plugin_1, ["one"])
        provides_map.set_providers_for(plugin_1, ["one_again"])
      end

      it "should return unique providers" do
        expect(provides_map.find_providers_for(%w{one one_again})).to eq([plugin_1])
      end
    end
  end

  describe "when looking up providers for multi-level attributes" do
    describe "when the full attribute exists in the map" do
      before do
        provides_map.set_providers_for(plugin_1, ["top/middle/bottom"])
      end

      it "should collect the provider" do
        expect(provides_map.find_providers_for(["top/middle/bottom"])).to eq([plugin_1])
      end
    end
  end

  describe "when setting multi-level attributes" do
    describe "when the attribute contains //" do
      it "should raise an Ohai::Exceptions::AttributeSyntaxError" do
        expect { provides_map.set_providers_for(plugin_1, ["this/plugin//is/bad"]) }.to raise_error(Ohai::Exceptions::AttributeSyntaxError, "Attribute contains duplicate '/' characters: this/plugin//is/bad")
      end
    end

    describe "when the attribute has a trailing slash" do
      it "should raise an Ohai::Exceptions::AttributeSyntaxError" do
        expect { provides_map.set_providers_for(plugin_1, ["this/plugin/is/bad/"]) }.to raise_error(Ohai::Exceptions::AttributeSyntaxError, "Attribute contains a trailing '/': this/plugin/is/bad/")
      end
    end
  end

  describe "when looking for providers of attributes specified in CLI" do
    before do
      provides_map.set_providers_for(plugin_1, ["cat/whiskers"])
      provides_map.set_providers_for(plugin_2, ["cat/paws", "cat/paws/toes"])
      provides_map.set_providers_for(plugin_3, ["cat/mouth/teeth"])
    end

    it "should find providers for subattributes if any exists when the attribute doesn't have a provider" do
      providers = provides_map.deep_find_providers_for(["cat"])
      expect(providers).to have(3).plugins
      expect(providers).to include(plugin_1)
      expect(providers).to include(plugin_2)
      expect(providers).to include(plugin_3)
    end

    it "should find providers for the first parent attribute when the attribute or any subattributes doesn't have a provider" do
      providers = provides_map.deep_find_providers_for(["cat/paws/front"])
      expect(providers).to eq([plugin_2])
    end
  end

  describe "when looking for the closest providers" do
    describe "and the full attribute is provided" do
      before do
        provides_map.set_providers_for(plugin_1, ["do/not/eat/metal"])
      end

      it "should return the provider of the full attribute" do
        expect(provides_map.find_closest_providers_for(["do/not/eat/metal"])).to eql([plugin_1])
      end
    end

    describe "and the full attribute is not provided" do
      before do
        provides_map.set_providers_for(plugin_1, ["do/not/eat"])
      end

      it "should not raise error if a parent attribute is provided" do
        expect { provides_map.find_closest_providers_for(["do/not/eat/plastic"]) }.not_to raise_error
      end

      it "should return the providers of the closest parent attribute" do
        provides_map.set_providers_for(plugin_2, ["do/not"]) # set a less-specific parent
        expect(provides_map.find_closest_providers_for(["do/not/eat/glass"])).to eql([plugin_1])
      end

      it "should raise error if the least-specific parent is not an attribute" do
        expect { provides_map.find_closest_providers_for(["please/eat/your/vegetables"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "No such attribute: 'please/eat/your/vegetables'")
      end

      it "should raise error if no parent attribute has a provider" do
        expect { provides_map.find_closest_providers_for(["do/not"]) }.to raise_error(Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: 'do/not'")
      end
    end
  end

  describe "when listing all plugins" do
    before(:each) do
      provides_map.set_providers_for(plugin_1, ["one"])
      provides_map.set_providers_for(plugin_2, ["two"])
      provides_map.set_providers_for(plugin_3, ["stub/three"])
      provides_map.set_providers_for(plugin_4, ["foo/bar/four", "also/this/four"])
    end

    it "should find all the plugins providing attributes" do
      all_plugins = provides_map.all_plugins
      expect(all_plugins).to have(4).plugins
      expect(all_plugins).to include(plugin_1)
      expect(all_plugins).to include(plugin_2)
      expect(all_plugins).to include(plugin_3)
      expect(all_plugins).to include(plugin_4)
    end

    describe "with an attribute filter" do
      it "finds plugins with a single level of attribute" do
        expect(provides_map.all_plugins("one")).to eq([plugin_1])
      end

      it "finds plugins with an exact match for multiple levels of attribute" do
        expect(provides_map.all_plugins("stub/three")).to eq([plugin_3])
      end

      it "finds plugins that provide subattributes of the requested path" do
        expect(provides_map.all_plugins("stub")).to eq([plugin_3])
        expect(provides_map.all_plugins("foo/bar")).to eq([plugin_4])
      end
    end
  end

end