forked from chef/omnibus
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmetadata_spec.rb
More file actions
368 lines (310 loc) · 12.5 KB
/
metadata_spec.rb
File metadata and controls
368 lines (310 loc) · 12.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
require "spec_helper"
module Omnibus
describe Metadata do
let(:instance) do
double(described_class, path: "/path/to/package.deb.metadata.json")
end
let(:package_path) { "/path/to/package.deb" }
let(:license_path) { "/opt/project/LICENSE" }
let(:package) do
double(Package,
name: "package",
path: package_path,
md5: "abc123",
sha1: "abc123",
sha256: "abcd1234",
sha512: "abcdef123456"
)
end
let(:project) do
double(Project,
name: "some-project",
friendly_name: "Some Project",
homepage: "https://some.project.io",
build_version: "1.2.3",
build_iteration: "1",
license: "Apache-2.0",
built_manifest: double(Manifest,
to_hash: {
manifest_format: 2,
build_version: "1.2.3",
build_git_revision: "SHA",
license: "Apache-2.0",
}
),
license_file_path: license_path
)
end
let(:data) { { foo: "bar" } }
let(:license_file_content) do
<<-EOH
some_project 1.2.3 license: "Apache-2.0"
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
...
EOH
end
subject { described_class.new(package, data) }
describe ".generate" do
let(:metadata_json_content) { StringIO.new }
let(:package_path) { "/path/to/package.deb" }
before do
allow(File).to receive(:exist?).with(package_path).and_return(true)
allow(Package).to receive(:new).with(package_path).and_return(package)
allow(File).to receive(:exist?).with(license_path).and_return(true)
allow(File).to receive(:read).with(license_path).and_return(license_file_content)
# mock out `Metadata#save`
allow(File).to receive(:open).with("#{package_path}.metadata.json", "w+").and_yield(metadata_json_content)
end
it "creates a *.metadata.json file with the correct content" do
described_class.generate(package_path, project)
expect(metadata_json_content.string).to include_json(
basename: "package",
md5: "abc123",
sha1: "abc123",
sha256: "abcd1234",
sha512: "abcdef123456",
platform: "ubuntu",
platform_version: "12.04",
arch: "x86_64",
name: "some-project",
friendly_name: "Some Project",
homepage: "https://some.project.io",
version: "1.2.3",
iteration: "1",
version_manifest: {
manifest_format: 2,
build_version: "1.2.3",
build_git_revision: "SHA",
license: "Apache-2.0",
},
license_content: license_file_content
)
end
context "the package file does not exist" do
before do
allow(File).to receive(:exist?).with(package_path).and_return(false)
end
it "raises an exception" do
expect { described_class.generate(package_path, project) }.to raise_error(Omnibus::NoPackageFile)
end
end
context "the license file does not exist" do
before do
allow(File).to receive(:exist?).with(license_path).and_return(false)
end
it "does not include the license content" do
described_class.generate(package_path, project)
expect(metadata_json_content.string).to include_json(
license_content: ""
)
end
end
end
describe ".arch" do
let(:architecture) { "x86_64" }
before do
stub_ohai(platform: "ubuntu", version: "12.04") do |data|
data["kernel"]["machine"] = architecture
end
end
it "returns the architecture" do
expect(described_class.arch).to eq("x86_64")
end
context "on solaris" do
before do
stub_ohai(platform: "solaris2", version: "5.11") do |data|
data["platform"] = "solaris2"
data["kernel"]["machine"] = architecture
end
end
context "architecture is Intel-based" do
let(:architecture) { "i86pc" }
it "returns i386" do
expect(described_class.arch).to eq("i386")
end
end
context "architecture is SPARC-based" do
let(:architecture) { "sun4v" }
it "returns sparc" do
expect(described_class.arch).to eq("sparc")
end
end
end
context "on windows" do
before do
stub_ohai(platform: "windows", version: "2012R2") do |data|
data["kernel"]["machine"] = architecture
end
end
it "returns a 32-bit value based on Config.windows_arch being set to x86" do
expect(Config).to receive(:windows_arch).and_return(:x86)
expect(described_class.arch).to eq("i386")
end
end
end
describe ".platform_shortname" do
it "returns el on rhel" do
stub_ohai(platform: "redhat", version: "6.4")
expect(described_class.platform_shortname).to eq("el")
end
it "returns sles on suse" do
stub_ohai(platform: "suse", version: "12.0")
expect(described_class.platform_shortname).to eq("sles")
end
it "returns .platform on all other systems" do
stub_ohai(platform: "ubuntu", version: "12.04")
expect(described_class.platform_shortname).to eq("ubuntu")
end
end
describe ".platform_version" do
shared_examples "a version manipulator" do |platform_shortname, version, expected|
context "on #{platform_shortname}-#{version}" do
it "returns the correct value" do
stub_ohai(platform: "ubuntu", version: "12.04") do |data|
data["platform"] = platform_shortname
data["platform_version"] = version
end
expect(described_class.platform_version).to eq(expected)
end
end
end
it_behaves_like "a version manipulator", "aix", "7.1", "7.1"
it_behaves_like "a version manipulator", "arch", "rolling", "rolling"
it_behaves_like "a version manipulator", "centos", "5.9.6", "5"
it_behaves_like "a version manipulator", "debian", "7.1", "7"
it_behaves_like "a version manipulator", "debian", "6.9", "6"
it_behaves_like "a version manipulator", "el", "6.5", "6"
it_behaves_like "a version manipulator", "fedora", "11.5", "11"
it_behaves_like "a version manipulator", "freebsd", "10.0", "10"
it_behaves_like "a version manipulator", "gentoo", "2004.3", "2004.3"
it_behaves_like "a version manipulator", "ios_xr", "6.0.0.14I", "6"
it_behaves_like "a version manipulator", "mac_os_x", "10.9.1", "10.9"
it_behaves_like "a version manipulator", "mac_os_x", "10.15.7", "10.15"
it_behaves_like "a version manipulator", "mac_os_x", "11.2.1", "11"
it_behaves_like "a version manipulator", "nexus", "5.0", "5"
it_behaves_like "a version manipulator", "omnios", "r151010", "r151010"
it_behaves_like "a version manipulator", "openbsd", "5.4.4", "5.4"
it_behaves_like "a version manipulator", "opensuse", "5.9", "5.9"
it_behaves_like "a version manipulator", "pidora", "11.5", "11"
it_behaves_like "a version manipulator", "raspbian", "7.1", "7"
it_behaves_like "a version manipulator", "rhel", "6.5", "6"
it_behaves_like "a version manipulator", "slackware", "12.0.1", "12.0"
it_behaves_like "a version manipulator", "sles", "11.2", "11"
it_behaves_like "a version manipulator", "suse", "12.0", "12"
it_behaves_like "a version manipulator", "smartos", "20120809T221258Z", "20120809T221258Z"
it_behaves_like "a version manipulator", "solaris2", "5.9", "5.9"
it_behaves_like "a version manipulator", "ubuntu", "10.04", "10.04"
it_behaves_like "a version manipulator", "ubuntu", "10.04.04", "10.04"
it_behaves_like "a version manipulator", "windows", "5.0.2195", "2000"
it_behaves_like "a version manipulator", "windows", "5.1.2600", "xp"
it_behaves_like "a version manipulator", "windows", "5.2.3790", "2003r2"
it_behaves_like "a version manipulator", "windows", "6.0.6001", "2008"
it_behaves_like "a version manipulator", "windows", "6.1.7600", "7"
it_behaves_like "a version manipulator", "windows", "6.1.7601", "2008r2"
it_behaves_like "a version manipulator", "windows", "6.2.9200", "2012"
it_behaves_like "a version manipulator", "windows", "6.3.9200", "2012r2"
it_behaves_like "a version manipulator", "windows", "6.3.9600", "2012r2"
it_behaves_like "a version manipulator", "windows", "10.0.10240", "10"
context "given an unknown platform" do
before do
stub_ohai(platform: "ubuntu", version: "12.04") do |data|
data["platform"] = "bacon"
data["platform_version"] = "1.crispy"
end
end
it "raises an exception" do
expect { described_class.platform_version }
.to raise_error(UnknownPlatform)
end
end
context "given an unknown windows platform version" do
before do
stub_ohai(platform: "ubuntu", version: "12.04") do |data|
data["platform"] = "windows"
data["platform_version"] = "1.2.3"
end
end
it "raises an exception" do
expect { described_class.platform_version }
.to raise_error(UnknownPlatformVersion)
end
end
end
describe ".for_package" do
it "raises an exception when the file does not exist" do
allow(File).to receive(:read).and_raise(Errno::ENOENT)
expect { described_class.for_package(package) }
.to raise_error(NoPackageMetadataFile)
end
it "returns a metadata object" do
allow(File).to receive(:read).and_return('{ "platform": "ubuntu" }')
expect(described_class.for_package(package)).to be_a(described_class)
end
it "loads the metadata from disk" do
allow(File).to receive(:read).and_return('{ "platform": "ubuntu" }')
instance = described_class.for_package(package)
expect(instance[:platform]).to eq("ubuntu")
end
it "ensures platform version is properly truncated" do
allow(File).to receive(:read).and_return('{ "platform": "el", "platform_version": "5.10" }')
instance = described_class.for_package(package)
expect(instance[:platform_version]).to eq("5")
end
it "correctly truncates sles platform versions" do
allow(File).to receive(:read).and_return('{ "platform": "sles", "platform_version": "11.2" }')
instance = described_class.for_package(package)
expect(instance[:platform_version]).to eq("11")
end
it "ensures an iteration exists" do
allow(File).to receive(:read).and_return("{}")
instance = described_class.for_package(package)
expect(instance[:iteration]).to eq(1)
end
it "does not change existing iterations" do
allow(File).to receive(:read).and_return('{ "iteration": 4 }')
instance = described_class.for_package(package)
expect(instance[:iteration]).to eq(4)
end
end
describe ".path_for" do
it "returns the postfixed .metadata.json" do
expect(described_class.path_for(package))
.to eq("/path/to/package.deb.metadata.json")
end
end
describe '#name' do
it "returns the basename of the package" do
expect(subject.name).to eq("package.deb.metadata.json")
end
end
describe '#path' do
it "delegates to .path_for" do
expect(described_class).to receive(:path_for).once
subject.path
end
end
describe '#save' do
let(:file) { double(File) }
before { allow(File).to receive(:open).and_yield(file) }
it "saves the file to disk" do
expect(file).to receive(:write).once
subject.save
end
end
describe '#to_json' do
it "generates pretty JSON" do
expect(subject.to_json.strip).to eq <<-EOH.gsub(/^ {10}/, "").strip
{
"foo": "bar"
}
EOH
end
end
end
end