rds-rotate-db-snapshots 0.5.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,116 +0,0 @@
1
- require 'helper'
2
- class Test
3
- include RdsRotateDbSnapshots::Actions
4
-
5
- attr_reader :options
6
-
7
- def initialize(script_name: nil, options: {})
8
- @script_name = script_name
9
- @options = options
10
- @backoff_counter = 0
11
- end
12
-
13
- def rds_client
14
- @rds_client ||= RdsRotateDbSnapshots::RdsClient.new(@options)
15
- end
16
- alias client rds_client
17
-
18
- def reset_backoff
19
- @backoff_counter = 0
20
- end
21
-
22
- def time_periods
23
- @options[:time_periods]
24
- end
25
-
26
- private
27
-
28
- def backoff
29
- @backoff_counter += 1
30
- end
31
- end
32
-
33
- describe RdsRotateDbSnapshots::Actions do
34
- let(:test_class) { Test.new(script_name: 'test', options: options) }
35
- let(:client) { double("client") }
36
- let(:time_periods) do
37
- {
38
- hourly: { seconds: 60 * 60, format: '%Y-%m-%d-%H', keep: 0, keeping: {} },
39
- daily: { seconds: 24 * 60 * 60, format: '%Y-%m-%d', keep: 1, keeping: {} },
40
- weekly: { seconds: 7 * 24 * 60 * 60, format: '%Y-%W', keep: 0, keeping: {} },
41
- monthly: { seconds: 30 * 24 * 60 * 60, format: '%Y-%m', keep: 0, keeping: {} }
42
- }
43
- end
44
- let(:options) do
45
- {
46
- aws_access_key: 'ACCESS_KEY',
47
- aws_secret_access_key: 'SECRET_KEY',
48
- aws_region: 'REGION',
49
- pattern: 'test',
50
- dry_run: false,
51
- backoff_limit: 15,
52
- time_periods: time_periods
53
- }
54
- end
55
- let(:rds_snapshots) do
56
- [
57
- { snapshot_create_time: Time.now - (24 * 3600), db_instance_identifier: 'test_db',
58
- db_snapshot_identifier: 'test_snapshot' },
59
- { snapshot_create_time: Time.now, db_instance_identifier: 'test_db', db_snapshot_identifier: 'test_snapshot2' }
60
- ]
61
- end
62
-
63
- before do
64
- allow(client).to receive(:describe_db_snapshots).and_return(rds_snapshots)
65
- allow(test_class).to receive(:client).and_return(client)
66
- end
67
-
68
- describe "#rotate_em" do
69
- it "deletes the snapshots that are not part of the specified time periods" do
70
- expect(client).to receive(:delete_db_snapshot)
71
- test_class.rotate_em(rds_snapshots)
72
- end
73
-
74
- context 'when the snapshot is not matched with pattern' do
75
- let(:rds_snapshots) do
76
- [
77
- { snapshot_create_time: Time.now - (49 * 3600), db_instance_identifier: 'test_db',
78
- db_snapshot_identifier: 'other_snapshot' },
79
- { snapshot_create_time: Time.now - (48 * 3600), db_instance_identifier: 'test_db',
80
- db_snapshot_identifier: 'test_snapshot' },
81
- { snapshot_create_time: Time.now, db_instance_identifier: 'test_db',
82
- db_snapshot_identifier: 'test_snapshot2' }
83
- ]
84
- end
85
-
86
- it "deletes the snapshots that are matched with pattern" do
87
- expect(client).to receive(:delete_db_snapshot).with(db_snapshot_identifier: 'test_snapshot')
88
- test_class.rotate_em(rds_snapshots)
89
- end
90
- end
91
- end
92
-
93
- describe "#create_snapshot" do
94
- it "creates a snapshot with the specified name" do
95
- expect(client).to receive(:create_db_snapshot)
96
- test_class.create_snapshot('test', ['test_db'])
97
- end
98
-
99
- context "when snaphost name is invalid" do
100
- it "raises an error SystemExit" do
101
- expect { test_class.create_snapshot('$#', ['test_db']) }.to raise_error(SystemExit)
102
- end
103
- end
104
- end
105
-
106
- describe "#get_db_snapshots" do
107
- let(:snapshots) { double('snapshots', db_snapshots: rds_snapshots) }
108
-
109
- it "returns the list of snapshots from the client" do
110
- allow(snapshots).to receive(:[]).with(:marker).and_return(nil)
111
- expect(client).to receive(:describe_db_snapshots).and_return(snapshots)
112
- snapshots = test_class.get_db_snapshots(options)
113
- expect(snapshots).to eq(rds_snapshots)
114
- end
115
- end
116
- end
@@ -1,30 +0,0 @@
1
- require 'helper'
2
-
3
- describe RdsRotateDbSnapshots::OptionsParser do
4
- subject { described_class.new(script_name: script_name, cli: true).parse! }
5
-
6
- let(:script_name) { "rds_rotate_snapshots.rb" }
7
-
8
- describe "#parse!" do
9
- before { ARGV.clear }
10
-
11
- it "parses options correctly" do
12
- ARGV.push("--aws-access-key", "ACCESS_KEY", "--aws-secret-access-key", "SECRET_KEY", "--aws-region", "REGION",
13
- "--pattern", "PATTERN", "--backoff-limit", "20", "--create-snapshot", "snapshot")
14
- options = subject
15
-
16
- expect(options[:aws_access_key]).to eq("ACCESS_KEY")
17
- expect(options[:aws_secret_access_key]).to eq("SECRET_KEY")
18
- expect(options[:aws_region]).to eq("REGION")
19
- expect(options[:pattern]).to eq("PATTERN")
20
- expect(options[:backoff_limit]).to eq("20")
21
- expect(options[:create_snapshot]).to eq("snapshot")
22
- end
23
-
24
- it "raises NotImplementedError when by-tags option is passed and it is not implemented" do
25
- ARGV.push("--by-tags", "tag=value,tag2=value")
26
-
27
- expect { subject }.to raise_error(RdsRotateDbSnapshots::OptionsParser::NotImplementedError)
28
- end
29
- end
30
- end
@@ -1,34 +0,0 @@
1
- require 'helper'
2
-
3
- describe RdsRotateDbSnapshots::RdsClient do
4
- let(:options) do
5
- {
6
- aws_access_key: "ACCESS_KEY",
7
- aws_secret_access_key: "SECRET_KEY",
8
- aws_session_token: "SESSION_TOKEN",
9
- aws_region: "REGION"
10
- }
11
- end
12
- let(:rds_client) { described_class.new(options) }
13
-
14
- it 'configures the client with the correct credentials and region' do
15
- expect(rds_client.instance_variable_get(:@client).config.credentials)
16
- .to have_attributes(access_key_id: "ACCESS_KEY", secret_access_key: "SECRET_KEY", session_token: "SESSION_TOKEN")
17
- expect(rds_client.instance_variable_get(:@client).config.region).to eq("REGION")
18
- end
19
-
20
- it 'delegates describe_db_snapshots method to the @client object' do
21
- expect(rds_client.instance_variable_get(:@client)).to receive(:describe_db_snapshots)
22
- rds_client.describe_db_snapshots
23
- end
24
-
25
- it 'delegates create_db_snapshot method to the @client object' do
26
- expect(rds_client.instance_variable_get(:@client)).to receive(:create_db_snapshot)
27
- rds_client.create_db_snapshot("test-snapshot")
28
- end
29
-
30
- it 'delegates delete_db_snapshot method to the @client object' do
31
- expect(rds_client.instance_variable_get(:@client)).to receive(:delete_db_snapshot)
32
- rds_client.delete_db_snapshot("test-snapshot")
33
- end
34
- end
@@ -1,63 +0,0 @@
1
- require "helper"
2
-
3
- describe RdsRotateDbSnapshots do
4
- subject { described_class.new(script_name: script_name, cli: cli) }
5
-
6
- let(:script_name) { "test" }
7
- let(:cli) { true }
8
-
9
- before do
10
- allow(Aws::RDS::Client).to receive(:new)
11
- end
12
-
13
- describe "on include" do
14
- it "adds action methods to the base class" do
15
- expect(described_class.instance_methods).to include(:rotate_em)
16
- expect(described_class.instance_methods).to include(:create_snapshot)
17
- expect(described_class.instance_methods).to include(:get_db_snapshots)
18
- expect(described_class.instance_methods).to include(:rotate_by_tags)
19
- expect(described_class.instance_methods).to include(:client)
20
- expect(described_class.instance_methods).to include(:time_periods)
21
- end
22
- end
23
-
24
- describe "#client" do
25
- it "returns an RdsClient" do
26
- expect(subject.client).to be_a(RdsRotateDbSnapshots::RdsClient)
27
- end
28
- end
29
-
30
- describe "#rds_client" do
31
- it "returns an RdsClient" do
32
- expect(subject.rds_client).to be_a(RdsRotateDbSnapshots::RdsClient)
33
- end
34
- end
35
-
36
- describe "#reset_backoff" do
37
- it "resets backoff counter" do
38
- subject.instance_variable_set(:@backoff_counter, 1)
39
- subject.reset_backoff
40
- expect(subject.instance_variable_get(:@backoff_counter)).to eq(0)
41
- end
42
- end
43
-
44
- describe "#time_periods" do
45
- it "returns time periods" do
46
- expect(subject.time_periods).to eq(
47
- daily: { format: "%Y-%m-%d", keep: 0, keeping: {}, seconds: 86_400 },
48
- hourly: { format: "%Y-%m-%d-%H", keep: 0, keeping: {}, seconds: 3600 },
49
- monthly: { format: "%Y-%m", keep: 0, keeping: {}, seconds: 2_592_000 },
50
- weekly: { format: "%Y-%W", keep: 0, keeping: {}, seconds: 604_800 },
51
- yearly: { format: "%Y", keep: 0, keeping: {}, seconds: 31_104_000 }
52
- )
53
- end
54
- end
55
-
56
- describe "#backoff" do
57
- it "backs off" do
58
- subject.instance_variable_set(:@backoff_counter, 1)
59
- expect(subject).to receive(:sleep)
60
- subject.send(:backoff)
61
- end
62
- end
63
- end