From 492419ec7b8047991cbc5b4fb520b2917c651d44 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Mon, 28 Feb 2022 17:15:24 +0100 Subject: [PATCH] rework initialization logic Signed-off-by: jkoberg --- settings/pkg/store/metadata/assignments.go | 6 +-- .../pkg/store/metadata/assignments_test.go | 9 ++-- settings/pkg/store/metadata/bundles.go | 12 ++--- settings/pkg/store/metadata/bundles_test.go | 48 +++++++++---------- settings/pkg/store/metadata/permissions.go | 6 +-- settings/pkg/store/metadata/store.go | 20 ++++---- settings/pkg/store/metadata/store_test.go | 8 ++-- settings/pkg/store/metadata/values.go | 8 ++-- settings/pkg/store/metadata/values_test.go | 19 ++++---- 9 files changed, 66 insertions(+), 70 deletions(-) diff --git a/settings/pkg/store/metadata/assignments.go b/settings/pkg/store/metadata/assignments.go index 6c311b8b213..07862ca0d4d 100644 --- a/settings/pkg/store/metadata/assignments.go +++ b/settings/pkg/store/metadata/assignments.go @@ -10,7 +10,7 @@ import ( ) // ListRoleAssignments loads and returns all role assignments matching the given assignment identifier. -func (s Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleAssignment, error) { +func (s *Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleAssignment, error) { s.Init() assIDs, err := s.mdc.ReadDir(nil, accountPath(accountUUID)) if err != nil { @@ -36,7 +36,7 @@ func (s Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleA } // WriteRoleAssignment appends the given role assignment to the existing assignments of the respective account. -func (s Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.UserRoleAssignment, error) { +func (s *Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.UserRoleAssignment, error) { s.Init() // as per https://github.com/owncloud/product/issues/103 "Each user can have exactly one role" err := s.mdc.Delete(nil, accountPath(accountUUID)) @@ -62,7 +62,7 @@ func (s Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.Use } // RemoveRoleAssignment deletes the given role assignment from the existing assignments of the respective account. -func (s Store) RemoveRoleAssignment(assignmentID string) error { +func (s *Store) RemoveRoleAssignment(assignmentID string) error { s.Init() accounts, err := s.mdc.ReadDir(nil, accountsFolderLocation) if err != nil { diff --git a/settings/pkg/store/metadata/assignments_test.go b/settings/pkg/store/metadata/assignments_test.go index ec78aed68b4..a2b56e600a0 100644 --- a/settings/pkg/store/metadata/assignments_test.go +++ b/settings/pkg/store/metadata/assignments_test.go @@ -14,19 +14,16 @@ var ( einstein = "a4d07560-a670-4be9-8d60-9b547751a208" //marie = "3c054db3-eec1-4ca4-b985-bc56dcf560cb" - s = Store{ - Logger: logger, - mdc: mdc, - l: &sync.Mutex{}, + s = &Store{ + l: &sync.Mutex{}, } - logger = olog.NewLogger( olog.Color(true), olog.Pretty(true), olog.Level("info"), ) - mdc = NewMDC() + mdc = NewMDC(s) bundles = []*settingsmsg.Bundle{ { diff --git a/settings/pkg/store/metadata/bundles.go b/settings/pkg/store/metadata/bundles.go index 309685afda9..a94b69ae18b 100644 --- a/settings/pkg/store/metadata/bundles.go +++ b/settings/pkg/store/metadata/bundles.go @@ -11,7 +11,7 @@ import ( ) // ListBundles returns all bundles in the dataPath folder that match the given type. -func (s Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []string) ([]*settingsmsg.Bundle, error) { +func (s *Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []string) ([]*settingsmsg.Bundle, error) { // TODO: this is needed for initialization - we need to find a better way to fix this if s.mdc == nil && len(bundleIDs) == 1 && bundleIDs[0] == "71881883-1768-46bd-a24d-a356a2afdf7f" { return []*settingsmsg.Bundle{{ @@ -59,7 +59,7 @@ func (s Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []strin } // ReadBundle tries to find a bundle by the given id within the dataPath. -func (s Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) { +func (s *Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) { s.Init() b, err := s.mdc.SimpleDownload(nil, bundlePath(bundleID)) if err != nil { @@ -71,12 +71,12 @@ func (s Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) { } // ReadSetting tries to find a setting by the given id within the dataPath. -func (s Store) ReadSetting(settingID string) (*settingsmsg.Setting, error) { +func (s *Store) ReadSetting(settingID string) (*settingsmsg.Setting, error) { return nil, errors.New("not implemented") } // WriteBundle sends the givens record to the metadataclient. returns `record` for legacy reasons -func (s Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, error) { +func (s *Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, error) { s.Init() b, err := json.Marshal(record) if err != nil { @@ -86,7 +86,7 @@ func (s Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, err } // AddSettingToBundle adds the given setting to the bundle with the given bundleID. -func (s Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) (*settingsmsg.Setting, error) { +func (s *Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) (*settingsmsg.Setting, error) { s.Init() b, err := s.ReadBundle(bundleID) if err != nil { @@ -106,7 +106,7 @@ func (s Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) } // RemoveSettingFromBundle removes the setting from the bundle with the given ids. -func (s Store) RemoveSettingFromBundle(bundleID string, settingID string) error { +func (s *Store) RemoveSettingFromBundle(bundleID string, settingID string) error { return errors.New("not implemented") } diff --git a/settings/pkg/store/metadata/bundles_test.go b/settings/pkg/store/metadata/bundles_test.go index 7e8e47817a5..c557dfcc6a1 100644 --- a/settings/pkg/store/metadata/bundles_test.go +++ b/settings/pkg/store/metadata/bundles_test.go @@ -1,10 +1,9 @@ package store import ( - "sync" "testing" - olog "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/gofrs/uuid" settingsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/settings/v0" "github.com/stretchr/testify/require" ) @@ -103,7 +102,7 @@ var bundleScenarios = []struct { } var ( - appendTestBundleID = "append-test-bundle" + appendTestBundleID = uuid.Must(uuid.NewV4()).String() appendTestSetting1 = &settingsmsg.Setting{ Id: "append-test-setting-1", @@ -139,17 +138,16 @@ var ( ) func TestBundles(t *testing.T) { - mdc := NewMDC() - s := Store{ - Logger: olog.NewLogger( - olog.Color(true), - olog.Pretty(true), - olog.Level("info"), - ), - - l: &sync.Mutex{}, - mdc: mdc, - } + //s := &Store{ + //Logger: olog.NewLogger( + //olog.Color(true), + //olog.Pretty(true), + //olog.Level("info"), + //), + + //l: &sync.Mutex{}, + //} + //NewMDC(s) // write bundles for i := range bundleScenarios { @@ -187,17 +185,17 @@ func TestBundles(t *testing.T) { } func TestAppendSetting(t *testing.T) { - mdc := NewMDC() - s := Store{ - Logger: olog.NewLogger( - olog.Color(true), - olog.Pretty(true), - olog.Level("info"), - ), - - l: &sync.Mutex{}, - mdc: mdc, - } + //mdc := NewMDC() + //s := Store{ + //Logger: olog.NewLogger( + //olog.Color(true), + //olog.Pretty(true), + //olog.Level("info"), + //), + + //l: &sync.Mutex{}, + //mdc: mdc, + //} // appending to non existing bundle creates new _, err := s.AddSettingToBundle(appendTestBundleID, appendTestSetting1) diff --git a/settings/pkg/store/metadata/permissions.go b/settings/pkg/store/metadata/permissions.go index 7819aea38b2..0a6133deb69 100644 --- a/settings/pkg/store/metadata/permissions.go +++ b/settings/pkg/store/metadata/permissions.go @@ -7,16 +7,16 @@ import ( ) // ListPermissionsByResource collects all permissions from the provided roleIDs that match the requested resource -func (s Store) ListPermissionsByResource(resource *settingsmsg.Resource, roleIDs []string) ([]*settingsmsg.Permission, error) { +func (s *Store) ListPermissionsByResource(resource *settingsmsg.Resource, roleIDs []string) ([]*settingsmsg.Permission, error) { return nil, errors.New("not implemented") } // ReadPermissionByID finds the permission in the roles, specified by the provided roleIDs -func (s Store) ReadPermissionByID(permissionID string, roleIDs []string) (*settingsmsg.Permission, error) { +func (s *Store) ReadPermissionByID(permissionID string, roleIDs []string) (*settingsmsg.Permission, error) { return nil, errors.New("not implemented") } // ReadPermissionByName finds the permission in the roles, specified by the provided roleIDs -func (s Store) ReadPermissionByName(name string, roleIDs []string) (*settingsmsg.Permission, error) { +func (s *Store) ReadPermissionByName(name string, roleIDs []string) (*settingsmsg.Permission, error) { return nil, errors.New("not implemented") } diff --git a/settings/pkg/store/metadata/store.go b/settings/pkg/store/metadata/store.go index 28d3d66110d..84e09b74ef5 100644 --- a/settings/pkg/store/metadata/store.go +++ b/settings/pkg/store/metadata/store.go @@ -46,7 +46,7 @@ type Store struct { } // Init initialize the store once, later calls are noops -func (s Store) Init() { +func (s *Store) Init() { if s.mdc != nil { return } @@ -58,7 +58,7 @@ func (s Store) Init() { //b := backoff.NewExponentialBackOff() //b.MaxElapsedTime = 4 * time.Second //backoff.Retry(func() error { - err = s.initMetadataClient() + err = s.initMetadataClient(NewMetadataClient(s.cfg)) //return err //}, b) @@ -72,12 +72,12 @@ func (s Store) Init() { // New creates a new store func New(cfg *config.Config, initstore func(settings.Manager)) settings.Manager { s := Store{ - Logger: olog.NewLogger( - olog.Color(cfg.Log.Color), - olog.Pretty(cfg.Log.Pretty), - olog.Level(cfg.Log.Level), - olog.File(cfg.Log.File), - ), + //Logger: olog.NewLogger( + //olog.Color(cfg.Log.Color), + //olog.Pretty(cfg.Log.Pretty), + //olog.Level(cfg.Log.Level), + //olog.File(cfg.Log.File), + //), initStore: initstore, l: &sync.Mutex{}, init: &sync.Once{}, @@ -97,8 +97,8 @@ func NewMetadataClient(cfg *config.Config) MetadataClient { } // we need to lazy initialize the MetadataClient because metadata service might not be ready -func (s Store) initMetadataClient() error { - s.mdc = NewMetadataClient(s.cfg) +func (s *Store) initMetadataClient(mdc MetadataClient) error { + s.mdc = mdc // TODO: this fails because of authentication issues err := s.mdc.Init(nil, settingsSpaceID) diff --git a/settings/pkg/store/metadata/store_test.go b/settings/pkg/store/metadata/store_test.go index c42f3d02772..c963901688e 100644 --- a/settings/pkg/store/metadata/store_test.go +++ b/settings/pkg/store/metadata/store_test.go @@ -42,13 +42,15 @@ type MockedMetadataClient struct { } // NewMDC instantiates a mocked MetadataClient -func NewMDC() MetadataClient { +func NewMDC(s *Store) MetadataClient { + var mdc MetadataClient switch testtype { case "unit": - return &MockedMetadataClient{data: make(map[string][]byte)} + mdc = &MockedMetadataClient{data: make(map[string][]byte)} case "integration": - return NewMetadataClient(&config.Config{}) + mdc = NewMetadataClient(&config.Config{}) } + s.initMetadataClient(mdc) return nil } diff --git a/settings/pkg/store/metadata/values.go b/settings/pkg/store/metadata/values.go index 4f7c23c5393..df238836897 100644 --- a/settings/pkg/store/metadata/values.go +++ b/settings/pkg/store/metadata/values.go @@ -13,12 +13,12 @@ import ( // If the bundleId is empty, it's ignored for filtering. // If the accountUUID is empty, only values with empty accountUUID are returned. // If the accountUUID is not empty, values with an empty or with a matching accountUUID are returned. -func (s Store) ListValues(bundleID, accountUUID string) ([]*settingsmsg.Value, error) { +func (s *Store) ListValues(bundleID, accountUUID string) ([]*settingsmsg.Value, error) { return nil, errors.New("not implemented") } // ReadValue tries to find a value by the given valueId within the dataPath -func (s Store) ReadValue(valueID string) (*settingsmsg.Value, error) { +func (s *Store) ReadValue(valueID string) (*settingsmsg.Value, error) { s.Init() b, err := s.mdc.SimpleDownload(nil, valuePath(valueID)) if err != nil { @@ -29,12 +29,12 @@ func (s Store) ReadValue(valueID string) (*settingsmsg.Value, error) { } // ReadValueByUniqueIdentifiers tries to find a value given a set of unique identifiers -func (s Store) ReadValueByUniqueIdentifiers(accountUUID, settingID string) (*settingsmsg.Value, error) { +func (s *Store) ReadValueByUniqueIdentifiers(accountUUID, settingID string) (*settingsmsg.Value, error) { return nil, errors.New("not implemented") } // WriteValue writes the given value into a file within the dataPath -func (s Store) WriteValue(value *settingsmsg.Value) (*settingsmsg.Value, error) { +func (s *Store) WriteValue(value *settingsmsg.Value) (*settingsmsg.Value, error) { s.Init() b, err := json.Marshal(value) if err != nil { diff --git a/settings/pkg/store/metadata/values_test.go b/settings/pkg/store/metadata/values_test.go index 2921e3d0e56..462460fbcdf 100644 --- a/settings/pkg/store/metadata/values_test.go +++ b/settings/pkg/store/metadata/values_test.go @@ -3,7 +3,6 @@ package store import ( "testing" - olog "github.com/owncloud/ocis/ocis-pkg/log" settingsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/settings/v0" "github.com/stretchr/testify/require" ) @@ -46,15 +45,15 @@ var valueScenarios = []struct { } func TestValues(t *testing.T) { - mdc := NewMDC() - s := Store{ - Logger: olog.NewLogger( - olog.Color(true), - olog.Pretty(true), - olog.Level("info"), - ), - mdc: mdc, - } + //mdc := NewMDC() + //s := Store{ + //Logger: olog.NewLogger( + //olog.Color(true), + //olog.Pretty(true), + //olog.Level("info"), + //), + //mdc: mdc, + //} for i := range valueScenarios { index := i t.Run(valueScenarios[index].name, func(t *testing.T) {