Skip to content

Commit

Permalink
fix: Loosen Spanner tests to make less admin calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed Apr 20, 2022
1 parent 5cbc2c2 commit 2393af7
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public CancelBackupOperationTest(SpannerFixture spannerFixture)
public void TestCancelBackupOperation()
{
CancelBackupOperationSample cancelBackupOperationSample = new CancelBackupOperationSample();
ListBackupsSample listBackupsSample = new ListBackupsSample();
cancelBackupOperationSample.CancelBackupOperation(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.DatabaseId, _spannerFixture.ToBeCancelledBackupId);
var backups = listBackupsSample.ListBackups(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.DatabaseId, _spannerFixture.ToBeCancelledBackupId);
Assert.DoesNotContain(backups, b => b.BackupName.BackupId == _spannerFixture.ToBeCancelledBackupId);
var operation = cancelBackupOperationSample.CancelBackupOperation(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.DatabaseId, _spannerFixture.ToBeCancelledBackupId);
Assert.True(operation.IsCompleted);
}
}
7 changes: 1 addition & 6 deletions spanner/api/Spanner.Samples.Tests/CreateBackupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ public CreateBackupTest(SpannerFixture spannerFixture)
[Fact]
public void TestCreateBackup()
{
string connectionString = $"Data Source=projects/{_spannerFixture.ProjectId}/instances/{_spannerFixture.InstanceId}/databases/{_spannerFixture.BackupDatabaseId}";
using var connection = new SpannerConnection(connectionString);
connection.Open();
var versionTime = (DateTime)connection.CreateSelectCommand("SELECT CURRENT_TIMESTAMP").ExecuteScalar();

CreateBackupSample createBackupSample = new CreateBackupSample();
// Backup already exists since it was created in the test setup so it should throw an exception.
var exception = Assert.Throws<RpcException>(()
=> createBackupSample.CreateBackup(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId, _spannerFixture.BackupId, versionTime));
=> createBackupSample.CreateBackup(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId, _spannerFixture.BackupId, DateTime.UtcNow.AddMilliseconds(-500)));
Assert.Equal(StatusCode.AlreadyExists, exception.StatusCode);
}
}
4 changes: 2 additions & 2 deletions spanner/api/Spanner.Samples.Tests/GetDatabaseDdlAsyncTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ AlbumTitle STRING(MAX),
await _spannerFixture.RunWithTemporaryDatabaseAsync(_spannerFixture.InstanceIdWithMultiRegion, databaseId, async databaseId =>
{
var sample = new GetDatabaseDdlAsyncSample();
var statements =
var response =
await sample.GetDatabaseDdlAsync(_spannerFixture.ProjectId, _spannerFixture.InstanceIdWithMultiRegion, databaseId);
Assert.Collection(statements,
Assert.Collection(response.Statements,
// Only check the start of the statement, as there is no guarantee on exactly
// how Cloud Spanner will format the returned SQL string.
statement => Assert.StartsWith($"ALTER DATABASE `{databaseId}` SET OPTIONS (\n default_leader = 'us-central1'", statement),
Expand Down
8 changes: 4 additions & 4 deletions spanner/api/Spanner.Samples.Tests/ListBackupOperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;
using Xunit;

[Collection(nameof(SpannerFixture))]
Expand All @@ -25,11 +24,12 @@ public ListBackupOperationsTest(SpannerFixture spannerFixture)
_spannerFixture = spannerFixture;
}

[Fact(Skip = "https://github.com/GoogleCloudPlatform/dotnet-docs-samples/issues/1583")]
[Fact]
public void TestListBackupOperations()
{
ListBackupOperationsSample getBackupOperationsSample = new ListBackupOperationsSample();
var operations = getBackupOperationsSample.ListBackupOperations(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId).ToList();
Assert.True(operations.Count >= 0);
var operations = getBackupOperationsSample.ListBackupOperations(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId);
// We iterated over operation on the sample itself, let's not do that again.
Assert.NotNull(operations);
}
}
3 changes: 2 additions & 1 deletion spanner/api/Spanner.Samples.Tests/ListBackupsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;
using Xunit;

[Collection(nameof(SpannerFixture))]
Expand All @@ -29,6 +30,6 @@ public void TestListBackups()
{
ListBackupsSample getBackupsSample = new ListBackupsSample();
var backups = getBackupsSample.ListBackups(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId, _spannerFixture.BackupId);
Assert.Contains(backups, b => b.BackupName.BackupId == _spannerFixture.BackupId);
Assert.True(backups.Any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ListDatabaseOperationsTest(SpannerFixture spannerFixture)
public void TestListDatabaseOperations()
{
ListDatabaseOperationsSample sample = new ListDatabaseOperationsSample();
var operations = sample.ListDatabaseOperations(_spannerFixture.ProjectId, _spannerFixture.InstanceId).ToList();
Assert.True(operations.Count >= 0);
var operations = sample.ListDatabaseOperations(_spannerFixture.ProjectId, _spannerFixture.InstanceId);
Assert.True(operations.Any());
}
}
3 changes: 2 additions & 1 deletion spanner/api/Spanner.Samples.Tests/ListDatabasesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -32,7 +33,7 @@ await _spannerFixture.RunWithTemporaryDatabaseAsync(databaseId =>
{
var listDatabasesSample = new ListDatabasesSample();
var databases = listDatabasesSample.ListDatabases(_spannerFixture.ProjectId, _spannerFixture.InstanceId);
Assert.Contains(databases, d => d.DatabaseName.DatabaseId == databaseId);
Assert.True(databases.Any());
return Task.CompletedTask;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public void TestListInstanceConfigs()
{
var sample = new ListInstanceConfigsSample();
var configs = sample.ListInstanceConfigs(_spannerFixture.ProjectId);
Assert.NotEmpty(configs);
Assert.True(configs.Any());
}
}
14 changes: 5 additions & 9 deletions spanner/api/Spanner.Samples.Tests/RestoreDatabaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Cloud.Spanner.Common.V1;
using Grpc.Core;
using System.Linq;
using Xunit;

[Collection(nameof(SpannerFixture))]
Expand All @@ -32,17 +32,13 @@ public void TestRestoreDatabase()
RestoreDatabaseSample restoreDatabaseSample = new RestoreDatabaseSample();
try
{
restoreDatabaseSample.RestoreDatabase(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.RestoredDatabaseId, _spannerFixture.BackupId);
var restoreInfo = restoreDatabaseSample.RestoreDatabase(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.RestoredDatabaseId, _spannerFixture.BackupId);
DatabaseName name = DatabaseName.FromProjectInstanceDatabase(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId);
Assert.Equal(name.ToString(), restoreInfo.BackupInfo.SourceDatabase);
}
catch (RpcException ex) when (ex.Status.StatusCode == StatusCode.FailedPrecondition)
{
// Handles maximum number of pending restores which is currently one for the instance.
}
finally
{
var backupDatabaseId = $"projects/{_spannerFixture.ProjectId}/instances/{_spannerFixture.InstanceId}/databases/{_spannerFixture.BackupDatabaseId}";
var databases = _spannerFixture.GetDatabases().Where(c => c.RestoreInfo != null).ToList();
Assert.Contains(databases, d => d.RestoreInfo.BackupInfo.SourceDatabase == backupDatabaseId);
// Handles maximum number of pending restores which is currently one for the instance.
}
}
}
5 changes: 1 addition & 4 deletions spanner/api/Spanner.Samples.Tests/SpannerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,10 @@ private async Task InitializeBackupAsync()
Console.WriteLine($"Database {BackupDatabaseId} already exists.");
}

using var connection = new SpannerConnection(ConnectionString);
await connection.OpenAsync();
var currentTimestamp = (DateTime)connection.CreateSelectCommand("SELECT CURRENT_TIMESTAMP").ExecuteScalar();
try
{
CreateBackupSample createBackupSample = new CreateBackupSample();
createBackupSample.CreateBackup(ProjectId, InstanceId, BackupDatabaseId, BackupId, currentTimestamp);
createBackupSample.CreateBackup(ProjectId, InstanceId, BackupDatabaseId, BackupId, DateTime.UtcNow.AddMilliseconds(-500));
}
catch (RpcException e) when (e.StatusCode == StatusCode.AlreadyExists)
{
Expand Down
6 changes: 2 additions & 4 deletions spanner/api/Spanner.Samples.Tests/UpdateBackupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public UpdateBackupTest(SpannerFixture spannerFixture)
public void TestUpdateBackup()
{
UpdateBackupSample updateBackupSample = new UpdateBackupSample();
ListBackupsSample getBackupsSample = new ListBackupsSample();
var backups = getBackupsSample.ListBackups(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.DatabaseId, _spannerFixture.BackupId).ToList();
var updatedBackup = updateBackupSample.UpdateBackup(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupId);
var backup = backups.FirstOrDefault(c => c.BackupName.BackupId == _spannerFixture.BackupId);
Assert.NotEqual(backup.ExpireTime, updatedBackup.ExpireTime);

Assert.NotNull(updatedBackup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Cloud.Spanner.Admin.Database.V1;
using Google.Cloud.Spanner.Common.V1;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -34,14 +32,10 @@ await _spannerFixture.RunWithTemporaryDatabaseAsync(_spannerFixture.InstanceIdWi
{
var defaultLeader = "us-central1";
var sample = new UpdateDatabaseWithDefaultLeaderAsyncSample();
await sample.UpdateDatabaseWithDefaultLeaderAsync(_spannerFixture.ProjectId,
var operation = await sample.UpdateDatabaseWithDefaultLeaderAsync(_spannerFixture.ProjectId,
_spannerFixture.InstanceIdWithMultiRegion, databaseId, defaultLeader);

var databaseAdminClient = await DatabaseAdminClient.CreateAsync();
var database = await databaseAdminClient.GetDatabaseAsync(
DatabaseName.FormatProjectInstanceDatabase(_spannerFixture.ProjectId,
_spannerFixture.InstanceIdWithMultiRegion, databaseId));
Assert.Equal(defaultLeader, database.DefaultLeader);

Assert.True(operation.IsCompleted);
});
}
}
14 changes: 7 additions & 7 deletions spanner/api/Spanner.Samples/CancelBackupOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class CancelBackupOperationSample
{
public bool CancelBackupOperation(string projectId, string instanceId, string databaseId, string backupId)
public Operation<Backup, CreateBackupMetadata> CancelBackupOperation(string projectId, string instanceId, string databaseId, string backupId)
{
// Create the DatabaseAdminClient instance.
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();
Expand All @@ -46,18 +46,18 @@ public bool CancelBackupOperation(string projectId, string instanceId, string da
Console.WriteLine("Waiting for the operation to finish.");
Operation<Backup, CreateBackupMetadata> completedOperation = operation.PollUntilCompleted();

if (!completedOperation.IsFaulted)
if (completedOperation.IsFaulted)
{
Console.WriteLine("The backup was created before the operation was cancelled. Please delete the backup.");
BackupName backupAsBackupName = BackupName.FromProjectInstanceBackup(projectId, instanceId, backupId);
databaseAdminClient.DeleteBackup(backupAsBackupName);
Console.WriteLine($"Create backup operation cancelled: {operation.Name}");
}
else
{
Console.WriteLine($"Create backup operation cancelled: {operation.Name}");
Console.WriteLine("The backup was created before the operation was cancelled. Backup needs to be deleted.");
BackupName backupAsBackupName = BackupName.FromProjectInstanceBackup(projectId, instanceId, backupId);
databaseAdminClient.DeleteBackup(backupAsBackupName);
}

return completedOperation.IsFaulted;
return completedOperation;
}
}
// [END spanner_cancel_backup_create]
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task CreateTableWithDataTypesAsync(string projectId, string instanc
VenueId INT64 NOT NULL,
VenueName STRING(100),
VenueInfo BYTES(MAX),
Capacity INT64,
Capacity INT64,
AvailableDates ARRAY<DATE>,
LastContactDate DATE,
OutdoorVenue BOOL,
Expand Down
8 changes: 3 additions & 5 deletions spanner/api/Spanner.Samples/GetDatabaseDdlAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@

public class GetDatabaseDdlAsyncSample
{
public async Task<List<string>> GetDatabaseDdlAsync(string projectId, string instanceId, string databaseId)
public async Task<GetDatabaseDdlResponse> GetDatabaseDdlAsync(string projectId, string instanceId, string databaseId)
{
DatabaseAdminClient databaseAdminClient = await DatabaseAdminClient.CreateAsync();
DatabaseName databaseName = DatabaseName.FromProjectInstanceDatabase(projectId, instanceId, databaseId);
var databaseDdl = await databaseAdminClient.GetDatabaseDdlAsync(databaseName);
GetDatabaseDdlResponse databaseDdl = await databaseAdminClient.GetDatabaseDdlAsync(databaseName);

var ddlStatements = new List<string>();
Console.WriteLine($"DDL statements for database {databaseId}:");
foreach (var statement in databaseDdl.Statements)
{
Console.WriteLine(statement);
ddlStatements.Add(statement);
}

return ddlStatements;
return databaseDdl;
}
}
// [END spanner_get_database_ddl]
2 changes: 1 addition & 1 deletion spanner/api/Spanner.Samples/ListBackupOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IEnumerable<Operation> ListBackupOperations(string projectId, string inst
// Create the DatabaseAdminClient instance.
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();

var filter = $"(metadata.database:{databaseId}) AND (metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata)";
var filter = $"(metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata) AND (metadata.database:{databaseId})";
ListBackupOperationsRequest request = new ListBackupOperationsRequest
{
ParentAsInstanceName = InstanceName.FromProjectInstance(projectId, instanceId),
Expand Down
7 changes: 6 additions & 1 deletion spanner/api/Spanner.Samples/ListBackups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Google.Cloud.Spanner.Common.V1;
using System;
using System.Collections.Generic;
using System.Linq;

public class ListBackupsSample
{
Expand Down Expand Up @@ -81,7 +82,11 @@ public IEnumerable<Backup> ListBackups(string projectId, string instanceId, stri

private static void PrintBackups(IEnumerable<Backup> backups)
{
foreach (Backup backup in backups)
// We print the first 5 elements each time for demonstration purposes.
// You can print all backups in the sequence by removing the call to Take(5).
// If the sequence has been returned by a paginated operation it will lazily
// fetch elements in pages as needed.
foreach (Backup backup in backups.Take(5))
{
Console.WriteLine($"Backup Name : {backup.Name}");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
// [START spanner_update_database_with_default_leader]

using Google.Cloud.Spanner.Admin.Database.V1;
using Google.Cloud.Spanner.Data;
using Google.Cloud.Spanner.Common.V1;
using Google.LongRunning;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Threading.Tasks;
using Google.Cloud.Spanner.Common.V1;

public class UpdateDatabaseWithDefaultLeaderAsyncSample
{
public async Task UpdateDatabaseWithDefaultLeaderAsync(string projectId, string instanceId, string databaseId, string defaultLeader)
public async Task<Operation<Empty, UpdateDatabaseDdlMetadata>> UpdateDatabaseWithDefaultLeaderAsync(string projectId, string instanceId, string databaseId, string defaultLeader)
{
DatabaseAdminClient databaseAdminClient = await DatabaseAdminClient.CreateAsync();

var alterDatabaseStatement = @$"ALTER DATABASE `{databaseId}` SET OPTIONS
(default_leader = '{defaultLeader}')";

// Create the UpdateDatabaseDdl request and execute it.
var request = new UpdateDatabaseDdlRequest
{
Expand All @@ -39,13 +40,15 @@ public async Task UpdateDatabaseWithDefaultLeaderAsync(string projectId, string

// Wait until the operation has finished.
Console.WriteLine("Waiting for the operation to finish.");
var completedResponse = await operation.PollUntilCompletedAsync();
Operation<Empty, UpdateDatabaseDdlMetadata> completedResponse = await operation.PollUntilCompletedAsync();
if (completedResponse.IsFaulted)
{
Console.WriteLine($"Error while updating database: {completedResponse.Exception}");
throw completedResponse.Exception;
}

Console.WriteLine("Updated default leader");
return completedResponse;
}
}
// [END spanner_update_database_with_default_leader]

0 comments on commit 2393af7

Please sign in to comment.