Skip to content

Commit

Permalink
Merge pull request #829 from peacocb/peacocb-828-dos-on-ancestry-post
Browse files Browse the repository at this point in the history
database: Handle FindAncestryAndRollback datastore.Begin() error
  • Loading branch information
jzelinskie authored Aug 27, 2019
2 parents 35191d6 + 1ddc053 commit 41c8d9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
3 changes: 1 addition & 2 deletions database/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ func PersistNamespacesAndCommit(datastore Datastore, namespaces []Namespace) err
// rollback.
func FindAncestryAndRollback(datastore Datastore, name string) (Ancestry, bool, error) {
tx, err := datastore.Begin()
defer tx.Rollback()

if err != nil {
return Ancestry{}, false, err
}
defer tx.Rollback()

return tx.FindAncestry(name)
}
Expand Down
33 changes: 33 additions & 0 deletions database/dbutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package database

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTxCreationFailure(t *testing.T) {
mdb := &MockDatastore{}

mdb.FctBegin = func() (Session, error) {
return nil, errors.New("Deliberately failed Begin")
}

_, _, err := FindAncestryAndRollback(mdb, "mockAncestry")
assert.Error(t, err)
}

0 comments on commit 41c8d9c

Please sign in to comment.