Skip to content

Commit a787bfe

Browse files
committed
address some minor inconsistencies and add more test cases on the
schedule commands
1 parent 84318c9 commit a787bfe

9 files changed

Lines changed: 73 additions & 5 deletions

internal/cmd/backup/restore_trigger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func newRestoreTriggerCommand(s *state.State) *cobra.Command {
5454
}
5555

5656
fmt.Fprintf(cmd.OutOrStdout(), "Restore of backup %s started.\n", backupID)
57+
fmt.Fprintln(cmd.OutOrStdout(), "Run 'qcloud backup restore list' to track progress.")
5758
return nil
5859
},
5960
}.CobraCommand(s)

internal/cmd/backup/restore_trigger_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,21 @@ func TestRestoreTrigger_WithForce(t *testing.T) {
2626
stdout, _, err := testutil.Exec(t, env, "backup", "restore", "trigger", "backup-abc", "--force")
2727
require.NoError(t, err)
2828
assert.Equal(t, "backup-abc", capturedBackupID)
29-
assert.Contains(t, stdout, "backup-abc")
30-
assert.Contains(t, stdout, "started")
29+
assert.Contains(t, stdout, "Restore of backup backup-abc started.")
30+
assert.Contains(t, stdout, "Run 'qcloud backup restore list' to track progress.")
31+
}
32+
33+
func TestRestoreTrigger_Aborted(t *testing.T) {
34+
env := testutil.NewTestEnv(t)
35+
t.Cleanup(env.Cleanup)
36+
37+
env.BackupServer.RestoreBackupFunc = func(_ context.Context, _ *backupv1.RestoreBackupRequest) (*backupv1.RestoreBackupResponse, error) {
38+
panic("RestoreBackup must not be called when aborted")
39+
}
40+
41+
stdout, _, err := testutil.Exec(t, env, "backup", "restore", "trigger", "backup-abc")
42+
require.NoError(t, err)
43+
assert.Contains(t, stdout, "Aborted.")
3144
}
3245

3346
func TestRestoreTrigger_APIError(t *testing.T) {

internal/cmd/backup/schedule_create_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,21 @@ func TestScheduleCreate_MissingFlags(t *testing.T) {
9191
_, _, err := testutil.Exec(t, env, "backup", "schedule", "create", "--cluster-id=cluster-abc")
9292
require.Error(t, err)
9393
}
94+
95+
func TestScheduleCreate_MissingClusterID(t *testing.T) {
96+
env := testutil.NewTestEnv(t)
97+
t.Cleanup(env.Cleanup)
98+
99+
_, _, err := testutil.Exec(t, env, "backup", "schedule", "create",
100+
"--schedule=0 2 * * *", "--retention-days=30")
101+
require.Error(t, err)
102+
}
103+
104+
func TestScheduleCreate_MissingRetentionDays(t *testing.T) {
105+
env := testutil.NewTestEnv(t)
106+
t.Cleanup(env.Cleanup)
107+
108+
_, _, err := testutil.Exec(t, env, "backup", "schedule", "create",
109+
"--cluster-id=cluster-abc", "--schedule=0 2 * * *")
110+
require.Error(t, err)
111+
}

internal/cmd/backup/schedule_describe.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func nextScheduleRun(cronExpr string) (time.Time, bool) {
2727

2828
func newScheduleDescribeCommand(s *state.State) *cobra.Command {
2929
cmd := base.DescribeCmd[*backupv1.BackupSchedule]{
30-
Use: "describe <schedule-id>",
31-
Short: "Describe a backup schedule",
30+
Use: "describe <schedule-id>",
31+
Short: "Describe a backup schedule",
32+
Long: `Describe a backup schedule.
33+
34+
The --cluster-id flag is required because the API requires the cluster ID to look up a schedule by ID.`,
3235
Args: util.ExactArgs(1, "a schedule ID"),
3336
ValidArgsFunction: scheduleIDCompletion(s),
3437
Fetch: func(s *state.State, cmd *cobra.Command, args []string) (*backupv1.BackupSchedule, error) {

internal/cmd/backup/schedule_describe_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestScheduleDescribe_TextOutput(t *testing.T) {
3838
assert.Contains(t, stdout, "schedule-1")
3939
assert.Contains(t, stdout, "cluster-abc")
4040
assert.Contains(t, stdout, "0 2 * * *")
41+
assert.Contains(t, stdout, "Next Run:")
4142
assert.Contains(t, stdout, "ACTIVE")
4243
}
4344

internal/cmd/backup/schedule_list_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestScheduleList_TableOutput(t *testing.T) {
3939
assert.Contains(t, stdout, "CLUSTER")
4040
assert.Contains(t, stdout, "SCHEDULE")
4141
assert.Contains(t, stdout, "STATUS")
42+
assert.Contains(t, stdout, "NEXT RUN")
4243
assert.Contains(t, stdout, "schedule-1")
4344
assert.Contains(t, stdout, "cluster-abc")
4445
assert.Contains(t, stdout, "0 2 * * *")

internal/cmd/backup/schedule_update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ func newScheduleUpdateCommand(s *state.State) *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "update <schedule-id>",
2424
Short: "Update a backup schedule",
25-
Args: util.ExactArgs(1, "a schedule ID"),
25+
Long: `Update a backup schedule.
26+
27+
The --cluster-id flag is required because the API requires the cluster ID to look up a schedule by ID.`,
28+
Args: util.ExactArgs(1, "a schedule ID"),
2629
}
2730
cmd.Flags().String("cluster-id", "", "Cluster ID (required)")
2831
cmd.Flags().String("schedule", "", "New cron schedule expression in UTC, e.g. '0 2 * * *'")

internal/cmd/backup/schedule_update_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ func TestScheduleUpdate_InvalidRetention(t *testing.T) {
8787
require.Error(t, err)
8888
}
8989

90+
func TestScheduleUpdate_WithRetention(t *testing.T) {
91+
env := testutil.NewTestEnv(t)
92+
t.Cleanup(env.Cleanup)
93+
94+
env.BackupServer.GetBackupScheduleFunc = func(_ context.Context, _ *backupv1.GetBackupScheduleRequest) (*backupv1.GetBackupScheduleResponse, error) {
95+
return &backupv1.GetBackupScheduleResponse{
96+
BackupSchedule: &backupv1.BackupSchedule{Id: "schedule-1", Schedule: "0 2 * * *"},
97+
}, nil
98+
}
99+
100+
var capturedRetentionDays int64
101+
env.BackupServer.UpdateBackupScheduleFunc = func(_ context.Context, req *backupv1.UpdateBackupScheduleRequest) (*backupv1.UpdateBackupScheduleResponse, error) {
102+
if req.GetBackupSchedule().GetRetentionPeriod() != nil {
103+
capturedRetentionDays = int64(req.GetBackupSchedule().GetRetentionPeriod().AsDuration().Hours()) / 24
104+
}
105+
return &backupv1.UpdateBackupScheduleResponse{
106+
BackupSchedule: &backupv1.BackupSchedule{Id: "schedule-1"},
107+
}, nil
108+
}
109+
110+
_, _, err := testutil.Exec(t, env, "backup", "schedule", "update", "schedule-1",
111+
"--cluster-id=cluster-abc", "--retention-days=14")
112+
require.NoError(t, err)
113+
assert.Equal(t, int64(14), capturedRetentionDays)
114+
}
115+
90116
func TestScheduleUpdate_MissingClusterID(t *testing.T) {
91117
env := testutil.NewTestEnv(t)
92118
t.Cleanup(env.Cleanup)

internal/cmd/base/describe.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type DescribeCmd[T any] struct {
1414
Use string
1515
Short string
16+
Long string
1617
Args cobra.PositionalArgs
1718
Fetch func(s *state.State, cmd *cobra.Command, args []string) (T, error)
1819
PrintText func(cmd *cobra.Command, out io.Writer, resource T) error
@@ -24,6 +25,7 @@ func (dc DescribeCmd[T]) CobraCommand(s *state.State) *cobra.Command {
2425
cmd := &cobra.Command{
2526
Use: dc.Use,
2627
Short: dc.Short,
28+
Long: dc.Long,
2729
Args: dc.Args,
2830
RunE: func(cmd *cobra.Command, args []string) error {
2931
resource, err := dc.Fetch(s, cmd, args)

0 commit comments

Comments
 (0)