diff options
author | Bryan McLellan <btm@opscode.com> | 2013-04-12 15:29:17 -0700 |
---|---|---|
committer | Mike Javorski <mike.javorski@gmail.com> | 2013-04-12 16:28:24 -0700 |
commit | 7e38fae002d09d8c5efb0afebb097728a9da436c (patch) | |
tree | 391c0e54aeb42007d18136af7fdbcd3770a03d50 | |
parent | c19b4928f49a312ba4160a03c5253ce680e89b99 (diff) | |
download | ohai-7e38fae002d09d8c5efb0afebb097728a9da436c.tar.gz |
OHAI-434: Add unit tests for Ec2Metadata#best_api_version
-rw-r--r-- | spec/unit/mixin/ec2_metadata_spec.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/spec/unit/mixin/ec2_metadata_spec.rb b/spec/unit/mixin/ec2_metadata_spec.rb new file mode 100644 index 00000000..0d2060c7 --- /dev/null +++ b/spec/unit/mixin/ec2_metadata_spec.rb @@ -0,0 +1,72 @@ +# +# Author:: Bryan McLellan <btm@loftninjas.org> +# Copyright:: Copyright (c) 2013 Opscode, 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 CONDIT"Net::HTTP Response"NS OF ANY KIND, either express 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') +require 'ohai/mixin/ec2_metadata' + +describe Ohai::Mixin::Ec2Metadata do + let(:mixin) { + metadata_object = Object.new.extend(Ohai::Mixin::Ec2Metadata) + http_client = mock("Net::HTTP client") + http_client.stub!(:get).and_return(response) + metadata_object.stub!(:http_client).and_return(http_client) + metadata_object + } + + context "#best_api_version" do + context "with a sorted list of metadata versions" do + let(:response) { mock("Net::HTTP Response", :body => "1.0\n2011-05-01\n2012-01-12\nUnsupported", :code => "200") } + + it "returns the most recent version" do + mixin.best_api_version.should == "2012-01-12" + end + end + + context "with an unsorted list of metadata versions" do + let(:response) { mock("Net::HTTP Response", :body => "1.0\n2009-04-04\n2007-03-01\n2011-05-01\n2008-09-01\nUnsupported", :code => "200") } + + it "returns the most recent version" do + mixin.best_api_version.should == "2011-05-01" + end + end + + context "when no supported versions are found" do + let(:response) { mock("Net::HTTP Response", :body => "2020-01-01\nUnsupported", :code => "200") } + + it "raises an error" do + lambda { mixin.best_api_version}.should raise_error + end + end + + context "when the response code is 404" do + let(:response) { mock("Net::HTTP Response", :body => "1.0\n2011-05-01\n2012-01-12\nUnsupported", :code => "404") } + + it "raises an error" do + lambda { mixin.best_api_version}.should raise_error + end + end + + context "when the response code is unexpected" do + let(:response) { mock("Net::HTTP Response", :body => "1.0\n2011-05-01\n2012-01-12\nUnsupported", :code => "418") } + + it "raises an error" do + lambda { mixin.best_api_version}.should raise_error + end + end + end +end |