Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit c837be0

Browse files
authored
test: combine methods of testing metadata (cloudfoundry#1933)
Removes previous patterns for testing metadata and replaces them with a new, more fluent method. [#173082202](https://www.pivotaltracker.com/story/show/173082202)
1 parent ac648d8 commit c837be0

37 files changed

Lines changed: 487 additions & 461 deletions

spec/request/app_manifests_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@
107107

108108
expect(app_model.service_bindings.length).to eq 1
109109
expect(app_model.service_bindings.first.service_instance).to eq service_instance
110-
expect(app_model.labels.map { |label| { key: label.key_name, value: label.value } }).
111-
to match_array([{ key: 'potato', value: 'yam' }, { key: 'songs', value: 'missing' }])
112-
expect(app_model.annotations.map { |a| { key: a.key, value: a.value } }).
113-
to match_array([{ key: 'potato', value: 'idaho' }, { key: 'juice', value: 'newton' }])
110+
expect(app_model).to have_labels(
111+
{ key: 'potato', value: 'yam' },
112+
{ prefix: 'myspace.com', key: 'songs', value: 'missing' },
113+
)
114+
expect(app_model).to have_annotations(
115+
{ key: 'potato', value: 'idaho' },
116+
{ key: 'juice', value: 'newton' },
117+
)
114118
end
115119

116120
context 'sidecars' do

spec/request/deployments_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,13 @@
294294
expect(last_response.status).to eq(201)
295295

296296
deployment = VCAP::CloudController::DeploymentModel.last
297-
expect(deployment.labels.map(&:value)).to contain_exactly('stable', 'mashed')
298-
expect(deployment.annotations.map(&:value)).to contain_exactly('idaho')
297+
expect(deployment).to have_labels(
298+
{ prefix: 'seriouseats.com', key: 'potato', value: 'mashed' },
299+
{ prefix: nil, key: 'release', value: 'stable' }
300+
)
301+
expect(deployment).to have_annotations(
302+
{ key: 'potato', value: 'idaho' },
303+
)
299304

300305
expect(parsed_response).to be_a_response_like({
301306
'guid' => deployment.guid,

spec/request/service_instances_spec.rb

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -847,12 +847,8 @@ def check_filtered_instances(*instances)
847847
expect(instance.tags).to contain_exactly('foo', 'bar', 'baz')
848848
expect(instance.credentials).to match({ 'foo' => 'bar', 'baz' => 'qux' })
849849
expect(instance.space).to eq(space)
850-
851-
expect_metadata(
852-
instance,
853-
annotations: [{ prefix: nil, key: 'foo', value: 'bar' }],
854-
labels: [{ prefix: nil, key: 'baz', value: 'qux' }]
855-
)
850+
expect(instance).to have_annotations({ prefix: nil, key: 'foo', value: 'bar' })
851+
expect(instance).to have_labels({ prefix: nil, key: 'baz', value: 'qux' })
856852
end
857853

858854
context 'when the name has already been taken' do
@@ -946,11 +942,8 @@ def check_filtered_instances(*instances)
946942
expect(instance.space).to eq(space)
947943
expect(instance.service_plan).to eq(service_plan)
948944

949-
expect_metadata(
950-
instance,
951-
annotations: [{ prefix: nil, key: 'foo', value: 'bar' }],
952-
labels: [{ prefix: nil, key: 'baz', value: 'qux' }]
953-
)
945+
expect(instance).to have_annotations({ prefix: nil, key: 'foo', value: 'bar' })
946+
expect(instance).to have_labels({ prefix: nil, key: 'baz', value: 'qux' })
954947

955948
expect(instance.last_operation.type).to eq('create')
956949
expect(instance.last_operation.state).to eq('in progress')
@@ -1580,18 +1573,15 @@ def check_filtered_instances(*instances)
15801573
service_instance.reload
15811574
expect(service_instance.tags).to eq(%w(baz quz))
15821575

1583-
expect_metadata(
1584-
service_instance,
1585-
annotations: [
1586-
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1587-
{ prefix: nil, key: 'potato', value: 'idaho' },
1588-
{ prefix: nil, key: 'style', value: 'mashed' },
1589-
],
1590-
labels: [
1591-
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' },
1592-
{ prefix: nil, key: 'potato', value: 'yam' },
1593-
{ prefix: nil, key: 'style', value: 'baked' }
1594-
]
1576+
expect(service_instance).to have_annotations(
1577+
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1578+
{ prefix: nil, key: 'potato', value: 'idaho' },
1579+
{ prefix: nil, key: 'style', value: 'mashed' },
1580+
)
1581+
expect(service_instance).to have_labels(
1582+
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' },
1583+
{ prefix: nil, key: 'potato', value: 'yam' },
1584+
{ prefix: nil, key: 'style', value: 'baked' }
15951585
)
15961586

15971587
expect(service_instance.last_operation.type).to eq('update')
@@ -1684,16 +1674,13 @@ def check_filtered_instances(*instances)
16841674
service_instance.reload
16851675
expect(service_instance.reload.tags).to eq(%w(foo bar))
16861676

1687-
expect_metadata(
1688-
service_instance,
1689-
annotations: [
1690-
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1691-
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1692-
],
1693-
labels: [
1694-
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1695-
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' }
1696-
]
1677+
expect(service_instance).to have_annotations(
1678+
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1679+
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1680+
)
1681+
expect(service_instance).to have_labels(
1682+
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1683+
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' }
16971684
)
16981685
end
16991686

@@ -1921,16 +1908,13 @@ def check_filtered_instances(*instances)
19211908
service_instance.reload
19221909
expect(service_instance.reload.tags).to eq(%w(foo bar))
19231910
expect(service_instance.service_plan).to eq(original_service_plan)
1924-
expect_metadata(
1925-
service_instance,
1926-
annotations: [
1927-
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1928-
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1929-
],
1930-
labels: [
1931-
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1932-
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' }
1933-
]
1911+
expect(service_instance).to have_annotations(
1912+
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1913+
{ prefix: 'pre.fix', key: 'fox', value: 'bushy' },
1914+
)
1915+
expect(service_instance).to have_labels(
1916+
{ prefix: 'pre.fix', key: 'to_delete', value: 'value' },
1917+
{ prefix: 'pre.fix', key: 'tail', value: 'fluffy' }
19341918
)
19351919
end
19361920

@@ -2506,10 +2490,10 @@ def check_filtered_instances(*instances)
25062490
let(:guid) { service_instance.guid }
25072491
let(:request_body) {
25082492
{
2509-
metadata: {
2510-
labels: { unit: 'metre', distance: '1003' },
2511-
annotations: { location: 'london' }
2512-
}
2493+
metadata: {
2494+
labels: { unit: 'metre', distance: '1003' },
2495+
annotations: { location: 'london' }
2496+
}
25132497
}
25142498
}
25152499

@@ -2525,10 +2509,10 @@ def check_filtered_instances(*instances)
25252509
api_call.call(admin_headers)
25262510
expect(last_response).to have_status_code(200)
25272511
expect(parsed_response['last_operation']).to include({
2528-
'type' => 'create',
2529-
'state' => 'in progress',
2530-
'description' => 'almost there, I promise'
2531-
})
2512+
'type' => 'create',
2513+
'state' => 'in progress',
2514+
'description' => 'almost there, I promise'
2515+
})
25322516
end
25332517
end
25342518

@@ -3866,27 +3850,6 @@ def create_user_provided_json(instance, labels: {}, annotations: {})
38663850
}
38673851
end
38683852

3869-
def expect_metadata(instance, annotations: [], labels: [])
3870-
a = instance.annotations.map do |e|
3871-
{
3872-
prefix: e.key_prefix,
3873-
key: e.key_name,
3874-
value: e.value,
3875-
}
3876-
end
3877-
3878-
l = instance.labels.map do |e|
3879-
{
3880-
prefix: e.key_prefix,
3881-
key: e.key_name,
3882-
value: e.value,
3883-
}
3884-
end
3885-
3886-
expect(a).to match_array(annotations)
3887-
expect(l).to match_array(labels)
3888-
end
3889-
38903853
def share_service_instance(instance, target_space)
38913854
enable_sharing!
38923855

spec/request/space_manifests_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@
140140

141141
expect(app1_model.service_bindings.length).to eq 1
142142
expect(app1_model.service_bindings.first.service_instance).to eq service_instance
143-
expect(app1_model.labels.map { |label| { key: label.key_name, value: label.value } }).
144-
to match_array([{ key: 'potato', value: 'yam' }, { key: 'songs', value: 'missing' }])
145-
expect(app1_model.annotations.map { |a| { key: a.key, value: a.value } }).
146-
to match_array([{ key: 'potato', value: 'idaho' }, { key: 'juice', value: 'newton' }])
143+
expect(app1_model).to have_labels({ key: 'potato', value: 'yam' }, { prefix: 'myspace.com', key: 'songs', value: 'missing' })
144+
expect(app1_model).to have_annotations({ key: 'potato', value: 'idaho' }, { key: 'juice', value: 'newton' })
147145

148146
app2_model.reload
149147
lifecycle_data = app2_model.lifecycle_data
@@ -158,10 +156,12 @@
158156

159157
expect(app2_model.service_bindings.length).to eq 1
160158
expect(app2_model.service_bindings.first.service_instance).to eq service_instance
161-
expect(app2_model.labels.map { |label| { key: label.key_name, value: label.value } }).
162-
to match_array([{ key: 'potato', value: 'yam' },])
163-
expect(app2_model.annotations.map { |a| { key: a.key, value: a.value } }).
164-
to match_array([{ key: 'potato', value: 'idaho' }, { key: 'juice', value: 'newton' },])
159+
expect(app2_model).to have_labels(
160+
{ key: 'potato', value: 'yam' }
161+
)
162+
expect(app2_model).to have_annotations(
163+
{ key: 'potato', value: 'idaho' }, { key: 'juice', value: 'newton' }
164+
)
165165
end
166166

167167
context 'service bindings' do

spec/support/matchers/contain_metadata.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
RSpec::Matchers.define :have_annotations do |*expected|
2+
actual_annotations = []
3+
4+
match do |actual|
5+
actual_annotations = actual.annotations.map do |annotation|
6+
{
7+
prefix: annotation.key_prefix,
8+
key: annotation.key_name,
9+
value: annotation.value,
10+
}
11+
end
12+
13+
if expected.any?
14+
expected = expected.map do |annotation|
15+
{
16+
prefix: annotation.with_indifferent_access[:prefix],
17+
key: annotation.with_indifferent_access[:key],
18+
value: annotation.with_indifferent_access[:value],
19+
}
20+
end
21+
22+
expect(actual_annotations).to match_array(expected)
23+
else # Situation: expect(thing).to have_annotations
24+
expect(actual_annotations).not_to be_empty
25+
end
26+
end
27+
28+
failure_message do
29+
if expected.any?
30+
[
31+
"Annotations don't match!",
32+
"Expected #{expected}",
33+
" got #{actual_annotations}",
34+
].join("\n")
35+
else
36+
'Expected annotations but found none!'
37+
end
38+
end
39+
40+
failure_message_when_negated do
41+
if expected.any?
42+
"Annotations unexpectedly match: got #{actual_annotations}"
43+
else
44+
"Expected no annotations but got #{actual_annotations}"
45+
end
46+
end
47+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
RSpec::Matchers.define :have_labels do |*expected|
2+
actual_labels = []
3+
4+
match do |actual|
5+
actual_labels = actual.labels.map do |label|
6+
{
7+
prefix: label.key_prefix,
8+
key: label.key_name,
9+
value: label.value,
10+
}
11+
end
12+
13+
if expected.any?
14+
expected = expected.map do |label|
15+
{
16+
prefix: label.with_indifferent_access[:prefix],
17+
key: label.with_indifferent_access[:key],
18+
value: label.with_indifferent_access[:value],
19+
}
20+
end
21+
22+
expect(actual_labels).to match_array(expected)
23+
else # Situation: expect(thing).to have_labels
24+
expect(actual_labels).not_to be_empty
25+
end
26+
end
27+
28+
failure_message do
29+
if expected.any?
30+
[
31+
"Labels don't match!",
32+
"Expected #{expected}",
33+
" got #{actual_labels}",
34+
].join("\n")
35+
else
36+
'Expected labels but found none!'
37+
end
38+
end
39+
40+
failure_message_when_negated do
41+
if expected.any?
42+
"Labels unexpectedly match: got #{actual_labels}"
43+
else
44+
"Expected no labels but got #{actual_labels}"
45+
end
46+
end
47+
end

spec/unit/actions/app_create_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ module VCAP::CloudController
4949
expect(app.name).to eq('my-app')
5050
expect(app.space).to eq(space)
5151
expect(app.environment_variables).to eq(environment_variables.stringify_keys)
52-
expect(app.labels.map(&:value)).to contain_exactly('stable', 'mashed')
53-
expect(app.annotations.map(&:value)).to contain_exactly('Bummer-boy', 'Bums you out')
52+
53+
expect(app).to have_labels(
54+
{ prefix: 'seriouseats.com', key: 'potato', value: 'mashed' },
55+
{ prefix: nil, key: 'release', value: 'stable' }
56+
)
57+
expect(app).to have_annotations(
58+
{ key: 'superhero', value: 'Bummer-boy' },
59+
{ key: 'superpower', value: 'Bums you out' }
60+
)
5461
end
5562

5663
describe 'created process' do

spec/unit/actions/app_update_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ module VCAP::CloudController
193193

194194
it 'updates the annotations' do
195195
app_update.update(app_model, message, lifecycle)
196-
expect(app_model.annotations.map(&:value)).to contain_exactly(
197-
'Bill tel(1111111) email(bill@fixme), Bob tel(222222) pager(3333333#555) email(bob@fixme)',
198-
'new-value'
196+
expect(app_model).to have_annotations(
197+
{ key: 'contacts', value: 'Bill tel(1111111) email(bill@fixme), Bob tel(222222) pager(3333333#555) email(bob@fixme)' },
198+
{ key: 'existing_anno', value: 'new-value' },
199199
)
200200
expect(AppAnnotationModel.find(resource_guid: app_model.guid, key: 'please')).to be_nil
201201
end

0 commit comments

Comments
 (0)