From f61d47ee268175af83cfc81646daf65c40766b0f Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Sat, 9 Jan 2010 12:22:45 -0800 Subject: [PATCH] cleanup aws server specs and extract some to shared examples --- spec/aws/models/ec2/server_spec.rb | 91 ++++++------------------- spec/aws/models/ec2/servers_spec.rb | 60 ++++++---------- spec/shared_examples/server_examples.rb | 43 ++++++++++++ spec/spec_helper.rb | 2 +- 4 files changed, 84 insertions(+), 112 deletions(-) create mode 100644 spec/shared_examples/server_examples.rb diff --git a/spec/aws/models/ec2/server_spec.rb b/spec/aws/models/ec2/server_spec.rb index 1364eba72..086fc82e0 100644 --- a/spec/aws/models/ec2/server_spec.rb +++ b/spec/aws/models/ec2/server_spec.rb @@ -1,7 +1,22 @@ require File.dirname(__FILE__) + '/../../../spec_helper' +require File.dirname(__FILE__) + '/../../../shared_examples/server_examples' describe 'Fog::AWS::EC2::Server' do + it_should_behave_like "Server" + + subject { @server = @servers.new(:image_id => GENTOO_AMI) } + + before(:each) do + @servers = ec2.servers + end + + after(:each) do + if @server && !@server.new_record? + eventually { @server.destroy } + end + end + describe "#initialize" do it "should remap attributes from parser" do @@ -38,18 +53,8 @@ describe 'Fog::AWS::EC2::Server' do describe "#addresses" do it "should return a Fog::AWS::EC2::Addresses" do - server = ec2.servers.create(:image_id => GENTOO_AMI) - server.addresses.should be_a(Fog::AWS::EC2::Addresses) - server.destroy - end - - end - - describe "#destroy" do - - it "should return true if the server is deleted" do - server = ec2.servers.create(:image_id => GENTOO_AMI) - server.destroy.should be_true + subject.save + subject.addresses.should be_a(Fog::AWS::EC2::Addresses) end end @@ -63,19 +68,6 @@ describe 'Fog::AWS::EC2::Server' do end end - describe "#collection" do - - it "should return a Fog::AWS::EC2::Servers" do - ec2.servers.new.collection.should be_a(Fog::AWS::EC2::Servers) - end - - it "should be the servers the server is related to" do - servers = ec2.servers - servers.new.collection.should == servers - end - - end - describe "#key_pair" do it "should have tests" end @@ -104,56 +96,11 @@ describe 'Fog::AWS::EC2::Server' do end - describe "#reload" do - - before(:each) do - @server = ec2.servers.create(:image_id => GENTOO_AMI) - @reloaded = @server.reload - end - - after(:each) do - @server.destroy - end - - it "should return a Fog::AWS::EC2::Server" do - @reloaded.should be_a(Fog::AWS::EC2::Server) - end - - it "should reset attributes to remote state" do - @server.attributes.should == @reloaded.attributes - end - - end - - describe "#save" do - - before(:each) do - @server = ec2.servers.new(:image_id => GENTOO_AMI) - end - - it "should return true when it succeeds" do - @server.save.should be_true - @server.destroy - end - - it "should not exist in servers before save" do - ec2.servers.get(@server.id).should be_nil - end - - it "should exist in buckets after save" do - @server.save - ec2.servers.get(@server.id).should_not be_nil - @server.destroy - end - - end - describe "#volumes" do it "should return a Fog::AWS::EC2::Volumes" do - server = ec2.servers.create(:image_id => GENTOO_AMI) - server.volumes.should be_a(Fog::AWS::EC2::Volumes) - server.destroy + subject.save + subject.volumes.should be_a(Fog::AWS::EC2::Volumes) end end diff --git a/spec/aws/models/ec2/servers_spec.rb b/spec/aws/models/ec2/servers_spec.rb index d658a0db6..54db529b7 100644 --- a/spec/aws/models/ec2/servers_spec.rb +++ b/spec/aws/models/ec2/servers_spec.rb @@ -2,67 +2,49 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe 'Fog::AWS::EC2::Servers' do - describe "#all" do - - it "should return a Fog::AWS::EC2::Servers" do - ec2.servers.all.should be_a(Fog::AWS::EC2::Servers) - end - - it "should include persisted servers" do - server = ec2.servers.create(:image_id => GENTOO_AMI) - ec2.servers.get(server.id).should_not be_nil - server.destroy - end + subject { @server = @servers.create(:image_id => GENTOO_AMI) } + before(:each) do + @servers = ec2.servers end - describe "#create" do - - before(:each) do - @server = ec2.servers.create(:image_id => GENTOO_AMI) - end - - after(:each) do + after(:each) do + if @server && !@server.new_record? @server.destroy end + end - it "should return a Fog::AWS::EC2::Server" do - @server.should be_a(Fog::AWS::EC2::Server) - end + describe "#all" do - it "should exist on ec2" do - ec2.servers.get(@server.id).should_not be_nil + it "should include persisted servers" do + eventually do + @servers.all.map {|server| server.id}.should include(subject.id) + end end end describe "#get" do - it "should return a Fog::AWS::EC2::Server if a matching server exists" do - server = ec2.servers.create(:image_id => GENTOO_AMI) - get = ec2.servers.get(server.id) - server.attributes.should == get.attributes - server.destroy + it "should return a matching server if one exists" do + eventually do + get = @servers.get(subject.id) + subject.attributes.should == get.attributes + end end it "should return nil if no matching server exists" do - ec2.servers.get('i-00000000').should be_nil - end - - end - - describe "#new" do - - it "should return a Fog::AWS::EC2::Server" do - ec2.servers.new(:image_id => GENTOO_AMI).should be_a(Fog::AWS::EC2::Server) + @servers.get('i-00000000').should be_nil end end describe "#reload" do - it "should return a Fog::AWS::EC2::Servers" do - ec2.servers.all.reload.should be_a(Fog::AWS::EC2::Servers) + it "should reset attributes to remote state" do + servers = @servers.all + reloaded = servers.reload + servers.attributes.should == reloaded.attributes end end diff --git a/spec/shared_examples/server_examples.rb b/spec/shared_examples/server_examples.rb new file mode 100644 index 000000000..3b7a02e15 --- /dev/null +++ b/spec/shared_examples/server_examples.rb @@ -0,0 +1,43 @@ +shared_examples_for "Server" do + + describe "#destroy" do + + it "should return true if the server is deleted" do + subject.save + subject.destroy.should be_true + end + + end + + describe "#reload" do + + it "should reset attributes to remote state" do + subject.save + eventually do + @reloaded = subject.reload + end + subject.attributes.should == @reloaded.attributes + end + + end + + describe "#save" do + + it "should return true when it succeeds" do + subject.save.should be_true + end + + it "should not exist remotely before save" do + @servers.get(subject.id).should be_nil + end + + it "should exist remotely after save" do + subject.save + eventually do + @servers.get(subject.id).should_not be_nil + end + end + + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 07cbd7212..e22283579 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -72,7 +72,7 @@ def eventually(max_delay = 16, &block) sleep(delay) yield break - rescue Excon::Errors::Error => error + rescue => error raise error if delay >= max_delay end end