From 47940beac5e8174a5a9b8146b576c9b5e7e0a75f Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 8 May 2025 10:43:31 -0600 Subject: [PATCH] Fix import with schema=false --- .../cmd/import-test/relations-only-schema.zed | 6 ++ .../relations-only-validation-file.yaml | 3 + internal/cmd/import_test.go | 55 +++++++++++++++++++ internal/cmd/validate_test.go | 15 +++++ 4 files changed, 79 insertions(+) create mode 100644 internal/cmd/import-test/relations-only-schema.zed create mode 100644 internal/cmd/import-test/relations-only-validation-file.yaml diff --git a/internal/cmd/import-test/relations-only-schema.zed b/internal/cmd/import-test/relations-only-schema.zed new file mode 100644 index 00000000..053bff6d --- /dev/null +++ b/internal/cmd/import-test/relations-only-schema.zed @@ -0,0 +1,6 @@ +definition user {} + +definition resource { + relation user: user + permission view = user +} diff --git a/internal/cmd/import-test/relations-only-validation-file.yaml b/internal/cmd/import-test/relations-only-validation-file.yaml new file mode 100644 index 00000000..b54a4c08 --- /dev/null +++ b/internal/cmd/import-test/relations-only-validation-file.yaml @@ -0,0 +1,3 @@ +--- +relationships: >- + resource:1#user@user:1 diff --git a/internal/cmd/import_test.go b/internal/cmd/import_test.go index 071e5027..9efdf8d5 100644 --- a/internal/cmd/import_test.go +++ b/internal/cmd/import_test.go @@ -1,6 +1,7 @@ package cmd import ( + "os" "path/filepath" "testing" @@ -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) +} diff --git a/internal/cmd/validate_test.go b/internal/cmd/validate_test.go index 83e87927..e86f731f 100644 --- a/internal/cmd/validate_test.go +++ b/internal/cmd/validate_test.go @@ -1,11 +1,13 @@ package cmd import ( + "fmt" "path/filepath" "regexp" "testing" "github.com/stretchr/testify/require" + "github.com/gookit/color" zedtesting "github.com/authzed/zed/internal/testing" ) @@ -16,6 +18,14 @@ func stripDuration(s string) string { 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() @@ -59,6 +69,8 @@ func TestValidatePreRun(t *testing.T) { 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 @@ -293,4 +305,7 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida require.Equal(tc.expectNonZeroStatusCode, shouldError) }) } + + fmt.Println("term color level after tests") + fmt.Println(color.TermColorLevel().String()) }