Mock a Helm Upgrade Install Command in Python

Use unittest mock to simulate a Helm upgrade --install call for testing automation scripts without a real cluster.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 13 views 0 copies

Python code

25 lines
Python 3.9+
from unittest.mock import MagicMock, patch


class HelmClient:
    def upgrade_install(self, release, chart, namespace="default"):
        # Simulates the helm upgrade --install command
        return f"Release {release} upgraded/installed in {namespace} using chart {chart}"


@patch("helm_client.HelmClient.upgrade_install")
def test_upgrade_install(mock_upgrade_install):
    mock_upgrade_install.return_value = "Mock: release 'myapp' installed successfully"

    client = HelmClient()
    result = client.upgrade_install("myapp", "stable/nginx", "prod")

    assert "myapp" in result
    assert "prod" in result
    print(result)


if __name__ == "__main__":
    real_client = HelmClient()
    print(real_client.upgrade_install("myapp", "stable/nginx"))
    test_upgrade_install()

Output

stdout
Release myapp upgraded/installed in default using chart stable/nginx
Mock: release 'myapp' installed successfully

How it works

The @patch decorator replaces HelmClient.upgrade_install with a MagicMock for the test function, letting you set a fixed return_value and assert on it. Inside the test, mock_upgrade_install is a MagicMock that acts as a fake Helm execution, so no real helm binary or cluster is needed. The client.upgrade_install(...) call returns the mock result, and assertions validate that the call included the right release and namespace. This pattern isolates the test from external Helm side effects, making it fast and deterministic for CI/CD pipelines or internal tooling tests.

Common mistakes

  • Patching the wrong target path — the string must match where the name is looked up (often the module that imports it, not just the defining module).
  • Asserting against the real return value instead of the mock's `return_value`, which fails when the mock is active.
  • Forgetting to reset mocks between tests, causing shared-state issues in larger test suites.

Variations

  1. Use `unittest.mock.Mock` with keyword arguments like `patch.object(HelmClient, 'upgrade_install', return_value=...)` for more control.
  2. Apply `patch()` as a context manager with `with patch(...) as mock:` for scoped mocking within a single function.

Real-world use cases

  • Unit-testing a release automation script that calls Helm, so you can validate error handling without a cluster.
  • Simulating Helm deployment steps in CI/CD pipeline tests to avoid slow or flaky integration steps.
  • Building a fake Helm client for a chat-ops or dashboard tool that triggers installs, testing logic before live commands.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.