Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ python3 -m unittest tests.<package>.<module>
| EO shared mailboxes - sanity | `python3 -m unittest tests.eo_shared_mail_boxes.sanity_tests` | No |
| EO shared mailboxes - load | `python3 -m unittest tests.eo_shared_mail_boxes.load_tests` | Yes |
| EO group mailboxes - sanity | `python3 -m unittest tests.eo_group_mailboxes.sanity_tests` | No |
| Integration (live, see below) | `python3 -m unittest tests.integration.exchange_test tests.integration.chat_test tests.integration.files_test tests.integration.failure_handling_test` | No, but needs credentials |

## Load tests need a fixture first

Expand Down Expand Up @@ -55,7 +56,8 @@ TEST_DATA_PATH=tests/files/test_data/state_7.json python3 -m unittest tests.file

## Keep the tests offline

No test may reach the network. `EOInPlaceArchiveEstimator` authenticates its
No test may reach the network, except the integration tests in
`tests/integration/` (see the last section). `EOInPlaceArchiveEstimator` authenticates its
auxiliary `TokenManager` inside `__init__`, so its tests patch
`TokenManager.authenticate_all` in `setUp` before constructing the estimator.
If that patch is dropped, authentication fails against the fake tenant, the
Expand All @@ -71,3 +73,11 @@ pattern to any new estimator that authenticates during construction.
| `FileNotFoundError: Test data not found at ...` | Fixture never generated | Run the suite's `data_state_creator.py` |
| A load test reports counts that are all `0` | The estimator hit the network during construction and set the stop event | Patch the estimator's authentication in `setUp` |
| A load test is off by a small constant | The fixture's expectations disagree with the estimator | Regenerate the fixture; if it still disagrees, the generator and estimator have genuinely diverged |

## Integration tests call Microsoft

Every suite above runs offline. The suite in `tests/integration/` is the one
exception: it runs the planners end to end against a real Microsoft 365 test
tenant. It is skipped unless credentials are provided, so running it without
them makes no network calls. See
[tests/integration/README.md](integration/README.md).
164 changes: 164 additions & 0 deletions tests/integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Integration tests

These tests run the Migration Planner end to end against a real Microsoft 365
tenant and check that nothing breaks: the scan completes, and the report and
batch files are written. They do not check the counts or ETAs the planner
reports; the unit tests in `tests/` cover those calculations.

Unlike the rest of `tests/`, they call Microsoft, so they are skipped unless you
provide credentials.

| Module | What it runs | Calls Microsoft |
| --- | --- | --- |
| `exchange_test` | Exchange Online planner | Yes |
| `chat_test` | Teams and Chat planner: a deep scan at 100% sampling, and a "Last 6 Months" scan | Yes |
| `files_test` | Files planner on a SharePoint site and a OneDrive user, in deep scan and shallow scan | Yes |
| `failure_handling_test` | Scans made to fail on purpose: a wrong client secret, and a shallow scan without a registered certificate | Yes |
| `redaction_test` | The placeholder replacement described under "What the tests print" | No |

## What each scan test checks

- The scan reports no error and reports completion, within its time limit.
- The planner's run log has no `[ERROR]` or `[WARNING]` line. The planner only
logs a failure for a single mailbox, site or team and still reports
completion, so this is the only place such a failure shows. Throttled
requests that succeed on retry are logged without these tags.
- The report CSV is written and lists the scanned entries, each once.
- The batch CSVs are written with the right header.
- Shallow scan only: the certificate is accepted and no library failed.

## Running

Run from the repository root:

```bash
pip install -r requirements.txt

export M365_TENANT_ID=...
export M365_CLIENT_ID=...
export M365_CLIENT_SECRET=...
export MP_IT_SCAN_INPUTS=/path/to/scan_inputs.json

python3 -m unittest tests.integration.exchange_test tests.integration.chat_test \
tests.integration.files_test tests.integration.failure_handling_test -v
```

Requirements, beyond the planner's own:

- **A display.** The planners keep their scan flow in the window classes, so
each scan creates a window object. On Windows and macOS the tests use your
desktop. On Linux they use `xvfb-run` if it is installed, so nothing appears
on screen; otherwise they use your display.
- **`tkinter`**, which ships with Python on Windows and macOS. On Debian or
Ubuntu install `python3-tk`.

Each scan runs in its own process and its own temporary folder, so the tests
leave no `outputs/`, `logs/` or `data/` in the repository. The temporary folders
are deleted when the run ends.

## What to scan

The tests never scan a whole tenant. Each scan is started the way a person
starts one with "Upload CSV": the test writes a CSV with one user (or one site)
and gives it to the planner. The entries come from a scan inputs file.

Copy `scan_inputs.example.json`, replace the entries with ones from your test
tenant, and point `MP_IT_SCAN_INPUTS` at your copy. Without that variable the
tests look for `tests/integration/scan_inputs.json`. Do not check your copy in:
it names your tenant.

- A case that is missing from the file is skipped, so you can start with one
planner.
- `input` is the CSV the planner is given: its header and rows.
- `options` sets planner settings by variable name.
- `timeout_seconds` is the time limit for the scan.
- List one small user per planner, so a scan takes about a minute. The Files
planner also has a case for one SharePoint site.
- For Chat, choose a user who belongs to only a few teams. The deep scan reads
every team the user is in.
- The Chat "Last 6 Months" scan is not limited by the CSV in the same way. It
estimates from Microsoft's usage reports for the whole tenant, and it needs
the `Reports.Read.All` permission.

## What the tests print

CI logs of a public repository can be read by anyone. Before a test prints
anything the planner wrote, `redaction.py` replaces the client secret, tenant
ID, client ID, tenant name, user and site with placeholders such as `<TENANT>`,
`<USER>` and `<SITE>`, in every form it knows (for example the user's OneDrive
address). A failed test therefore still shows the planner's message:

```
[WARNING] User '<USER>' was not found in the tenant. Skipped.
```

To see the output without placeholders, rerun locally with
`MP_IT_RAW_OUTPUT=1`. Add `MP_IT_KEEP_FILES=1` to keep the planner's reports and
logs, which are otherwise deleted when the run ends.

## Credentials

| Variable | Use |
| --- | --- |
| `M365_TENANT_ID`, `M365_CLIENT_ID`, `M365_CLIENT_SECRET` | Used by every test |
| `M365_EXCHANGE_*`, `M365_CHAT_*`, `M365_FILES_*` | Optional. The same three names with a planner prefix override the above for that planner, for when one tenant cannot serve all three. |
| `MP_IT_REQUIRE_CREDENTIALS=1` | Fail instead of skipping when credentials are missing. Set this in CI. |
| `MP_IT_CERTIFICATE_DIR` | Folder holding the registered `certificate.pem` and `passkey.pfx`, for shallow scan. |
| `MP_IT_RAW_OUTPUT=1` | Print the planner's output as it is, without placeholders. For local debugging only. |
| `MP_IT_KEEP_FILES=1` | Keep the scan outputs and logs after the run. For local debugging only. |

The tests never print the credentials. See "What the tests print" below.

### Shallow scan

Shallow scan signs in with a certificate, not the client secret. The tests do
not upload one. They rely on a certificate that is already registered on the
app, and one certificate serves every run:

1. Run a shallow scan once. The planner creates `certificate.pem` and
`passkey.pfx` under `certificate/<tenant>_<client>/`.
2. Upload `certificate.pem` to the app registration (Certificates & secrets >
Certificates). This is done once, by hand.
3. Keep both files. `passkey.pfx` holds the private key, so store it as
carefully as the client secret.

The `certificate/` folder is not checked in, so a clean checkout has no
certificate and the planner would create a new, unregistered one. To avoid
that, set `MP_IT_CERTIFICATE_DIR` to a folder holding the two files; the tests
copy them into place before a shallow scan. With `MP_IT_REQUIRE_CREDENTIALS=1`
a missing certificate fails the test instead of creating a new one.

The certificate is valid for two years. `passkey.pfx` is encrypted with the
client secret, so rotating the client secret means creating and uploading a new
certificate as well.

The shallow scan tests run the planner's own certificate check first and fail
with its message if Microsoft does not accept the certificate. To leave shallow
scan out for an entry, set `"shallow_scan": false` on its case.

## Failure handling tests

The planner reports a failure on a queue instead of raising, so a failed scan
can look like a passed one. `failure_handling_test` makes scans fail on purpose
and checks that the failure is reported and no report is written:

- a wrong client secret, for each planner;
- a shallow scan whose certificate is not registered on the app;
- a shallow scan with no certificate files where one is required.

The certificate tests give the planner an empty temporary folder to keep its
certificate in, so the registered certificate is not touched.

## How the tests drive the planner

`planner_runner.py` creates the planner's window object, sets its variables in
code, and calls `execute_migration_scan()` directly. The dialogs in
`start_scan()` are skipped. The planner reports failures on a queue instead of
raising, so every test first checks that the queue holds no error.

## Adding another tool

`harness.py` does not depend on the planner. It reads credentials and the scan
inputs file, and runs a runner module in a child process. To cover another tool,
add a runner module for it next to `planner_runner.py`, and a test module that
calls it. `redaction.py` is shared as well.
Loading