Skip to content

Commit

Permalink
database: Handle FindAncestryAndRollback datastore.Begin() error
Browse files Browse the repository at this point in the history
Do not call rollback() if transaction is not created.

Fixes #828
  • Loading branch information
peacocb committed Aug 1, 2019
1 parent 517e3e5 commit 1ddc053
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 1ddc053

Please sign in to comment.