From 535717a025a951b6e2b5e5dd0806a4a7988eaf26 Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 19 Aug 2020 10:13:16 +0200 Subject: [PATCH] Add tests for filesystem store implementation --- pkg/store/filesystem/bundles_test.go | 116 +++++++++++++++++++++++++++ pkg/store/filesystem/store_test.go | 39 +++++++++ pkg/store/filesystem/values_test.go | 70 ++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 pkg/store/filesystem/bundles_test.go create mode 100644 pkg/store/filesystem/store_test.go create mode 100644 pkg/store/filesystem/values_test.go diff --git a/pkg/store/filesystem/bundles_test.go b/pkg/store/filesystem/bundles_test.go new file mode 100644 index 0000000000..96c90f8db3 --- /dev/null +++ b/pkg/store/filesystem/bundles_test.go @@ -0,0 +1,116 @@ +package store + +import ( + "testing" + + olog "github.com/owncloud/ocis-pkg/v2/log" + "github.com/owncloud/ocis-settings/pkg/proto/v0" + "github.com/stretchr/testify/assert" +) + +var bundleScenarios = []struct { + name string + bundle *proto.Bundle +}{ + { + name: "generic-test-file-resource", + bundle: &proto.Bundle{ + Id: bundle1, + Type: proto.Bundle_TYPE_DEFAULT, + Extension: extension1, + DisplayName: "test1", + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_FILE, + Id: "beep", + }, + Settings: []*proto.Setting{ + { + Id: setting1, + Description: "test-desc-1", + DisplayName: "test-displayname-1", + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_FILE, + Id: "bleep", + }, + Value: &proto.Setting_IntValue{ + IntValue: &proto.Int{ + Min: 0, + Max: 42, + }, + }, + }, + }, + }, + }, + { + name: "generic-test-system-resource", + bundle: &proto.Bundle{ + Id: bundle2, + Type: proto.Bundle_TYPE_DEFAULT, + Extension: extension2, + DisplayName: "test1", + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_SYSTEM, + }, + Settings: []*proto.Setting{ + { + Id: setting2, + Description: "test-desc-2", + DisplayName: "test-displayname-2", + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_SYSTEM, + }, + Value: &proto.Setting_IntValue{ + IntValue: &proto.Int{ + Min: 0, + Max: 42, + }, + }, + }, + }, + }, + }, +} + +func TestBundles(t *testing.T) { + s := Store{ + dataPath: dataRoot, + Logger: olog.NewLogger( + olog.Color(true), + olog.Pretty(true), + olog.Level("info"), + ), + } + + // write bundles + for i := range bundleScenarios { + index := i + t.Run(bundleScenarios[index].name, func(t *testing.T) { + filePath := s.buildFilePathForBundle(bundleScenarios[index].bundle.Id, true) + if err := s.writeRecordToFile(bundleScenarios[index].bundle, filePath); err != nil { + t.Error(err) + } + assert.FileExists(t, filePath) + }) + } + + // check that ListBundles only returns bundles with type DEFAULT + bundles, err := s.ListBundles(proto.Bundle_TYPE_DEFAULT) + if err != nil { + t.Error(err) + } + for i := range bundles { + assert.Equal(t, proto.Bundle_TYPE_DEFAULT, bundles[i].Type) + } + + // check that ListRoles only returns bundles with type ROLE + roles, err := s.ListBundles(proto.Bundle_TYPE_ROLE) + if err != nil { + t.Error(err) + } + for i := range roles { + assert.Equal(t, proto.Bundle_TYPE_ROLE, roles[i].Type) + } + + burnRoot() +} diff --git a/pkg/store/filesystem/store_test.go b/pkg/store/filesystem/store_test.go new file mode 100644 index 0000000000..5f3becdf37 --- /dev/null +++ b/pkg/store/filesystem/store_test.go @@ -0,0 +1,39 @@ +package store + +import ( + "os" + "path/filepath" +) + +const ( + // account UUIDs + accountUUID1 = "c4572da7-6142-4383-8fc6-efde3d463036" + //accountUUID2 = "e11f9769-416a-427d-9441-41a0e51391d7" + //accountUUID3 = "633ecd77-1980-412a-8721-bf598a330bb4" + + // extension names + extension1 = "test-extension-1" + extension2 = "test-extension-2" + + // bundle ids + bundle1 = "2f06addf-4fd2-49d5-8f71-00fbd3a3ec47" + bundle2 = "2d745744-749c-4286-8e92-74a24d8331c5" + bundle3 = "d8fd27d1-c00b-4794-a658-416b756a72ff" + + // setting ids + setting1 = "c7ebbc8b-d15a-4f2e-9d7d-d6a4cf858d1a" + setting2 = "3fd9a3d9-20b7-40d4-9294-b22bb5868c10" + setting3 = "24bb9535-3df4-42f1-a622-7c0562bec99f" + + // value ids + value1 = "fd3b6221-dc13-4a22-824d-2480495f1cdb" + value2 = "2a0bd9b0-ca1d-491a-8c56-d2ddfd68ded8" + //value3 = "b42702d2-5e4d-4d73-b133-e1f9e285355e" + + dataRoot = "/var/tmp/herecomesthesun" +) + +func burnRoot() { + os.RemoveAll(filepath.Join(dataRoot, "values")) + os.RemoveAll(filepath.Join(dataRoot, "bundles")) +} diff --git a/pkg/store/filesystem/values_test.go b/pkg/store/filesystem/values_test.go new file mode 100644 index 0000000000..d121fc70db --- /dev/null +++ b/pkg/store/filesystem/values_test.go @@ -0,0 +1,70 @@ +package store + +import ( + "testing" + + olog "github.com/owncloud/ocis-pkg/v2/log" + "github.com/owncloud/ocis-settings/pkg/proto/v0" + "github.com/stretchr/testify/assert" +) + +var valueScenarios = []struct { + name string + value *proto.Value +}{ + { + name: "generic-test-with-system-resource", + value: &proto.Value{ + Id: value1, + BundleId: bundle1, + SettingId: setting1, + AccountUuid: accountUUID1, + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_SYSTEM, + }, + Value: &proto.Value_StringValue{ + StringValue: "lalala", + }, + }, + }, + { + name: "generic-test-with-file-resource", + value: &proto.Value{ + Id: value2, + BundleId: bundle1, + SettingId: setting2, + AccountUuid: accountUUID1, + Resource: &proto.Resource{ + Type: proto.Resource_TYPE_FILE, + Id: "adfba82d-919a-41c3-9cd1-5a3f83b2bf76", + }, + Value: &proto.Value_StringValue{ + StringValue: "tralala", + }, + }, + }, +} + +func TestValues(t *testing.T) { + s := Store{ + dataPath: dataRoot, + Logger: olog.NewLogger( + olog.Color(true), + olog.Pretty(true), + olog.Level("info"), + ), + } + for i := range valueScenarios { + index := i + t.Run(valueScenarios[index].name, func(t *testing.T) { + + filePath := s.buildFilePathForValue(valueScenarios[index].value.Id, true) + if err := s.writeRecordToFile(valueScenarios[index].value, filePath); err != nil { + t.Error(err) + } + assert.FileExists(t, filePath) + }) + } + + burnRoot() +}