From 97da31627fa64dd452e9bbd284f26ab423c8e492 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 15 Oct 2020 11:46:25 +0200 Subject: [PATCH] refactor test suite --- .../indexer/index/cs3/autoincrement_test.go | 13 ++- .../pkg/indexer/index/cs3/non_unique_test.go | 15 ++- accounts/pkg/indexer/index/cs3/unique_test.go | 10 +- .../pkg/indexer/index/disk/non_unique_test.go | 9 +- .../pkg/indexer/index/disk/unique_test.go | 9 +- accounts/pkg/indexer/indexer_test.go | 86 +++++++++++---- accounts/pkg/indexer/test/data.go | 102 +++++++----------- accounts/pkg/indexer/test/helpers.go | 10 +- accounts/pkg/service/v0/groups.go | 2 +- 9 files changed, 143 insertions(+), 113 deletions(-) diff --git a/accounts/pkg/indexer/index/cs3/autoincrement_test.go b/accounts/pkg/indexer/index/cs3/autoincrement_test.go index fa1d955d50..fa7a2a65c2 100644 --- a/accounts/pkg/indexer/index/cs3/autoincrement_test.go +++ b/accounts/pkg/indexer/index/cs3/autoincrement_test.go @@ -10,8 +10,11 @@ import ( "github.com/stretchr/testify/assert" ) +const cs3RootFolder = "/var/tmp/ocis/storage/users/data" + func TestAutoincrementIndexAdd(t *testing.T) { - dataDir := WriteIndexTestDataCS3(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) cfg := generateConfig() sut := NewAutoincrementIndex( @@ -23,8 +26,7 @@ func TestAutoincrementIndexAdd(t *testing.T) { option.WithProviderAddr(cfg.Repo.CS3.ProviderAddr), ) - err := sut.Init() - assert.NoError(t, err) + assert.NoError(t, sut.Init()) for i := 0; i < 5; i++ { res, err := sut.Add("abcdefg-123", "") @@ -36,7 +38,8 @@ func TestAutoincrementIndexAdd(t *testing.T) { } func BenchmarkAutoincrementIndexAdd(b *testing.B) { - dataDir := WriteIndexBenchmarkDataCS3(b, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(b, err) cfg := generateConfig() sut := NewAutoincrementIndex( @@ -48,7 +51,7 @@ func BenchmarkAutoincrementIndexAdd(b *testing.B) { option.WithProviderAddr(cfg.Repo.CS3.ProviderAddr), ) - err := sut.Init() + err = sut.Init() assert.NoError(b, err) for n := 0; n < b.N; n++ { diff --git a/accounts/pkg/indexer/index/cs3/non_unique_test.go b/accounts/pkg/indexer/index/cs3/non_unique_test.go index 7f41b8b2cd..da7656d2dd 100644 --- a/accounts/pkg/indexer/index/cs3/non_unique_test.go +++ b/accounts/pkg/indexer/index/cs3/non_unique_test.go @@ -1,20 +1,19 @@ package cs3 import ( + "os" + "path" + "testing" + "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/indexer/option" . "github.com/owncloud/ocis/accounts/pkg/indexer/test" "github.com/stretchr/testify/assert" - "os" - "path" - "testing" ) func TestCS3NonUniqueIndex_FakeSymlink(t *testing.T) { - //go setupMetadataStorage() - //defer cancelFunc() - - dataDir := WriteIndexTestDataCS3(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) cfg := config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -40,7 +39,7 @@ func TestCS3NonUniqueIndex_FakeSymlink(t *testing.T) { option.WithProviderAddr(cfg.Repo.CS3.ProviderAddr), ) - err := sut.Init() + err = sut.Init() assert.NoError(t, err) res, err := sut.Add("abcdefg-123", "mikey") diff --git a/accounts/pkg/indexer/index/cs3/unique_test.go b/accounts/pkg/indexer/index/cs3/unique_test.go index e3bdc7c982..eb0b7cf1af 100644 --- a/accounts/pkg/indexer/index/cs3/unique_test.go +++ b/accounts/pkg/indexer/index/cs3/unique_test.go @@ -12,7 +12,8 @@ import ( ) func TestCS3UniqueIndex_FakeSymlink(t *testing.T) { - dataDir := WriteIndexTestDataCS3(t,Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) cfg := config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -38,7 +39,7 @@ func TestCS3UniqueIndex_FakeSymlink(t *testing.T) { option.WithProviderAddr(cfg.Repo.CS3.ProviderAddr), ) - err := sut.Init() + err = sut.Init() assert.NoError(t, err) res, err := sut.Add("abcdefg-123", "mikey") @@ -61,7 +62,8 @@ func TestCS3UniqueIndex_FakeSymlink(t *testing.T) { } func TestCS3UniqueIndexSearch(t *testing.T) { - dataDir := WriteIndexTestDataCS3(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) cfg := config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -87,7 +89,7 @@ func TestCS3UniqueIndexSearch(t *testing.T) { option.WithProviderAddr(cfg.Repo.CS3.ProviderAddr), ) - err := sut.Init() + err = sut.Init() assert.NoError(t, err) _, err = sut.Add("hijklmn-456", "mikey") diff --git a/accounts/pkg/indexer/index/disk/non_unique_test.go b/accounts/pkg/indexer/index/disk/non_unique_test.go index 6fbecd3fd2..60352bac21 100644 --- a/accounts/pkg/indexer/index/disk/non_unique_test.go +++ b/accounts/pkg/indexer/index/disk/non_unique_test.go @@ -2,15 +2,16 @@ package disk import ( "fmt" + "os" + "path" + "testing" + "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/indexer/errors" "github.com/owncloud/ocis/accounts/pkg/indexer/index" "github.com/owncloud/ocis/accounts/pkg/indexer/option" . "github.com/owncloud/ocis/accounts/pkg/indexer/test" "github.com/stretchr/testify/assert" - "os" - "path" - "testing" ) func TestNonUniqueIndexAdd(t *testing.T) { @@ -78,7 +79,7 @@ func TestNonUniqueIndexSearch(t *testing.T) { // entity: used to get the fully qualified name for the index root path. func getNonUniqueIdxSut(t *testing.T, entity interface{}, indexBy string) (index.Index, string) { - dataPath := WriteIndexTestData(t, Data, "ID") + dataPath, _ := WriteIndexTestData(Data, "ID", "") cfg := config.Config{ Repo: config.Repo{ Disk: config.Disk{ diff --git a/accounts/pkg/indexer/index/disk/unique_test.go b/accounts/pkg/indexer/index/disk/unique_test.go index 5c29bcf28c..8d88f5d9b3 100644 --- a/accounts/pkg/indexer/index/disk/unique_test.go +++ b/accounts/pkg/indexer/index/disk/unique_test.go @@ -1,15 +1,16 @@ package disk import ( + "os" + "path" + "testing" + "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/indexer/errors" "github.com/owncloud/ocis/accounts/pkg/indexer/index" "github.com/owncloud/ocis/accounts/pkg/indexer/option" . "github.com/owncloud/ocis/accounts/pkg/indexer/test" "github.com/stretchr/testify/assert" - "os" - "path" - "testing" ) func TestUniqueLookupSingleEntry(t *testing.T) { @@ -99,7 +100,7 @@ func TestErrors(t *testing.T) { } func getUniqueIdxSut(t *testing.T, indexBy string, entityType interface{}) (index.Index, string) { - dataPath := WriteIndexTestData(t, Data, "ID") + dataPath, _ := WriteIndexTestData(Data, "ID", "") cfg := config.Config{ Repo: config.Repo{ Disk: config.Disk{ diff --git a/accounts/pkg/indexer/indexer_test.go b/accounts/pkg/indexer/indexer_test.go index b8b690bb52..f5be6c7aea 100644 --- a/accounts/pkg/indexer/indexer_test.go +++ b/accounts/pkg/indexer/indexer_test.go @@ -1,11 +1,14 @@ package indexer import ( - "github.com/owncloud/ocis/accounts/pkg/indexer/option" "os" "path" "testing" + "github.com/owncloud/ocis/accounts/pkg/proto/v0" + + "github.com/owncloud/ocis/accounts/pkg/indexer/option" + "github.com/owncloud/ocis/accounts/pkg/config" _ "github.com/owncloud/ocis/accounts/pkg/indexer/index/cs3" _ "github.com/owncloud/ocis/accounts/pkg/indexer/index/disk" @@ -13,8 +16,11 @@ import ( "github.com/stretchr/testify/assert" ) +const cs3RootFolder = "/var/tmp/ocis/storage/users/data" + func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestDataCS3(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ CS3: config.CS3{ @@ -26,7 +32,7 @@ func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) + err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -37,7 +43,8 @@ func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { } func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestDataCS3(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", cs3RootFolder) + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ CS3: config.CS3{ @@ -49,7 +56,7 @@ func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique", nil) + err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -60,7 +67,8 @@ func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) { } func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -69,7 +77,7 @@ func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) + err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -84,7 +92,8 @@ func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) { } func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -93,7 +102,7 @@ func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) + err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -104,7 +113,8 @@ func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) { } func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -113,7 +123,7 @@ func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) + err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -134,7 +144,8 @@ func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) { } func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -143,7 +154,7 @@ func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement", &option.Bound{Lower: 5}) + err = indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement", &option.Bound{Lower: 5}) assert.NoError(t, err) res1, err := indexer.Add(Data["users"][0]) @@ -165,7 +176,8 @@ func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) { } func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -174,7 +186,7 @@ func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) + err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -193,7 +205,8 @@ func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) { } func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -202,7 +215,7 @@ func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) + err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -222,7 +235,8 @@ func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) { } func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -231,7 +245,7 @@ func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) + err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) err = indexer.AddIndex(&User{}, "Email", "ID", "users", "unique", nil) @@ -280,7 +294,8 @@ func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) { } func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") + dataDir, err := WriteIndexTestData(Data, "ID", "") + assert.NoError(t, err) indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ Disk: config.Disk{ @@ -289,7 +304,7 @@ func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) + err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -303,3 +318,32 @@ func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) { _ = os.RemoveAll(dataDir) } + +func TestIndexer_Disk_AutoIncrementIndex(t *testing.T) { + scenarios := []struct { + name string + indexOn string + entity *proto.Account + }{ + { + name: "create an index on a valid autoincrement field", + indexOn: "Number", + entity: &proto.Account{}, + }, + } + + for i := range scenarios { + t.Run(scenarios[i].name, func(t *testing.T) { + + }) + } +} + +func setup() error { + //dataDir := WriteIndexTestData(t, Data, "ID") + return nil +} + +func teardown() error { + return nil +} diff --git a/accounts/pkg/indexer/test/data.go b/accounts/pkg/indexer/test/data.go index fc15d6194c..98b2916a36 100644 --- a/accounts/pkg/indexer/test/data.go +++ b/accounts/pkg/indexer/test/data.go @@ -5,19 +5,18 @@ import ( "io/ioutil" "os" "path" - "testing" ) // User is a user. type User struct { ID, UserName, Email string - UID int + UID int } // Pet is a pet. type Pet struct { ID, Kind, Color, Name string - UID int + UID int } // Data mock data. @@ -37,76 +36,57 @@ var Data = map[string][]interface{}{ } // WriteIndexTestData writes mock data to disk. -func WriteIndexTestData(t *testing.T, m map[string][]interface{}, pk string) string { - rootDir := CreateTmpDir(t) +func WriteIndexTestData(m map[string][]interface{}, privateKey, dir string) (string, error) { + rootDir, err := getRootDir(dir) + if err != nil { + return "", err + } + + err = writeData(m, privateKey, rootDir) + if err != nil { + return "", err + } + + return rootDir, nil +} + +// getRootDir allows for some minimal behavior on destination on disk. Testing the cs3 api behavior locally means +// keeping track of where the cs3 data lives on disk, this function allows for multiplexing whether or not to use a +// temporary folder or an already defined one. +func getRootDir(dir string) (string, error) { + var rootDir string + var err error + + if dir != "" { + rootDir = dir + } else { + rootDir, err = CreateTmpDir() + if err != nil { + return "", err + } + } + return rootDir, nil +} + +// writeData writes test data to disk on rootDir location Marshaled as json. +func writeData(m map[string][]interface{}, privateKey string, rootDir string) error { for dirName := range m { fileTypePath := path.Join(rootDir, dirName) if err := os.MkdirAll(fileTypePath, 0777); err != nil { - t.Fatal(err) + return err } for _, u := range m[dirName] { data, err := json.Marshal(u) if err != nil { - t.Fatal(err) + return err } - pkVal := ValueOf(u, pk) + pkVal := ValueOf(u, privateKey) if err := ioutil.WriteFile(path.Join(fileTypePath, pkVal), data, 0777); err != nil { - t.Fatal(err) + return err } } } - - return rootDir -} - -// WriteIndexTestDataCS3 writes more data to disk. -func WriteIndexTestDataCS3(t *testing.T, m map[string][]interface{}, pk string) string { - rootDir := "/var/tmp/ocis/storage/users/data" - for dirName := range m { - fileTypePath := path.Join(rootDir, dirName) - - if err := os.MkdirAll(fileTypePath, 0777); err != nil { - t.Fatal(err) - } - for _, u := range m[dirName] { - data, err := json.Marshal(u) - if err != nil { - t.Fatal(err) - } - - pkVal := ValueOf(u, pk) - if err := ioutil.WriteFile(path.Join(fileTypePath, pkVal), data, 0777); err != nil { - t.Fatal(err) - } - } - } - - return rootDir -} - -// WriteIndexBenchmarkDataCS3 writes more data to disk. -func WriteIndexBenchmarkDataCS3(b *testing.B, m map[string][]interface{}, pk string) string { - rootDir := "/var/tmp/ocis/storage/users/data" - for dirName := range m { - fileTypePath := path.Join(rootDir, dirName) - - if err := os.MkdirAll(fileTypePath, 0777); err != nil { - b.Fatal(err) - } - for _, u := range m[dirName] { - data, err := json.Marshal(u) - if err != nil { - b.Fatal(err) - } - - pkVal := ValueOf(u, pk) - if err := ioutil.WriteFile(path.Join(fileTypePath, pkVal), data, 0777); err != nil { - b.Fatal(err) - } - } - } - - return rootDir + return nil } diff --git a/accounts/pkg/indexer/test/helpers.go b/accounts/pkg/indexer/test/helpers.go index 41235d6bbb..eaad855ec3 100644 --- a/accounts/pkg/indexer/test/helpers.go +++ b/accounts/pkg/indexer/test/helpers.go @@ -6,17 +6,16 @@ import ( "path" "reflect" "strings" - "testing" ) // CreateTmpDir creates a temporary dir for tests data. -func CreateTmpDir(t *testing.T) string { +func CreateTmpDir() (string, error) { name, err := ioutil.TempDir("/var/tmp", "testfiles-") if err != nil { - t.Fatal(err) + return "", err } - return name + return name, nil } // ValueOf gets the value of a type v on a given field . @@ -39,7 +38,8 @@ func getType(v interface{}) (reflect.Value, error) { return rv, nil } -// GetTypeFQN formats a valid name from a type . +// GetTypeFQN formats a valid name from a type . This is a duplication of the already existing function in the +// indexer package, but since there is a circular dependency we chose to duplicate it. func GetTypeFQN(t interface{}) string { typ, _ := getType(t) typeName := path.Join(typ.Type().PkgPath(), typ.Type().Name()) diff --git a/accounts/pkg/service/v0/groups.go b/accounts/pkg/service/v0/groups.go index 2d4d39239c..ee4b674051 100644 --- a/accounts/pkg/service/v0/groups.go +++ b/accounts/pkg/service/v0/groups.go @@ -25,7 +25,7 @@ func (s Service) expandMembers(g *proto.Group) { if err := s.repo.LoadAccount(context.Background(), g.Members[i].Id, a); err == nil { expanded = append(expanded, a) } else { - // log errors but continue execution for now + // log errors but con/var/tmp/ocis-accounts-store-408341811tinue execution for now s.log.Error().Err(err).Str("id", g.Members[i].Id).Msg("could not load account") } }