From 6d39cedf084042c7f20f9eab58ce4939edad4fba Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 14 Oct 2020 14:48:20 +0200 Subject: [PATCH] Add failing test for indexer on autoincrement index --- .../pkg/indexer/index/cs3/autoincrement.go | 13 ++- .../indexer/index/cs3/autoincrement_test.go | 44 ++++----- .../pkg/indexer/index/disk/autoincrement.go | 7 +- accounts/pkg/indexer/indexer_test.go | 94 +++++++++++++------ accounts/pkg/indexer/option/option.go | 2 +- accounts/pkg/indexer/test/helpers.go | 2 +- 6 files changed, 99 insertions(+), 63 deletions(-) diff --git a/accounts/pkg/indexer/index/cs3/autoincrement.go b/accounts/pkg/indexer/index/cs3/autoincrement.go index 30534bb8b..0e088d635 100644 --- a/accounts/pkg/indexer/index/cs3/autoincrement.go +++ b/accounts/pkg/indexer/index/cs3/autoincrement.go @@ -127,11 +127,16 @@ func (idx AutoincrementIndex) Lookup(v string) ([]string, error) { } func (idx AutoincrementIndex) Add(id, v string) (string, error) { - next, err := idx.next() - if err != nil { - return "", err + var newName string + if v == "" { + next, err := idx.next() + if err != nil { + return "", err + } + newName = path.Join(idx.indexRootDir, strconv.Itoa(next)) + } else { + newName = path.Join(idx.indexRootDir, v) } - newName := path.Join(idx.indexRootDir, strconv.Itoa(next)) if err := idx.createSymlink(id, newName); err != nil { if os.IsExist(err) { return "", &idxerrs.AlreadyExistsErr{TypeName: idx.typeName, Key: idx.indexBy, Value: v} diff --git a/accounts/pkg/indexer/index/cs3/autoincrement_test.go b/accounts/pkg/indexer/index/cs3/autoincrement_test.go index 1bbe3f566..3dbc2d015 100644 --- a/accounts/pkg/indexer/index/cs3/autoincrement_test.go +++ b/accounts/pkg/indexer/index/cs3/autoincrement_test.go @@ -12,19 +12,7 @@ import ( func TestAutoincrementIndexAdd(t *testing.T) { dataDir := WriteIndexTestDataCS3(t, Data, "ID") - cfg := config.Config{ - Repo: config.Repo{ - Disk: config.Disk{ - Path: "", - }, - CS3: config.CS3{ - ProviderAddr: "0.0.0.0:9215", - DataURL: "http://localhost:9216", - DataPrefix: "data", - JWTSecret: "Pive-Fumkiu4", - }, - }, - } + cfg := generateConfig() sut := NewAutoincrementIndex( option.WithTypeName(GetTypeFQN(User{})), @@ -49,19 +37,7 @@ func TestAutoincrementIndexAdd(t *testing.T) { func BenchmarkAutoincrementIndexAdd(b *testing.B) { dataDir := WriteIndexBenchmarkDataCS3(b, Data, "ID") - cfg := config.Config{ - Repo: config.Repo{ - Disk: config.Disk{ - Path: "", - }, - CS3: config.CS3{ - ProviderAddr: "0.0.0.0:9215", - DataURL: "http://localhost:9216", - DataPrefix: "data", - JWTSecret: "Pive-Fumkiu4", - }, - }, - } + cfg := generateConfig() sut := NewAutoincrementIndex( option.WithTypeName(GetTypeFQN(User{})), @@ -85,3 +61,19 @@ func BenchmarkAutoincrementIndexAdd(b *testing.B) { _ = os.RemoveAll(dataDir) } + +func generateConfig() config.Config { + return config.Config{ + Repo: config.Repo{ + Disk: config.Disk{ + Path: "", + }, + CS3: config.CS3{ + ProviderAddr: "0.0.0.0:9215", + DataURL: "http://localhost:9216", + DataPrefix: "data", + JWTSecret: "Pive-Fumkiu4", + }, + }, + } +} diff --git a/accounts/pkg/indexer/index/disk/autoincrement.go b/accounts/pkg/indexer/index/disk/autoincrement.go index b6e42946a..77d544215 100644 --- a/accounts/pkg/indexer/index/disk/autoincrement.go +++ b/accounts/pkg/indexer/index/disk/autoincrement.go @@ -101,7 +101,12 @@ func (idx Autoincrement) Lookup(v string) ([]string, error) { func (idx Autoincrement) Add(id, v string) (string, error) { oldName := filepath.Join(idx.filesDir, id) - newName := filepath.Join(idx.indexRootDir, strconv.Itoa(idx.next())) + var newName string + if v == "" { + newName = filepath.Join(idx.indexRootDir, strconv.Itoa(idx.next())) + } else { + newName = filepath.Join(idx.indexRootDir, v) + } err := os.Symlink(oldName, newName) if errors.Is(err, os.ErrExist) { return "", &idxerrs.AlreadyExistsErr{TypeName: idx.typeName, Key: idx.indexBy, Value: v} diff --git a/accounts/pkg/indexer/indexer_test.go b/accounts/pkg/indexer/indexer_test.go index ecdb9c13f..418a06bb0 100644 --- a/accounts/pkg/indexer/indexer_test.go +++ b/accounts/pkg/indexer/indexer_test.go @@ -7,31 +7,12 @@ import ( . "github.com/owncloud/ocis/accounts/pkg/indexer/test" "github.com/stretchr/testify/assert" "os" + "path" "testing" ) -func TestIndexer_AddWithUniqueIndex(t *testing.T) { - dataDir := WriteIndexTestData(t, Data, "ID") - indexer := CreateIndexer(&config.Config{ - Repo: config.Repo{ - Disk: config.Disk{ - Path: dataDir, - }, - }, - }) - - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") - assert.NoError(t, err) - - u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} - _, err = indexer.Add(u) - assert.NoError(t, err) - - _ = os.RemoveAll(dataDir) -} - -func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) { - dir := WriteIndexTestDataCS3(t, Data, "ID") +func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { + dataDir := WriteIndexTestDataCS3(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ CS3: config.CS3{ @@ -50,10 +31,10 @@ func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) { _, err = indexer.Add(u) assert.NoError(t, err) - _ = os.RemoveAll(dir) + _ = os.RemoveAll(dataDir) } -func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) { +func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) { dataDir := WriteIndexTestDataCS3(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -76,7 +57,7 @@ func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) { _ = os.RemoveAll(dataDir) } -func TestIndexer_FindByWithUniqueIndex(t *testing.T) { +func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -100,7 +81,27 @@ func TestIndexer_FindByWithUniqueIndex(t *testing.T) { _ = os.RemoveAll(dataDir) } -func TestIndexer_AddWithNonUniqueIndex(t *testing.T) { +func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) { + dataDir := WriteIndexTestData(t, Data, "ID") + indexer := CreateIndexer(&config.Config{ + Repo: config.Repo{ + Disk: config.Disk{ + Path: dataDir, + }, + }, + }) + + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") + assert.NoError(t, err) + + u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} + _, err = indexer.Add(u) + assert.NoError(t, err) + + _ = os.RemoveAll(dataDir) +} + +func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -126,9 +127,42 @@ func TestIndexer_AddWithNonUniqueIndex(t *testing.T) { assert.NoError(t, err) t.Log(res) + + _ = os.RemoveAll(dataDir) } -func TestIndexer_DeleteWithNonUniqueIndex(t *testing.T) { +func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) { + dataDir := WriteIndexTestData(t, Data, "ID") + indexer := CreateIndexer(&config.Config{ + Repo: config.Repo{ + Disk: config.Disk{ + Path: dataDir, + }, + }, + }) + + err := indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement") + assert.NoError(t, err) + + res1, err := indexer.Add(Data["users"][0]) + assert.NoError(t, err) + assert.Equal(t, "UID", res1[0].Field) + assert.Equal(t, "0", path.Base(res1[0].Value)) + + res2, err := indexer.Add(Data["users"][1]) + assert.NoError(t, err) + assert.Equal(t, "UID", res2[0].Field) + assert.Equal(t, "1", path.Base(res2[0].Value)) + + resFindBy, err := indexer.FindBy(User{}, "UID", "1") + assert.NoError(t, err) + assert.Equal(t, "hijklmn-456", resFindBy[0]) + t.Log(resFindBy) + + _ = os.RemoveAll(dataDir) +} + +func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -156,7 +190,7 @@ func TestIndexer_DeleteWithNonUniqueIndex(t *testing.T) { _ = os.RemoveAll(dataDir) } -func TestIndexer_SearchWithNonUniqueIndex(t *testing.T) { +func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -185,7 +219,7 @@ func TestIndexer_SearchWithNonUniqueIndex(t *testing.T) { _ = os.RemoveAll(dataDir) } -func TestIndexer_UpdateWithUniqueIndex(t *testing.T) { +func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ @@ -243,7 +277,7 @@ func TestIndexer_UpdateWithUniqueIndex(t *testing.T) { _ = os.RemoveAll(dataDir) } -func TestIndexer_UpdateWithNonUniqueIndex(t *testing.T) { +func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, Data, "ID") indexer := CreateIndexer(&config.Config{ Repo: config.Repo{ diff --git a/accounts/pkg/indexer/option/option.go b/accounts/pkg/indexer/option/option.go index 80f695128..af578886f 100644 --- a/accounts/pkg/indexer/option/option.go +++ b/accounts/pkg/indexer/option/option.go @@ -21,7 +21,7 @@ type Options struct { ProviderAddr string } -// WithEntity sets the JWTSecret field. +// WithEntity sets the Entity field. func WithEntity(val interface{}) Option { return func(o *Options) { o.Entity = val diff --git a/accounts/pkg/indexer/test/helpers.go b/accounts/pkg/indexer/test/helpers.go index 3d19a22fb..41235d6bb 100644 --- a/accounts/pkg/indexer/test/helpers.go +++ b/accounts/pkg/indexer/test/helpers.go @@ -11,7 +11,7 @@ import ( // CreateTmpDir creates a temporary dir for tests data. func CreateTmpDir(t *testing.T) string { - name, err := ioutil.TempDir("/var/tmp", "testfiles-*") + name, err := ioutil.TempDir("/var/tmp", "testfiles-") if err != nil { t.Fatal(err) }