skipcheck

TM1 Testcontainers · Docs · Containers

Tm1EngineContainer

using Skipcheck.Tm1.Testcontainers;

A TM1 v12 engine (Planning Analytics 3.x) in Docker, for tests: the tm1-engine image with TM1 from a Tm1EngineSource, and the MongoDB it keeps its metadata in, on a network of their own. For TM1 11, use Tm1ServerContainer.

One engine holds instances, each holding databases. Most tests use the one instance, tm1, and a database of their own: RequiresTm1EngineFactAttribute gives each test class one. A container you make yourself has the databases you name with WithDatabase, or one called tm1:

await using var engine = new Tm1EngineContainer(source).WithDatabase("planning").WithUser("analyst", "Analysts");
await engine.StartAsync();
var planning = engine.Database("planning");
// http://localhost:54321/tm1/api/v1/Databases('planning')/
var url = planning.RestUrl;
var analyst = planning.Login("analyst");
// ...

TM1 v12 has no passwords of its own. A test logs in through the instance's application (Tm1Login), which this container creates and keeps the credentials of.

Starting takes about 40 seconds, and a database about 10 more to create (CreateDatabaseAsync). The image exists for x86-64 only, as TM1 does; on Apple Silicon it runs under Rosetta.

Members

DefaultImage staticThe tm1-engine image, without its tag (the library's version, or edge).
EngineImage staticThe setting that replaces DefaultImage (not used with Tm1EngineSource.PrivateImage), e.g.
DatastoreImage staticThe MongoDB image the engine's datastore runs.
Port staticThe port of TM1's gateway inside the container.
DefaultInstance staticThe instance databases are in unless one is named.
DefaultDatabase staticThe database a container has when none is named.
DatastoreAlias staticThe datastore's name on the engine's network.
CacheVolume staticThe volume package installs are cached in, shared by every engine container on this Docker host (and with the Java, Python and TypeScript tests).
OAuth2Realm staticThe Keycloak realm an engine with WithOAuth2 takes tokens from.
OAuth2Client staticThe client of that realm a test asks for tokens as, and the audience its tokens carry.
PassThroughAlias staticThe name a TM1 server has on the engine's network when logins are passed to it (WithPassThrough).
Tm1EngineContainer constructorTM1 from source, in the default image for it.
Tm1EngineContainer constructorTM1 from source, in an image of your own, such as a locally built one.
SharedAsync staticThe engine most tests share.
WithDatabaseAdds a database, in the instance tm1.
WithDatabaseAdds a database in a named instance, which is created with an application of its own.
WithUserAdds a user to every database, in exactly these groups (created if missing).
WithOAuth2Takes OAuth 2.0 logins.
WithPassThroughPasses logins on to a TM1 11 server ("pass-through").
PassThrough propertyThe TM1 server logins are passed to, or null without WithPassThrough.
WithLoginGives a user a password to log in with through OAuth 2.0 (WithOAuth2).
Keycloak propertyThe Keycloak the engine takes OAuth 2.0 tokens from, or null without WithOAuth2.
Source propertyWhere TM1 comes from.
Datastore propertyThe MongoDB the engine keeps its metadata in, started and stopped with it.
Url propertyThe gateway as seen from the host, ending in /, e.g.
ManagementUrl propertyThe management API, which lists and creates instances, applications and databases, ending in /, e.g.
RootClientId propertyThe client ID of the engine's root credentials, for the management API.
RootClientSecret propertyThe client secret of the engine's root credentials.
Tm1Version propertyThe version of TM1 the engine runs.
DatabasesAsyncThe databases the engine has now, in every instance, read from the engine again.
DatabaseA database of the instance tm1.
DatabaseA database of a named instance.
CreateDatabaseAsyncCreates a database in the instance tm1 of the running engine, with the users of WithUser.
CreateDatabaseAsyncCreates a database in a named instance of the running engine, which must exist.
DropDatabaseAsyncDeletes a database of the running engine, with everything in it.
DockerImageName propertyThe image it runs.
EnvMap propertyIts environment variables, so far.
CommandParts propertyThe command it runs, if not the image's.
ExposedPorts propertyThe ports it exposes on the Docker host.
Network propertyThe network it joins, or null for Docker's default bridge.
NetworkAliases propertyIts names on Network.
Dependencies propertyWhat starts before it.
Container propertyThe started container.
IsRunning property
Host propertyThe host its ports are mapped on.
ContainerId propertyThe Docker container's ID.
GetMappedPortThe host port port is mapped to.
WithEnvSets an environment variable.
WithEnvSets environment variables.
WithCommandRuns commandParts in place of the image's command.
WithExposedPortsExposes ports on the Docker host, each on a free port.
WithNetworkJoins network (created when the first container on it starts).
WithNetworkAliasesAdds names it's reached by on Network.
DependsOnStarts startables before it.
WithExtraHostAdds an /etc/hosts entry.
WithReuseKeeps the container for later runs, when reuse is switched on as Testcontainers for Java has it (testcontainers.reuse.enable=true in ~/.testcontainers.properties, or TESTCONTAINERS_REUSE_ENABLE=true); otherwise it's removed after the run.
WithCopyFileToContainerCopies a host file or folder into the container before it starts.
WithStartupTimeoutHow long to wait for it to be ready.
StartAsync
StopAsync
DisposeAsyncStops it.
GetLogsAsyncIts log, so far.

DefaultImage

static
const string DefaultImage

The tm1-engine image, without its tag (the library's version, or edge).

EngineImage

static
const string EngineImage

The setting that replaces DefaultImage (not used with Tm1EngineSource.PrivateImage), e.g. tm1-engine:local for a local build.

DatastoreImage

static
const string DatastoreImage

The MongoDB image the engine's datastore runs.

Port

static
const int Port

The port of TM1's gateway inside the container: the REST API, logins and management.

DefaultInstance

static
const string DefaultInstance

The instance databases are in unless one is named.

DefaultDatabase

static
const string DefaultDatabase

The database a container has when none is named.

DatastoreAlias

static
const string DatastoreAlias

The datastore's name on the engine's network.

CacheVolume

static
const string CacheVolume

The volume package installs are cached in, shared by every engine container on this Docker host (and with the Java, Python and TypeScript tests). Only the first container to see a package installs it. Remove the volume to clear it.

OAuth2Realm

static
const string OAuth2Realm

The Keycloak realm an engine with WithOAuth2 takes tokens from.

OAuth2Client

static
const string OAuth2Client

The client of that realm a test asks for tokens as, and the audience its tokens carry.

PassThroughAlias

static
const string PassThroughAlias

The name a TM1 server has on the engine's network when logins are passed to it (WithPassThrough).

Tm1EngineContainer

constructor
Tm1EngineContainer(Tm1EngineSource source)

TM1 from source, in the default image for it: a private image as it is; otherwise TM1_ENGINE_IMAGE if set; otherwise DefaultImage tagged with this library's version (edge for a development build).

Tm1EngineContainer

constructor
Tm1EngineContainer(string image, Tm1EngineSource source)

TM1 from source, in an image of your own, such as a locally built one.

SharedAsync

static
Task<Tm1EngineContainer> SharedAsync(CancellationToken cancellationToken = default)

The engine most tests share: one per test run, started on first use with the source Tm1EngineSource.Discover finds, and removed when the run ends. It has one database, tm1; Xunit.Tm1DatabaseFixture{TTestClass} creates one more for each test class. It isn't marked reusable: an engine and its datastore belong together, and a kept engine would keep every database tests left in it.

Throws

InvalidOperationException
No source is configured.

WithDatabase

Tm1EngineContainer WithDatabase(string name)

Adds a database, in the instance tm1. Without any, the container has one called tm1.

Parameters

name
The database's name: at least three letters, digits, _ or -. Sales and sales are two databases.

Throws

ArgumentException
The image can't take the name.

WithDatabase

Tm1EngineContainer WithDatabase(string instance, string name)

Adds a database in a named instance, which is created with an application of its own. An instance's users, sessions and applications are separate from another's.

Throws

ArgumentException
The image can't take a name.

WithUser

Tm1EngineContainer WithUser(string name, params string[] groups)

Adds a user to every database, in exactly these groups (created if missing). A new database has one user already: admin, in the ADMIN group.

Throws

ArgumentException
The image can't take a name, or the user is already added.

WithOAuth2

Tm1EngineContainer WithOAuth2(KeycloakContainer keycloak)

Takes OAuth 2.0 logins: tokens from a Keycloak, which gets a realm (tm1) with the users given a password by WithLogin. A test then logs in with Tm1Database.OAuth2Login. Logins through the engine's application (Tm1Database.Login) keep working beside it.

The Keycloak joins the engine's network, unless it has one already, which the engine then joins. It's yours to stop: the engine only starts it first. An engine has one Keycloak, and a Keycloak one engine: the same Keycloak again changes nothing.

// Starting the engine starts Keycloak first; the Keycloak is yours to stop
await using var keycloak = new KeycloakContainer();
await using var engine = new Tm1EngineContainer(source).WithUser("analyst", "Analysts").WithLogin("analyst", "analystpw").WithOAuth2(keycloak);
await engine.StartAsync();
var db = engine.Database("tm1");
// A token from Keycloak, with the analyst's password, sent to TM1 as Bearer
using var request = new HttpRequestMessage(HttpMethod.Get, db.RestUrl + "Cubes?$select=Name");
await db.OAuth2Login("analyst").AuthorizeWithTokenAsync(request);

Throws

InvalidOperationException
The engine has another Keycloak, or passes logins on (WithPassThrough); or this Keycloak is already running, or is another engine's.

WithPassThrough

Tm1EngineContainer WithPassThrough(Tm1ServerContainer server)

Passes logins on to a TM1 11 server ("pass-through"): TM1 v12 asks it who a login is, so that server's users log in here with what they have there. A test logs in with Tm1Database.PassThroughLogin. Logins through the engine's application (Tm1Database.Login) keep working beside it.

A user has to be in the database too: add each with WithUser. admin always is. A server in Cognos (CAM) security (Tm1ServerContainer.WithCamSecurity) answers a login with the user's CAMID. The engine's users are then Cognos's test users, each made under its CAMID, and the server's administrators are administrators here too. TM1 v12 learns no groups from Cognos: a user has the groups WithUser gives. Not together with WithOAuth2: with both, TM1 takes tokens and refuses the rest.

The server joins the engine's network, unless it has one already, which the engine and its datastore then join. Starting the engine starts the server first. Give it one that isn't started: it gets the name the engine reaches it by, PassThroughAlias, so one network holds one such server. The server is yours to stop.

// Starting the engine starts the TM1 11 server first; the server is yours to stop
await using var server = new Tm1ServerContainer(tm1).WithSeed(Tm1Seed.Sdata);
await using var engine = new Tm1EngineContainer(source).WithPassThrough(server);
await engine.StartAsync();
var db = engine.Database("tm1");
// The sample database's admin / apple, which TM1 v12 asks the server about
using var request = new HttpRequestMessage(HttpMethod.Get, db.RestUrl + "Cubes?$select=Name");
await db.PassThroughLogin().AuthorizeAsync(request);

Parameters

server
The TM1 server that checks logins.

Returns

This container.

Throws

InvalidOperationException
The engine passes logins to another server already, or takes OAuth 2.0 tokens; or the server is already running, or checks logins for another engine.

PassThrough

property
Tm1ServerContainer? PassThrough

The TM1 server logins are passed to, or null without WithPassThrough.

WithLogin

Tm1EngineContainer WithLogin(string user, string password)

Gives a user a password to log in with through OAuth 2.0 (WithOAuth2). TM1 v12 keeps no passwords itself: it's the Keycloak's. The user is one added with WithUser, or admin.

Throws

ArgumentException
The name isn't a user's, or the password is empty.

Keycloak

property
KeycloakContainer? Keycloak

The Keycloak the engine takes OAuth 2.0 tokens from, or null without WithOAuth2.

Source

property
Tm1EngineSource Source

Where TM1 comes from.

Datastore

property
DatastoreContainer Datastore

The MongoDB the engine keeps its metadata in, started and stopped with it.

Url

property
string Url

The gateway as seen from the host, ending in /, e.g. http://localhost:54321/.

ManagementUrl

property
string ManagementUrl

The management API, which lists and creates instances, applications and databases, ending in /, e.g. http://localhost:54321/manage/v1/. It takes the root credentials as Basic authentication (RootClientId, RootClientSecret).

RootClientId

property
string RootClientId

The client ID of the engine's root credentials, for the management API.

Throws

InvalidOperationException
It isn't started.

RootClientSecret

property
string RootClientSecret

The client secret of the engine's root credentials.

Throws

InvalidOperationException
It isn't started.

Tm1Version

property
Tm1Version Tm1Version

The version of TM1 the engine runs. Only available once started.

Throws

InvalidOperationException
It isn't started.

DatabasesAsync

Task<IReadOnlyList<Tm1Database>> DatabasesAsync(CancellationToken cancellationToken = default)

The databases the engine has now, in every instance, read from the engine again.

Throws

InvalidOperationException
It isn't started.

Database

Tm1Database Database(string name)

A database of the instance tm1.

Throws

ArgumentException
There's no such database.

Database

Tm1Database Database(string instance, string name)

A database of a named instance: of those the engine had when it started, or was last asked (DatabasesAsync, CreateDatabaseAsync, DropDatabaseAsync); before it starts, of those added with WithDatabase.

Throws

ArgumentException
There's no such database.

CreateDatabaseAsync

Task<Tm1Database> CreateDatabaseAsync(string name, CancellationToken cancellationToken = default)

Creates a database in the instance tm1 of the running engine, with the users of WithUser. Takes about 10 seconds.

Throws

InvalidOperationException
It isn't started, or TM1 refuses.

CreateDatabaseAsync

Task<Tm1Database> CreateDatabaseAsync(string instance, string name, CancellationToken cancellationToken = default)

Creates a database in a named instance of the running engine, which must exist. If that fails, what was made of the database is dropped again.

Throws

InvalidOperationException
It isn't started, or TM1 refuses.

DropDatabaseAsync

Task DropDatabaseAsync(Tm1Database database, CancellationToken cancellationToken = default)

Deletes a database of the running engine, with everything in it. Takes about 7 seconds.

Throws

InvalidOperationException
It isn't started, or TM1 refuses.

DockerImageName

property
string DockerImageName

The image it runs.

EnvMap

property
IReadOnlyDictionary<string, string> EnvMap

Its environment variables, so far.

CommandParts

property
IReadOnlyList<string> CommandParts

The command it runs, if not the image's.

ExposedPorts

property
IReadOnlyList<int> ExposedPorts

The ports it exposes on the Docker host.

Network

property
Network? Network

The network it joins, or null for Docker's default bridge.

NetworkAliases

property
IReadOnlyList<string> NetworkAliases

Its names on Network.

Dependencies

property
IReadOnlyCollection<IStartable> Dependencies

What starts before it.

Container

property
IContainer Container

The started container.

Throws

InvalidOperationException
It isn't started.

IsRunning

property
bool IsRunning

Host

property
string Host

The host its ports are mapped on.

ContainerId

property
string ContainerId

The Docker container's ID.

GetMappedPort

int GetMappedPort(int port)

The host port port is mapped to.

WithEnv

TSelf WithEnv(string name, string value)

Sets an environment variable.

WithEnv

TSelf WithEnv(IReadOnlyDictionary<string, string> variables)

Sets environment variables.

WithCommand

TSelf WithCommand(params string[] commandParts)

Runs commandParts in place of the image's command.

WithExposedPorts

TSelf WithExposedPorts(params int[] exposedPorts)

Exposes ports on the Docker host, each on a free port.

WithNetwork

TSelf WithNetwork(Network? network)

Joins network (created when the first container on it starts).

WithNetworkAliases

TSelf WithNetworkAliases(params string[] networkAliases)

Adds names it's reached by on Network.

DependsOn

TSelf DependsOn(params IStartable[] startables)

Starts startables before it.

WithExtraHost

TSelf WithExtraHost(string host, string ipAddress)

Adds an /etc/hosts entry.

WithReuse

TSelf WithReuse(bool reusable)

Keeps the container for later runs, when reuse is switched on as Testcontainers for Java has it (testcontainers.reuse.enable=true in ~/.testcontainers.properties, or TESTCONTAINERS_REUSE_ENABLE=true); otherwise it's removed after the run.

WithCopyFileToContainer

TSelf WithCopyFileToContainer(string hostPath, string containerPath)

Copies a host file or folder into the container before it starts.

WithStartupTimeout

TSelf WithStartupTimeout(TimeSpan timeout)

How long to wait for it to be ready.

StartAsync

Task StartAsync(CancellationToken cancellationToken = default)

StopAsync

Task StopAsync(CancellationToken cancellationToken = default)

DisposeAsync

ValueTask DisposeAsync()

Stops it.

GetLogsAsync

Task<string> GetLogsAsync(CancellationToken cancellationToken = default)

Its log, so far.