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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 106 additions & 52 deletions src/tests/amqp_imposter/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# AMQP Imposter

AMQP clients are difficult to test thoroughly against a real broker like RabbitMQ.
Many bugs lurk in rare method sequences that are difficult to reproduce deterministically or cover well with hand-written tests.
How does your client juggle multiple `basic.return -> basic.ack` interleaved with broker-initiated `connection.close`?
## About the project

This imposter lets you ask these questions of your code, discover violations of properties you expect to hold across _every_ interaction, and reliably reproduce known tricky interactions.
The imposter is an AMQP 0-9-1 server that knows which methods are legal at each step of a client-server interaction, and lets you control its behaviour.

It is an AMQP 0-9-1 server with a model of the legal actions available to it.
A small configuration language lets you tune its behaviour with:
In practice, many client bugs lurk in rare interactions that are hard to explore by handwriting tests.
With the imposter, you can:

- Injected `when-then`-style rules on model state transitions, to construct particular decision sequences, layered on top of...
- Weighted sampling from the legal actions using a seed, to let the imposter make its own decisions.

The imposter fills in the blanks (fields of a method or whole methods) with values it knows are valid in the current state, in pattern-matching style.
- **Fuzz** clients with weighted sampling and then check an interaction transcript.
- **Reproduce** tricky interactions with deterministic `when-then` rules on server state transitions, letting the imposter fill in the gaps with values that are legal in the current state.

It supports one channel over one connection at a time.

## Quick start
## Getting started

### Prerequisites

Python 3.15 or later.

### Installation

Add this package to your test dependencies.
See [Usage](#usage) for how to spawn an imposter server using Python or the command line.

## Usage

### Python

Expand Down Expand Up @@ -46,7 +53,7 @@ def test_my_client(seed: int) -> None:
The runs above each spawn their own imposter process, so they can be parallelised, for example using `pytest-xdist`.
If running serially, calling `imposter.configure(config)` will disconnect the client and prepare a new session on the same imposter process.

Optionally tune the behaviour of the imposter by pattern matching.
Optionally tune the behaviour of the imposter with weights and scripts.

```py
from amqp_imposter.control.client import (
Expand All @@ -59,8 +66,8 @@ script = (
when_receive(pb.BasicPublishPattern(), then_send=pb.BasicReturnPattern()),
)
weights = (
weighted(pb.BasicAckPattern(), pb.BASE_WEIGHT // 2),
weighted(pb.ConnectionClosePattern(class_id=0, method_id=0), pb.BASE_WEIGHT // 20),
weighted(pb.BasicAckSelector(), pb.BASE_WEIGHT // 2),
weighted(pb.ConnectionCloseSelector(class_id=0, method_id=0), pb.BASE_WEIGHT // 20),
)

config = pb.SessionConfig(script=script, weights=weights, seed=seed)
Expand All @@ -69,10 +76,10 @@ config = pb.SessionConfig(script=script, weights=weights, seed=seed)

### Other languages

Run the imposter as a foreground process:
Run the imposter as a foreground process from a command line:

```
uvx amqp-imposter@<version> [--socket <path>] [--amqp-port <port>]
amqp-imposter [--socket <path>] [--amqp-port <port>]
```

then read the first line of `stdout` for the control socket and AMQP port.
Expand All @@ -82,9 +89,33 @@ If the imposter created the socket, it will be cleaned up on `SIGINT`/`SIGTERM`.
{"socket": "/tmp/amqp-imposter-<pid>/control.sock", "amqp_port": 5672}
```

Configure the imposter over gRPC on the socket using the proto schema (see [Control](#control)).
Configure the imposter over gRPC on the socket using the proto schema (see [How does the control server work?](#how-does-the-control-server-work)).

## How does the AMQP server work?
## How it works

### Internal packages

```mermaid
graph TD;
amqp-->model;
control-->amqp;
control-->model;
client["control/client"]-. "gRPC" .->control;
client_under_test[" "]-. "AMQP 0-9-1" .->amqp;
classDef external stroke-dasharray: 5 5;
classDef invisible fill:none,stroke:none;
class client external;
class client_under_test invisible;
```

**Package** | **Purpose**
:--- | :---
[model](amqp_imposter/model) | Core logic and model state
[amqp](amqp_imposter/amqp) | AMQP server, framing and session data
[control](amqp_imposter/control) | Control server, configuration and management
[control/client](amqp_imposter/control/client) | Built-in Python client for the control server

### How does the AMQP server work?

The actual AMQP server is thin and sits atop the more complex logic model.
It frames its output, puts it on the wire, and tracks session state (the model state machine, a transcript of the interaction, etc.).
Expand All @@ -94,77 +125,100 @@ The connection is in plaintext.
If the server detects that the client-server interaction has stalled (timed out idling since the last ingress method), it samples egress methods from the model to jumpstart it.
If there are no samples, then the client must owe us ingress.

## How does the control server work?
### How does the control server work?

The control plane to manage the server speaks gRPC over a Unix domain socket.

Talk to it in gRPC to:

- Configure: set the model parameters, reset the transcript and disconnect the client
- Reset: default the model parameters, reset the transcript and disconnect the client
- Status: see the AMQP port, whether there is an active client, the model parameters
- Stream the transcript: see the events and methods exchanged
- **Configure:** set the model parameters, reset the transcript and disconnect the client
- **Reset:** default the model parameters, reset the transcript and disconnect the client
- **Status:** see the AMQP port, whether there is an active client, the model parameters
- **Stream:** a copy of the transcript, with the events and methods exchanged
- **Watch:** stream transcript lines as they are recorded, until the session is reset

This lets us generate typed messages for all of these to use with the language of the client under test.
It also makes it easy to represent some more complex AMQP data types, such as tables.
Generate client stubs from `proto/amqp_imposter/control/v1/imposter.proto`.
gRPC lets us generate typed messages for all of these to use with the language of the client under test.
It can also represent more complex AMQP data types, like tables.

Note on development: `tox -e proto` regenerates types from the schema.

## How does the model work?
### How does the model work?

During a session, what the imposter needs to track is the methods it may send next.
The server model is responsible for tracking these sendables.
It is a pure core and strongly decoupled from the imposter harness.

A good rule of thumb for maintaining this separation is "can I interact/play with the model nicely from a REPL?"

### Methods and method patterns
#### Methods and method patterns

Because the sendable methods are infinite, we have to represent this bag in terms of partially-specified methods.
A method field belongs to one of two categories:

- **Exact**. It is always fixed and determined by the model. If I ask "is this method legal now", the answer partially depends on these fields. Example: `basic.getok(delivery-tag=1)`.
- **Arbitrary**. It can be empty or fixed. If I ask "is this method legal now", the answer ignores any empty fields. When sending a method, empty arbitraries can be filled in with any value. For example, `basic.getok(body="a")` is as legal as `basic.getok(body="bc")`. The field `basic.getok(redelivered=False)` is as legal as `basic.getok(redelivered=True)` on a first-time delivery to a channel, but `True` is fixed in the representation of a known redelivery.
- **Exact:** It is always derived from the model state.
- When a method is in the bag, it has concrete values in all exact fields.
- Example: `delivery-tag` in `basic.get-ok`.
- **Arbitrary:** It is not always derived from the model state.
- When a method is in the bag, it does not necessarily have concrete values in all arbitrary fields.
- Example: `redelivered` in `basic.get-ok`. It is sometimes in the bag with a concrete value, for instance if the message was requeued during this session. Other times, it can vary. A first-time delivery in this session could have been redelivered before, or not. In that case it is left empty, and a concrete value is generated when it's time to send it.

#### Model sampling

### Model sampling
Sampling is configured with selectors, which can specify methods and any of their exact fields.

Sampling is configured with patterns on methods and their exact fields.
For example, you may set a high weight on `BasicReturnSelector()`, and some nonzero weight on `ConnectionCloseSelector(class_id=0, method_id=0)` to see that your producer republishes correctly after a disconnect.

For example, you may set a high weight on `basic.return(*)` and some on `connection.close(class-id=0, method-id=0, *)` to see that your producer republishes correctly.
The sampling algorithm collects the matches for all patterns in order, picks the lucky pattern, samples one of its methods uniformly, and fills in the empty arbitraries.
You can weight `channel.close(reply-text="NOT_FORCED", *)` and `connection.close(reply-text="NOT_FORCED", *)` to configure separate probabilities for forced and unforced errors.
The sampling algorithm collects the matches for all selectors in order, picks the lucky selector, samples one of its methods uniformly, and fills in the empty arbitraries.

There is one special pattern, _stop_.
The model keeps generating methods for a burst until no methods are left or this pattern is sampled.
You could separately weight `ChannelCloseSelector(reply_text="NOT_FORCED")` and `ConnectionCloseSelector(reply_text="NOT_FORCED")` to make unforced errors less likely to happen. (A forced error would be one that the model has to send, whereas unforced means it could choose to ignore it, like a missing exchange, which may or may not exist in a real broker.)

You do not have to manually weight everything -- there is a set of implicit patterns at the bottom of the order, weighting each method and the stop at 100 and all server-initiated teardown and non-forced errors at 0.
There is one special weight, the "stop weight".
The model keeps generating methods for a burst until no methods are left or this weight is sampled.

On idle timeout, the imposter will also pass the special _idle weights_ to the model, at the the top of the order.
There is also a set of implicit selectors at the bottom of the order, to provide some defaults.
The implicit selectors weight everything as 100 with the exception of unforced and server-initiated teradown, which are weighted as 0.

On idle timeout, the imposter will also pass the special "idle weights" to the model, at the top of the order.
For example, you can use this to lower the chance of broker-initiated teardown during longer pauses by the client.

### Model scripting
#### Model scripting

Scripting is configured with patterns that activate after any send or receive transition.

They sit above weighted patterns in the resolution order, including above the stop pattern.
They sit above weighted selectors in the resolution order, including above the stop weight.
Therefore, you can configure the rules you want and let the imposter fall through to its own judgement when there is no match.

For example, configuring `when=received(basic.publish(*)), then_send=basic.ack(*)` means that after applying a received publish, the model should send an ack.
If the ack is not valid at that point, it will try the next matching pattern, in order.

Use the configurable _max sends_ to bound the bursts, 1,000 by default.
It is always a finite value, as a misconfigured ruleset could produce infinite loops: `when=sent(basic.deliver(*)), then_send=basic.deliver(*)`.
Unlike selectors, patterns can also fix arbitrary fields.

Use the configurable "max burst length" to bound the bursts.
It is 1,000 by default and has to be a finite value to break out of infinite loops.

#### Model loop

Putting it all together:

```mermaid
flowchart TD
receive(["receive M"]) --> script
idle(["idle timeout"]) -- "W := IW + W" --> sample
sample -- "yes" --> send
script{"∃ when P then P′<br>P ~ M, P′ ~ sendable M′"} -- "yes, M′ := M′ ⊓ P′" --> send
script -- "no" --> sample{"W samples M′"}
sample -- "no, stop" ---> waiting(["idle wait"])
send["send M′"] -- "M := M′" --> script
```

Here, `~` denotes [unifiability](https://en.wikipedia.org/wiki/Unification_(computer_science)) and `⊓` the unification itself.
In our case, two methods are unifiable if they do not have fields with conflicting concrete values (see [Methods and method patterns](#methods-and-method-patterns)).

`W` represents the weighted selectors and `IW` the idle weighted selectors.

### Representing topology
#### Representing topology

There is no persistent broker state; anything is fair game on a connection as long as it does not contradict the state of the interaction.
If your client declares an exchange, that does not permanently make the exchange exist inside the AMQP server you are talking to.
For example, in the imposter, publishing to an undeclared exchange will not deterministically produce channel error and publishing to a declared one may still 404.
For example, in the imposter, publishing to an undeclared exchange will not deterministically produce a channel error and publishing to a declared one may still 404.

In practice, vhosts are shared resources and their topologies volatile.
All those states -- existence and non-existence, changed arguments, changed message counts, etc. -- need to be represented by the model, so it does not need to track past declarations and bindings.

## Decision register
### Decision register

See [model/docs](amqp_imposter/model/docs/).
1 change: 0 additions & 1 deletion src/tests/amqp_imposter/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Enhancements
- Also redeliver across a disconnect
- `then_send` script clauses should be able to fix Arbitrary fields to a desired value
- Fully adhere to the negotiated capabilities
- Support anonymous consumer when the `Basic.Consume` is `no-wait`
- Extend table generator to produce nesting
Expand Down
Loading
Loading