Skip to main content

Testcontainers module for SpiceDB. Based on Guillermo Mariscal's SpiceDB Testcontainer written in Go.

Project description

Testcontainers SpiceDB - Python

Python module for Testcontainers that provides a SpiceDB container for integration testing.

This is a Python port of the Go testcontainers-spicedb module. All credit to @Mariscal6 for the original module

Installation

Install from source:

pip install -e .

Requirements

  • Python 3.8+
  • Docker

Usage

Basic Usage

from testcontainers_spicedb import SpiceDBContainer

# Using context manager (recommended)
with SpiceDBContainer(image="authzed/spicedb:v1.47.1") as spicedb:
    endpoint = spicedb.get_endpoint()
    secret_key = spicedb.get_secret_key()

    # Use endpoint and secret_key with your SpiceDB client
    # ...

Custom Secret Key

from testcontainers_spicedb import SpiceDBContainer

with SpiceDBContainer(secret_key="mycustomsecret") as spicedb:
    endpoint = spicedb.get_endpoint()
    secret_key = spicedb.get_secret_key()  # Returns "mycustomsecret"

Pre-load Schema

from testcontainers_spicedb import SpiceDBContainer

schema = """
    definition user {}

    definition document {
        relation owner: user
        relation viewer: user
        permission view = viewer + owner
    }
"""

with SpiceDBContainer(
    model=schema,
    model_secret_key="somepresharedkey"
) as spicedb:
    endpoint = spicedb.get_endpoint()
    # Schema is already loaded, ready to use

With SpiceDB Client

from testcontainers_spicedb import SpiceDBContainer
from authzed.api.v1 import Client, WriteSchemaRequest

with SpiceDBContainer() as spicedb:
    # Create client
    client = Client(
        spicedb.get_endpoint(),
        spicedb.get_secret_key(),
        use_tls=False
    )

    # Write schema
    schema = """
        definition user {}
        definition resource {
            relation owner: user
        }
    """
    response = client.WriteSchema(WriteSchemaRequest(schema=schema))

Testing

Run tests using pytest:

pytest tests/

Examples

See the examples/ directory for more usage examples:

python examples/example_spicedb.py

API Reference

SpiceDBContainer

Main container class for SpiceDB.

Parameters:

  • image (str): Docker image to use (default: "authzed/spicedb:v1.47.1")
  • secret_key (str): gRPC pre-shared key (default: "somepresharedkey")
  • model (str, optional): Schema to load on startup
  • model_secret_key (str, optional): Secret key for schema writer
  • schema_writer (callable, optional): Custom schema writer function
  • port (int): Port to expose (default: 50051)

Methods:

  • get_endpoint(): Returns the gRPC endpoint (host:port)
  • get_secret_key(): Returns the configured secret key
  • start(): Starts the container
  • stop(): Stops and removes the container

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testcontainers_spicedb-0.1.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

testcontainers_spicedb-0.1.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file testcontainers_spicedb-0.1.1.tar.gz.

File metadata

  • Download URL: testcontainers_spicedb-0.1.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for testcontainers_spicedb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1b67694b5d5ed43a95515144495914b3bd60cf88d8029eebc8df7113f35e0649
MD5 e940a3db54414bcbc9f287c24522eafc
BLAKE2b-256 3fc69e799963172ccaeebb5d7a7422c9a35a8210d9ccec468adaf070c893fb03

See more details on using hashes here.

File details

Details for the file testcontainers_spicedb-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for testcontainers_spicedb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b30caea43e8b49103b5b62cd7d9de94d18632633c333325ff45544e5ffaf35ff
MD5 d5d008d34ebff20ff37b0e8af348ed8c
BLAKE2b-256 21ff12dbe9f7f192620eb0e58052d98febe8cd4c4aa59f52b875fe86f2299a22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page