Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added unit tests for AWS::RDS::DBSnapshot.

  • Loading branch information...
commit 7ccd9b09542711f697dcc667a6a9898ddf09fff2 1 parent 48a9f67
Trevor Rowe trevorrowe authored
Showing with 219 additions and 15 deletions.
  1. +33 −15 lib/aws/rds/db_snapshot.rb
  2. +186 −0 spec/aws/rds/db_snapshot_spec.rb
48 lib/aws/rds/db_snapshot.rb
View
@@ -64,7 +64,8 @@ def initialize db_snapshot_id, options = {}
attribute :db_instance_id,
:from => :db_instance_identifier,
- :static => true
+ :static => true,
+ :alias => :db_instance_identifier
attribute :engine, :static => true
@@ -88,12 +89,12 @@ def initialize db_snapshot_id, options = {}
attribute :instance_create_time, :static => true
- populates_from(:create_db_snapshot) do |resp|
+ populates_from(:create_db_snapshot, :copy_db_snapshot) do |resp|
resp.data if resp[:db_snapshot_identifier] == id
end
populates_from(:describe_db_snapshots) do |resp|
- resp.data[:db_snapshots].find{|j| j[:db_snapshot_identifier] == db_snapshot_identifier }
+ resp.data[:db_snapshots].find{|s| s[:db_snapshot_identifier] == id }
end
# @return [DBInstance]
@@ -102,13 +103,18 @@ def db_instance
end
# Copies this database snapshot.
- # @param [String] db_instance_id
- # @return [nil]
- def copy db_instance_id
- options = {:source_db_snapshot_identifier => db_snapshot_identifier,
- :target_db_snapshot_identifier => db_instance_id}
- client.copy_db_snapshot(options)
- nil
+ # @param [String] new_snapshot_id
+ # @return [DBSnapshot]
+ def copy new_snapshot_id
+
+ options = {}
+ options[:source_db_snapshot_identifier] = id
+ options[:target_db_snapshot_identifier] = new_snapshot_id
+ resp = client.copy_db_snapshot(options)
+
+ DBSnapshot.new_from(:copy_db_snapshot, resp,
+ resp[:db_snapshot_identifier], :config => config)
+
end
# Deletes this database snapshot.
@@ -118,16 +124,28 @@ def delete
nil
end
- # Restores the database instance from this snapshot.
- # @param [String] db_instance_id
- # @see Client#restore_db_instance_from_db_snapshot
- def restore_instance db_instance_id, options = {}
- options[:db_instance_identifier] = db_instance_id
+ # Restores the database instance from this snapshot. You may optionally
+ # specify the db instance id if you wish to restore to a different db.
+ #
+ # @param (see Client#restore_db_instance_from_db_snapshot)
+ # @option (see Client#restore_db_instance_from_db_snapshot)
+ def restore_instance options = {}
options[:db_snapshot_identifier] = db_snapshot_identifier
+ options[:db_instance_identifier] ||= db_instance_id
client.restore_db_instance_from_db_snapshot(options)
nil
end
+ # @return [Boolean] Returns +true+ if the db snapshot exists.
+ def exists?
+ begin
+ get_resource
+ true
+ rescue AWS::RDS::Errors::DBSnapshotNotFound
+ false
+ end
+ end
+
protected
def resource_identifiers
186 spec/aws/rds/db_snapshot_spec.rb
View
@@ -0,0 +1,186 @@
+# Copyright 2011-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"). You
+# may not use this file except in compliance with the License. A copy of
+# the License is located at
+#
+# http://aws.amazon.com/apache2.0/
+#
+# or in the "license" file accompanying this file. This file is
+# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
+# ANY KIND, either express or implied. See the License for the specific
+# language governing permissions and limitations under the License.
+
+require 'spec_helper'
+
+module AWS
+ class RDS
+ describe DBSnapshot do
+
+ let(:config) { stub_config }
+
+ let(:client) { config.rds_client }
+
+ let(:snapshot) { DBSnapshot.new('id', :config => config) }
+
+ let(:response) { client.stub_for(:describe_db_snapshots) }
+
+ let(:details) {{
+ :db_snapshot_identifier => snapshot.id,
+ :db_instance_identifier => 'instance-id',
+ }}
+
+ before(:each) do
+ response.data[:db_snapshots] = [details]
+ client.stub(:describe_db_snapshots).and_return(response)
+ end
+
+ context '#db_snapshot_identifier' do
+
+ it 'is set in the constructor' do
+ DBSnapshot.new('abc').db_snapshot_identifier.should eq('abc')
+ end
+
+ it 'is aliased as #db_snapshot_id' do
+ DBSnapshot.new('abc').db_snapshot_id.should eq('abc')
+ end
+
+ it 'is aliased as #id' do
+ DBSnapshot.new('abc').id.should eq('abc')
+ end
+
+ end
+
+ context 'attributes' do
+
+ let(:now) { Time.now }
+
+ let(:details) {{
+ :db_snapshot_identifier => snapshot.id,
+ :allocated_storage => 123,
+ :availability_zone => 'az-name',
+ :db_instance_identifier => 'db-id',
+ :engine => 'mysql',
+ :engine_version => '5.0',
+ :license_model => 'model',
+ :master_username => 'root',
+ :port => 3306,
+ :vpc_id => 123,
+ :snapshot_type => 'type',
+ :status => 'status',
+ :snapshot_create_time => now,
+ :instance_create_time => now,
+ }}
+
+ it 'extracts attributes' do
+ s = snapshot
+ s.allocated_storage.should eq(123)
+ s.availability_zone_name.should eq('az-name')
+ s.db_instance_id.should eq('db-id')
+ s.engine.should eq('mysql')
+ s.engine_version.should eq('5.0')
+ s.license_model.should eq('model')
+ s.master_username.should eq('root')
+ s.port.should eq(3306)
+ s.vpc_id.should eq(123)
+ s.snapshot_type.should eq('type')
+ s.status.should eq('status')
+ s.created_at.should eq(now)
+ s.instance_create_time.should eq(now)
+ end
+
+ end
+
+ context '#db_instance' do
+
+ it 'returns a DBInstance' do
+ snapshot.db_instance.should be_a(DBInstance)
+ end
+
+ it 'returns a DBInstance with the proper config' do
+ snapshot.db_instance.config.should eq(config)
+ end
+
+ it 'returns a DBInstance with the proper id' do
+ snapshot.db_instance.id.should eq(snapshot.db_instance_id)
+ end
+
+ end
+
+ context '#copy' do
+
+ let(:resp) { client.stub_for(:copy_db_snapshot) }
+
+ before(:each) do
+ resp.data[:db_snapshot_identifier] = 'new-snapshot'
+ client.stub(:copy_db_snapshot).and_return(resp)
+ end
+
+ it 'calls #copy_db_snapshot on the client' do
+ client.should_receive(:copy_db_snapshot).with({
+ :source_db_snapshot_identifier => snapshot.id,
+ :target_db_snapshot_identifier => 'new-snapshot',
+ }).and_return(resp)
+
+ snapshot.copy('new-snapshot')
+
+ end
+
+ it 'returns a new snapshot' do
+ s = snapshot.copy 'new-snapshot'
+ s.should be_a(DBSnapshot)
+ s.id.should eq('new-snapshot')
+ s.config.should eq(config)
+ end
+
+ end
+
+ context '#delete' do
+
+ it 'calls #delete_db_snapshot on the client' do
+ client.should_receive(:delete_db_snapshot).
+ with(:db_snapshot_identifier => snapshot.id)
+ snapshot.delete
+ end
+
+ end
+
+ context '#restore_instance' do
+
+ it 'calls #restore_db_instance_from_db_snapshot on the client' do
+ client.should_receive(:restore_db_instance_from_db_snapshot).with({
+ :db_snapshot_identifier => snapshot.id,
+ :db_instance_identifier => snapshot.db_instance_id,
+ })
+ snapshot.restore_instance
+ end
+
+ it 'accepts a :db_instance_identifier option' do
+ client.should_receive(:restore_db_instance_from_db_snapshot).with({
+ :db_snapshot_identifier => snapshot.id,
+ :db_instance_identifier => 'abc',
+ })
+ snapshot.restore_instance :db_instance_identifier => 'abc'
+ end
+
+ end
+
+ context '#exists?' do
+
+ it 'returns true if the describe call does not raise' do
+ client.should_receive(:describe_db_snapshots).
+ with(:db_snapshot_identifier => snapshot.id)
+ snapshot.exists?.should eq(true)
+ end
+
+ it 'returns false if the describe call raises an error' do
+ client.stub(:describe_db_snapshots).
+ and_raise(AWS::RDS::Errors::DBSnapshotNotFound)
+ snapshot.exists?.should eq(false)
+ end
+
+ end
+
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.