skipcheck

TM1 Testcontainers · Docs · Containers

Tm1EngineContainer

from skipcheck_tm1_testcontainers import Tm1EngineContainer

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: RequiresTm1Engine gives each test class one. A container you make yourself has the databases you name with with_database, or one called tm1:

# Entering the block starts the engine, and leaving it stops it
with Tm1EngineContainer(source).with_database("planning").with_user("analyst", "Analysts") as engine:
    planning: Tm1Database = engine.database("planning")
    # http://localhost:54321/tm1/api/v1/Databases('planning')/
    url: str = planning.rest_url
    analyst: Tm1Login = 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 (create_database). The image exists for x86-64 only, as TM1 does; on Apple Silicon it runs under Rosetta.

Members

DEFAULT_IMAGEThe tm1-engine image, without a tag.
ENGINE_IMAGEOverrides the tm1-engine image (not used with PrivateImage), e.g.
DATASTORE_IMAGEThe MongoDB image the engine's datastore runs.
PORTThe port of TM1's gateway inside the container.
DEFAULT_INSTANCEThe instance databases are in unless one is named.
DEFAULT_DATABASEThe database a container has when none is named.
DATASTORE_ALIASThe datastore's name on the engine's network.
CACHE_VOLUMEThe volume package installs are cached in, shared by every engine container on this Docker host.
PASS_THROUGH_ALIASThe name a TM1 server has on the engine's network when logins are passed to it (with_pass_through).
OAUTH2_REALMThe Keycloak realm an engine with with_oauth2 takes tokens from.
OAUTH2_CLIENTThe client of that realm a test asks for tokens as, and the audience its tokens carry.
__init__ constructorA container that gets TM1 from source, with the default image for it.
with_networkPuts the engine, and its datastore with it, on a network.
shared staticThe engine most tests share.
with_databaseAdds a database.
with_userAdds a user to every database, in exactly these groups (created if missing).
with_oauth2Takes OAuth 2.0 logins.
with_pass_throughPasses logins on to a TM1 11 server ("pass-through").
pass_through propertyThe TM1 server logins are passed to, or None without with_pass_through.
with_loginGives a user a password to log in with through OAuth 2.0 (with_oauth2).
keycloak propertyThe Keycloak the engine takes OAuth 2.0 tokens from, or None without with_oauth2.
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, e.g.
management_url propertyThe management API, which lists and creates instances, applications and databases, e.g.
root_client_id propertyThe client ID of the engine's root credentials, for the management API.
root_client_secret propertyThe client secret of the engine's root credentials.
tm1_version propertyThe version of TM1 the engine runs.
databasesThe databases the engine has now, in every instance.
databaseA database.
create_databaseCreates a database in the running engine, with the users of with_user.
drop_databaseDeletes a database of the running engine, with everything in it.
startStarts the datastore, then the engine, and waits until TM1 is set up.
configureCalled before the container is created.
container_is_startedCalled once the gateway answers.
stopStops the engine, then its datastore, and removes the network if the engine made it.

DEFAULT_IMAGE

DEFAULT_IMAGE: ClassVar[str]

The tm1-engine image, without a tag.

ENGINE_IMAGE

ENGINE_IMAGE: ClassVar[str]

Overrides the tm1-engine image (not used with PrivateImage), e.g. tm1-engine:local for a local build. Read as a system property, then an environment variable.

DATASTORE_IMAGE

DATASTORE_IMAGE: ClassVar[str]

The MongoDB image the engine's datastore runs.

PORT

PORT: ClassVar[int]

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

DEFAULT_INSTANCE

DEFAULT_INSTANCE: ClassVar[str]

The instance databases are in unless one is named.

DEFAULT_DATABASE

DEFAULT_DATABASE: ClassVar[str]

The database a container has when none is named.

DATASTORE_ALIAS

DATASTORE_ALIAS: ClassVar[str]

The datastore's name on the engine's network. It's the same for every engine, so give each engine a network of its own, as it has unless you set one: two engines on one network would each find either's datastore under this name.

CACHE_VOLUME

CACHE_VOLUME: ClassVar[str]

The volume package installs are cached in, shared by every engine container on this Docker host. Only the first container to see a package installs it. Remove the volume to clear it.

PASS_THROUGH_ALIAS

PASS_THROUGH_ALIAS: ClassVar[str]

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

OAUTH2_REALM

OAUTH2_REALM: ClassVar[str]

The Keycloak realm an engine with with_oauth2 takes tokens from.

OAUTH2_CLIENT

OAUTH2_CLIENT: ClassVar[str]

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

__init__

constructor
__init__(source: Tm1EngineSource, image: str | None = None) -> None

A container that gets TM1 from source, with the default image for it.

with_network

with_network(network: Any) -> Self

Puts the engine, and its datastore with it, on a network. They have one of their own unless this is called; a network that's given is never removed by the engine. A RuntimeError if the engine takes tokens from a Keycloak (with_oauth2), or passes logins to a TM1 server (with_pass_through), that's on another.

shared

static
shared() -> Tm1EngineContainer

The engine most tests share: one per test process, started on first use with the source Tm1EngineSource.discover finds, and stopped when the session or the process ends. It has one database, tm1; the tm1_database fixture creates one more for each test module.

It isn't marked reusable: an engine and its datastore belong together, and a kept engine would keep every database tests left in it. A RuntimeError if no source is configured.

with_database

with_database(instance_or_name: str, name: str | None = None) -> Self

Adds a database: with_database(name) in the instance tm1, or with_database(instance, name) 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. Without any, the container has one database called tm1. A database's name is at least three letters, digits, _ or -, and an instance's at least one; Sales and sales are two databases.

with_user

with_user(name: str, *groups: str) -> Self

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.

with_oauth2

with_oauth2(keycloak: KeycloakContainer) -> Self

Takes OAuth 2.0 logins: tokens from a Keycloak, which gets a realm (tm1) with the users given a password by with_login. A test then logs in with Tm1Database.oauth2_login. 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, so give it one that isn't started yet, and stop it after the engine.

# A Keycloak that isn't started: the engine starts it first, with the users' passwords
keycloak = KeycloakContainer()
engine = Tm1EngineContainer(source).with_user("analyst", "Analysts").with_login("analyst", "analystpw").with_oauth2(keycloak)
# Entering the block starts Keycloak, then the engine; the Keycloak is yours to close
with closing(keycloak), engine:
    db: Tm1Database = engine.database("tm1")
    # A token from Keycloak, with the analyst's password, sent to TM1 as Bearer
    headers = db.oauth2_login("analyst").authorize_with_token({})
    response = httpx2.get(db.rest_url + "Cubes?$select=Name", headers=headers)

with_pass_through

with_pass_through(server: Tm1ServerContainer) -> Self

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.pass_through_login. Logins through the engine's application (Tm1Database.login) keep working beside it.

* A user has to be in the database too: add each with with_user. admin always is. * **A server in Cognos (CAM) security** (Tm1ServerContainer.with_cam_security) 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 with_user gives. * Not together with with_oauth2: 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, PASS_THROUGH_ALIAS, so one network holds one such server. It's yours to stop, after the engine.

# A TM1 11 server that isn't started: the engine starts it first
server = Tm1ServerContainer(tm1).with_seed(Tm1Seed.SDATA)
engine = Tm1EngineContainer(source).with_pass_through(server)
# Entering the block starts the server, then the engine; the server is yours to close
with closing(server), engine:
    db: Tm1Database = engine.database("tm1")
    # The sample database's admin / apple, which TM1 v12 asks the server about
    headers = db.pass_through_login().authorize({})
    response = httpx2.get(db.rest_url + "Cubes?$select=Name", headers=headers)

pass_through

property
pass_through: Tm1ServerContainer | None

The TM1 server logins are passed to, or None without with_pass_through.

with_login

with_login(user: str, password: str) -> Self

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

keycloak

property
keycloak: KeycloakContainer | None

The Keycloak the engine takes OAuth 2.0 tokens from, or None without with_oauth2.

source

property
source: Tm1EngineSource

Where TM1 comes from.

datastore

property
datastore: GenericContainer

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

url

property
url: str

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

management_url

property
management_url: str

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

root_client_id

property
root_client_id: str

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

root_client_secret

property
root_client_secret: str

The client secret of the engine's root credentials.

tm1_version

property
tm1_version: Tm1Version

The version of TM1 the engine runs. Only available once started: a RuntimeError before.

databases

databases() -> list[Tm1Database]

The databases the engine has now, in every instance.

database

database(instance_or_name: str, name: str | None = None) -> Tm1Database

A database: database(name) of the instance tm1, or database(instance, name) of a named instance. A ValueError if there's no such database.

create_database

create_database(instance_or_name: str, name: str | None = None) -> Tm1Database

Creates a database in the running engine, with the users of with_user: create_database(name) in the instance tm1, or create_database(instance, name) in a named instance, which must exist. Takes about 10 seconds.

drop_database

drop_database(database: Tm1Database) -> None

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

start

start() -> Self

Starts the datastore, then the engine, and waits until TM1 is set up.

configure

configure() -> None

Called before the container is created: passes the databases and the users to the image.

container_is_started

container_is_started() -> None

Called once the gateway answers: waits for the image to set TM1 up, and reads its version.

stop

stop(force: bool = True, delete_volume: bool = True) -> None

Stops the engine, then its datastore, and removes the network if the engine made it. A Keycloak or a TM1 server of yours that's still on that network keeps it until it's stopped, and removes it then.