Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/OpenStack/MetaAPI/API/Images.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ sub image_from_name {
return unless ref $images;

if (scalar @$images > 1) {
warn
"image_from_name: more than one image sharing the same name '$name'";
return $images;
my @ids = map { $_->{id} // 'unknown' } @$images;
die "image_from_name: multiple images found for name '$name'"
. " (ids: " . join(', ', @ids) . ")."
. " Use image_from_uid to select a specific image";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a behavior change, but it makes sense to have a consistent and unique reply regarding the function name

}

return $images->[0];
Expand Down
85 changes: 85 additions & 0 deletions t/service-image.t
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,58 @@ ok $api, "got one api object" or die;
"image_from_uid rejects truncated UUID";
}

{
note "Testing images() is not exposed at MetaAPI level";

ok !$api->can('images'),
"images() is not available via MetaAPI (use image_from_uid or image_from_name)";
}

{
note "Testing image_from_uid requires uid parameter";

like dies { $api->image_from_uid(undef) },
qr/image_from_uid: uid is required/,
"image_from_uid dies when uid is undef";
}

{
note "Testing image_from_name requires name parameter";

like dies { $api->image_from_name(undef) },
qr/image_from_name: name is required/,
"image_from_name dies when name is undef";
}

{
note "Testing image_from_name with duplicate image names";

mock_get_request(
'http://127.0.0.1:9292/v2/images?name=in:%22duplicate-image%22',
application_json(json_for_duplicate_images()),
);

like dies { $api->image_from_name('duplicate-image') },
qr/multiple images found for name 'duplicate-image'/,
"image_from_name dies on duplicate names";

like dies { $api->image_from_name('duplicate-image') },
qr/Use image_from_uid to select a specific image/,
"error message suggests using image_from_uid";
}

{
note "Testing image_from_name with no results";

mock_get_request(
'http://127.0.0.1:9292/v2/images?name=in:%22nonexistent%22',
application_json(json_for_no_images()),
);

is $api->image_from_name('nonexistent'), undef,
"image_from_name returns undef when no images found";
}

done_testing;

sub json_for_image {
Expand Down Expand Up @@ -194,4 +246,37 @@ sub json_for_image_name {
JSON
}

sub json_for_duplicate_images {
return <<'JSON';
{
"first" : "/v2/images?name=in%3A%22duplicate-image%22",
"schema" : "/v2/schemas/images",
"images" : [
{
"id" : "aaaa1111-2222-3333-4444-555566667777",
"name" : "duplicate-image",
"status" : "active",
"visibility" : "shared"
},
{
"id" : "bbbb1111-2222-3333-4444-555566667777",
"name" : "duplicate-image",
"status" : "active",
"visibility" : "shared"
}
]
}
JSON
}

sub json_for_no_images {
return <<'JSON';
{
"first" : "/v2/images?name=in%3A%22nonexistent%22",
"schema" : "/v2/schemas/images",
"images" : []
}
JSON
}

__END__