-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathresource_match_spec.rb
More file actions
128 lines (113 loc) · 6.53 KB
/
resource_match_spec.rb
File metadata and controls
128 lines (113 loc) · 6.53 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
require 'spec_helper'
module VCAP::CloudController
RSpec.describe ResourceMatch do
include_context 'resource pool'
let(:descriptors) do
[
{ 'sha1' => 0, 'size' => 500, 'mode' => '666' },
{ 'sha1' => 1, 'size' => 1024, 'mode' => '666' },
{ 'sha1' => 2, 'size' => 10_011, 'mode' => '666' },
{ 'sha1' => 3, 'size' => 50_011, 'mode' => '666' },
{ 'sha1' => 4, 'size' => 90_011, 'mode' => '666' },
{ 'sha1' => 5, 'size' => 11_100_000, 'mode' => '666' },
{ 'sha1' => 6, 'size' => 22_200_000, 'mode' => '666' },
{ 'sha1' => 7, 'size' => 32_200_000, 'mode' => '666' },
{ 'sha1' => 8, 'size' => 1_110_000_000, 'mode' => '666' }
]
end
describe '#match_resources' do
before do
@resource_pool.add_directory(@tmpdir)
end
it 'returns an empty list when no resources match' do
res = ResourceMatch.new([@nonexisting_descriptor]).match_resources
expect(res).to eq([])
end
it 'returns a resource that matches' do
res = ResourceMatch.new([@descriptors.first, @nonexisting_descriptor]).match_resources
expect(res).to eq([@descriptors.first])
end
it 'returns many resources that match' do
res = ResourceMatch.new(@descriptors + [@nonexisting_descriptor]).match_resources
expect(res).to eq(@descriptors)
end
context 'one resource with a not allowed size and one with a not allowed mode' do
before do
# 'size_allowed?' and 'mode_allowed?' return 'false' once
allow(@resource_pool).to receive(:size_allowed?).and_return(false, true)
allow(@resource_pool).to receive(:mode_allowed?).and_return(false, true)
end
it 'returns two resources less' do
res = ResourceMatch.new(@descriptors).match_resources
expect(res.length).to eq(@descriptors.length - 2)
end
end
it 'does not break when the sha1 is not long enough to generate a key' do
expect do
ResourceMatch.new([{ 'sha1' => 0, 'size' => 123, 'mode' => '666' }]).match_resources
ResourceMatch.new([{ 'sha1' => 'abc', 'size' => 234, 'mode' => '666' }]).match_resources
end.not_to raise_error
end
describe 'logging' do
let(:maximum_file_size) { 500.megabytes } # this is arbitrary
let(:minimum_file_size) { 3.kilobytes }
it 'correctly calculates and logs the time for each file size range' do
Timecop.freeze do
allow(ResourcePool.instance).to receive(:resource_known?) do
Timecop.freeze(2.seconds.from_now) { true }
end
expect(Steno.logger('cc.resource_pool')).to receive(:info).once.with('starting resource matching', {
total_resources_to_match: 6,
resource_count_by_size: {
'1KB or less': 0,
'1KB to 100KB': 3,
'100KB to 1MB': 0,
'1MB to 100MB': 3,
'100MB to 1GB': 0,
'1GB or more': 0
}
})
expect(Steno.logger('cc.resource_pool')).to receive(:info).once.with('done matching resources', {
total_resources_to_match: 6,
total_resource_match_time: '12.0 seconds',
resource_count_by_size: {
'1KB or less': 0,
'1KB to 100KB': 3,
'100KB to 1MB': 0,
'1MB to 100MB': 3,
'100MB to 1GB': 0,
'1GB or more': 0
},
resource_match_time_by_size: {
'1KB or less': '0.0 seconds',
'1KB to 100KB': '6.0 seconds',
'100KB to 1MB': '0.0 seconds',
'1MB to 100MB': '6.0 seconds',
'100MB to 1GB': '0.0 seconds',
'1GB or more': '0.0 seconds'
}
})
ResourceMatch.new(descriptors).match_resources
end
after { Timecop.return }
end
end
describe '#resource_count_by_filesize' do
let(:maximum_file_size) { 500.megabytes } # this is arbitrary
let(:minimum_file_size) { 3.kilobytes }
it 'correctly calculates the quantity of each file size range' do
histogram = ResourceMatch.new(descriptors).resource_count_by_filesize
# Filters out descriptors outside of range
expected_histogram = {
'1KB or less': 0,
'1KB to 100KB': 3,
'100KB to 1MB': 0,
'1MB to 100MB': 3,
'100MB to 1GB': 0,
'1GB or more': 0
}
expect(histogram).to eq(expected_histogram)
end
end
end
end