2023-04-13 10:16:48 +02:00
|
|
|
package testsuite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
2023-09-13 06:03:25 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
2023-04-13 10:16:48 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type repositoryTestCase struct {
|
|
|
|
Name string
|
|
|
|
Skip bool
|
2023-09-13 06:03:25 +02:00
|
|
|
Run func(ctx context.Context, t *testing.T, store share.Store) error
|
2023-04-13 10:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var repositoryTestCases = []repositoryTestCase{
|
|
|
|
{
|
|
|
|
Name: "Update resource attributes",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
origin := app.ID("test")
|
|
|
|
resourceID := share.ResourceID("test")
|
|
|
|
|
|
|
|
// Try to create resource without attributes
|
2023-09-13 06:03:25 +02:00
|
|
|
_, err := store.UpdateAttributes(ctx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("err should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.Is(err, share.ErrAttributeRequired) {
|
2023-09-13 06:03:25 +02:00
|
|
|
return errors.Errorf("err: expected share.ErrAttributeRequired, got '%v'", err)
|
2023-04-13 10:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
attributes := []share.Attribute{
|
|
|
|
share.NewBaseAttribute("my_text_attr", share.TypeText, "foo"),
|
|
|
|
share.NewBaseAttribute("my_number_attr", share.TypeNumber, 5),
|
|
|
|
share.NewBaseAttribute("my_path_attr", share.TypePath, "/my/path"),
|
|
|
|
share.NewBaseAttribute("my_bool_attr", share.TypeBool, true),
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err := store.UpdateAttributes(ctx, origin, resourceID, attributes...)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isNil := reflect.ValueOf(resource).IsNil()
|
|
|
|
if isNil {
|
|
|
|
return errors.New("resource should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := resourceID, resource.ID(); e != g {
|
|
|
|
return errors.Errorf("resource.ID(): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := origin, resource.Origin(); e != g {
|
|
|
|
return errors.Errorf("resource.Origin(): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := 4, len(resource.Attributes()); e != g {
|
|
|
|
return errors.Errorf("len(resource.Attributes()): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Find resources by attribute name",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/find_resources_by_attribute_name.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resources, err := store.FindResources(ctx, share.WithName("my_number"))
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isNil := reflect.ValueOf(resources).IsNil()
|
|
|
|
if isNil {
|
|
|
|
return errors.New("resources should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := 2, len(resources); e != g {
|
|
|
|
return errors.Errorf("len(resources): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Find resources by attribute type",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/find_resources_by_attribute_type.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resources, err := store.FindResources(ctx, share.WithType(share.TypePath))
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isNil := reflect.ValueOf(resources).IsNil()
|
|
|
|
if isNil {
|
|
|
|
return errors.New("resources should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := 1, len(resources); e != g {
|
|
|
|
return errors.Errorf("len(resources): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Find resources by attribute type and name",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/find_resources_by_attribute_type_and_name.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resources, err := store.FindResources(ctx, share.WithType(share.TypeText), share.WithName("my_attr"))
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isNil := reflect.ValueOf(resources).IsNil()
|
|
|
|
if isNil {
|
|
|
|
return errors.New("resources should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := 1, len(resources); e != g {
|
|
|
|
return errors.Errorf("len(resources): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Get resource",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/get_resource.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := app.ID("app1.edge.app")
|
|
|
|
resourceID := share.ResourceID("res-1")
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err := store.GetResource(ctx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isNil := reflect.ValueOf(resource).IsNil()
|
|
|
|
if isNil {
|
|
|
|
return errors.New("resources should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := origin, resource.Origin(); e != g {
|
|
|
|
return errors.Errorf("resource.Origin(): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := resourceID, resource.ID(); e != g {
|
|
|
|
return errors.Errorf("resource.ID(): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err = store.GetResource(ctx, origin, "unexistant-id")
|
2023-04-13 10:16:48 +02:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("err should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.Is(err, share.ErrNotFound) {
|
|
|
|
return errors.Errorf("err: expected share.ErrNotFound, got '%+v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Delete resource",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/delete_resource.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := app.ID("app1.edge.app")
|
|
|
|
resourceID := share.ResourceID("res-1")
|
|
|
|
|
|
|
|
// It should delete an existing resource
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := store.DeleteResource(ctx, origin, resourceID); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
_, err := store.GetResource(ctx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("err should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The resource should be deleted
|
|
|
|
if !errors.Is(err, share.ErrNotFound) {
|
|
|
|
return errors.Errorf("err: expected share.ErrNotFound, got '%+v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should not delete an unexistant resource
|
2023-09-13 06:03:25 +02:00
|
|
|
err = store.DeleteResource(ctx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("err should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.Is(err, share.ErrNotFound) {
|
|
|
|
return errors.Errorf("err: expected share.ErrNotFound, got '%+v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
otherOrigin := app.ID("app2.edge.app")
|
|
|
|
|
|
|
|
// It should not delete a resource with the same id and another origin
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err := store.GetResource(ctx, otherOrigin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("err should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := otherOrigin, resource.Origin(); e != g {
|
|
|
|
return errors.Errorf("resource.Origin(): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Delete attributes",
|
|
|
|
Skip: false,
|
2023-09-13 06:03:25 +02:00
|
|
|
Run: func(ctx context.Context, t *testing.T, store share.Store) error {
|
|
|
|
if err := loadTestData(ctx, "testdata/delete_attributes.json", store); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := app.ID("app1.edge.app")
|
|
|
|
resourceID := share.ResourceID("res-1")
|
|
|
|
|
|
|
|
// It should delete specified attributes
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := store.DeleteAttributes(ctx, origin, resourceID, "my_text", "my_bool"); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err := store.GetResource(ctx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, g := 1, len(resource.Attributes()); e != g {
|
|
|
|
return errors.Errorf("len(resource.Attributes()): expected '%v', got '%v'", e, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := share.GetAttribute(resource, "my_number", share.TypeNumber)
|
|
|
|
if attr == nil {
|
|
|
|
return errors.New("attr shoudl not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func runRepositoryTests(t *testing.T, newRepo NewTestStoreFunc) {
|
2023-04-13 10:16:48 +02:00
|
|
|
for _, tc := range repositoryTestCases {
|
|
|
|
func(tc repositoryTestCase) {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
if tc.Skip {
|
|
|
|
t.SkipNow()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
repo, err := newRepo(tc.Name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tc.Run(ctx, t, repo); err != nil {
|
|
|
|
t.Errorf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonResource struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Origin string `json:"origin"`
|
|
|
|
Attributes []jsonAttribute `json:"attributes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonAttribute struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type share.ValueType `json:"type"`
|
|
|
|
Value any `json:"value"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func loadTestData(ctx context.Context, jsonFile string, store share.Store) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
data, err := testData.ReadFile(jsonFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resources []jsonResource
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resources); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range resources {
|
|
|
|
attributes := make([]share.Attribute, len(res.Attributes))
|
|
|
|
|
|
|
|
for idx, attr := range res.Attributes {
|
|
|
|
attributes[idx] = share.NewBaseAttribute(
|
|
|
|
attr.Name,
|
|
|
|
attr.Type,
|
|
|
|
attr.Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
_, err := store.UpdateAttributes(ctx, app.ID(res.Origin), share.ResourceID(res.ID), attributes...)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|