|
| 1 | +package backup_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + backupv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/cluster/backup/v1" |
| 12 | + |
| 13 | + "github.com/qdrant/qcloud-cli/internal/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestBackupCreate_Success(t *testing.T) { |
| 17 | + env := testutil.NewTestEnv(t) |
| 18 | + t.Cleanup(env.Cleanup) |
| 19 | + |
| 20 | + env.BackupServer.CreateBackupFunc = func(_ context.Context, req *backupv1.CreateBackupRequest) (*backupv1.CreateBackupResponse, error) { |
| 21 | + assert.Equal(t, "test-account-id", req.GetBackup().GetAccountId()) |
| 22 | + assert.Equal(t, "cluster-abc", req.GetBackup().GetClusterId()) |
| 23 | + return &backupv1.CreateBackupResponse{ |
| 24 | + Backup: &backupv1.Backup{Id: "backup-new", ClusterId: "cluster-abc"}, |
| 25 | + }, nil |
| 26 | + } |
| 27 | + |
| 28 | + stdout, _, err := testutil.Exec(t, env, "backup", "create", "--cluster-id=cluster-abc", "--retention-days=7") |
| 29 | + require.NoError(t, err) |
| 30 | + assert.Contains(t, stdout, "backup-new") |
| 31 | + assert.Contains(t, stdout, "cluster-abc") |
| 32 | +} |
| 33 | + |
| 34 | +func TestBackupCreate_WithRetention(t *testing.T) { |
| 35 | + env := testutil.NewTestEnv(t) |
| 36 | + t.Cleanup(env.Cleanup) |
| 37 | + |
| 38 | + var capturedRetention int64 |
| 39 | + env.BackupServer.CreateBackupFunc = func(_ context.Context, req *backupv1.CreateBackupRequest) (*backupv1.CreateBackupResponse, error) { |
| 40 | + if req.GetBackup().GetRetentionPeriod() != nil { |
| 41 | + capturedRetention = int64(req.GetBackup().GetRetentionPeriod().AsDuration().Hours()) / 24 |
| 42 | + } |
| 43 | + return &backupv1.CreateBackupResponse{ |
| 44 | + Backup: &backupv1.Backup{Id: "backup-ret", ClusterId: "cluster-abc"}, |
| 45 | + }, nil |
| 46 | + } |
| 47 | + |
| 48 | + _, _, err := testutil.Exec(t, env, "backup", "create", "--cluster-id=cluster-abc", "--retention-days=7") |
| 49 | + require.NoError(t, err) |
| 50 | + assert.Equal(t, int64(7), capturedRetention) |
| 51 | +} |
| 52 | + |
| 53 | +func TestBackupCreate_JSONOutput(t *testing.T) { |
| 54 | + env := testutil.NewTestEnv(t) |
| 55 | + t.Cleanup(env.Cleanup) |
| 56 | + |
| 57 | + env.BackupServer.CreateBackupFunc = func(_ context.Context, _ *backupv1.CreateBackupRequest) (*backupv1.CreateBackupResponse, error) { |
| 58 | + return &backupv1.CreateBackupResponse{ |
| 59 | + Backup: &backupv1.Backup{Id: "backup-json", ClusterId: "cluster-123"}, |
| 60 | + }, nil |
| 61 | + } |
| 62 | + |
| 63 | + stdout, _, err := testutil.Exec(t, env, "backup", "create", "--cluster-id=cluster-123", "--retention-days=7", "--json") |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + var result struct { |
| 67 | + ID string `json:"id"` |
| 68 | + ClusterID string `json:"clusterId"` |
| 69 | + } |
| 70 | + require.NoError(t, json.Unmarshal([]byte(stdout), &result)) |
| 71 | + assert.Equal(t, "backup-json", result.ID) |
| 72 | +} |
| 73 | + |
| 74 | +func TestBackupCreate_InvalidRetention(t *testing.T) { |
| 75 | + env := testutil.NewTestEnv(t) |
| 76 | + t.Cleanup(env.Cleanup) |
| 77 | + |
| 78 | + _, _, err := testutil.Exec(t, env, "backup", "create", "--cluster-id=cluster-abc", "--retention-days=0") |
| 79 | + require.Error(t, err) |
| 80 | +} |
| 81 | + |
| 82 | +func TestBackupCreate_MissingClusterID(t *testing.T) { |
| 83 | + env := testutil.NewTestEnv(t) |
| 84 | + t.Cleanup(env.Cleanup) |
| 85 | + |
| 86 | + _, _, err := testutil.Exec(t, env, "backup", "create") |
| 87 | + require.Error(t, err) |
| 88 | +} |
| 89 | + |
| 90 | +func TestBackupCreate_APIError(t *testing.T) { |
| 91 | + env := testutil.NewTestEnv(t) |
| 92 | + t.Cleanup(env.Cleanup) |
| 93 | + |
| 94 | + env.BackupServer.CreateBackupFunc = func(_ context.Context, _ *backupv1.CreateBackupRequest) (*backupv1.CreateBackupResponse, error) { |
| 95 | + return nil, assert.AnError |
| 96 | + } |
| 97 | + |
| 98 | + _, _, err := testutil.Exec(t, env, "backup", "create", "--cluster-id=cluster-abc", "--retention-days=7") |
| 99 | + require.Error(t, err) |
| 100 | +} |
0 commit comments