Skip to content

Fix import with schema=false #510

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions internal/cmd/import-test/relations-only-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
}
3 changes: 3 additions & 0 deletions internal/cmd/import-test/relations-only-validation-file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
relationships: >-
resource:1#user@user:1
55 changes: 55 additions & 0 deletions internal/cmd/import_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -59,3 +60,57 @@ func TestImportCmdHappyPath(t *testing.T) {
require.NoError(err)
require.Equal(resp.Permissionship, v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION)
}

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

// Set up client
ctx := t.Context()
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)

// Write the schema out-of-band so that the import is hitting a realized schema
schemaBytes, err := os.ReadFile(filepath.Join("import-test", "relations-only-schema.zed"))
require.NoError(err)
_, err = c.WriteSchema(ctx, &v1.WriteSchemaRequest{
Schema: string(schemaBytes),
})
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)
}
15 changes: 15 additions & 0 deletions internal/cmd/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

Check failure on line 1 in internal/cmd/validate_test.go

View workflow job for this annotation

GitHub Actions / Lint Go

Please run gofumpt. diff --git a/internal/cmd/validate_test.go b/internal/cmd/validate_test.go index e86f731..f0c07ee 100644 --- a/internal/cmd/validate_test.go +++ b/internal/cmd/validate_test.go @@ -6,8 +6,8 @@ import ( "regexp" "testing" - "github.com/stretchr/testify/require" "github.com/gookit/color" + "github.com/stretchr/testify/require" zedtesting "github.com/authzed/zed/internal/testing" )

import (
"fmt"
"path/filepath"
"regexp"
"testing"

"github.com/stretchr/testify/require"
"github.com/gookit/color"

zedtesting "github.com/authzed/zed/internal/testing"
)
Expand All @@ -16,6 +18,14 @@
return durationRegex.ReplaceAllString(s, "(Xs)")
}

func TestMain(m *testing.M) {
// Disable color output in tests
fmt.Println("Disabling color")
color.Disable()

m.Run()
}

func TestValidatePreRun(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -59,6 +69,8 @@
func TestValidate(t *testing.T) {
t.Parallel()

fmt.Println("term color level before tests")
fmt.Println(color.TermColorLevel().String())
testCases := map[string]struct {
schemaTypeFlag string
files []string
Expand Down Expand Up @@ -293,4 +305,7 @@
require.Equal(tc.expectNonZeroStatusCode, shouldError)
})
}

fmt.Println("term color level after tests")
fmt.Println(color.TermColorLevel().String())
}
Loading