Skip to content

Commit 9aa9920

Browse files
Merge pull request #225 from carolynvs/init-from-backingstore
Initialize storage from backing stores
2 parents a6382d9 + a83b6fd commit 9aa9920

12 files changed

+65
-32
lines changed

action/action_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cnabio/cnab-go/claim"
1414
"github.com/cnabio/cnab-go/driver"
1515
"github.com/cnabio/cnab-go/driver/debug"
16-
"github.com/cnabio/cnab-go/utils/crud"
1716
"github.com/cnabio/cnab-go/valuesource"
1817

1918
"github.com/hashicorp/go-multierror"
@@ -993,7 +992,7 @@ func TestGetOutputsGeneratedByAction(t *testing.T) {
993992

994993
func TestSaveAction(t *testing.T) {
995994
t.Run("save output", func(t *testing.T) {
996-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
995+
cp := claim.NewMockStore(nil, nil)
997996
c := newClaim(claim.ActionInstall)
998997
r, err := c.NewResult(claim.StatusSucceeded)
999998
require.NoError(t, err, "NewResult failed")
@@ -1023,7 +1022,7 @@ func TestSaveAction(t *testing.T) {
10231022
})
10241023

10251024
t.Run("do not save output", func(t *testing.T) {
1026-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
1025+
cp := claim.NewMockStore(nil, nil)
10271026
c := newClaim(claim.ActionInstall)
10281027
r, err := c.NewResult(claim.StatusSucceeded)
10291028
require.NoError(t, err, "NewResult failed")

action/example_install_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/cnabio/cnab-go/claim"
99
"github.com/cnabio/cnab-go/driver"
1010
"github.com/cnabio/cnab-go/driver/lookup"
11-
"github.com/cnabio/cnab-go/utils/crud"
1211
"github.com/cnabio/cnab-go/valuesource"
1312
)
1413

@@ -22,7 +21,7 @@ func Example_install() {
2221
}
2322

2423
// Use an in-memory store with no encryption
25-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
24+
cp := claim.NewMockStore(nil, nil)
2625

2726
// Create the action that will execute the operation
2827
a := action.New(d, cp)

action/example_invoke_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/cnabio/cnab-go/action"
77
"github.com/cnabio/cnab-go/claim"
88
"github.com/cnabio/cnab-go/driver/lookup"
9-
"github.com/cnabio/cnab-go/utils/crud"
109
"github.com/cnabio/cnab-go/valuesource"
1110
)
1211

@@ -20,7 +19,7 @@ func Example_invoke() {
2019
}
2120

2221
// Use an in-memory store with no encryption
23-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
22+
cp := claim.NewMockStore(nil, nil)
2423

2524
// Save an existing claim for the install action which has already taken place
2625
// This sets up data for us to use during our upgrade example

action/example_running_status_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/cnabio/cnab-go/action"
1010
"github.com/cnabio/cnab-go/claim"
1111
"github.com/cnabio/cnab-go/driver/lookup"
12-
"github.com/cnabio/cnab-go/utils/crud"
1312
"github.com/cnabio/cnab-go/valuesource"
1413
)
1514

@@ -41,7 +40,7 @@ func Example_runningStatus() {
4140
}
4241

4342
// Use an in-memory store with no encryption
44-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
43+
cp := claim.NewMockStore(nil, nil)
4544

4645
// Save an existing claim for the install action which has already taken place
4746
// This sets up data for us to use during our upgrade example

action/example_upgrade_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/cnabio/cnab-go/claim"
99
"github.com/cnabio/cnab-go/driver"
1010
"github.com/cnabio/cnab-go/driver/lookup"
11-
"github.com/cnabio/cnab-go/utils/crud"
1211
"github.com/cnabio/cnab-go/valuesource"
1312
)
1413

@@ -22,7 +21,7 @@ func Example_upgrade() {
2221
}
2322

2423
// Use an in-memory store with no encryption
25-
cp := claim.NewClaimStore(crud.NewMockStore(), nil, nil)
24+
cp := claim.NewMockStore(nil, nil)
2625

2726
// Save an existing claim for the install action which has already taken place
2827
// This sets up data for us to use during our upgrade example

claim/claimstore.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Store struct {
4545

4646
// NewClaimStore creates a persistent store for claims using the specified
4747
// backing key-blob store.
48-
func NewClaimStore(store crud.Store, encrypt EncryptionHandler, decrypt EncryptionHandler) Store {
48+
func NewClaimStore(store *crud.BackingStore, encrypt EncryptionHandler, decrypt EncryptionHandler) Store {
4949
if encrypt == nil {
5050
encrypt = noOpEncryptionHandler
5151
}
@@ -55,7 +55,7 @@ func NewClaimStore(store crud.Store, encrypt EncryptionHandler, decrypt Encrypti
5555
}
5656

5757
return Store{
58-
backingStore: crud.NewBackingStore(store),
58+
backingStore: store,
5959
encrypt: encrypt,
6060
decrypt: decrypt,
6161
}
@@ -80,6 +80,11 @@ var noOpEncryptionHandler = func(data []byte) ([]byte, error) {
8080
return data, nil
8181
}
8282

83+
// GetBackingStore returns the data store behind this claim store.
84+
func (s Store) GetBackingStore() *crud.BackingStore {
85+
return s.backingStore
86+
}
87+
8388
func (s Store) ListInstallations() ([]string, error) {
8489
names, err := s.backingStore.List(ItemTypeInstallations, "")
8590
sort.Strings(names)

claim/claimstore_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var b64decode = func(src []byte) ([]byte, error) {
6868
// RESULT_ID_2_OUTPUT_2
6969
func generateClaimData(t *testing.T) (Provider, crud.MockStore) {
7070
backingStore := crud.NewMockStore()
71-
cp := NewClaimStore(backingStore, nil, nil)
71+
cp := NewClaimStore(crud.NewBackingStore(backingStore), nil, nil)
7272

7373
bun := bundle.Bundle{
7474
Definitions: map[string]*definition.Schema{
@@ -178,7 +178,7 @@ func TestCanSaveReadAndDelete(t *testing.T) {
178178

179179
storeDir := filepath.Join(tempDir, "claimstore")
180180
datastore := crud.NewFileSystemStore(storeDir, NewClaimStoreFileExtensions())
181-
store := NewClaimStore(datastore, nil, nil)
181+
store := NewClaimStore(crud.NewBackingStore(datastore), nil, nil)
182182

183183
err = store.SaveClaim(c1)
184184
must.NoError(err, "SaveClaim failed")
@@ -219,7 +219,8 @@ func TestCanUpdate(t *testing.T) {
219219
defer os.RemoveAll(tempDir)
220220

221221
storeDir := filepath.Join(tempDir, "claimstore")
222-
store := NewClaimStore(crud.NewFileSystemStore(storeDir, NewClaimStoreFileExtensions()), nil, nil)
222+
datastore := crud.NewFileSystemStore(storeDir, NewClaimStoreFileExtensions())
223+
store := NewClaimStore(crud.NewBackingStore(datastore), nil, nil)
223224

224225
err = store.SaveClaim(c1)
225226
require.NoError(t, err)
@@ -616,7 +617,8 @@ func TestCanUpdateOutputs(t *testing.T) {
616617
defer os.RemoveAll(tempDir)
617618

618619
storeDir := filepath.Join(tempDir, "claimstore")
619-
store := NewClaimStore(crud.NewFileSystemStore(storeDir, NewClaimStoreFileExtensions()), nil, nil)
620+
fsStore := crud.NewFileSystemStore(storeDir, NewClaimStoreFileExtensions())
621+
store := NewClaimStore(crud.NewBackingStore(fsStore), nil, nil)
620622

621623
err = store.SaveClaim(claim)
622624
must.NoError(err, "Failed to store claim")
@@ -657,8 +659,8 @@ func TestCanUpdateOutputs(t *testing.T) {
657659
}
658660

659661
func TestStore_EncryptClaims(t *testing.T) {
660-
backingStore := crud.NewMockStore()
661-
s := NewClaimStore(backingStore, b64encode, b64decode)
662+
s := NewMockStore(b64encode, b64decode)
663+
backingStore := s.GetBackingStore()
662664

663665
err := s.SaveClaim(exampleClaim)
664666
require.NoError(t, err, "SaveClaim failed")
@@ -683,8 +685,8 @@ func TestStore_EncryptOutputs(t *testing.T) {
683685
writeOnly := func(value bool) *bool {
684686
return &value
685687
}
686-
backingStore := crud.NewMockStore()
687-
s := NewClaimStore(backingStore, b64encode, b64decode)
688+
s := NewMockStore(b64encode, b64decode)
689+
backingStore := s.GetBackingStore()
688690

689691
b := bundle.Bundle{
690692
Definitions: map[string]*definition.Schema{

claim/mock_store.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package claim
2+
3+
import (
4+
"github.com/cnabio/cnab-go/utils/crud"
5+
)
6+
7+
// NewMockStore creates a mock claim store for unit testing.
8+
func NewMockStore(encrypt EncryptionHandler, decrypt EncryptionHandler) Store {
9+
return NewClaimStore(crud.NewBackingStore(crud.NewMockStore()), encrypt, decrypt)
10+
}

credentials/credstore.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ type Store struct {
2222

2323
// NewCredentialStore creates a persistent store for credential sets using the specified
2424
// backing key-blob store.
25-
func NewCredentialStore(store crud.Store) Store {
25+
func NewCredentialStore(store *crud.BackingStore) Store {
2626
return Store{
27-
backingStore: crud.NewBackingStore(store),
27+
backingStore: store,
2828
}
2929
}
3030

31+
// GetBackingStore returns the data store behind this credentials store.
32+
func (s Store) GetBackingStore() *crud.BackingStore {
33+
return s.backingStore
34+
}
35+
3136
// List lists the names of the stored credential sets.
3237
func (s Store) List() ([]string, error) {
3338
return s.backingStore.List(ItemType, "")

credentials/credstore_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
)
1111

1212
func TestCredentialStore_HandlesNotFoundError(t *testing.T) {
13-
mockStore := crud.NewMockStore()
13+
cs := NewMockStore()
14+
mockStore := cs.GetBackingStore().GetDataStore().(crud.MockStore)
1415
mockStore.ReadMock = func(itemType string, name string) (bytes []byte, err error) {
1516
// Change the default error message to test that we are checking
1617
// inside the error message and not matching it exactly
1718
return nil, errors.New("wrapping error message: " + crud.ErrRecordDoesNotExist.Error())
1819
}
19-
cs := NewCredentialStore(mockStore)
2020

2121
_, err := cs.Read("missing cred set")
2222
assert.EqualError(t, err, ErrNotFound.Error())

credentials/mock_store.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package credentials
2+
3+
import (
4+
"github.com/cnabio/cnab-go/utils/crud"
5+
)
6+
7+
// NewMockStore creates a mock credentials store for unit testing.
8+
func NewMockStore() Store {
9+
return NewCredentialStore(crud.NewBackingStore(crud.NewMockStore()))
10+
}

utils/crud/backingstore.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ type BackingStore struct {
2323
close func() error
2424

2525
// backingStore being wrapped.
26-
backingStore Store
26+
datastore Store
2727
}
2828

2929
func NewBackingStore(store Store) *BackingStore {
3030
backingStore := BackingStore{
31-
AutoClose: true,
32-
backingStore: store,
31+
AutoClose: true,
32+
datastore: store,
3333
}
3434

3535
if connectable, ok := store.(HasConnect); ok {
@@ -43,6 +43,12 @@ func NewBackingStore(store Store) *BackingStore {
4343
return &backingStore
4444
}
4545

46+
// GetStore returns the data store, e.g. filesystem, mongodb, managed by this
47+
// wrapper.
48+
func (s *BackingStore) GetDataStore() Store {
49+
return s.datastore
50+
}
51+
4652
func (s *BackingStore) Connect() error {
4753
if s.opened {
4854
return nil
@@ -76,7 +82,7 @@ func (s *BackingStore) List(itemType string, group string) ([]string, error) {
7682
return nil, err
7783
}
7884

79-
return s.backingStore.List(itemType, group)
85+
return s.datastore.List(itemType, group)
8086
}
8187

8288
func (s *BackingStore) Save(itemType string, group string, name string, data []byte) error {
@@ -86,7 +92,7 @@ func (s *BackingStore) Save(itemType string, group string, name string, data []b
8692
return err
8793
}
8894

89-
return s.backingStore.Save(itemType, group, name, data)
95+
return s.datastore.Save(itemType, group, name, data)
9096
}
9197

9298
func (s *BackingStore) Read(itemType string, name string) ([]byte, error) {
@@ -96,7 +102,7 @@ func (s *BackingStore) Read(itemType string, name string) ([]byte, error) {
96102
return nil, err
97103
}
98104

99-
return s.backingStore.Read(itemType, name)
105+
return s.datastore.Read(itemType, name)
100106
}
101107

102108
// ReadAll retrieves all the items with the specified prefix
@@ -131,7 +137,7 @@ func (s *BackingStore) Delete(itemType string, name string) error {
131137
return err
132138
}
133139

134-
return s.backingStore.Delete(itemType, name)
140+
return s.datastore.Delete(itemType, name)
135141
}
136142

137143
func (s *BackingStore) shouldAutoConnect() bool {

0 commit comments

Comments
 (0)