Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import behavior #461

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/validationfile"
"github.com/jzelinskie/cobrautil/v2"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -76,7 +77,7 @@ func importCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
var p decode.SchemaRelationships
var p validationfile.ValidationFile
if _, _, err := decoder(&p); err != nil {
return err
}
Expand All @@ -87,15 +88,15 @@ func importCmdFunc(cmd *cobra.Command, args []string) error {
}

if cobrautil.MustGetBool(cmd, "schema") {
if err := importSchema(cmd.Context(), client, p.Schema, prefix); err != nil {
if err := importSchema(cmd.Context(), client, p.Schema.Schema, prefix); err != nil {
return err
}
}

if cobrautil.MustGetBool(cmd, "relationships") {
batchSize := cobrautil.MustGetInt(cmd, "batch-size")
workers := cobrautil.MustGetInt(cmd, "workers")
if err := importRelationships(cmd.Context(), client, p.Relationships, prefix, batchSize, workers); err != nil {
if err := importRelationships(cmd.Context(), client, p.Relationships.RelationshipsString, prefix, batchSize, workers); err != nil {
return err
}
}
Expand Down
62 changes: 62 additions & 0 deletions internal/cmd/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"context"
"path/filepath"
"testing"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/stretchr/testify/require"

"github.com/authzed/zed/internal/client"
zedtesting "github.com/authzed/zed/internal/testing"
)

var fullyConsistent = &v1.Consistency{Requirement: &v1.Consistency_FullyConsistent{FullyConsistent: true}}

func TestImportCmdHappyPath(t *testing.T) {
require := require.New(t)
cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t,
zedtesting.StringFlag{FlagName: "schema-definition-prefix"},
zedtesting.BoolFlag{FlagName: "schema", FlagValue: true},
zedtesting.BoolFlag{FlagName: "relationships", FlagValue: true},
zedtesting.IntFlag{FlagName: "batch-size", FlagValue: 100},
zedtesting.IntFlag{FlagName: "workers", FlagValue: 1},
)
f := filepath.Join("test", "happy-path-validation-file.yaml")

// Set up client
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv := zedtesting.NewTestServer(ctx, t)
go func() {
require.NoError(srv.Run(ctx))
}()
conn, err := srv.GRPCDialContext(ctx)
require.NoError(err)

originalClient := client.NewClient
defer func() {
client.NewClient = originalClient
}()

client.NewClient = zedtesting.ClientFromConn(conn)

c, err := zedtesting.ClientFromConn(conn)(cmd)
require.NoError(err)

// Run the import and assert we don't have errors
err = importCmdFunc(cmd, []string{f})
require.NoError(err)

// Run a check with full consistency to see whether the relationships
// and schema are written
resp, err := c.CheckPermission(ctx, &v1.CheckPermissionRequest{
Consistency: fullyConsistent,
Subject: &v1.SubjectReference{Object: &v1.ObjectReference{ObjectType: "user", ObjectId: "1"}},
Permission: "view",
Resource: &v1.ObjectReference{ObjectType: "resource", ObjectId: "1"},
})
require.NoError(err)
require.Equal(resp.Permissionship, v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION)
}
9 changes: 9 additions & 0 deletions internal/cmd/test/happy-path-validation-file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
schemaFile: "./happy-path-validation-schema.zed"
relationships: >-
resource:1#user@user:1
assertions:
assertTrue:
- "resource:1#user@user:1"
assertFalse:
- "resource:1#user@user:2"
6 changes: 6 additions & 0 deletions internal/cmd/test/happy-path-validation-schema.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
definition user {}

definition resource {
relation user: user
permission view = user
}
5 changes: 4 additions & 1 deletion internal/decode/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func unmarshalAsYAMLOrSchemaWithFile(data []byte, out interface{}, filename stri
if err := yaml.Unmarshal(data, out); err != nil {
return false, err
}
validationFile := out.(*validationfile.ValidationFile)
validationFile, ok := out.(*validationfile.ValidationFile)
if !ok {
return false, fmt.Errorf("could not cast unmarshalled file to validationfile")
}

// Need to join the original filepath with the requested filepath
// to construct the path to the referenced schema file.
Expand Down
Loading