summaryrefslogtreecommitdiff
path: root/spec/unit/mixin/ec2_metadata_spec.rb
blob: 1f5a65a287a5cbe981dfc698847e89a34ef134c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# 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 = double("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) { double("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) { double("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 (using string sort)" do
        mixin.best_api_version.should == "2011-05-01"
      end
    end

    context "when no supported versions are found" do
      let(:response) { double("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) { double("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) { double("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

  context "#metadata_get" do
    context "when the response code is unexpected" do
      let(:response) { double("Net::HTTP Response", :body => "", :code => "418") }

      it "raises an error" do
        lambda { mixin.metadata_get('', '2012-01-12') }.should raise_error(RuntimeError)
      end
    end
  end
end