/
packager_spec.rb
217 lines (184 loc) · 7.55 KB
/
packager_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require "spec_helper"
describe "Backup::Packager" do
let(:packager) { Backup::Packager }
it "should include Utilities::Helpers" do
expect(packager.instance_eval("class << self; self; end")
.include?(Backup::Utilities::Helpers)).to eq(true)
end
describe "#package!" do
let(:model) { mock }
let(:package) { mock }
let(:encryptor) { mock }
let(:splitter) { mock }
let(:pipeline) { mock }
let(:procedure) { mock }
let(:s) { sequence "" }
context "when pipeline command is successful" do
it "should setup variables and perform packaging procedures" do
model.expects(:package).in_sequence(s).returns(package)
model.expects(:encryptor).in_sequence(s).returns(encryptor)
model.expects(:splitter).in_sequence(s).returns(splitter)
Backup::Pipeline.expects(:new).in_sequence(s).returns(pipeline)
Backup::Logger.expects(:info).in_sequence(s).with(
"Packaging the backup files..."
)
packager.expects(:procedure).in_sequence(s).returns(procedure)
procedure.expects(:call).in_sequence(s)
pipeline.expects(:success?).in_sequence(s).returns(true)
Backup::Logger.expects(:info).in_sequence(s).with(
"Packaging Complete!"
)
packager.package!(model)
expect(packager.instance_variable_get(:@package)).to be(package)
expect(packager.instance_variable_get(:@encryptor)).to be(encryptor)
expect(packager.instance_variable_get(:@splitter)).to be(splitter)
expect(packager.instance_variable_get(:@pipeline)).to be(pipeline)
end
end # context 'when pipeline command is successful'
context "when pipeline command is not successful" do
it "should raise an error" do
model.expects(:package).in_sequence(s).returns(package)
model.expects(:encryptor).in_sequence(s).returns(encryptor)
model.expects(:splitter).in_sequence(s).returns(splitter)
Backup::Pipeline.expects(:new).in_sequence(s).returns(pipeline)
Backup::Logger.expects(:info).in_sequence(s).with(
"Packaging the backup files..."
)
packager.expects(:procedure).in_sequence(s).returns(procedure)
procedure.expects(:call).in_sequence(s)
pipeline.expects(:success?).in_sequence(s).returns(false)
pipeline.expects(:error_messages).in_sequence(s).returns("pipeline_errors")
expect do
packager.package!(model)
end.to raise_error(
Backup::Packager::Error,
"Packager::Error: Failed to Create Backup Package\n" \
" pipeline_errors"
)
expect(packager.instance_variable_get(:@package)).to be(package)
expect(packager.instance_variable_get(:@encryptor)).to be(encryptor)
expect(packager.instance_variable_get(:@splitter)).to be(splitter)
expect(packager.instance_variable_get(:@pipeline)).to be(pipeline)
end
end # context 'when pipeline command is successful'
end # describe '#package!'
describe "#procedure" do
module Fake
def self.stack_trace
@stack ||= []
end
class Encryptor
def encrypt_with
Fake.stack_trace << :encryptor_before
yield "encryption_command", ".enc"
Fake.stack_trace << :encryptor_after
end
end
class Splitter
def split_with
Fake.stack_trace << :splitter_before
yield "splitter_command"
Fake.stack_trace << :splitter_after
end
end
class Package
attr_accessor :trigger, :extension
def basename
"base_filename." + extension
end
end
end
let(:package) { Fake::Package.new }
let(:encryptor) { Fake::Encryptor.new }
let(:splitter) { Fake::Splitter.new }
let(:pipeline) { mock }
let(:s) { sequence "" }
before do
Fake.stack_trace.clear
packager.expects(:utility).with(:tar).returns("tar")
packager.instance_variable_set(:@package, package)
packager.instance_variable_set(:@pipeline, pipeline)
package.trigger = "model_trigger"
package.extension = "tar"
end
context "when no encryptor or splitter are defined" do
it "should package the backup without encryption into a single file" do
packager.expects(:utility).with(:cat).returns("cat")
packager.instance_variable_set(:@encryptor, nil)
packager.instance_variable_set(:@splitter, nil)
pipeline.expects(:add).in_sequence(s).with(
"tar -cf - -C '#{Backup::Config.tmp_path}' 'model_trigger'", [0, 1]
)
pipeline.expects(:<<).in_sequence(s).with(
"cat > #{File.join(Backup::Config.tmp_path, "base_filename.tar")}"
)
pipeline.expects(:run).in_sequence(s)
packager.send(:procedure).call
end
end
context "when only an encryptor is configured" do
it "should package the backup with encryption" do
packager.expects(:utility).with(:cat).returns("cat")
packager.instance_variable_set(:@encryptor, encryptor)
packager.instance_variable_set(:@splitter, nil)
pipeline.expects(:add).in_sequence(s).with(
"tar -cf - -C '#{Backup::Config.tmp_path}' 'model_trigger'", [0, 1]
)
pipeline.expects(:<<).in_sequence(s).with("encryption_command")
pipeline.expects(:<<).in_sequence(s).with(
"cat > #{File.join(Backup::Config.tmp_path, "base_filename.tar.enc")}"
)
pipeline.expects(:run).in_sequence(s).with do
Fake.stack_trace << :command_executed
true
end
packager.send(:procedure).call
expect(Fake.stack_trace).to eq([
:encryptor_before, :command_executed, :encryptor_after
])
end
end
context "when only a splitter is configured" do
it "should package the backup without encryption through the splitter" do
packager.expects(:utility).with(:cat).never
packager.instance_variable_set(:@encryptor, nil)
packager.instance_variable_set(:@splitter, splitter)
pipeline.expects(:add).in_sequence(s).with(
"tar -cf - -C '#{Backup::Config.tmp_path}' 'model_trigger'", [0, 1]
)
pipeline.expects(:<<).in_sequence(s).with("splitter_command")
pipeline.expects(:run).in_sequence(s).with do
Fake.stack_trace << :command_executed
true
end
packager.send(:procedure).call
expect(Fake.stack_trace).to eq([
:splitter_before, :command_executed, :splitter_after
])
end
end
context "when both an encryptor and a splitter are configured" do
it "should package the backup with encryption through the splitter" do
packager.expects(:utility).with(:cat).never
packager.instance_variable_set(:@encryptor, encryptor)
packager.instance_variable_set(:@splitter, splitter)
pipeline.expects(:add).in_sequence(s).with(
"tar -cf - -C '#{Backup::Config.tmp_path}' 'model_trigger'", [0, 1]
)
pipeline.expects(:<<).in_sequence(s).with("encryption_command")
pipeline.expects(:<<).in_sequence(s).with("splitter_command")
pipeline.expects(:run).in_sequence(s).with do
Fake.stack_trace << :command_executed
true
end
packager.send(:procedure).call
expect(Fake.stack_trace).to eq([
:encryptor_before, :splitter_before,
:command_executed,
:splitter_after, :encryptor_after
])
expect(package.extension).to eq("tar.enc")
end
end
end # describe '#procedure'
end