ec2-metadata 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +15 -14
 - data/README.rdoc +45 -1
 - data/Rakefile +5 -0
 - data/VERSION +1 -1
 - data/bin/ec2-metadata +44 -0
 - data/ec2-metadata.gemspec +85 -0
 - data/lib/ec2_metadata.rb +27 -5
 - data/lib/ec2_metadata/base.rb +36 -3
 - data/lib/ec2_metadata/command.rb +68 -0
 - data/lib/ec2_metadata/dummy.rb +38 -0
 - data/lib/ec2_metadata/dummy.yml +34 -0
 - data/lib/ec2_metadata/http_client.rb +29 -0
 - data/lib/hash_key_orderable.rb +23 -0
 - data/spec/ec2_metadata/base_spec.rb +16 -15
 - data/spec/ec2_metadata/command_spec.rb +101 -0
 - data/spec/ec2_metadata/dummy_spec.rb +92 -0
 - data/spec/ec2_metadata/http_client_spec.rb +34 -0
 - data/spec/ec2_metadata/revision_spec.rb +2 -2
 - data/spec/ec2_metadata/root_spec.rb +7 -7
 - data/spec/ec2_metadata_spec.rb +8 -8
 - data/spec/hash_key_orderable_spec.rb +50 -0
 - data/spec/introduction_spec.rb +27 -27
 - data/spec/rcov.opts +1 -0
 - data/spec/to_hash_spec.rb +121 -0
 - metadata +24 -7
 - data/lib/ec2_metadata/named_base.rb +0 -17
 
    
        data/spec/ec2_metadata_spec.rb
    CHANGED
    
    | 
         @@ -9,10 +9,10 @@ describe Ec2Metadata do 
     | 
|
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                SIMPLE_ATTR_KEYS.each do |attr_key|
         
     | 
| 
       11 
11 
     | 
    
         
             
                  it "(#{attr_key.gsub(/-/, '_')}) should return value of respose for http://169.254.169.254/latest/meta-data/#{attr_key}" do
         
     | 
| 
       12 
     | 
    
         
            -
                     
     | 
| 
       13 
     | 
    
         
            -
                     
     | 
| 
       14 
     | 
    
         
            -
                     
     | 
| 
       15 
     | 
    
         
            -
                     
     | 
| 
      
 12 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/").once.and_return(REVISIONS.join("\n"))
         
     | 
| 
      
 13 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/").once.and_return(DATA_TYPES.join("\n"))
         
     | 
| 
      
 14 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
         
     | 
| 
      
 15 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/#{attr_key}").once.and_return("latest_#{attr_key}")
         
     | 
| 
       16 
16 
     | 
    
         
             
                    Ec2Metadata[attr_key].should == "latest_#{attr_key}"
         
     | 
| 
       17 
17 
     | 
    
         
             
                    Ec2Metadata[attr_key.to_sym].should == "latest_#{attr_key}"
         
     | 
| 
       18 
18 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -21,12 +21,12 @@ describe Ec2Metadata do 
     | 
|
| 
       21 
21 
     | 
    
         
             
                REVISIONS.each do |rev|
         
     | 
| 
       22 
22 
     | 
    
         
             
                  describe "with revision #{rev}" do
         
     | 
| 
       23 
23 
     | 
    
         
             
                    it "('#{rev}')[attr_key] should return value of respose for http://169.254.169.254/#{rev}/meta-data/attr_key" do
         
     | 
| 
       24 
     | 
    
         
            -
                       
     | 
| 
       25 
     | 
    
         
            -
                       
     | 
| 
       26 
     | 
    
         
            -
                       
     | 
| 
      
 24 
     | 
    
         
            +
                      Ec2Metadata.should_receive(:get).with("/").and_return(REVISIONS.join("\n"))
         
     | 
| 
      
 25 
     | 
    
         
            +
                      Ec2Metadata.should_receive(:get).with("/#{rev}/").once.and_return(DATA_TYPES.join("\n"))
         
     | 
| 
      
 26 
     | 
    
         
            +
                      Ec2Metadata.should_receive(:get).with("/#{rev}/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                      SIMPLE_ATTR_KEYS.each do |attr_key|
         
     | 
| 
       29 
     | 
    
         
            -
                         
     | 
| 
      
 29 
     | 
    
         
            +
                        Ec2Metadata.should_receive(:get).with("/#{rev}/meta-data/#{attr_key}").once.and_return("#{rev}_#{attr_key}")
         
     | 
| 
       30 
30 
     | 
    
         
             
                        Ec2Metadata[rev][attr_key].should == "#{rev}_#{attr_key}"
         
     | 
| 
       31 
31 
     | 
    
         
             
                        Ec2Metadata[rev.to_sym][attr_key.to_sym].should == "#{rev}_#{attr_key}"
         
     | 
| 
       32 
32 
     | 
    
         
             
                      end
         
     | 
| 
         @@ -0,0 +1,50 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'hash_key_orderable'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            describe HashKeyOrderable do
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              describe :each do
         
     | 
| 
      
 7 
     | 
    
         
            +
                it "should each with key order" do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}
         
     | 
| 
      
 9 
     | 
    
         
            +
                  hash.extend(HashKeyOrderable)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  hash.key_order = %w(b d c a)
         
     | 
| 
      
 11 
     | 
    
         
            +
                  actuals = []
         
     | 
| 
      
 12 
     | 
    
         
            +
                  hash.each do |key, value|
         
     | 
| 
      
 13 
     | 
    
         
            +
                    actuals << key
         
     | 
| 
      
 14 
     | 
    
         
            +
                  end
         
     | 
| 
      
 15 
     | 
    
         
            +
                  actuals.should == hash.key_order
         
     | 
| 
      
 16 
     | 
    
         
            +
                end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                it "should use original each without key_order" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                  hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}
         
     | 
| 
      
 20 
     | 
    
         
            +
                  hash.extend(HashKeyOrderable)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  hash.should_receive(:each_without_key_order) # original each method
         
     | 
| 
      
 22 
     | 
    
         
            +
                  hash.each{ }
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                it "should appear remain key after key_order in each" do
         
     | 
| 
      
 26 
     | 
    
         
            +
                  hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}
         
     | 
| 
      
 27 
     | 
    
         
            +
                  hash.extend(HashKeyOrderable)
         
     | 
| 
      
 28 
     | 
    
         
            +
                  hash.key_order = %w(b e d)
         
     | 
| 
      
 29 
     | 
    
         
            +
                  actuals = []
         
     | 
| 
      
 30 
     | 
    
         
            +
                  hash.each do |key, value|
         
     | 
| 
      
 31 
     | 
    
         
            +
                    actuals << key
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                  actuals[0..2].should == hash.key_order
         
     | 
| 
      
 34 
     | 
    
         
            +
                  actuals[3..4].sort.should == %w(a c)
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                it "should ignore unexist key in key_order" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                  hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}
         
     | 
| 
      
 39 
     | 
    
         
            +
                  hash.extend(HashKeyOrderable)
         
     | 
| 
      
 40 
     | 
    
         
            +
                  hash.key_order = %w(b z x d)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  actuals = []
         
     | 
| 
      
 42 
     | 
    
         
            +
                  hash.each do |key, value|
         
     | 
| 
      
 43 
     | 
    
         
            +
                    actuals << key
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
                  actuals[0..1].should == %w(b d)
         
     | 
| 
      
 46 
     | 
    
         
            +
                  actuals[2..4].sort.should == %w(a c e)
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
              end
         
     | 
| 
      
 50 
     | 
    
         
            +
            end   
         
     | 
    
        data/spec/introduction_spec.rb
    CHANGED
    
    | 
         @@ -4,31 +4,31 @@ describe Ec2Metadata do 
     | 
|
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
              before do
         
     | 
| 
       6 
6 
     | 
    
         
             
                Ec2Metadata.clear_instance
         
     | 
| 
       7 
     | 
    
         
            -
                 
     | 
| 
      
 7 
     | 
    
         
            +
                Ec2Metadata.should_receive(:get).with("/").once.and_return(REVISIONS.join("\n"))
         
     | 
| 
       8 
8 
     | 
    
         
             
              end
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
              describe "normal usage" do
         
     | 
| 
       11 
11 
     | 
    
         
             
                before do
         
     | 
| 
       12 
     | 
    
         
            -
                   
     | 
| 
      
 12 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/").once.and_return(DATA_TYPES.join("\n"))
         
     | 
| 
       13 
13 
     | 
    
         
             
                end
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
15 
     | 
    
         
             
                describe "user-data" do
         
     | 
| 
       16 
16 
     | 
    
         
             
                  it "should access user-data" do
         
     | 
| 
       17 
17 
     | 
    
         
             
                    msg = "message when instance was launched"
         
     | 
| 
       18 
     | 
    
         
            -
                     
     | 
| 
      
 18 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/user-data").once.and_return(msg)
         
     | 
| 
       19 
19 
     | 
    
         
             
                    Ec2Metadata[:user_data].should == msg
         
     | 
| 
       20 
20 
     | 
    
         
             
                  end
         
     | 
| 
       21 
21 
     | 
    
         
             
                end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                describe "default path /latest/meta-data/" do
         
     | 
| 
       24 
24 
     | 
    
         
             
                  before do
         
     | 
| 
       25 
     | 
    
         
            -
                     
     | 
| 
      
 25 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
         
     | 
| 
       26 
26 
     | 
    
         
             
                  end
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                  describe "should return public-hostname" do
         
     | 
| 
       29 
29 
     | 
    
         
             
                    before do
         
     | 
| 
       30 
30 
     | 
    
         
             
                      @public_hostname = "ec2-75-101-241-136.compute-1.amazonaws.com".freeze
         
     | 
| 
       31 
     | 
    
         
            -
                       
     | 
| 
      
 31 
     | 
    
         
            +
                      Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-hostname").once.and_return(@public_hostname)
         
     | 
| 
       32 
32 
     | 
    
         
             
                    end
         
     | 
| 
       33 
33 
     | 
    
         
             
                    it("with underscore Symbol") {Ec2Metadata[:public_hostname].should == @public_hostname}
         
     | 
| 
       34 
34 
     | 
    
         
             
                    it("with underscore String") {Ec2Metadata['public_hostname'].should == @public_hostname}
         
     | 
| 
         @@ -37,20 +37,20 @@ describe Ec2Metadata do 
     | 
|
| 
       37 
37 
     | 
    
         
             
                  end
         
     | 
| 
       38 
38 
     | 
    
         | 
| 
       39 
39 
     | 
    
         
             
                  it "should access placement data such as 'availavility zone'" do
         
     | 
| 
       40 
     | 
    
         
            -
                     
     | 
| 
      
 40 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/placement/").
         
     | 
| 
       41 
41 
     | 
    
         
             
                      and_return("availability-zone\nanother-placement-data")
         
     | 
| 
       42 
     | 
    
         
            -
                     
     | 
| 
      
 42 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/placement/availability-zone").
         
     | 
| 
       43 
43 
     | 
    
         
             
                      and_return("us-east-1a")
         
     | 
| 
       44 
44 
     | 
    
         
             
                    Ec2Metadata[:placement].keys.should == ["availability-zone", "another-placement-data"]
         
     | 
| 
       45 
45 
     | 
    
         
             
                    Ec2Metadata[:placement][:availability_zone].should == "us-east-1a"
         
     | 
| 
       46 
46 
     | 
    
         
             
                  end
         
     | 
| 
       47 
47 
     | 
    
         | 
| 
       48 
48 
     | 
    
         
             
                  it "should access block-device-mapping" do
         
     | 
| 
       49 
     | 
    
         
            -
                     
     | 
| 
       50 
     | 
    
         
            -
                     
     | 
| 
       51 
     | 
    
         
            -
                     
     | 
| 
       52 
     | 
    
         
            -
                     
     | 
| 
       53 
     | 
    
         
            -
                     
     | 
| 
      
 49 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/").and_return(["ami", "ephemeral0", "root", "swap"].join("\n"))
         
     | 
| 
      
 50 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/ami").and_return("sda1")
         
     | 
| 
      
 51 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/ephemeral0").and_return("sda2")
         
     | 
| 
      
 52 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/root").and_return("/dev/sda1")
         
     | 
| 
      
 53 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/swap").and_return("sda3")
         
     | 
| 
       54 
54 
     | 
    
         
             
                    Ec2Metadata[:block_device_mapping].keys.should == ["ami", "ephemeral0", "root", "swap"]
         
     | 
| 
       55 
55 
     | 
    
         
             
                    Ec2Metadata[:block_device_mapping][:ami].should == "sda1"
         
     | 
| 
       56 
56 
     | 
    
         
             
                    Ec2Metadata[:block_device_mapping][:ephemeral0].should == "sda2"
         
     | 
| 
         @@ -59,18 +59,18 @@ describe Ec2Metadata do 
     | 
|
| 
       59 
59 
     | 
    
         
             
                  end
         
     | 
| 
       60 
60 
     | 
    
         | 
| 
       61 
61 
     | 
    
         
             
                  it "should access some public-keys" do
         
     | 
| 
       62 
     | 
    
         
            -
                     
     | 
| 
       63 
     | 
    
         
            -
                     
     | 
| 
       64 
     | 
    
         
            -
                     
     | 
| 
       65 
     | 
    
         
            -
                     
     | 
| 
       66 
     | 
    
         
            -
                     
     | 
| 
      
 62 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/").and_return("0=keypair0\n1=keypair1")
         
     | 
| 
      
 63 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/0/").and_return("openssh-key")
         
     | 
| 
      
 64 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/0/openssh-key").and_return("ssh-rsa 1234567890")
         
     | 
| 
      
 65 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/1/").and_return("another-key")
         
     | 
| 
      
 66 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/1/another-key").and_return("xxxxxxx abcdefghij")
         
     | 
| 
       67 
67 
     | 
    
         
             
                    Ec2Metadata[:public_keys].keys.should == ["0", '1']
         
     | 
| 
       68 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][0]. 
     | 
| 
       69 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][0].keys.should == ["openssh-key"]
         
     | 
| 
       70 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][0][:openssh_key].should == "ssh-rsa 1234567890"
         
     | 
| 
       71 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][1]. 
     | 
| 
       72 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][1].keys.should == ["another-key"]
         
     | 
| 
       73 
     | 
    
         
            -
                    Ec2Metadata[:public_keys][1][:another_key].should == "xxxxxxx abcdefghij"
         
     | 
| 
      
 68 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][0].keys.should == ["keypair0"]
         
     | 
| 
      
 69 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][0]["keypair0"].keys.should == ["openssh-key"]
         
     | 
| 
      
 70 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][0]["keypair0"][:openssh_key].should == "ssh-rsa 1234567890"
         
     | 
| 
      
 71 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][1].keys.should == ["keypair1"]
         
     | 
| 
      
 72 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][1]["keypair1"].keys.should == ["another-key"]
         
     | 
| 
      
 73 
     | 
    
         
            +
                    Ec2Metadata[:public_keys][1]["keypair1"][:another_key].should == "xxxxxxx abcdefghij"
         
     | 
| 
       74 
74 
     | 
    
         
             
                  end
         
     | 
| 
       75 
75 
     | 
    
         | 
| 
       76 
76 
     | 
    
         
             
                end
         
     | 
| 
         @@ -78,26 +78,26 @@ describe Ec2Metadata do 
     | 
|
| 
       78 
78 
     | 
    
         | 
| 
       79 
79 
     | 
    
         
             
              describe "revision? '2007-01-19' specified" do
         
     | 
| 
       80 
80 
     | 
    
         
             
                before do
         
     | 
| 
       81 
     | 
    
         
            -
                   
     | 
| 
      
 81 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/2007-01-19/").once.and_return(DATA_TYPES.join("\n"))
         
     | 
| 
       82 
82 
     | 
    
         
             
                end
         
     | 
| 
       83 
83 
     | 
    
         | 
| 
       84 
84 
     | 
    
         
             
                describe "user-data" do
         
     | 
| 
       85 
85 
     | 
    
         
             
                  it "should access user-data" do
         
     | 
| 
       86 
86 
     | 
    
         
             
                    msg = "message when instance was launched"
         
     | 
| 
       87 
     | 
    
         
            -
                     
     | 
| 
      
 87 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/2007-01-19/user-data").once.and_return(msg)
         
     | 
| 
       88 
88 
     | 
    
         
             
                    Ec2Metadata['2007-01-19'][:user_data].should == msg
         
     | 
| 
       89 
89 
     | 
    
         
             
                  end
         
     | 
| 
       90 
90 
     | 
    
         
             
                end
         
     | 
| 
       91 
91 
     | 
    
         | 
| 
       92 
92 
     | 
    
         
             
                describe "default path /2007-01-19/meta-data/" do
         
     | 
| 
       93 
93 
     | 
    
         
             
                  before do
         
     | 
| 
       94 
     | 
    
         
            -
                     
     | 
| 
      
 94 
     | 
    
         
            +
                    Ec2Metadata.should_receive(:get).with("/2007-01-19/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
         
     | 
| 
       95 
95 
     | 
    
         
             
                  end
         
     | 
| 
       96 
96 
     | 
    
         | 
| 
       97 
97 
     | 
    
         
             
                  describe "should return public-hostname" do
         
     | 
| 
       98 
98 
     | 
    
         
             
                    before do
         
     | 
| 
       99 
99 
     | 
    
         
             
                      @public_hostname = "ec2-75-101-241-136.compute-1.amazonaws.com".freeze
         
     | 
| 
       100 
     | 
    
         
            -
                       
     | 
| 
      
 100 
     | 
    
         
            +
                      Ec2Metadata.should_receive(:get).with("/2007-01-19/meta-data/public-hostname").once.and_return(@public_hostname)
         
     | 
| 
       101 
101 
     | 
    
         
             
                    end
         
     | 
| 
       102 
102 
     | 
    
         
             
                    it("with underscore Symbol") {Ec2Metadata['2007-01-19'][:public_hostname].should == @public_hostname}
         
     | 
| 
       103 
103 
     | 
    
         
             
                    it("with underscore String") {Ec2Metadata['2007-01-19']['public_hostname'].should == @public_hostname}
         
     | 
    
        data/spec/rcov.opts
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --exclude "spec/*,gems/*" 
         
     | 
| 
         @@ -0,0 +1,121 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Ec2Metadata do
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              BASE_YAML_HASH = {
         
     | 
| 
      
 6 
     | 
    
         
            +
                'user-data' => "user-data-line1\nuser-data-line2\nuser-data-line3",
         
     | 
| 
      
 7 
     | 
    
         
            +
                'meta-data' => {
         
     | 
| 
      
 8 
     | 
    
         
            +
                  'ami-id' => 'ami-abcdef01',
         
     | 
| 
      
 9 
     | 
    
         
            +
                  'ami-launch-index' => '0',
         
     | 
| 
      
 10 
     | 
    
         
            +
                  'ami-manifest-path' =>  'akm2000-us-west-1/dev-20100406-01.manifest.xml',
         
     | 
| 
      
 11 
     | 
    
         
            +
                  'ancestor-ami-ids' => 'ami-c32e7f86',
         
     | 
| 
      
 12 
     | 
    
         
            +
                  'instance-id' => 'i-12345678',
         
     | 
| 
      
 13 
     | 
    
         
            +
                  'instance-type' => 'm1.small',
         
     | 
| 
      
 14 
     | 
    
         
            +
                  'public-hostname' => "ec2-75-101-241-136.compute-1.amazonaws.com",
         
     | 
| 
      
 15 
     | 
    
         
            +
                  'local-hostname' => "ip-10-123-123-123",
         
     | 
| 
      
 16 
     | 
    
         
            +
                  'hostname' => "ip-10-123-123-123",
         
     | 
| 
      
 17 
     | 
    
         
            +
                  'local-ipv4' => "10.123.123.123",
         
     | 
| 
      
 18 
     | 
    
         
            +
                  'public-ipv4' => "75.101.241.136",
         
     | 
| 
      
 19 
     | 
    
         
            +
                  'public-keys' => {
         
     | 
| 
      
 20 
     | 
    
         
            +
                    '0' => {
         
     | 
| 
      
 21 
     | 
    
         
            +
                      'west-dev01' => {'openssh-key' => "ssh-rsa 1234567890"}}
         
     | 
| 
      
 22 
     | 
    
         
            +
                  },
         
     | 
| 
      
 23 
     | 
    
         
            +
                  'block-device-mapping' => {
         
     | 
| 
      
 24 
     | 
    
         
            +
                    "ami" => "sda1",
         
     | 
| 
      
 25 
     | 
    
         
            +
                    "ephemeral0" => "sda2",
         
     | 
| 
      
 26 
     | 
    
         
            +
                    "root" => "/dev/sda1",
         
     | 
| 
      
 27 
     | 
    
         
            +
                    "swap" => "sda3"
         
     | 
| 
      
 28 
     | 
    
         
            +
                  },
         
     | 
| 
      
 29 
     | 
    
         
            +
                  'placement' => {
         
     | 
| 
      
 30 
     | 
    
         
            +
                    'availability-zone' => 'us-west-1b'
         
     | 
| 
      
 31 
     | 
    
         
            +
                  }
         
     | 
| 
      
 32 
     | 
    
         
            +
                }
         
     | 
| 
      
 33 
     | 
    
         
            +
              }
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              describe :to_hash do
         
     | 
| 
      
 37 
     | 
    
         
            +
                it "root" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                  Ec2Metadata.clear_instance
         
     | 
| 
      
 39 
     | 
    
         
            +
                  revisions = %w(1.0 2009-04-04 latest)
         
     | 
| 
      
 40 
     | 
    
         
            +
                  data_types = %w(user-data meta-data)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  attrs = %w(ami-id hostname instance-id local-hostname local-ipv4 public-hostname public-ipv4 
         
     | 
| 
      
 42 
     | 
    
         
            +
                    ami-launch-index ami-manifest-path ancestor-ami-ids
         
     | 
| 
      
 43 
     | 
    
         
            +
                    instance-type
         
     | 
| 
      
 44 
     | 
    
         
            +
                    public-keys/ block-device-mapping/ placement/)
         
     | 
| 
      
 45 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/").once.and_return(revisions.join("\n"))
         
     | 
| 
      
 46 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/").once.and_return(data_types.join("\n"))
         
     | 
| 
      
 47 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/user-data").once.and_return((1..3).map{|n| "user-data-line#{n}"}.join("\n"))
         
     | 
| 
      
 48 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/").once.and_return(attrs.join("\n"))
         
     | 
| 
      
 49 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/ami-id").once.and_return('ami-abcdef01')
         
     | 
| 
      
 50 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/ami-launch-index").once.and_return('0')
         
     | 
| 
      
 51 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/ami-manifest-path").once.and_return('akm2000-us-west-1/dev-20100406-01.manifest.xml')
         
     | 
| 
      
 52 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/ancestor-ami-ids").once.and_return('ami-c32e7f86')
         
     | 
| 
      
 53 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/instance-id").once.and_return('i-12345678')
         
     | 
| 
      
 54 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/instance-type").once.and_return('m1.small')
         
     | 
| 
      
 55 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-hostname").once.and_return('ec2-75-101-241-136.compute-1.amazonaws.com')
         
     | 
| 
      
 56 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/local-hostname").once.and_return('ip-10-123-123-123')
         
     | 
| 
      
 57 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/hostname").once.and_return('ip-10-123-123-123')
         
     | 
| 
      
 58 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/local-ipv4").once.and_return('10.123.123.123')
         
     | 
| 
      
 59 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-ipv4").once.and_return('75.101.241.136')
         
     | 
| 
      
 60 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/").once.and_return('0=west-dev01')
         
     | 
| 
      
 61 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/0/").once.and_return('openssh-key')
         
     | 
| 
      
 62 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/public-keys/0/openssh-key").once.and_return('ssh-rsa 1234567890')
         
     | 
| 
      
 63 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/").once.and_return(%w(ami ephemeral0 root swap).join("\n"))
         
     | 
| 
      
 64 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/ami").once.and_return("sda1")
         
     | 
| 
      
 65 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/ephemeral0").once.and_return("sda2")
         
     | 
| 
      
 66 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/root").once.and_return("/dev/sda1")
         
     | 
| 
      
 67 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/block-device-mapping/swap").once.and_return("sda3")
         
     | 
| 
      
 68 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/placement/").once.and_return('availability-zone')
         
     | 
| 
      
 69 
     | 
    
         
            +
                  Ec2Metadata.should_receive(:get).with("/latest/meta-data/placement/availability-zone").once.and_return('us-west-1b')
         
     | 
| 
      
 70 
     | 
    
         
            +
                  
         
     | 
| 
      
 71 
     | 
    
         
            +
                  Ec2Metadata.to_hash.should == BASE_YAML_HASH
         
     | 
| 
      
 72 
     | 
    
         
            +
                end
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
              describe :from_hash do
         
     | 
| 
      
 76 
     | 
    
         
            +
                it "root" do
         
     | 
| 
      
 77 
     | 
    
         
            +
                  Ec2Metadata.clear_instance
         
     | 
| 
      
 78 
     | 
    
         
            +
                  Ec2Metadata.from_hash({
         
     | 
| 
      
 79 
     | 
    
         
            +
                    'user-data' => "user-data-line1\nuser-data-line2\nuser-data-line3",
         
     | 
| 
      
 80 
     | 
    
         
            +
                    'meta-data' => {
         
     | 
| 
      
 81 
     | 
    
         
            +
                      'ami-id' => 'ami-abcdef01',
         
     | 
| 
      
 82 
     | 
    
         
            +
                      'instance-id' => 'i-12345678',
         
     | 
| 
      
 83 
     | 
    
         
            +
                      'public-hostname' => "ec2-75-101-241-136.compute-1.amazonaws.com",
         
     | 
| 
      
 84 
     | 
    
         
            +
                      'local-hostname' => "ip-10-123-123-123",
         
     | 
| 
      
 85 
     | 
    
         
            +
                      'hostname' => "ip-10-123-123-123",
         
     | 
| 
      
 86 
     | 
    
         
            +
                      'local-ipv4' => "10.123.123.123",
         
     | 
| 
      
 87 
     | 
    
         
            +
                      'public-ipv4' => "75.101.241.136",
         
     | 
| 
      
 88 
     | 
    
         
            +
                      'public_keys' => [{'openssh-key' => "ssh-rsa 1234567890"}]
         
     | 
| 
      
 89 
     | 
    
         
            +
                    }
         
     | 
| 
      
 90 
     | 
    
         
            +
                  })
         
     | 
| 
      
 91 
     | 
    
         
            +
                  Ec2Metadata[:user_data].should == "user-data-line1\nuser-data-line2\nuser-data-line3"
         
     | 
| 
      
 92 
     | 
    
         
            +
                  Ec2Metadata[:ami_id].should == 'ami-abcdef01'
         
     | 
| 
      
 93 
     | 
    
         
            +
                  Ec2Metadata['instance-id'].should == 'i-12345678'
         
     | 
| 
      
 94 
     | 
    
         
            +
                  Ec2Metadata['public-hostname'].should == "ec2-75-101-241-136.compute-1.amazonaws.com"
         
     | 
| 
      
 95 
     | 
    
         
            +
                  Ec2Metadata['local-hostname'].should == "ip-10-123-123-123"
         
     | 
| 
      
 96 
     | 
    
         
            +
                  Ec2Metadata['hostname'].should == "ip-10-123-123-123"
         
     | 
| 
      
 97 
     | 
    
         
            +
                  Ec2Metadata['local-ipv4'].should == "10.123.123.123"
         
     | 
| 
      
 98 
     | 
    
         
            +
                  Ec2Metadata['public-ipv4'].should == "75.101.241.136"
         
     | 
| 
      
 99 
     | 
    
         
            +
                  Ec2Metadata['public-keys']['0'].keys.should == ['openssh-key']
         
     | 
| 
      
 100 
     | 
    
         
            +
                  Ec2Metadata['public-keys']['0']['openssh-key'].should == "ssh-rsa 1234567890"
         
     | 
| 
      
 101 
     | 
    
         
            +
                end
         
     | 
| 
      
 102 
     | 
    
         
            +
                
         
     | 
| 
      
 103 
     | 
    
         
            +
                it "public-keys by Hash" do
         
     | 
| 
      
 104 
     | 
    
         
            +
                  Ec2Metadata.clear_instance
         
     | 
| 
      
 105 
     | 
    
         
            +
                  Ec2Metadata.from_hash(BASE_YAML_HASH)
         
     | 
| 
      
 106 
     | 
    
         
            +
                  Ec2Metadata['public-keys']['0'].keys.should == ['west-dev01']
         
     | 
| 
      
 107 
     | 
    
         
            +
                  Ec2Metadata['public-keys']['0']['west-dev01'].keys.should == ['openssh-key']
         
     | 
| 
      
 108 
     | 
    
         
            +
                  Ec2Metadata['public-keys']['0']['west-dev01']['openssh-key'].should == "ssh-rsa 1234567890"
         
     | 
| 
      
 109 
     | 
    
         
            +
                  Ec2Metadata['block-device-mapping'].keys.sort.should == %w(ami ephemeral0 root swap).sort
         
     | 
| 
      
 110 
     | 
    
         
            +
                  Ec2Metadata['block-device-mapping']['ami'].should == "sda1"
         
     | 
| 
      
 111 
     | 
    
         
            +
                  Ec2Metadata['block-device-mapping']['ephemeral0'].should == "sda2"
         
     | 
| 
      
 112 
     | 
    
         
            +
                  Ec2Metadata['block-device-mapping']['root'].should == "/dev/sda1"
         
     | 
| 
      
 113 
     | 
    
         
            +
                  Ec2Metadata['block-device-mapping']['swap'].should == "sda3"
         
     | 
| 
      
 114 
     | 
    
         
            +
                  Ec2Metadata['placement'].keys.should == ['availability-zone']
         
     | 
| 
      
 115 
     | 
    
         
            +
                  Ec2Metadata['placement']['availability-zone'].should == 'us-west-1b'
         
     | 
| 
      
 116 
     | 
    
         
            +
                end
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
              end
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
            end   
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version 
     | 
|
| 
       4 
4 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       5 
5 
     | 
    
         
             
              segments: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              - 0
         
     | 
| 
       7 
     | 
    
         
            -
              -  
     | 
| 
      
 7 
     | 
    
         
            +
              - 2
         
     | 
| 
       8 
8 
     | 
    
         
             
              - 0
         
     | 
| 
       9 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       10 
10 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       11 
11 
     | 
    
         
             
            authors: 
         
     | 
| 
       12 
12 
     | 
    
         
             
            - Takeshi AKIMA
         
     | 
| 
         @@ -14,8 +14,8 @@ autorequire: 
     | 
|
| 
       14 
14 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       15 
15 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       16 
16 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
            date: 2010-04- 
     | 
| 
       18 
     | 
    
         
            -
            default_executable: 
         
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2010-04-08 00:00:00 +09:00
         
     | 
| 
      
 18 
     | 
    
         
            +
            default_executable: ec2-metadata
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     | 
| 
       21 
21 
     | 
    
         
             
              name: rspec
         
     | 
| 
         @@ -33,8 +33,8 @@ dependencies: 
     | 
|
| 
       33 
33 
     | 
    
         
             
              version_requirements: *id001
         
     | 
| 
       34 
34 
     | 
    
         
             
            description: ec2-metadata provides to access metadata, and you can use in outside of ec2 like in ec2
         
     | 
| 
       35 
35 
     | 
    
         
             
            email: akm2000@gmail.com
         
     | 
| 
       36 
     | 
    
         
            -
            executables:  
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
      
 36 
     | 
    
         
            +
            executables: 
         
     | 
| 
      
 37 
     | 
    
         
            +
            - ec2-metadata
         
     | 
| 
       38 
38 
     | 
    
         
             
            extensions: []
         
     | 
| 
       39 
39 
     | 
    
         | 
| 
       40 
40 
     | 
    
         
             
            extra_rdoc_files: 
         
     | 
| 
         @@ -47,18 +47,30 @@ files: 
     | 
|
| 
       47 
47 
     | 
    
         
             
            - README.rdoc
         
     | 
| 
       48 
48 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       49 
49 
     | 
    
         
             
            - VERSION
         
     | 
| 
      
 50 
     | 
    
         
            +
            - bin/ec2-metadata
         
     | 
| 
      
 51 
     | 
    
         
            +
            - ec2-metadata.gemspec
         
     | 
| 
       50 
52 
     | 
    
         
             
            - lib/ec2_metadata.rb
         
     | 
| 
       51 
53 
     | 
    
         
             
            - lib/ec2_metadata/base.rb
         
     | 
| 
       52 
     | 
    
         
            -
            - lib/ec2_metadata/ 
     | 
| 
      
 54 
     | 
    
         
            +
            - lib/ec2_metadata/command.rb
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib/ec2_metadata/dummy.rb
         
     | 
| 
      
 56 
     | 
    
         
            +
            - lib/ec2_metadata/dummy.yml
         
     | 
| 
      
 57 
     | 
    
         
            +
            - lib/ec2_metadata/http_client.rb
         
     | 
| 
       53 
58 
     | 
    
         
             
            - lib/ec2_metadata/revision.rb
         
     | 
| 
       54 
59 
     | 
    
         
             
            - lib/ec2_metadata/root.rb
         
     | 
| 
      
 60 
     | 
    
         
            +
            - lib/hash_key_orderable.rb
         
     | 
| 
       55 
61 
     | 
    
         
             
            - spec/ec2_metadata/base_spec.rb
         
     | 
| 
      
 62 
     | 
    
         
            +
            - spec/ec2_metadata/command_spec.rb
         
     | 
| 
      
 63 
     | 
    
         
            +
            - spec/ec2_metadata/dummy_spec.rb
         
     | 
| 
      
 64 
     | 
    
         
            +
            - spec/ec2_metadata/http_client_spec.rb
         
     | 
| 
       56 
65 
     | 
    
         
             
            - spec/ec2_metadata/revision_spec.rb
         
     | 
| 
       57 
66 
     | 
    
         
             
            - spec/ec2_metadata/root_spec.rb
         
     | 
| 
       58 
67 
     | 
    
         
             
            - spec/ec2_metadata_spec.rb
         
     | 
| 
      
 68 
     | 
    
         
            +
            - spec/hash_key_orderable_spec.rb
         
     | 
| 
       59 
69 
     | 
    
         
             
            - spec/introduction_spec.rb
         
     | 
| 
      
 70 
     | 
    
         
            +
            - spec/rcov.opts
         
     | 
| 
       60 
71 
     | 
    
         
             
            - spec/spec.opts
         
     | 
| 
       61 
72 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
      
 73 
     | 
    
         
            +
            - spec/to_hash_spec.rb
         
     | 
| 
       62 
74 
     | 
    
         
             
            has_rdoc: true
         
     | 
| 
       63 
75 
     | 
    
         
             
            homepage: http://github.com/akm/ec2-metadata
         
     | 
| 
       64 
76 
     | 
    
         
             
            licenses: []
         
     | 
| 
         @@ -91,8 +103,13 @@ specification_version: 3 
     | 
|
| 
       91 
103 
     | 
    
         
             
            summary: ec2-metadata provides to access metadata
         
     | 
| 
       92 
104 
     | 
    
         
             
            test_files: 
         
     | 
| 
       93 
105 
     | 
    
         
             
            - spec/ec2_metadata/base_spec.rb
         
     | 
| 
      
 106 
     | 
    
         
            +
            - spec/ec2_metadata/command_spec.rb
         
     | 
| 
      
 107 
     | 
    
         
            +
            - spec/ec2_metadata/dummy_spec.rb
         
     | 
| 
      
 108 
     | 
    
         
            +
            - spec/ec2_metadata/http_client_spec.rb
         
     | 
| 
       94 
109 
     | 
    
         
             
            - spec/ec2_metadata/revision_spec.rb
         
     | 
| 
       95 
110 
     | 
    
         
             
            - spec/ec2_metadata/root_spec.rb
         
     | 
| 
       96 
111 
     | 
    
         
             
            - spec/ec2_metadata_spec.rb
         
     | 
| 
      
 112 
     | 
    
         
            +
            - spec/hash_key_orderable_spec.rb
         
     | 
| 
       97 
113 
     | 
    
         
             
            - spec/introduction_spec.rb
         
     | 
| 
       98 
114 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
      
 115 
     | 
    
         
            +
            - spec/to_hash_spec.rb
         
     | 
| 
         @@ -1,17 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'ec2_metadata'
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            module Ec2Metadata
         
     | 
| 
       4 
     | 
    
         
            -
              class NamedBase < Base
         
     | 
| 
       5 
     | 
    
         
            -
                attr_reader :name
         
     | 
| 
       6 
     | 
    
         
            -
                
         
     | 
| 
       7 
     | 
    
         
            -
                def initialize(name, path, default_child_key = nil)
         
     | 
| 
       8 
     | 
    
         
            -
                  super(path, default_child_key)
         
     | 
| 
       9 
     | 
    
         
            -
                  @name = name
         
     | 
| 
       10 
     | 
    
         
            -
                end
         
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
                def to_s
         
     | 
| 
       13 
     | 
    
         
            -
                  @name
         
     | 
| 
       14 
     | 
    
         
            -
                end
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
              end
         
     | 
| 
       17 
     | 
    
         
            -
            end
         
     |