2

Three tips for testing Fog with RSpec


Fog is a great ruby library for managing cloud computing services but one weakness is its scarce documentation particularly around testing Fog. These are some RSpec testing tips we’ve learnt working on QuotaGuard. We use Amazon at the moment so specific examples may refer to instances, S3 or load balancers but the principles are the same across any cloud provider.

Tip One – Enable Mocking & Reset before each test

In Mocking mode Fog mocks all of its interactions with external services for you so you don’t have to worry about creating mock Amazon interfaces or mock S3 objects. Being paranoid we enable mocking in the before block of each RSpec test suite.

Calling Fog::Mock.reset clears Fog’s in-memory representation of your mock cloud interactions so we call this to make sure we’re starting fresh with each test.

before(:each) do
  Fog.mock!
  Fog::Mock.reset
end

Tip two – Set up your cloud environment

As Fog’s mocking mode has no interaction with your live cloud services it has no idea what your setup is. It doesn’t know your bucket names, your instance id, your load balancer locations. Nothing. Its like starting with a completely blank cloud. This means that to test anything you have to first create the environment around it.

For example, if I want to test that I can launch an EC2 instance all the dependencies must be created first:

def launch_instance
  @connection.servers.create(:availability_zone => 'us-east-1d', :tags => {"Name" => "proxy-#{SecureRandom.hex(4)}","Role" => "proxy"}, :key_name => 'ec2_keypair', :availability_zone => 'us-east-1d', :image_id => 'ami-3d7e2e54', :flavor_id =>  't1.micro', :user_data => File.read(File.join(File.dirname(__FILE__), "proxy_user_data.sh")))
end

Notice that I’ve specified a :key_name. Normally this refers to a key pair already imported in your Amazon console and Fog checks that this exists before allowing you to create the instance. As I’m in mocking mode I must import this named key pair in the mock environment before testing the launch_instance method.

it "creates instances" do
   ec2_manager = Ec2Manager.new
   ec2_manager.import_key_pair('ec2_keypair',"Blahblahblah")
   ip,id = @ec2_manager.create_instance
   expect(ip).to match /^(?:[0-9]{1,3}.){3}[0-9]{1,3}$/
   expect(id).to be
end

Tip three – Watch for the delay

By default Fog simulates the delay associated with certain cloud operations, for example adding a delay between when you create an instance and when that instance reports itself as ready. When testing it can be a good idea to keep this delay to make sure you’re not writing code on the assumption that instances are instantly available but may not be required in every test across a large test suite.

You can easily turn off the delay by setting it to 0:

Fog::Mock.delay = 0

The default delay is 1 so you can reset it to that when needed:

Fog::Mock.delay = 1

Quota Guard Team

2 Comments

    • Just one thing has changed.

      It should be:

      @ec2 = Fog::Compute.new provider: ‘AWS’, region: ‘us-west-2’
      @ec2.import_key_pair(‘ec2_keypair’,”Blahblahblah”)

Comments are closed.