1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

random ip address support

This commit is contained in:
mlincoln 2013-07-23 17:29:58 -03:00
parent ce3b3936f5
commit 6e166ad323
3 changed files with 32 additions and 0 deletions

View file

@ -8,6 +8,7 @@ require 'formatador'
require 'openssl'
require 'time'
require 'timeout'
require 'ipaddr'
# internal core dependencies
require "fog/version"

View file

@ -34,6 +34,22 @@ module Fog
raise Fog::Errors::MockNotImplemented.new("Contributions welcome!")
end
def self.random_ip(opts = {:version => :v4})
version = opts[:version]
if version == :v6
bit_length = 128
family = Socket::AF_INET6
elsif version == :v4
bit_length = 32
family = Socket::AF_INET
else
raise ArgumentError, "Unknown IP version: #{version}"
end
seed = 1 + rand((2**bit_length)-1)
IPAddr.new(seed, family).to_s
end
def self.random_base64(length)
random_selection(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",

View file

@ -52,6 +52,21 @@ Shindo.tests('Fog mocking', 'core') do
end
end
tests('Fog::Mock.random_ip') do
tests('Fog::Mock.random_ip').returns(true, "default to ipv4") do
IPAddr.new(Fog::Mock.random_ip).ipv4?
end
tests('Fog::Mock.random_ip').returns(true, "explicit ipv4") do
IPAddr.new(Fog::Mock.random_ip({:version => :v4})).ipv4?
end
tests('Fog::Mock.random_ip({:version => :v6})').returns(true, "changes to ipv6") do
IPAddr.new(Fog::Mock.random_ip({:version => :v6})).ipv6?
end
tests('Fog::Mock.random_ip({:version => :v5})').raises(ArgumentError) do
IPAddr.new(Fog::Mock.random_ip({:version => :v5})).ipv4?
end
end
tests('Fog::Mock.not_implemented').raises(Fog::Errors::MockNotImplemented) do
Fog::Mock.not_implemented
end