|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package admin |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + adminpb "cloud.google.com/go/bigtable/admin/apiv2/adminpb" |
| 25 | + "google.golang.org/api/option" |
| 26 | + "google.golang.org/grpc" |
| 27 | + "google.golang.org/grpc/codes" |
| 28 | + "google.golang.org/grpc/credentials/insecure" |
| 29 | + "google.golang.org/grpc/status" |
| 30 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 31 | +) |
| 32 | + |
| 33 | +type testEnv struct { |
| 34 | + client *BigtableTableAdminClient |
| 35 | + project string |
| 36 | + instance string |
| 37 | + cluster string |
| 38 | +} |
| 39 | + |
| 40 | +func setupIntegration(t *testing.T) *testEnv { |
| 41 | + t.Helper() |
| 42 | + if testing.Short() { |
| 43 | + t.Skip("Skipping integration test in short mode") |
| 44 | + } |
| 45 | + |
| 46 | + ctx := context.Background() |
| 47 | + emulatorHost := os.Getenv("BIGTABLE_EMULATOR_HOST") |
| 48 | + var opts []option.ClientOption |
| 49 | + project := os.Getenv("GCLOUD_TESTS_GOLANG_PROJECT_ID") |
| 50 | + instance := os.Getenv("GCLOUD_TESTS_BIGTABLE_INSTANCE") |
| 51 | + cluster := os.Getenv("GCLOUD_TESTS_BIGTABLE_CLUSTER") |
| 52 | + |
| 53 | + if emulatorHost != "" { |
| 54 | + t.Logf("Using emulator at %s", emulatorHost) |
| 55 | + opts = append(opts, |
| 56 | + option.WithEndpoint(emulatorHost), |
| 57 | + option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 58 | + ) |
| 59 | + if project == "" { |
| 60 | + project = "test-project" |
| 61 | + } |
| 62 | + if instance == "" { |
| 63 | + instance = "test-instance" |
| 64 | + } |
| 65 | + if cluster == "" { |
| 66 | + cluster = "test-cluster" |
| 67 | + } |
| 68 | + } else if project == "" || instance == "" { |
| 69 | + t.Skip("Missing GCLOUD_TESTS_GOLANG_PROJECT_ID or GCLOUD_TESTS_BIGTABLE_INSTANCE for non-emulator run") |
| 70 | + } |
| 71 | + |
| 72 | + client, err := NewBigtableTableAdminClient(ctx, opts...) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Failed to create client: %v", err) |
| 75 | + } |
| 76 | + t.Cleanup(func() { |
| 77 | + client.Close() |
| 78 | + }) |
| 79 | + |
| 80 | + return &testEnv{ |
| 81 | + client: client, |
| 82 | + project: project, |
| 83 | + instance: instance, |
| 84 | + cluster: cluster, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestIntegration_RestoreTable(t *testing.T) { |
| 89 | + env := setupIntegration(t) |
| 90 | + if os.Getenv("BIGTABLE_EMULATOR_HOST") == "" && env.cluster == "" { |
| 91 | + t.Skip("Missing GCLOUD_TESTS_BIGTABLE_CLUSTER for non-emulator run") |
| 92 | + } |
| 93 | + |
| 94 | + ctx := context.Background() |
| 95 | + client := env.client |
| 96 | + |
| 97 | + suffix := time.Now().Format("20060102-150405") |
| 98 | + sourceTableID := fmt.Sprintf("src-table-%s", suffix) |
| 99 | + backupID := fmt.Sprintf("backup-%s", suffix) |
| 100 | + restoredTableID := fmt.Sprintf("restored-table-%s", suffix) |
| 101 | + |
| 102 | + instancePath := fmt.Sprintf("projects/%s/instances/%s", env.project, env.instance) |
| 103 | + sourceTablePath := fmt.Sprintf("%s/tables/%s", instancePath, sourceTableID) |
| 104 | + clusterPath := fmt.Sprintf("%s/clusters/%s", instancePath, env.cluster) |
| 105 | + backupPath := fmt.Sprintf("%s/backups/%s", clusterPath, backupID) |
| 106 | + restoredTablePath := fmt.Sprintf("%s/tables/%s", instancePath, restoredTableID) |
| 107 | + |
| 108 | + // 1. Create source table |
| 109 | + _, err := client.CreateTable(ctx, &adminpb.CreateTableRequest{ |
| 110 | + Parent: instancePath, |
| 111 | + TableId: sourceTableID, |
| 112 | + Table: &adminpb.Table{}, |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("Failed to create source table: %v", err) |
| 116 | + } |
| 117 | + t.Cleanup(func() { |
| 118 | + client.DeleteTable(ctx, &adminpb.DeleteTableRequest{Name: sourceTablePath}) |
| 119 | + }) |
| 120 | + |
| 121 | + // 2. Create backup |
| 122 | + expireTime := time.Now().Add(7 * time.Hour) |
| 123 | + opCreateBackup, err := client.CreateBackup(ctx, &adminpb.CreateBackupRequest{ |
| 124 | + Parent: clusterPath, |
| 125 | + BackupId: backupID, |
| 126 | + Backup: &adminpb.Backup{ |
| 127 | + SourceTable: sourceTablePath, |
| 128 | + ExpireTime: timestamppb.New(expireTime), |
| 129 | + }, |
| 130 | + }) |
| 131 | + if err != nil { |
| 132 | + st, ok := status.FromError(err) |
| 133 | + if ok && st.Code() == codes.Unimplemented { |
| 134 | + t.Skip("Emulator does not support CreateBackup") |
| 135 | + } |
| 136 | + t.Fatalf("Failed to initiate backup: %v", err) |
| 137 | + } |
| 138 | + t.Cleanup(func() { |
| 139 | + client.DeleteBackup(ctx, &adminpb.DeleteBackupRequest{Name: backupPath}) |
| 140 | + }) |
| 141 | + |
| 142 | + _, err = opCreateBackup.Wait(ctx) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Backup LRO failed: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + // 3. Restore table |
| 148 | + err = client.RestoreTable(ctx, &adminpb.RestoreTableRequest{ |
| 149 | + Parent: instancePath, |
| 150 | + TableId: restoredTableID, |
| 151 | + Source: &adminpb.RestoreTableRequest_Backup{ |
| 152 | + Backup: backupPath, |
| 153 | + }, |
| 154 | + }) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("RestoreTable failed: %v", err) |
| 157 | + } |
| 158 | + t.Cleanup(func() { |
| 159 | + client.DeleteTable(ctx, &adminpb.DeleteTableRequest{Name: restoredTablePath}) |
| 160 | + }) |
| 161 | + |
| 162 | + // 4. Verify restored table exists |
| 163 | + restoredTable, err := client.GetTable(ctx, &adminpb.GetTableRequest{ |
| 164 | + Name: restoredTablePath, |
| 165 | + }) |
| 166 | + if err != nil { |
| 167 | + t.Fatalf("Failed to get restored table: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + if restoredTable.Name != restoredTablePath { |
| 171 | + t.Errorf("Expected restored table name %q, got %q", restoredTablePath, restoredTable.Name) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestIntegration_WaitForConsistency(t *testing.T) { |
| 176 | + env := setupIntegration(t) |
| 177 | + |
| 178 | + ctx := context.Background() |
| 179 | + client := env.client |
| 180 | + |
| 181 | + suffix := time.Now().Format("20060102-150405") |
| 182 | + tableID := fmt.Sprintf("repl-test-table-%s", suffix) |
| 183 | + instancePath := fmt.Sprintf("projects/%s/instances/%s", env.project, env.instance) |
| 184 | + tablePath := fmt.Sprintf("%s/tables/%s", instancePath, tableID) |
| 185 | + |
| 186 | + // Create table |
| 187 | + _, err := client.CreateTable(ctx, &adminpb.CreateTableRequest{ |
| 188 | + Parent: instancePath, |
| 189 | + TableId: tableID, |
| 190 | + Table: &adminpb.Table{}, |
| 191 | + }) |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("Failed to create table: %v", err) |
| 194 | + } |
| 195 | + t.Cleanup(func() { |
| 196 | + client.DeleteTable(ctx, &adminpb.DeleteTableRequest{Name: tablePath}) |
| 197 | + }) |
| 198 | + |
| 199 | + // Wait for replication |
| 200 | + err = client.WaitForConsistency(ctx, tablePath) |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("WaitForConsistency failed: %v", err) |
| 203 | + } |
| 204 | +} |
0 commit comments