Skip to content

Commit

Permalink
test(bigtable): Refactor and add nil check to AutomatedBackupPolicy t…
Browse files Browse the repository at this point in the history
…est (#10172)
  • Loading branch information
bhshkh committed May 15, 2024
1 parent 78c53a3 commit 9393cdf
Showing 1 changed file with 51 additions and 89 deletions.
140 changes: 51 additions & 89 deletions bigtable/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"cloud.google.com/go/iam"
"cloud.google.com/go/internal"
"cloud.google.com/go/internal/optional"
"cloud.google.com/go/internal/testutil"
"cloud.google.com/go/internal/uid"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -1693,6 +1694,16 @@ func TestIntegration_EnableChangeStream(t *testing.T) {
}
}

func equalOptionalDuration(a, b optional.Duration) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return int64(a.(time.Duration).Seconds()) == int64(b.(time.Duration).Seconds())
}

// Testing if automated backups works properly i.e.
// - Can create table with Automated Backups configured
// - Can update Automated Backup Policy on an existing table
Expand Down Expand Up @@ -1740,6 +1751,7 @@ func TestIntegration_AutomatedBackups(t *testing.T) {
if err := adminClient.CreateTableFromConf(ctx, &tableConf); err != nil {
t.Fatalf("Create table from config: %v", err)
}
defer deleteTable(ctx, t, adminClient, myTableName)

table, err := adminClient.TableInfo(ctx, myTableName)
if err != nil {
Expand All @@ -1749,18 +1761,15 @@ func TestIntegration_AutomatedBackups(t *testing.T) {
if table.AutomatedBackupConfig == nil {
t.Errorf("Expect Automated Backup Policy to be enabled for table: %v has info: %v", myTableName, table)
}

tableAbp := table.AutomatedBackupConfig.(*TableAutomatedBackupPolicy)

if int64(tableAbp.Frequency.(time.Duration).Seconds()) != int64(automatedBackupPolicy.Frequency.(time.Duration).Seconds()) {
if !equalOptionalDuration(tableAbp.Frequency, automatedBackupPolicy.Frequency) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
}

if int64(tableAbp.RetentionPeriod.(time.Duration).Seconds()) != int64(automatedBackupPolicy.RetentionPeriod.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
if !equalOptionalDuration(tableAbp.RetentionPeriod, automatedBackupPolicy.RetentionPeriod) {
t.Errorf("Expect automated backup policy retention period to be set for table: %v has info: %v", myTableName, table)
}

// Update automated backup policy, all fields
// Test update automated backup policy
retentionPeriod, err = time.ParseDuration("72h")
if err != nil {
t.Fatalf("RetentionPeriod not valid: %v", err)
Expand All @@ -1769,92 +1778,45 @@ func TestIntegration_AutomatedBackups(t *testing.T) {
if err != nil {
t.Fatalf("Frequency not valid: %v", err)
}
automatedBackupPolicy = TableAutomatedBackupPolicy{RetentionPeriod: retentionPeriod, Frequency: frequency}

if err := adminClient.UpdateTableWithAutomatedBackupPolicy(ctx, myTableName, automatedBackupPolicy); err != nil {
t.Fatalf("Update table from config: %v", err)
}

table, err = adminClient.TableInfo(ctx, myTableName)
if err != nil {
t.Fatalf("Getting table info: %v", err)
}

tableAbp = table.AutomatedBackupConfig.(*TableAutomatedBackupPolicy)

if table.AutomatedBackupConfig == nil {
t.Errorf("Expect Automated Backup Policy to be enabled for table: %v has info: %v", myTableName, table)
}

if int64(tableAbp.Frequency.(time.Duration).Seconds()) != int64(automatedBackupPolicy.Frequency.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
}

if int64(tableAbp.RetentionPeriod.(time.Duration).Seconds()) != int64(automatedBackupPolicy.RetentionPeriod.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy retention period to be set for table: %v has info: %v", myTableName, table)
}

// Update automated backup policy, just frequency
frequency, err = time.ParseDuration("24h")
if err != nil {
t.Fatalf("Frequency not valid: %v", err)
}
automatedBackupPolicy = TableAutomatedBackupPolicy{Frequency: frequency}

if err := adminClient.UpdateTableWithAutomatedBackupPolicy(ctx, myTableName, automatedBackupPolicy); err != nil {
t.Fatalf("Update table from config: %v", err)
}

table, err = adminClient.TableInfo(ctx, myTableName)
if err != nil {
t.Fatalf("Getting table info: %v", err)
}

tableAbp = table.AutomatedBackupConfig.(*TableAutomatedBackupPolicy)

if table.AutomatedBackupConfig == nil {
t.Errorf("Expect Automated Backup Policy to be enabled for table: %v has info: %v", myTableName, table)
}

if int64(tableAbp.Frequency.(time.Duration).Seconds()) != int64(automatedBackupPolicy.Frequency.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
}

if int64(tableAbp.RetentionPeriod.(time.Duration).Seconds()) != int64(automatedBackupPolicy.RetentionPeriod.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
}

// Update automated backup policy, just retention period
retentionPeriod, err = time.ParseDuration("72h")
if err != nil {
t.Fatalf("RetentionPeriod not valid: %v", err)
}
automatedBackupPolicy = TableAutomatedBackupPolicy{RetentionPeriod: retentionPeriod, Frequency: frequency}

if err := adminClient.UpdateTableWithAutomatedBackupPolicy(ctx, myTableName, automatedBackupPolicy); err != nil {
t.Fatalf("Update table from config: %v", err)
}

table, err = adminClient.TableInfo(ctx, myTableName)
if err != nil {
t.Fatalf("Getting table info: %v", err)
}

tableAbp = table.AutomatedBackupConfig.(*TableAutomatedBackupPolicy)

if table.AutomatedBackupConfig == nil {
t.Errorf("Expect Automated Backup Policy to be enabled for table: %v has info: %v", myTableName, table)
}
for _, testcase := range []struct {
desc string
bkpPolicy TableAutomatedBackupPolicy
}{
{
desc: "Update automated backup policy, just frequency",
bkpPolicy: TableAutomatedBackupPolicy{Frequency: frequency},
},
{
desc: "Update automated backup policy, just retention period",
bkpPolicy: TableAutomatedBackupPolicy{RetentionPeriod: retentionPeriod},
},
{
desc: "Update automated backup policy, all fields",
bkpPolicy: TableAutomatedBackupPolicy{RetentionPeriod: retentionPeriod, Frequency: frequency},
},
} {
if gotErr := adminClient.UpdateTableWithAutomatedBackupPolicy(ctx, myTableName, testcase.bkpPolicy); err != nil {
t.Fatalf("%v: Update table from config: %v", testcase.desc, gotErr)
}

if int64(tableAbp.Frequency.(time.Duration).Seconds()) != int64(automatedBackupPolicy.Frequency.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
}
gotTable, gotErr := adminClient.TableInfo(ctx, myTableName)
if gotErr != nil {
t.Fatalf("%v: Getting table info: %v", testcase.desc, gotErr)
}
if gotTable.AutomatedBackupConfig == nil {
t.Errorf("%v: Expect Automated Backup Policy to be enabled for table: %v has info: %v", testcase.desc, myTableName, gotTable)
}

if int64(tableAbp.RetentionPeriod.(time.Duration).Seconds()) != int64(automatedBackupPolicy.RetentionPeriod.(time.Duration).Seconds()) {
t.Errorf("Expect automated backup policy frequency to be set for table: %v has info: %v", myTableName, table)
gotTableAbp := gotTable.AutomatedBackupConfig.(*TableAutomatedBackupPolicy)
if testcase.bkpPolicy.Frequency != nil && !equalOptionalDuration(gotTableAbp.Frequency, testcase.bkpPolicy.Frequency) {
t.Errorf("%v: Expect automated backup policy frequency to be set for table: %v has info: %v", testcase.desc, myTableName, table)
}
if testcase.bkpPolicy.RetentionPeriod != nil && !equalOptionalDuration(gotTableAbp.RetentionPeriod, testcase.bkpPolicy.RetentionPeriod) {
t.Errorf("%v: Expect automated backup policy retention period to be set for table: %v has info: %v", testcase.desc, myTableName, table)
}
}

// Disable automated backups
// Test disable automated backups
if err := adminClient.UpdateTableDisableAutomatedBackupPolicy(ctx, myTableName); err != nil {
t.Fatalf("Update table from config: %v", err)
}
Expand Down

0 comments on commit 9393cdf

Please sign in to comment.