Skip to content

Commit

Permalink
add test for backupRestoreCmdFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
vroldanbet committed Dec 15, 2023
1 parent cdc457d commit 6d70225
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
Use: "restore <filename>",
Short: "Restore a permission system from a file",
Args: commands.StdinOrExactArgs(1),
RunE: restoreCmdFunc,
RunE: backupRestoreCmdFunc,
}

backupParseSchemaCmd = &cobra.Command{
Expand Down Expand Up @@ -95,7 +95,7 @@ func registerBackupCmd(rootCmd *cobra.Command) {
Use: "restore <filename>",
Short: "Restore a permission system from a file",
Args: cobra.MaximumNArgs(1),
RunE: restoreCmdFunc,
RunE: backupRestoreCmdFunc,
Hidden: true,
})

Expand Down Expand Up @@ -346,7 +346,7 @@ func openRestoreFile(filename string) (*os.File, int64, error) {
return f, stats.Size(), nil
}

func restoreCmdFunc(cmd *cobra.Command, args []string) error {
func backupRestoreCmdFunc(cmd *cobra.Command, args []string) error {
decoder, closer, err := decoderFromArgs(cmd, args)
if err != nil {
return err
Expand Down
53 changes: 53 additions & 0 deletions internal/cmd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,56 @@ func TestBackupCreateCmdFunc(t *testing.T) {
require.Equal(t, testRel, tuple.MustStringRelationship(rel))
require.Equal(t, resp.WrittenAt.Token, d.ZedToken().Token)
}

func TestBackupRestoreCmdFunc(t *testing.T) {
cmd := createTestCobraCommandWithFlagValue(t,
stringFlag{"prefix-filter", "test"},
boolFlag{"rewrite-legacy", false},
intFlag{"batch-size", 100},
int64Flag{"batches-per-transaction", 10},
)
backupName := createTestBackup(t, testSchema, testRelationships)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv := newServer(ctx, t)
go func() {
require.NoError(t, srv.Run(ctx))
}()
conn, err := srv.GRPCDialContext(ctx)
require.NoError(t, err)

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

client.NewClient = clientFromConn(conn)

c, err := clientFromConn(conn)(cmd)
require.NoError(t, err)
err = backupRestoreCmdFunc(cmd, []string{backupName})
require.NoError(t, err)

resp, err := c.ReadSchema(ctx, &v1.ReadSchemaRequest{})
require.NoError(t, err)
require.Equal(t, testSchema, resp.SchemaText)

rrCli, err := c.ReadRelationships(ctx, &v1.ReadRelationshipsRequest{
Consistency: &v1.Consistency{
Requirement: &v1.Consistency_FullyConsistent{
FullyConsistent: true,
},
},
RelationshipFilter: &v1.RelationshipFilter{
ResourceType: "test/resource",
},
})
require.NoError(t, err)

rrResp, err := rrCli.Recv()
require.NoError(t, err)

require.NoError(t, rrCli.CloseSend())
require.Equal(t, "test/resource:1#reader@test/user:1", tuple.MustStringRelationship(rrResp.Relationship))
}
14 changes: 14 additions & 0 deletions internal/cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ type boolFlag struct {
flagValue bool
}

type intFlag struct {
flagName string
flagValue int
}

type int64Flag struct {
flagName string
flagValue int64
}

func createTestCobraCommandWithFlagValue(t *testing.T, flagAndValues ...any) *cobra.Command {
t.Helper()

Expand All @@ -69,6 +79,10 @@ func createTestCobraCommandWithFlagValue(t *testing.T, flagAndValues ...any) *co
c.Flags().String(f.flagName, f.flagValue, "")
case boolFlag:
c.Flags().Bool(f.flagName, f.flagValue, "")
case intFlag:
c.Flags().Int(f.flagName, f.flagValue, "")
case int64Flag:
c.Flags().Int64(f.flagName, f.flagValue, "")
default:
t.Fatalf("unknown flag type: %T", f)
}
Expand Down

0 comments on commit 6d70225

Please sign in to comment.