diff --git a/lib/facter/ec2.rb b/lib/facter/ec2.rb index 2b28589a38..98c957f7dc 100644 --- a/lib/facter/ec2.rb +++ b/lib/facter/ec2.rb @@ -24,8 +24,8 @@ def userdata() end end -if (Facter::Util::EC2.has_euca_mac? || Facter::Util::EC2.has_ec2_arp?) && - Facter::Util::EC2.can_connect? +if (Facter::Util::EC2.has_euca_mac? || Facter::Util::EC2.has_openstack_mac? || + Facter::Util::EC2.has_ec2_arp?) && Facter::Util::EC2.can_connect? metadata userdata diff --git a/lib/facter/util/ec2.rb b/lib/facter/util/ec2.rb index 548803e02d..2ed6517074 100644 --- a/lib/facter/util/ec2.rb +++ b/lib/facter/util/ec2.rb @@ -26,6 +26,12 @@ def has_euca_mac? !!(Facter.value(:macaddress) =~ %r{^[dD]0:0[dD]:}) end + # Test if this host has a mac address used by OpenStack, which + # normally starts with 02:16:3E + def has_openstack_mac? + !!(Facter.value(:macaddress) =~ %r{^02:16:3[eE]}) + end + # Test if the host has an arp entry in its cache that matches the EC2 arp, # which is normally +fe:ff:ff:ff:ff:ff+. def has_ec2_arp? diff --git a/spec/unit/ec2_spec.rb b/spec/unit/ec2_spec.rb index 61209af7ca..01c54e7442 100755 --- a/spec/unit/ec2_spec.rb +++ b/spec/unit/ec2_spec.rb @@ -12,6 +12,7 @@ before :each do # This is an ec2 instance, not a eucalyptus instance Facter::Util::EC2.expects(:has_euca_mac?).at_least_once.returns(false) + Facter::Util::EC2.expects(:has_openstack_mac?).at_least_once.returns(false) Facter::Util::EC2.expects(:has_ec2_arp?).at_least_once.returns(true) # Assume we can connect @@ -95,6 +96,35 @@ before :each do # Return false for ec2, true for eucalyptus Facter::Util::EC2.expects(:has_euca_mac?).at_least_once.returns(true) + Facter::Util::EC2.expects(:has_openstack_mac?).at_least_once.returns(false) + Facter::Util::EC2.expects(:has_ec2_arp?).never + + # Assume we can connect + Facter::Util::EC2.expects(:can_connect?).at_least_once.returns(true) + end + + it "should create ec2_user_data fact" do + # No meta-data + Object.any_instance.expects(:open).\ + with("#{api_prefix}/2008-02-01/meta-data/").\ + at_least_once.returns(StringIO.new("")) + + Object.any_instance.expects(:open).\ + with("#{api_prefix}/2008-02-01/user-data/").\ + at_least_once.returns(StringIO.new("test")) + + # Force a fact load + Facter.collection.loader.load(:ec2) + + Facter.fact(:ec2_userdata).value.should == ["test"] + end + end + + describe "when running on openstack" do + before :each do + # Return false for ec2, true for eucalyptus + Facter::Util::EC2.expects(:has_openstack_mac?).at_least_once.returns(true) + Facter::Util::EC2.expects(:has_euca_mac?).at_least_once.returns(false) Facter::Util::EC2.expects(:has_ec2_arp?).never # Assume we can connect diff --git a/spec/unit/util/ec2_spec.rb b/spec/unit/util/ec2_spec.rb index e911373517..62fdcb744c 100755 --- a/spec/unit/util/ec2_spec.rb +++ b/spec/unit/util/ec2_spec.rb @@ -68,6 +68,22 @@ end end + describe "is_openstack_mac? method" do + it "should return true when the mac is an openstack one" do + Facter.expects(:value).with(:macaddress).\ + at_least_once.returns("02:16:3e:54:89:fd") + + Facter::Util::EC2.has_openstack_mac?.should == true + end + + it "should return false when the mac is not a openstack one" do + Facter.expects(:value).with(:macaddress).\ + at_least_once.returns("0c:1d:a0:bc:aa:02") + + Facter::Util::EC2.has_openstack_mac?.should == false + end + end + describe "can_connect? method" do it "returns true if api responds" do # Return something upon connecting to the root