Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: FS-2031 added docker compose and justfile #2066

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,34 @@ repos:
- id: lint
name: Linter
stages: [pre-commit, pre-merge-commit, manual]
entry: "scripts/lint-pre-commit.sh"
entry: just linter
language: python
types: [python]
require_serial: true
verbose: true
pass_filenames: false

- repo: local
hooks:
- id: static-analysis
name: Static analysis
entry: "scripts/static-pre-commit.sh"
entry: just static-analysis
language: python
types: [python]
require_serial: true
verbose: true
pass_filenames: false

- repo: local
hooks:
- id: docs
name: Build docs
entry: "scripts/build-docs-pre-commit.sh"
entry: just docs-build
language: python
files: ^docs
require_serial: true
verbose: true
pass_filenames: false

- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
Expand Down
110 changes: 68 additions & 42 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,89 @@

After cloning the project, you'll need to set up the development environment. Here are the guidelines on how to do this.

## Virtual Environment with `venv`
## Install Justfile Utility

Create a virtual environment in a directory using Python's `venv` module:
Install justfile on your system:

```bash
python -m venv venv
brew install justfile
```

That will create a `./venv/` directory with Python binaries, allowing you to install packages in an isolated environment.
View all available commands:

## Activate the Environment
```bash
just
```

Activate the new environment with:
## Init development environment

Build Python and create a virtual environment:

```bash
source ./venv/bin/activate
just init
```

Ensure you have the latest pip version in your virtual environment:
By default, this builds Python 3.8. If you need another version, pass it as an argument to the just command:

```bash
python -m pip install --upgrade pip
just init 3.11.5
```

## Installing Dependencies
To check available Python versions, refer to the pyproject.toml file in the project root.

After activating the virtual environment as described above, run:
## Activate the Environment

Activate the new environment with

For Unix-based systems:

```bash
pip install -e ".[dev]"
source ./venv/bin/activate
```

This will install all the dependencies and your local **FastStream** in your virtual environment.
For Windows (PowerShell):

### Using Your local **FastStream**
```bash
.\venv\Scripts\Activate.ps1
```

If you create a Python file that imports and uses **FastStream**, and run it with the Python from your local environment, it will use your local **FastStream** source code.
Install and configure pre-commit:

Whenever you update your local **FastStream** source code, it will automatically use the latest version when you run your Python file again. This is because it is installed with `-e`.
```bash
just pre-commit-install
```

This way, you don't have to "install" your local version to be able to test every change.
## Run all Dependencies

To use your local **FastStream CLI**, type:
Start all dependencies as docker containers:

```bash
python -m faststream ...
just up
```

Once you are done with development and running tests, you can stop the dependencies' docker containers by running:

```bash
just stop
# or
just down
```

## Running Tests

### Pytest
To run tests, use:

```bash
just test
```

To run tests with your current **FastStream** application and Python environment, use:
To run tests with coverage:

```bash
pytest tests
# or
./scripts/test.sh
# with coverage output
./scripts/test-cov.sh
just coverage-test
```

In your project, you'll find some *pytest marks*:
In your project, some tests are grouped under specific pytest marks:

* **slow**
* **rabbit**
Expand All @@ -75,34 +95,40 @@ In your project, you'll find some *pytest marks*:
* **redis**
* **all**

By default, running *pytest* will execute "not slow" tests.

To run all tests use:
By default, will execute "all" tests. You can specify marks to include or exclude tests:

```bash
pytest -m 'all'
just test kafka
# or
just test rabbit
# or
just test 'not confluent'
# or
just test 'not confluent and not nats'
# or
just coverage-test kafka
```

If you don't have a local broker instance running, you can run tests without those dependencies:
## Linter

Run all linters:

```bash
pytest -m 'not rabbit and not kafka and not nats and not redis and not confluent'
just linter
```

To run tests based on RabbitMQ, Kafka, or other dependencies, the following dependencies are needed to be started as docker containers:
## Static analysis

```yaml
{! includes/docker-compose.yaml !}
```

You can start the dependencies easily using provided script by running:
Run static analysis tools:

```bash
./scripts/start_test_env.sh
just static-analysis
```

Once you are done with development and running tests, you can stop the dependencies' docker containers by running:
## Pre-commit

Run pre-commit checks:

```bash
./scripts/stop_test_env.sh
just pre-commit
```
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ARG PYTHON_VERSION=3.8

FROM python:$PYTHON_VERSION

ENV PYTHONUNBUFFERED=1

COPY . /src

WORKDIR /src

RUN pip install --upgrade pip && pip install -e ".[dev]"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UV?

52 changes: 52 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services:
rabbitmq:
image: rabbitmq:alpine
ports:
- "5672:5672"
# https://semgrep.dev/r?q=yaml.docker-compose.security.no-new-privileges.no-new-privileges
security_opt:
- no-new-privileges:true

kafka:
image: bitnami/kafka:3.5.0
ports:
- "9092:9092"
environment:
KAFKA_ENABLE_KRAFT: "true"
KAFKA_CFG_NODE_ID: "1"
KAFKA_CFG_PROCESS_ROLES: "broker,controller"
KAFKA_CFG_CONTROLLER_LISTENER_NAMES: "CONTROLLER"
KAFKA_CFG_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093"
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: "CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT"
KAFKA_CFG_ADVERTISED_LISTENERS: "PLAINTEXT://127.0.0.1:9092"
KAFKA_BROKER_ID: "1"
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: "1@kafka:9093"
ALLOW_PLAINTEXT_LISTENER: "true"
# https://semgrep.dev/r?q=yaml.docker-compose.security.no-new-privileges.no-new-privileges
security_opt:
- no-new-privileges:true

nats:
image: nats
command: -js
ports:
- 4222:4222
- 8222:8222 # management
# https://semgrep.dev/r?q=yaml.docker-compose.security.no-new-privileges.no-new-privileges
security_opt:
- no-new-privileges:true

redis:
image: redis:alpine
ports:
- 6379:6379
# https://semgrep.dev/r?q=yaml.docker-compose.security.no-new-privileges.no-new-privileges
security_opt:
- no-new-privileges:true

faststream:
build: .
volumes:
- ./:/src
network_mode: "host"
tty: true
Loading
Loading