do_snapshot 0.6.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -2
  3. data/bin/do_snapshot +1 -0
  4. data/lib/do_snapshot.rb +1 -0
  5. data/lib/do_snapshot/adapter.rb +10 -7
  6. data/lib/do_snapshot/adapter/abstract.rb +2 -1
  7. data/lib/do_snapshot/adapter/{digitalocean_v2.rb → droplet_kit.rb} +50 -33
  8. data/lib/do_snapshot/cli.rb +1 -0
  9. data/lib/do_snapshot/command.rb +1 -0
  10. data/lib/do_snapshot/configuration.rb +1 -0
  11. data/lib/do_snapshot/core_ext/hash.rb +1 -0
  12. data/lib/do_snapshot/distribution.rb +1 -0
  13. data/lib/do_snapshot/gem_ext/resource_kit.rb +30 -0
  14. data/lib/do_snapshot/helpers.rb +1 -0
  15. data/lib/do_snapshot/log.rb +1 -0
  16. data/lib/do_snapshot/mail.rb +1 -0
  17. data/lib/do_snapshot/rspec.rb +12 -0
  18. data/lib/do_snapshot/rspec/adapter.rb +265 -0
  19. data/lib/do_snapshot/rspec/api_helpers.rb +45 -0
  20. data/lib/do_snapshot/rspec/api_v2_helpers.rb +157 -0
  21. data/lib/do_snapshot/rspec/environment.rb +120 -0
  22. data/lib/do_snapshot/rspec/uri_helpers.rb +17 -0
  23. data/lib/do_snapshot/runner.rb +1 -0
  24. data/lib/do_snapshot/version.rb +2 -1
  25. data/spec/do_snapshot/adapter/abstract_spec.rb +2 -1
  26. data/spec/do_snapshot/adapter/droplet_kit_spec.rb +9 -0
  27. data/spec/do_snapshot/adapter_spec.rb +3 -2
  28. data/spec/do_snapshot/cli_spec.rb +4 -3
  29. data/spec/do_snapshot/command_spec.rb +4 -3
  30. data/spec/do_snapshot/configuration_spec.rb +1 -0
  31. data/spec/do_snapshot/log_spec.rb +2 -1
  32. data/spec/do_snapshot/mail_spec.rb +2 -1
  33. data/spec/do_snapshot/runner_spec.rb +3 -2
  34. data/spec/do_snapshot_spec.rb +2 -1
  35. data/spec/spec_helper.rb +2 -4
  36. data/spec/support/aruba.rb +1 -0
  37. data/spec/support/matchers.rb +1 -0
  38. metadata +47 -20
  39. data/spec/do_snapshot/adapter/digitalocean_v2_spec.rb +0 -264
  40. data/spec/shared/api_helpers.rb +0 -41
  41. data/spec/shared/api_v2_helpers.rb +0 -153
  42. data/spec/shared/environment.rb +0 -116
  43. data/spec/shared/uri_helpers.rb +0 -13
@@ -1,41 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- RSpec.shared_context 'api_helpers' do
5
- # Stub helpers
6
- #
7
- def stub_with_id(request, id, fixture, status = 200)
8
- return unless request && fixture && id
9
- stub_request(:get, url_with_id(request, id))
10
- .to_return(status: status, body: fixture(fixture))
11
- end
12
-
13
- def stub_without_id(request, fixture, status = 200)
14
- return unless request && fixture
15
- stub_request(:get, request)
16
- .to_return(status: status, body: fixture(fixture))
17
- end
18
-
19
- def stub_with_id_name(request, id, name, fixture, status = 200)
20
- return unless request && fixture && id && name
21
- stub_request(:get, url_with_id_name(request, id, name))
22
- .to_return(status: status, body: fixture(fixture))
23
- end
24
-
25
- # Url helpers
26
- #
27
- def url_with_id(request, id)
28
- return unless request && id
29
- request.sub('[id]', id.to_s)
30
- end
31
-
32
- def url_with_event_id(request, droplet_id, id)
33
- return unless request && id && droplet_id
34
- request.sub('[id]', id.to_s).sub('[droplet_id]', droplet_id.to_s)
35
- end
36
-
37
- def url_with_id_name(request, id, name)
38
- return unless request && id && name
39
- request.sub('[id]', id.to_s).sub('[name]', name)
40
- end
41
- end
@@ -1,153 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- RSpec.shared_context 'api_v2_helpers' do
5
- let(:api_base) { 'https://api.digitalocean.com/v2' }
6
- let(:droplets_api_base) { "#{api_base}/droplets" }
7
- let(:api_access_token) { "Bearer #{access_token}" }
8
- let(:events_api_base) { "#{api_base}/droplets/[droplet_id]/actions" }
9
- let(:actions_api_base) { "#{api_base}/actions" }
10
- let(:images_api_base) { "#{api_base}/images" }
11
- let(:image_destroy_uri) { "#{images_api_base}/[id]" }
12
- let(:droplets_uri) { "#{droplets_api_base}?per_page=200" }
13
- let(:droplet_find_uri) { "#{droplets_api_base}/[id]?per_page=200" }
14
- let(:droplet_stop_uri) { "#{droplets_api_base}/[id]/actions" }
15
- let(:droplet_start_uri) { "#{droplets_api_base}/[id]/actions" }
16
- let(:snapshot_uri) { "#{droplets_api_base}/[id]/actions" }
17
- let(:event_find_uri) { "#{events_api_base}/[id]" }
18
- let(:action_find_uri) { "#{actions_api_base}/[id]?per_page=200" }
19
-
20
- # List of droplets
21
- #
22
- def stub_droplets
23
- stub_without_id(droplets_uri, 'v2/show_droplets')
24
- end
25
-
26
- def stub_droplets_empty
27
- stub_without_id(droplets_uri, 'v2/show_droplets_empty')
28
- end
29
-
30
- def stub_droplets_fail
31
- stub_without_id(droplets_uri, 'v2/error_message')
32
- end
33
-
34
- # Droplet data
35
- #
36
- def stub_droplet(id)
37
- stub_with_id(droplet_find_uri, id, 'v2/show_droplet')
38
- end
39
-
40
- def stub_droplet_fail(id)
41
- stub_with_id(droplet_find_uri, id, 'v2/error_message')
42
- end
43
-
44
- def stub_droplet_inactive(id)
45
- stub_with_id(droplet_find_uri, id, 'v2/show_droplet_inactive')
46
- end
47
-
48
- # Droplet actions
49
- #
50
- def stub_droplet_stop(id)
51
- stub_with_id(droplet_stop_uri, id, 'v2/show_event_power_off_start', :post,
52
- type: 'power_off'
53
- )
54
- end
55
-
56
- def stub_droplet_stop_fail(id)
57
- stub_with_id(droplet_stop_uri, id, 'v2/error_message', :post,
58
- {
59
- type: 'power_off'
60
- },
61
- 404
62
- )
63
- end
64
-
65
- def stub_droplet_start(id)
66
- stub_with_id(droplet_start_uri, id, 'v2/show_event_power_on_start', :post,
67
- type: 'power_on'
68
- )
69
- end
70
-
71
- def stub_droplet_start_done(id)
72
- stub_with_id(droplet_start_uri, id, 'v2/show_event_power_on_done', :post,
73
- type: 'power_on'
74
- )
75
- end
76
-
77
- def stub_droplet_start_fail(id)
78
- stub_with_id(droplet_start_uri, id, 'v2/error_message', :post,
79
- type: 'power_on'
80
- )
81
- end
82
-
83
- # Snapshot
84
- #
85
- def stub_droplet_snapshot(id, name)
86
- stub_with_id_name(snapshot_uri, id, name, 'v2/response_event', :post,
87
- type: 'snapshot',
88
- name: name
89
- )
90
- end
91
-
92
- def stub_droplet_snapshot_fail(id, name)
93
- stub_with_id_name(snapshot_uri, id, name, 'v2/error_message', :post,
94
- {
95
- type: 'snapshot',
96
- name: name
97
- },
98
- 404
99
- )
100
- end
101
-
102
- # Event status
103
- #
104
- def stub_event_done(id)
105
- stub_with_id(action_find_uri, id, 'v2/show_event_done')
106
- end
107
-
108
- def stub_event_fail(id)
109
- stub_with_id(action_find_uri, id, 'v2/error_message')
110
- end
111
-
112
- def stub_event_running(id)
113
- stub_with_id(action_find_uri, id, 'v2/show_event_start')
114
- end
115
-
116
- # Image actions
117
- #
118
- def stub_image_destroy(id)
119
- stub_with_id(image_destroy_uri, id, 'v2/empty', :delete, nil, 204)
120
- end
121
-
122
- def stub_image_destroy_fail(id)
123
- stub_with_id(image_destroy_uri, id, 'v2/error_message', :delete, nil, 404)
124
- end
125
-
126
- # Stub helpers
127
- #
128
- def stub_with_id(request, id, fixture, type = :get, body = nil, status = 200) # rubocop:disable Metrics/ParameterLists
129
- return unless request && fixture && id
130
- stub_request_body(type, url_with_id(request, id), body)
131
- .to_return(status: status, body: fixture(fixture))
132
- end
133
-
134
- def stub_without_id(request, fixture, type = :get, body = nil, status = 200)
135
- return unless request && fixture
136
- stub_request_body(type, request, body)
137
- .to_return(status: status, body: fixture(fixture))
138
- end
139
-
140
- def stub_with_id_name(request, id, name, fixture, type = :get, body = nil, status = 200) # rubocop:disable Metrics/ParameterLists
141
- return unless request && fixture && id && name
142
- stub_request_body(type, url_with_id_name(request, id, name), body)
143
- .to_return(status: status, body: fixture(fixture))
144
- end
145
-
146
- # Body Helpers
147
- #
148
- def stub_request_body(type, request, body)
149
- stub_response = stub_request(type, request).with(headers: { 'Authorization' => api_access_token })
150
- return stub_response.with(body: body) if body
151
- stub_response
152
- end
153
- end
@@ -1,116 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- RSpec.shared_context 'environment' do
5
- include_context 'api_helpers'
6
-
7
- def do_not_send_email
8
- allow(Pony).to receive(:deliver) { |mail| mail }
9
- end
10
-
11
- let(:client_key) { 'foo' }
12
- let(:api_key) { 'bar' }
13
- let(:access_token) { 'sometoken' }
14
- let(:event_id) { '7499' }
15
- let(:droplet_id) { '100823' }
16
- let(:image_id) { '5019770' }
17
- let(:image_id2) { '5019903' }
18
- let(:cli_env_nil) { Hash['DIGITAL_OCEAN_CLIENT_ID' => nil, 'DIGITAL_OCEAN_API_KEY' => nil, 'DIGITAL_OCEAN_ACCESS_TOKEN' => nil] }
19
- let(:cli_keys) { Thor::CoreExt::HashWithIndifferentAccess.new(digital_ocean_access_token: access_token) }
20
- let(:cli_keys_other) { Thor::CoreExt::HashWithIndifferentAccess.new(digital_ocean_access_token: 'NOTTOK') }
21
- let(:snapshot_name) { "example.com_#{DateTime.now.strftime('%Y_%m_%d')}" }
22
- let(:default_options) { Hash[protocol: 2, only: %w( 100823 ), exclude: %w(), keep: 3, stop: false, trace: true, clean: true, delay: 0, shutdown: true, timeout: 600] }
23
- let(:default_options_cli) { default_options.reject { |key, _| %w( droplets threads ).include?(key.to_s) } }
24
- let(:no_exclude) { [] }
25
- let(:exclude) { %w( 100824 100825 ) }
26
- let(:no_only) { [] }
27
- let(:only) { %w( 100823 100824 ) }
28
- let(:stop) { true }
29
- let(:no_stop) { false }
30
- let(:quiet) { true }
31
- let(:no_quiet) { false }
32
- let(:clean) { true }
33
- let(:no_clean) { false }
34
- let(:shutdown) { true }
35
- let(:timeout) { 600 }
36
- let(:delay) { 0 }
37
- let(:log_path) { "#{project_path}/log/test.log" }
38
- let(:mail_options) { Thor::CoreExt::HashWithIndifferentAccess.new(to: 'mail@somehost.com', from: 'from@host.com') }
39
- let(:smtp_options) { Thor::CoreExt::HashWithIndifferentAccess.new(address: 'smtp.gmail.com', port: '25', user_name: 'someuser', password: 'somepassword') }
40
- let(:log) { Thor::CoreExt::HashWithIndifferentAccess.new(log: log_path) }
41
-
42
- def stub_all_api(droplets = nil, active = false)
43
- drops = []
44
- droplets ||= [droplet_id]
45
- droplets.each do |droplet|
46
- drops.push Hash[
47
- stub_droplet: (active ? stub_droplet(droplet) : stub_droplet_inactive(droplet))
48
- ].merge(stub_droplet_api(droplet))
49
- end
50
- stubs = Hash[drops: drops]
51
- @stubs = stubs.merge(default_stub_api)
52
- end
53
-
54
- def stub_droplet_api(droplet)
55
- {
56
- stub_droplet_stop: stub_droplet_stop(droplet),
57
- stub_droplet_start: stub_droplet_start(droplet),
58
- stub_droplet_snapshot: stub_droplet_snapshot(droplet, snapshot_name)
59
- }
60
- end
61
-
62
- def default_stub_api
63
- {
64
- stub_event_done: stub_event_done(event_id),
65
- stub_droplets: stub_droplets,
66
- stub_image_destroy1: stub_image_destroy(image_id),
67
- stub_image_destroy2: stub_image_destroy(image_id2)
68
- }
69
- end
70
-
71
- def stub_cleanup
72
- @stubs ||= {}
73
- @stubs.each_pair do |_k, v|
74
- remove_request_stub(v) if v.class == WebMock::RequestStub
75
- next unless v.class == Array
76
-
77
- v.each do |d|
78
- d.each_pair do |_dk, dv|
79
- remove_request_stub(dv) if v.class == WebMock::RequestStub
80
- end
81
- end
82
- end
83
- end
84
-
85
- def reset_api_keys
86
- ENV['DIGITAL_OCEAN_API_KEY'] = nil
87
- ENV['DIGITAL_OCEAN_CLIENT_ID'] = nil
88
- ENV['DIGITAL_OCEAN_ACCESS_TOKEN'] = nil
89
- end
90
-
91
- def set_api_keys
92
- ENV['DIGITAL_OCEAN_API_KEY'] = api_key
93
- ENV['DIGITAL_OCEAN_CLIENT_ID'] = client_key
94
- ENV['DIGITAL_OCEAN_ACCESS_TOKEN'] = access_token
95
- end
96
-
97
- def reset_singletons
98
- DoSnapshot.configure do |config|
99
- # config.logger = Logger.new($stdout)
100
- config.verbose = false
101
- config.quiet = true
102
- end
103
- DoSnapshot.logger = DoSnapshot::Log.new
104
- DoSnapshot.mailer = DoSnapshot.config.mailer
105
- end
106
-
107
- before(:all) do
108
- WebMock.reset!
109
- end
110
-
111
- before(:each) do
112
- do_not_send_email
113
- set_api_keys
114
- reset_singletons
115
- end
116
- end
@@ -1,13 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- RSpec.shared_context 'uri_helpers' do
5
- let(:droplet_url) { url_with_id(droplet_find_uri, droplet_id) }
6
- let(:droplet_stop_url) { url_with_id(droplet_stop_uri, droplet_id) }
7
- let(:droplet_start_url) { url_with_id(droplet_start_uri, droplet_id) }
8
- let(:event_find_url) { url_with_id(event_find_uri, event_id) }
9
- let(:image_destroy_url) { url_with_id(image_destroy_uri, image_id) }
10
- let(:image_destroy2_url) { url_with_id(image_destroy_uri, image_id2) }
11
- let(:action_find_url) { url_with_id(action_find_uri, event_id) }
12
- let(:droplet_snapshot_url) { url_with_id_name(snapshot_uri, droplet_id, snapshot_name) }
13
- end