fix(sql storage): Query() should return ErrReleaseNotFound immediately when no records are found

Signed-off-by: Mike Ng <ming@redhat.com>
pull/8470/head
Mike Ng 5 years ago
parent ae8fcf445d
commit b86105aebc

@ -310,6 +310,10 @@ func (s *SQL) Query(labels map[string]string) ([]*rspb.Release, error) {
return nil, err
}
if len(records) == 0 {
return nil, ErrReleaseNotFound
}
var releases []*rspb.Release
for _, record := range records {
release, err := decodeRelease(record.Body)
@ -320,10 +324,6 @@ func (s *SQL) Query(labels map[string]string) ([]*rspb.Release, error) {
releases = append(releases, release)
}
if len(releases) == 0 {
return nil, ErrReleaseNotFound
}
return releases, nil
}

@ -292,6 +292,11 @@ func TestSqlUpdate(t *testing.T) {
func TestSqlQuery(t *testing.T) {
// Reflect actual use cases in ../storage.go
labelSetUnknown := map[string]string{
"name": "smug-pigeon",
"owner": sqlReleaseDefaultOwner,
"status": "unknown",
}
labelSetDeployed := map[string]string{
"name": "smug-pigeon",
"owner": sqlReleaseDefaultOwner,
@ -320,6 +325,15 @@ func TestSqlQuery(t *testing.T) {
sqlReleaseTableNamespaceColumn,
)
mock.
ExpectQuery(regexp.QuoteMeta(query)).
WithArgs("smug-pigeon", sqlReleaseDefaultOwner, "unknown", "default").
WillReturnRows(
mock.NewRows([]string{
sqlReleaseTableBodyColumn,
}),
).RowsWillBeClosed()
mock.
ExpectQuery(regexp.QuoteMeta(query)).
WithArgs("smug-pigeon", sqlReleaseDefaultOwner, "deployed", "default").
@ -353,6 +367,13 @@ func TestSqlQuery(t *testing.T) {
),
).RowsWillBeClosed()
_, err := sqlDriver.Query(labelSetUnknown)
if err == nil {
t.Errorf("Expected error {%v}, got nil", ErrReleaseNotFound)
} else if err != ErrReleaseNotFound {
t.Fatalf("failed to query for unknown smug-pigeon release: %v", err)
}
results, err := sqlDriver.Query(labelSetDeployed)
if err != nil {
t.Fatalf("failed to query for deployed smug-pigeon release: %v", err)

Loading…
Cancel
Save