The Simplest Way to Make Pulumi and Pytest Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams use pulumi and pytest together to check cloud code before live changes happen. Pulumi builds cloud stacks, while pytest runs tests on those planned resources. This setup lets developers catch bad rules in pull requests. You can check firewall settings, storage flags, and role setups in seconds. Teams write Python tests that inspect cloud plans before any real cloud deployment starts. How permissions and data flow between pulumi and pytest Data flows from your cloud code into the test r

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Teams use pulumi and pytest together to check cloud code before live changes happen. Pulumi builds cloud stacks, while pytest runs tests on those planned resources.

This setup lets developers catch bad rules in pull requests. You can check firewall settings, storage flags, and role setups in seconds. Teams write Python tests that inspect cloud plans before any real cloud deployment starts.

How permissions and data flow between pulumi and pytest

Data flows from your cloud code into the test runner through memory or state files. For unit tests, pytest mocks the cloud provider API in memory. Pulumi creates resource objects and sends their attributes to the mock calls. Then pytest reads those values and checks if they match your rules.

For live stack tests, the flow changes and needs real cloud keys. Pytest starts a Pulumi workspace run to build a temporary test stack. Pulumi logs in to your cloud provider using a service role and state backend key. After the stack finishes building, Pulumi sends output values like hostnames back to pytest. The test suite then sends network requests or database queries to check the live app.

When the tests finish, pytest tells Pulumi to tear down the test stack. Each step needs clear permissions so the test runner can read state and change resources. Without clean role limits, your test runner holds way too much power.

Common failure modes and how to scope roles

Many teams make the mistake of using one shared admin account for all test runs. A developer puts a broad cloud token into the CI runner to save time. Soon, every pull request job can read live secrets and change production systems. If a bad script runs in a test, it can wipe out real data.

Another big issue is unlogged queries during live stack checks. When pytest runs checks against a test database, it often uses a static password. Audit logs only show that the shared test user logged in. You cannot tell which developer or CI run sent a specific query. Also, failed test runs often leave behind stray cloud resources and active keys.

Pulumi Policy as Code + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix this, give your test runner a narrow role in a sandbox account. Never let CI test jobs use roles that touch production accounts or state buckets. Set up your CI system to trade short-lived identity tokens with your cloud provider. Scope the state backend permissions so the test role can only read test stacks.

Next, lock down the network paths between your test runner and the target resources. Place test databases and servers inside private subnets with strict firewall rules. If tests read sample user tables, use data masking to rewrite emails and phone numbers. Masking rewrites card numbers and national IDs in results so sensitive data stays safe. Log every test action and link it to the specific CI job and git commit.

Keeping local and CI test runs fast and safe

Developers want fast feedback on their laptops without holding static cloud keys on disk. Keep unit tests offline by using mocks for all basic property checks. Save live stack tests for merge queues where isolated runners handle the cloud login.

Make sure every live test run uses a unique stack name and clean state path. Add a cleanup step that runs even when a test crashes mid-flight. This simple habit keeps cloud bills low and removes old test endpoints right away.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Should I use unit mocks or live stacks with pulumi and pytest?

Use unit mocks for fast checks on tags, sizes, and basic security rules. Use live stacks only when you must test real cloud network paths and boot times.

How do I stop test runs from leaking cloud secrets in logs?

Mark sensitive values as secrets in your stack code so outputs stay hidden. Also, make sure your test assertions never print raw connection strings or keys on failure.

How can teams handle database checks during stack tests?

Run database checks with a scoped role that only has permissions for the test schema. Always clean up test users and drop temporary schemas before the test job ends.

The query runs. The emails stay hidden.