Skip to content

Commit

Permalink
Fix DropletKit Specs. Set DropletKit adapter by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
merqlove committed Aug 20, 2017
1 parent 24533ca commit e8b0424
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 24 deletions.
1 change: 1 addition & 0 deletions do_snapshot.gemspec
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.0'

spec.add_dependency 'droplet_kit', '~> 2.1.0'
spec.add_dependency 'barge', '~> 0.11'
spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'pony', '~> 1.1'
end
10 changes: 7 additions & 3 deletions lib/do_snapshot/adapter.rb
Expand Up @@ -10,19 +10,23 @@ module Adapter
class << self
def api(protocol, options = {})
konst = find_protocol(protocol)
fail DoSnapshot::NoProtocolError, "Not existing protocol: #{protocol}." unless
DoSnapshot::Adapter.const_defined?(konst)
error_protocol(protocol) unless DoSnapshot::Adapter.const_defined?(konst)
obj = DoSnapshot::Adapter.const_get(konst)
obj.new(options)
end

private

def error_protocol(protocol)
fail DoSnapshot::NoProtocolError, "Not existing protocol: #{protocol}."
end

def find_protocol(protocol)
if protocol.is_a?(String)
protocol
else
'Barge'
error_protocol(protocol) if protocol.is_a?(Integer) && protocol < 2
'DropletKit'
end
end
end
Expand Down
16 changes: 12 additions & 4 deletions lib/do_snapshot/adapter/droplet_kit.rb
Expand Up @@ -7,7 +7,13 @@ module Adapter
# Operating with Digital Ocean.
#
class DropletKit < Abstract
attr_reader :client
attr_reader :client, :per_page, :page

def initialize(*args)
@per_page = 1000
@page = 1
super(*args)
end

# Get single droplet from DigitalOcean
#
Expand All @@ -24,9 +30,11 @@ def droplet(id)
#
def droplets
# noinspection RubyResolve
response = client.droplets.all
fail :ropletListError unless response.respond_to?(:collection)
response = client.droplets.all(page: page, per_page: per_page)
response.many?
response
rescue ::NoMethodError => e
fail DropletListError, e
end

def snapshots(instance)
Expand All @@ -53,7 +61,7 @@ def power_on(id)
def stop_droplet(id)
# noinspection RubyResolve,RubyResolve
response = client.droplet_actions.power_off(droplet_id: id)

# noinspection RubyResolve
wait_shutdown(id, response.id)
rescue ::DropletKit::Error => e
Expand Down
21 changes: 12 additions & 9 deletions spec/do_snapshot/adapter/barge_spec.rb
Expand Up @@ -5,6 +5,10 @@
include_context 'environment'
include_context 'api_v2_helpers'

let(:droplets_uri) { "#{droplets_api_base}?per_page=200" }
let(:droplet_find_uri) { "#{droplets_api_base}/[id]?per_page=200" }
let(:action_find_uri) { "#{actions_api_base}/[id]?per_page=200" }

subject(:api) { described_class }
subject(:log) { DoSnapshot::Log }

Expand Down Expand Up @@ -62,10 +66,9 @@
it 'with error' do
stub_droplets_fail

# expect { instance.droplets }.to raise_error(DoSnapshot::DropletListError)
expect(instance.droplets.collection).to eq([])
# expect(DoSnapshot.logger.buffer)
# .to include 'Droplet Listing is failed to retrieve'
expect { instance.droplets }.to raise_error(DoSnapshot::DropletListError)
expect(DoSnapshot.logger.buffer)
.to include 'Droplet Listing is failed to retrieve'

expect(a_request(:get, droplets_uri))
.to have_been_made
Expand Down Expand Up @@ -249,16 +252,16 @@

droplet = instance.droplet(droplet_id)
expect { instance.cleanup_snapshots(droplet, 1) }
.not_to raise_error
.not_to raise_error
expect(DoSnapshot.logger.buffer)
.to include 'Destroy of snapshot 5019903 for droplet id: 100823 name: example.com is failed.'
.to include 'Destroy of snapshot 5019903 for droplet id: 100823 name: example.com is failed.'

expect(a_request(:get, droplet_url))
.to have_been_made
.to have_been_made
expect(a_request(:delete, image_destroy_url))
.to have_been_made
.to have_been_made
expect(a_request(:delete, image_destroy2_url))
.to have_been_made
.to have_been_made
end
end
end
Expand Down
13 changes: 8 additions & 5 deletions spec/do_snapshot/adapter/droplet_kit_spec.rb
Expand Up @@ -5,6 +5,10 @@
include_context 'environment'
include_context 'api_v2_helpers'

# let(:droplets_uri) { "#{droplets_api_base}?page=1&per_page=1000" }
# let(:droplet_find_uri) { "#{droplets_api_base}/[id]" }
# let(:action_find_uri) { "#{actions_api_base}/[id]" }

subject(:api) { described_class }
subject(:log) { DoSnapshot::Log }

Expand Down Expand Up @@ -53,7 +57,7 @@
it 'with droplets' do
stub_droplets

instance.droplets
expect(instance.droplets.many?).to be_truthy

expect(a_request(:get, droplets_uri))
.to have_been_made
Expand All @@ -62,10 +66,9 @@
it 'with error' do
stub_droplets_fail

# expect { instance.droplets }.to raise_error(DoSnapshot::DropletListError)
expect(instance.droplets.collection).to eq([])
# expect(DoSnapshot.logger.buffer)
# .to include 'Droplet Listing is failed to retrieve'
expect { instance.droplets }.to raise_error(DoSnapshot::DropletListError)
expect(DoSnapshot.logger.buffer)
.to include 'Droplet Listing is failed to retrieve'

expect(a_request(:get, droplets_uri))
.to have_been_made
Expand Down
2 changes: 1 addition & 1 deletion spec/do_snapshot/adapter_spec.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(_ = {}); end
describe '#api' do
it 'when adapter' do
api = adapter.api(2)
expect(api).to be_a_kind_of(DoSnapshot::Adapter::DigitaloceanV2)
expect(api).to be_a_kind_of(DoSnapshot::Adapter::DropletKit)
end

it 'when custom adapter' do
Expand Down
2 changes: 1 addition & 1 deletion spec/do_snapshot/cli_spec.rb
Expand Up @@ -6,7 +6,7 @@
include_context 'api_v2_helpers'

subject(:cli) { described_class }
subject(:api) { DoSnapshot::Adapter::DigitaloceanV2 }
subject(:api) { DoSnapshot::Adapter::DropletKit }

describe '.initialize' do
it 'with args & options' do
Expand Down
2 changes: 1 addition & 1 deletion spec/shared/api_v2_helpers.rb
Expand Up @@ -9,7 +9,7 @@
let(:actions_api_base) { "#{api_base}/actions" }
let(:images_api_base) { "#{api_base}/images" }
let(:image_destroy_uri) { "#{images_api_base}/[id]" }
let(:droplets_uri) { "#{droplets_api_base}" }
let(:droplets_uri) { "#{droplets_api_base}?page=1&per_page=1000" }
let(:droplet_find_uri) { "#{droplets_api_base}/[id]" }
let(:droplet_stop_uri) { "#{droplets_api_base}/[id]/actions" }
let(:droplet_start_uri) { "#{droplets_api_base}/[id]/actions" }
Expand Down

0 comments on commit e8b0424

Please sign in to comment.