|
| 1 | +package cluster_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + clusterv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/cluster/v1" |
| 11 | + |
| 12 | + "github.com/qdrant/qcloud-cli/internal/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCreateFromBackup_Success(t *testing.T) { |
| 16 | + env := testutil.NewTestEnv(t) |
| 17 | + |
| 18 | + env.Server.CreateClusterFromBackupCalls.Returns(&clusterv1.CreateClusterFromBackupResponse{ |
| 19 | + Cluster: &clusterv1.Cluster{Id: "cluster-restored", Name: "my-restored-cluster"}, |
| 20 | + }, nil) |
| 21 | + |
| 22 | + stdout, _, err := testutil.Exec(t, env, |
| 23 | + "cluster", "create-from-backup", |
| 24 | + "--backup-id", "backup-abc", |
| 25 | + "--name", "my-restored-cluster", |
| 26 | + ) |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + req, ok := env.Server.CreateClusterFromBackupCalls.Last() |
| 30 | + require.True(t, ok) |
| 31 | + assert.Equal(t, "test-account-id", req.GetAccountId()) |
| 32 | + assert.Equal(t, "backup-abc", req.GetBackupId()) |
| 33 | + assert.Equal(t, "my-restored-cluster", req.GetClusterName()) |
| 34 | + assert.Contains(t, stdout, "cluster-restored") |
| 35 | + assert.Contains(t, stdout, "my-restored-cluster") |
| 36 | +} |
| 37 | + |
| 38 | +func TestCreateFromBackup_MissingBackupID(t *testing.T) { |
| 39 | + env := testutil.NewTestEnv(t) |
| 40 | + |
| 41 | + _, _, err := testutil.Exec(t, env, |
| 42 | + "cluster", "create-from-backup", |
| 43 | + "--name", "my-restored-cluster", |
| 44 | + ) |
| 45 | + require.Error(t, err) |
| 46 | + assert.Equal(t, 0, env.Server.CreateClusterFromBackupCalls.Count()) |
| 47 | +} |
| 48 | + |
| 49 | +func TestCreateFromBackup_MissingName(t *testing.T) { |
| 50 | + env := testutil.NewTestEnv(t) |
| 51 | + |
| 52 | + _, _, err := testutil.Exec(t, env, |
| 53 | + "cluster", "create-from-backup", |
| 54 | + "--backup-id", "backup-abc", |
| 55 | + ) |
| 56 | + require.Error(t, err) |
| 57 | + assert.Equal(t, 0, env.Server.CreateClusterFromBackupCalls.Count()) |
| 58 | +} |
| 59 | + |
| 60 | +func TestCreateFromBackup_APIError(t *testing.T) { |
| 61 | + env := testutil.NewTestEnv(t) |
| 62 | + |
| 63 | + env.Server.CreateClusterFromBackupCalls.Returns(nil, assert.AnError) |
| 64 | + |
| 65 | + _, _, err := testutil.Exec(t, env, |
| 66 | + "cluster", "create-from-backup", |
| 67 | + "--backup-id", "backup-abc", |
| 68 | + "--name", "my-restored-cluster", |
| 69 | + ) |
| 70 | + require.Error(t, err) |
| 71 | +} |
| 72 | + |
| 73 | +func TestCreateFromBackup_NoWait(t *testing.T) { |
| 74 | + env := testutil.NewTestEnv(t) |
| 75 | + |
| 76 | + env.Server.CreateClusterFromBackupCalls.Always(func(_ context.Context, req *clusterv1.CreateClusterFromBackupRequest) (*clusterv1.CreateClusterFromBackupResponse, error) { |
| 77 | + return &clusterv1.CreateClusterFromBackupResponse{ |
| 78 | + Cluster: &clusterv1.Cluster{ |
| 79 | + Id: "cluster-restored", |
| 80 | + Name: req.GetClusterName(), |
| 81 | + }, |
| 82 | + }, nil |
| 83 | + }) |
| 84 | + |
| 85 | + stdout, _, err := testutil.Exec(t, env, |
| 86 | + "cluster", "create-from-backup", |
| 87 | + "--backup-id", "backup-abc", |
| 88 | + "--name", "my-restored-cluster", |
| 89 | + ) |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Contains(t, stdout, "cluster-restored") |
| 92 | + assert.Equal(t, 0, env.Server.GetClusterCalls.Count(), "GetCluster should not be called without --wait") |
| 93 | +} |
| 94 | + |
| 95 | +func TestCreateFromBackup_WaitSuccess(t *testing.T) { |
| 96 | + env := testutil.NewTestEnv(t) |
| 97 | + |
| 98 | + env.Server.CreateClusterFromBackupCalls.Always(func(_ context.Context, req *clusterv1.CreateClusterFromBackupRequest) (*clusterv1.CreateClusterFromBackupResponse, error) { |
| 99 | + return &clusterv1.CreateClusterFromBackupResponse{ |
| 100 | + Cluster: &clusterv1.Cluster{ |
| 101 | + Id: "cluster-restored", |
| 102 | + Name: req.GetClusterName(), |
| 103 | + }, |
| 104 | + }, nil |
| 105 | + }) |
| 106 | + env.Server.GetClusterCalls. |
| 107 | + OnCall(0, func(_ context.Context, _ *clusterv1.GetClusterRequest) (*clusterv1.GetClusterResponse, error) { |
| 108 | + return &clusterv1.GetClusterResponse{ |
| 109 | + Cluster: &clusterv1.Cluster{ |
| 110 | + Id: "cluster-restored", |
| 111 | + State: &clusterv1.ClusterState{Phase: clusterv1.ClusterPhase_CLUSTER_PHASE_CREATING}, |
| 112 | + }, |
| 113 | + }, nil |
| 114 | + }). |
| 115 | + Always(func(_ context.Context, _ *clusterv1.GetClusterRequest) (*clusterv1.GetClusterResponse, error) { |
| 116 | + return &clusterv1.GetClusterResponse{ |
| 117 | + Cluster: &clusterv1.Cluster{ |
| 118 | + Id: "cluster-restored", |
| 119 | + State: &clusterv1.ClusterState{ |
| 120 | + Phase: clusterv1.ClusterPhase_CLUSTER_PHASE_HEALTHY, |
| 121 | + Endpoint: &clusterv1.ClusterEndpoint{Url: "https://restored.aws.cloud.qdrant.io"}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, nil |
| 125 | + }) |
| 126 | + |
| 127 | + stdout, stderr, err := testutil.Exec(t, env, |
| 128 | + "cluster", "create-from-backup", |
| 129 | + "--backup-id", "backup-abc", |
| 130 | + "--name", "my-restored-cluster", |
| 131 | + "--wait", |
| 132 | + "--wait-timeout", "30s", |
| 133 | + "--wait-poll-interval", "10ms", |
| 134 | + ) |
| 135 | + require.NoError(t, err) |
| 136 | + assert.Contains(t, stderr, "cluster-restored") |
| 137 | + assert.Contains(t, stdout, "cluster-restored") |
| 138 | + assert.Contains(t, stdout, "https://restored.aws.cloud.qdrant.io") |
| 139 | + assert.Positive(t, env.Server.GetClusterCalls.Count()) |
| 140 | +} |
0 commit comments