Metadata-Version: 2.5
Name: skipcheck-tm1-testcontainers
Version: 0.1.1
Summary: Testcontainers support for running IBM Planning Analytics (TM1) servers in tests
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Requires-Python: >=3.11
Requires-Dist: httpx2>=2.16
Requires-Dist: pytest>=8
Requires-Dist: pyyaml<7,>=6.0
Requires-Dist: skipcheck-tm1
Requires-Dist: testcontainers>=4.15
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# skipcheck-tm1-testcontainers (Python)

Run tests against a real IBM Planning Analytics (TM1) server in Docker, with
[testcontainers-python](https://testcontainers-python.readthedocs.io). The Python port of the
Java [`tm1-testcontainers`](../../java/tm1-testcontainers/README.md) module: the same classes, sources,
settings, images, mounts and messages, named the Python way. Built on the
[`tm1-server`](../../docker/tm1-server) image.

> **No IBM software is included.** You supply your own licensed TM1 Server for Linux, and
> you're responsible for following your IBM license terms. Without it, tests that need TM1
> are skipped.

```sh
pip install -e ./python/tm1-client -e ./python/tm1-testcontainers
```

Requires Python 3.11+, testcontainers 4.15+ and [`skipcheck-tm1`](..).

To run the same parts by hand, outside tests (to develop against, or to look at in a browser), use
[`pa-setup`](../../docker/pa-setup/README.md). It starts the same images from one file, `pa-environment.yaml`.

## Writing a test

Installing the package adds a pytest plugin, so a test needs only the fixture:

```python
def test_lists_cubes(tm1_server):
    url = tm1_server.rest_url  # http://localhost:54321/api/v1/
    user = tm1_server.username  # admin, with no password
    ...
```

`tm1_server`, like Java's `@RequiresTm1`:

- **Skips** the test when Docker isn't running or no TM1 source is configured, so builds
  without IBM software (such as GitHub CI) stay green. A source that's configured but wrong
  fails instead of skipping.
- **Is** `Tm1ServerContainer.shared()`: one server per test process, started on first use and
  shared by every test, removed when the session ends.

`@pytest.mark.requires_tm1` skips the same way without the fixture. `requires_cognos`,
`requires_tm1_web` and `requires_paw`, with the fixtures `cognos` (the configured
`CognosSource`), `tm1_web` and `paw`, are Java's `@RequiresCognos`, `@RequiresTm1Web` and
`@RequiresPaw`.

For a container of your own:

```python
from skipcheck_tm1 import Tm1Version
from skipcheck_tm1_testcontainers import Tm1Seed, Tm1ServerContainer, Tm1Source

with Tm1ServerContainer(Tm1Source.InstallDir("/opt/ibm/tm1_64")).with_seed(Tm1Seed.SDATA).with_expected_version(Tm1Version.parse("11.8.03500.6")) as tm1:
    ...
```

## Where TM1 comes from

The same settings as Java, in the same order (`Tm1Source.discover()`), as environment variables:

| Setting | Source |
|---|---|
| `TM1_PRIVATE_IMAGE` | `Tm1Source.PrivateImage`: an image with TM1 built in |
| `TM1_HOME_DIR` | `Tm1Source.InstallDir`: an installed `tm1_64` folder, mounted read-only |
| `TM1_INSTALL_VOLUME` | `Tm1Source.InstallVolume`: a Docker volume holding an install |
| `TM1_BUNDLE_DIR` (`TM1_BUNDLE`) | `Tm1Source.Bundle`: a folder holding IBM's bundle (the newest, or the one named) |
| `TM1_BUNDLE_URL` (`TM1_BUNDLE_SHA256`) | `Tm1Source.BundleUrl`: a link to the bundle |

A bundle is unpacked into the `tm1-testcontainers-cache` volume once, shared with the Java and
.NET tests. `TM1_SERVER_IMAGE` replaces the `tm1-server` image (default
`ghcr.io/ajmyers/tm1-server`, tagged `edge` for development versions). Cognos (`COGNOS_*`), TM1
Web (`TM1_WEB_*`) and PAW (`PAW_ZIP`, `PAW_BUNDLE_DIR`, `PAW_ACCEPT_LICENSE`) read Java's
settings too.

## More than one server

```python
env = PlanningAnalyticsEnvironment.builder().tm1("planning").tm1("reporting").tm1_web().cognos(lambda c: c.with_user("admin", "adminpw", "Admins")).build()
with env:
    env.tm1("planning").rest_url
```

Several servers share an Admin Server (`Tm1AdminServerContainer`); Cognos puts every server in
CAM security, and `keycloak()` with `cam_security(Namespace.OIDC, ...)` uses OIDC.

### From pa-environment.yaml

`from_config` reads the [`pa-environment.yaml`](../../docker/pa-setup/README.md#pa-environmentyaml)
that `pa-setup` runs, so tests run the environment developers run, from one file:

```python
with PlanningAnalyticsEnvironment.from_config("pa-environment.yaml").build() as env:
    env.tm1("planning").rest_url
```

It returns the builder, configured, so you can still change it before `build()`. The file is
checked as `pa-setup` checks it, with the same defaults and messages.

- **Each part** maps onto the builder: `tm1.servers` (with `seed` and `cfg`), `tm1.adminServer`,
  `tm1Web` (with `heap`), `cognos` (`users`, `oidc` as `keycloak()`, `cam` as `cam_security`,
  `memory`) and `paw` (`acceptLicense`, `zip`).
- **Sources:** a part's source set on the builder wins; then `ibm`, the folder of IBM downloads;
  then the settings above (pa-setup's `.env` sets the same `*_URL` names); then the part's
  `images` entry, as a private image with IBM's software built in. With another source, an
  `images` entry is the image the part runs.
- **Ignored:** `name`, every port, and `files`. Testcontainers gives the environment its own
  network, random host ports and a new database each time.
- **Refused** with a `ValueError` naming the setting: `cognos` without `tm1.servers`, and
  `seed: 24retail`, which the environment can't run yet.

### Reusing a whole environment

Cognos takes minutes to start and PAW longer. A `PlanningAnalyticsEnvironment` keeps its
containers between runs when you ask for it with `reuse(True)` and have turned on Testcontainers
reuse: `testcontainers.reuse.enable=true` in `~/.testcontainers.properties`, or
`TESTCONTAINERS_REUSE_ENABLE=true`. Without the switch, `reuse(True)` has no effect.

```python
env = PlanningAnalyticsEnvironment.builder().tm1("planning").cognos(lambda c: c.with_user("admin", "adminpw", "Admins")).reuse(True).build()
env.start()  # seconds, once a run with this configuration has started it
```

- **Each configuration has its own containers.** The environment's identity is a hash of its
  configuration: every part's image, source, environment variables, command, network aliases,
  labels, exposed ports, copied files and `docker create` arguments (`with_kwargs`, volumes), the
  Cognos users and the CAM settings. Its containers carry it as the label
  `io.skipcheck.tm1.environment`, and its network is named `tm1-environment-<identity>`, so the
  reused containers still reach each other by name. A different configuration gets new
  containers and never picks up another's.
- **`stop()` and `close()` leave everything running**, PAW too, and so does leaving a `with`
  block.
- **What tests leave behind stays:** objects in TM1, sessions, Cognos's content store. Give
  test objects unique names and delete them.
- **Old configurations keep running.** Changing the configuration starts new containers; the
  old ones stay until you remove them, and Cognos alone holds about 8 GB. To start clean:

  ```sh
  docker rm -f $(docker ps -aq --filter label=io.skipcheck.tm1.environment)
  docker network rm $(docker network ls -q --filter label=io.skipcheck.tm1.environment)
  ```

- **PAW is one per Docker daemon** whatever the identity: IBM's compose files give its containers
  fixed names. Each start runs IBM's compose files again, which takes about a minute.
- **With your own `network(...)`,** reuse works only if it's the same network each run: a
  container is found by everything it was created with, its network's name too.

## From the Java module

| Java | Python |
|---|---|
| `tm1.getRestUrl()`, `getUsername()`, `getTm1Version()`… | `tm1.rest_url`, `username`, `tm1_version`… (getters are properties) |
| `withSeed`, `withCamSecurity(cognos, Namespace.OIDC, "admin")`… | `with_seed`, `with_cam_security(cognos, Namespace.OIDC, "admin")`… |
| `CognosContainer.camAuthorization(user, password, namespace)` (static) | `CognosContainer.cam_authorization_for(...)` |
| `Optional<Tm1Source> discover()` | `discover()` returning the source or `None`; it also takes a lookup (`settings.get`) |
| `IllegalArgumentException`, `IllegalStateException` | `ValueError`, `RuntimeError` |
| `Network.newNetwork()` | `Network.new_network()`: created when a container first starts on it |
| `@RequiresTm1` and its `Tm1ServerContainer` parameter | `@pytest.mark.requires_tm1` and the `tm1_server` fixture |
| `withReuse(true)` | `with_reuse(True)`, honoured when `testcontainers.reuse.enable=true` (testcontainers-python has no reuse of its own) |

Containers are set up without Docker, as Java's are, so wiring is tested anywhere; they connect
when started. Java also reads each setting as a system property; Python reads environment
variables only.

## Tests

```sh
pytest python/tm1-testcontainers
```

- `test_java_parity.py` reads the Java sources and requires every public class, constant and
  member here, listing the few Java-only ones with the reason.
- The other tests port the Java tests. The configuration ones run without Docker; the ones that
  start containers are skipped without a source, as in Java.
