Skip to content

Google Cloud Spanner

Google Cloud Spanner is a fully managed, horizontally scalable relational database. The module runs the Cloud Spanner emulator, which keeps its data in memory.

Add the following dependency to your project file:

NuGet
1
dotnet add package Testcontainers.Spanner

You can start a Spanner container instance from any .NET application. This example uses the default configuration:

1
private readonly SpannerContainer _spannerContainer = new SpannerBuilder(TestSession.GetImageFromDockerfile()).Build();

This example uses xUnit.net's IAsyncLifetime interface to manage the lifecycle of the container. The container is started in the InitializeAsync method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the DisposeAsync method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public async ValueTask InitializeAsync()
{
    await _spannerContainer.StartAsync()
        .ConfigureAwait(false);
}

public ValueTask DisposeAsync()
{
    return _spannerContainer.DisposeAsync();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task GetDatabaseDdlReturnsCreateTable()
{
    // Given
    const string projectId = "test-project";

    const string instanceId = "test-instance";

    const string databaseId = "test-database";

    const string createTable = "CREATE TABLE Users (\n  Id INT64 NOT NULL,\n  Name STRING(MAX),\n) PRIMARY KEY(Id)";

    var instanceName = new InstanceName(projectId, instanceId);

    var databaseName = new DatabaseName(projectId, instanceId, databaseId);

    var instance = new Instance();
    instance.Config = new InstanceConfigName(projectId, "emulator-config").ToString();
    instance.DisplayName = instanceId;
    instance.NodeCount = 1;

    var createDatabaseRequest = new CreateDatabaseRequest();
    createDatabaseRequest.ParentAsInstanceName = instanceName;
    createDatabaseRequest.CreateStatement = "CREATE DATABASE `" + databaseId + "`";
    createDatabaseRequest.ExtraStatements.Add(createTable);

    var instanceAdminClientBuilder = new InstanceAdminClientBuilder();
    instanceAdminClientBuilder.Endpoint = _spannerContainer.GetEmulatorGrpcEndpoint();
    instanceAdminClientBuilder.ChannelCredentials = ChannelCredentials.Insecure;

    var databaseAdminClientBuilder = new DatabaseAdminClientBuilder();
    databaseAdminClientBuilder.Endpoint = _spannerContainer.GetEmulatorGrpcEndpoint();
    databaseAdminClientBuilder.ChannelCredentials = ChannelCredentials.Insecure;

    var instanceAdminClient = await instanceAdminClientBuilder.BuildAsync(TestContext.Current.CancellationToken)
        .ConfigureAwait(true);

    var databaseAdminClient = await databaseAdminClientBuilder.BuildAsync(TestContext.Current.CancellationToken)
        .ConfigureAwait(true);

    var createInstanceOperation = await instanceAdminClient.CreateInstanceAsync(new ProjectName(projectId), instanceId, instance, TestContext.Current.CancellationToken)
        .ConfigureAwait(true);

    _ = await createInstanceOperation.PollUntilCompletedAsync()
        .ConfigureAwait(true);

    var createDatabaseOperation = await databaseAdminClient.CreateDatabaseAsync(createDatabaseRequest, TestContext.Current.CancellationToken)
        .ConfigureAwait(true);

    _ = await createDatabaseOperation.PollUntilCompletedAsync()
        .ConfigureAwait(true);

    // When
    var databaseDdl = await databaseAdminClient.GetDatabaseDdlAsync(databaseName, TestContext.Current.CancellationToken)
        .ConfigureAwait(true);

    // Then
    Assert.Equal(createTable, Assert.Single(databaseDdl.Statements));
    Assert.Equal(_spannerContainer.GetEmulatorGrpcEndpoint(), _spannerContainer.GetConnectionString());
}

The emulator starts without an instance or a database. The test example creates an instance with the emulator-config instance configuration and a database with a table, then reads the table definition back. Use GetEmulatorGrpcEndpoint() as the endpoint of the Spanner client and ChannelCredentials.Insecure as the channel credentials. GetEmulatorHttpEndpoint() returns the base address of the REST API.

The test example uses the following NuGet dependencies:

1
2
3
4
5
6
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit.v3"/>
<PackageReference Include="Google.Cloud.Spanner.Admin.Database.V1"/>
<PackageReference Include="Google.Cloud.Spanner.Admin.Instance.V1"/>

To execute the tests, use the command dotnet test from a terminal.

Tip

For the complete source code of this example and additional information, please refer to our test projects.

Default configuration

The builder applies the following default configuration:

  • Port: 9010 (see SpannerBuilder.SpannerGrpcPort)
  • Port: 9020 (see SpannerBuilder.SpannerHttpPort)
  • Wait strategy: Waits until a message matching .*Cloud Spanner emulator running\..* is logged.