|
2 | 2 |
|
3 | 3 | require 'spec_helper'
|
4 | 4 |
|
5 |
| -describe DatadogBackup::Core do |
| 5 | +describe DatadogBackup::Resources do |
6 | 6 | let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
7 | 7 | let(:api_client_double) { Faraday.new { |f| f.adapter :test, stubs } }
|
8 | 8 | let(:tempdir) { Dir.mktmpdir }
|
9 |
| - let(:core) do |
10 |
| - core = described_class.new( |
| 9 | + let(:resources) do |
| 10 | + resources = described_class.new( |
11 | 11 | action: 'backup',
|
12 | 12 | backup_dir: tempdir,
|
13 | 13 | diff_format: nil,
|
14 | 14 | resources: [],
|
15 | 15 | output_format: :json
|
16 | 16 | )
|
17 |
| - allow(core).to receive(:api_service).and_return(api_client_double) |
18 |
| - return core |
| 17 | + allow(resources).to receive(:api_service).and_return(api_client_double) |
| 18 | + return resources |
19 | 19 | end
|
20 | 20 |
|
21 | 21 | describe '#diff' do
|
22 |
| - subject(:diff) { core.diff('diff') } |
| 22 | + subject(:diff) { resources.diff('diff') } |
23 | 23 |
|
24 | 24 | before do
|
25 |
| - allow(core).to receive(:get_by_id).and_return({ 'text' => 'diff1', 'extra' => 'diff1' }) |
26 |
| - core.write_file('{"text": "diff2", "extra": "diff2"}', "#{tempdir}/core/diff.json") |
| 25 | + allow(resources).to receive(:get_by_id).and_return({ 'text' => 'diff1', 'extra' => 'diff1' }) |
| 26 | + resources.write_file('{"text": "diff2", "extra": "diff2"}', "#{tempdir}/resources/diff.json") |
27 | 27 | end
|
28 | 28 |
|
29 | 29 | it {
|
|
39 | 39 | end
|
40 | 40 |
|
41 | 41 | describe '#except' do
|
42 |
| - subject { core.except({ a: :b, b: :c }) } |
| 42 | + subject { resources.except({ a: :b, b: :c }) } |
43 | 43 |
|
44 | 44 | it { is_expected.to eq({ a: :b, b: :c }) }
|
45 | 45 | end
|
46 | 46 |
|
47 | 47 | describe '#initialize' do
|
48 |
| - subject(:mycore) { core } |
| 48 | + subject(:myresources) { resources } |
49 | 49 |
|
50 | 50 | it 'makes the subdirectories' do
|
51 | 51 | fileutils = class_double(FileUtils).as_stubbed_const
|
52 | 52 | allow(fileutils).to receive(:mkdir_p)
|
53 |
| - mycore |
54 |
| - expect(fileutils).to have_received(:mkdir_p).with("#{tempdir}/core") |
| 53 | + myresources |
| 54 | + expect(fileutils).to have_received(:mkdir_p).with("#{tempdir}/resources") |
55 | 55 | end
|
56 | 56 | end
|
57 | 57 |
|
58 | 58 | describe '#myclass' do
|
59 |
| - subject { core.myclass } |
| 59 | + subject { resources.myclass } |
60 | 60 |
|
61 |
| - it { is_expected.to eq 'core' } |
| 61 | + it { is_expected.to eq 'resources' } |
62 | 62 | end
|
63 | 63 |
|
64 | 64 | describe '#create' do
|
65 |
| - subject(:create) { core.create({ 'a' => 'b' }) } |
| 65 | + subject(:create) { resources.create({ 'a' => 'b' }) } |
66 | 66 |
|
67 | 67 | example 'it will post /api/v1/dashboard' do
|
68 |
| - allow(core).to receive(:api_version).and_return('v1') |
69 |
| - allow(core).to receive(:api_resource_name).and_return('dashboard') |
| 68 | + allow(resources).to receive(:api_version).and_return('v1') |
| 69 | + allow(resources).to receive(:api_resource_name).and_return('dashboard') |
70 | 70 | stubs.post('/api/v1/dashboard', { 'a' => 'b' }) { respond_with200({ 'id' => 'whatever-id-abc' }) }
|
71 | 71 | create
|
72 | 72 | stubs.verify_stubbed_calls
|
73 | 73 | end
|
74 | 74 | end
|
75 | 75 |
|
76 | 76 | describe '#update' do
|
77 |
| - subject(:update) { core.update('abc-123-def', { 'a' => 'b' }) } |
| 77 | + subject(:update) { resources.update('abc-123-def', { 'a' => 'b' }) } |
78 | 78 |
|
79 | 79 | example 'it puts /api/v1/dashboard' do
|
80 |
| - allow(core).to receive(:api_version).and_return('v1') |
81 |
| - allow(core).to receive(:api_resource_name).and_return('dashboard') |
| 80 | + allow(resources).to receive(:api_version).and_return('v1') |
| 81 | + allow(resources).to receive(:api_resource_name).and_return('dashboard') |
82 | 82 | stubs.put('/api/v1/dashboard/abc-123-def', { 'a' => 'b' }) { respond_with200({ 'id' => 'whatever-id-abc' }) }
|
83 | 83 | update
|
84 | 84 | stubs.verify_stubbed_calls
|
85 | 85 | end
|
86 | 86 |
|
87 | 87 | context 'when the id is not found' do
|
88 | 88 | before do
|
89 |
| - allow(core).to receive(:api_version).and_return('v1') |
90 |
| - allow(core).to receive(:api_resource_name).and_return('dashboard') |
| 89 | + allow(resources).to receive(:api_version).and_return('v1') |
| 90 | + allow(resources).to receive(:api_resource_name).and_return('dashboard') |
91 | 91 | stubs.put('/api/v1/dashboard/abc-123-def', { 'a' => 'b' }) { [404, {}, { 'id' => 'whatever-id-abc' }] }
|
92 | 92 | end
|
93 | 93 |
|
|
99 | 99 |
|
100 | 100 | describe '#restore' do
|
101 | 101 | before do
|
102 |
| - allow(core).to receive(:api_version).and_return('api-version-string') |
103 |
| - allow(core).to receive(:api_resource_name).and_return('api-resource-name-string') |
| 102 | + allow(resources).to receive(:api_version).and_return('api-version-string') |
| 103 | + allow(resources).to receive(:api_resource_name).and_return('api-resource-name-string') |
104 | 104 | stubs.get('/api/api-version-string/api-resource-name-string/abc-123-def') { respond_with200({ 'test' => 'ok' }) }
|
105 | 105 | stubs.get('/api/api-version-string/api-resource-name-string/bad-123-id') do
|
106 | 106 | [404, {}, { 'error' => 'blahblah_not_found' }]
|
107 | 107 | end
|
108 |
| - allow(core).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' }) |
| 108 | + allow(resources).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' }) |
109 | 109 | end
|
110 | 110 |
|
111 | 111 | context 'when id exists' do
|
112 |
| - subject(:restore) { core.restore('abc-123-def') } |
| 112 | + subject(:restore) { resources.restore('abc-123-def') } |
113 | 113 |
|
114 | 114 | example 'it calls out to update' do
|
115 |
| - allow(core).to receive(:update) |
| 115 | + allow(resources).to receive(:update) |
116 | 116 | restore
|
117 |
| - expect(core).to have_received(:update).with('abc-123-def', { 'load' => 'ok' }) |
| 117 | + expect(resources).to have_received(:update).with('abc-123-def', { 'load' => 'ok' }) |
118 | 118 | end
|
119 | 119 | end
|
120 | 120 |
|
121 | 121 | context 'when id does not exist on remote' do
|
122 |
| - subject(:restore_newly) { core.restore('bad-123-id') } |
| 122 | + subject(:restore_newly) { resources.restore('bad-123-id') } |
123 | 123 |
|
124 | 124 | let(:fileutils) { class_double(FileUtils).as_stubbed_const }
|
125 | 125 |
|
126 | 126 | before do
|
127 |
| - allow(core).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' }) |
| 127 | + allow(resources).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' }) |
128 | 128 | stubs.put('/api/api-version-string/api-resource-name-string/bad-123-id') do
|
129 | 129 | [404, {}, { 'error' => 'id not found' }]
|
130 | 130 | end
|
131 | 131 | stubs.post('/api/api-version-string/api-resource-name-string', { 'load' => 'ok' }) do
|
132 | 132 | respond_with200({ 'id' => 'my-new-id' })
|
133 | 133 | end
|
134 | 134 | allow(fileutils).to receive(:rm)
|
135 |
| - allow(core).to receive(:create).with({ 'load' => 'ok' }).and_return({ 'id' => 'my-new-id' }) |
136 |
| - allow(core).to receive(:get_and_write_file) |
137 |
| - allow(core).to receive(:find_file_by_id).with('bad-123-id').and_return('/path/to/bad-123-id.json') |
| 135 | + allow(resources).to receive(:create).with({ 'load' => 'ok' }).and_return({ 'id' => 'my-new-id' }) |
| 136 | + allow(resources).to receive(:get_and_write_file) |
| 137 | + allow(resources).to receive(:find_file_by_id).with('bad-123-id').and_return('/path/to/bad-123-id.json') |
138 | 138 | end
|
139 | 139 |
|
140 | 140 | example 'it calls out to create' do
|
141 | 141 | restore_newly
|
142 |
| - expect(core).to have_received(:create).with({ 'load' => 'ok' }) |
| 142 | + expect(resources).to have_received(:create).with({ 'load' => 'ok' }) |
143 | 143 | end
|
144 | 144 |
|
145 | 145 | example 'it saves the new file' do
|
146 | 146 | restore_newly
|
147 |
| - expect(core).to have_received(:get_and_write_file).with('my-new-id') |
| 147 | + expect(resources).to have_received(:get_and_write_file).with('my-new-id') |
148 | 148 | end
|
149 | 149 |
|
150 | 150 | example 'it deletes the old file' do
|
|
0 commit comments