Skip to content

Commit

Permalink
api: add elasticsearch connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jone committed Sep 6, 2024
1 parent be7a8b3 commit f22faf2
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
# Build the images with BUILDKIT_INLINE_CACHE=1, so that we can
# reuse layers in a later step. This works in combination with
# cache_from (in the compose file), thus we do not need to pull here.
- name: start elasticsearch
run: $COMPOSE up -d elasticsearch
- name: build images
run: $COMPOSE build --build-arg BUILDKIT_INLINE_CACHE=1
- name: push cache
Expand All @@ -42,6 +44,8 @@ jobs:
if: always()

# Run tests
- name: wait a bit for elasticsearch to start
run: sleep 5
- name: pytest
run: $COMPOSE run --rm api pytest

Expand Down
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ RUN --mount=type=cache,id=test,target=/root/.cache \
COPY pyproject.toml .flake8 /app/
COPY app/ /app/app/
COPY tests/ /app/tests/
ENV ENVIRONMENT=Testing
ENV ENVIRONMENT=TestingDocker
USER app


Expand Down
19 changes: 19 additions & 0 deletions api/app/elastic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from elasticsearch import Elasticsearch

from app.settings import settings


class Elastic:
def __init__(self) -> None:
self.connection = Elasticsearch(
[settings.elastic_url],
basic_auth=(settings.elastic_user, settings.elastic_password),
)

def delete_index(self) -> None:
if self.connection.indices.exists(index=settings.elastic_index):
self.connection.indices.delete(index=settings.elastic_index)

def create_index(self) -> None:
if not self.connection.indices.exists(index=settings.elastic_index):
self.connection.indices.create(index=settings.elastic_index)
16 changes: 15 additions & 1 deletion api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

class Settings(BaseSettings):
fastapi_debug: bool = False
elastic_url: str = None
elastic_user: str = "elastic"
elastic_password: str = None
elastic_index: str = "geophotoradar"


class ProductionSettings(Settings):
Expand All @@ -28,10 +32,20 @@ class ProductionSettings(Settings):

class DevelopmentSettings(Settings):
fastapi_debug: bool = True
elastic_url: str = "http://es01:9200"
elastic_password: str = "geophotoradar"


class TestingSettings(Settings):
pass
elastic_url: str = "http://localhost:9201"
elastic_password: str = "geophotoradar"
elastic_index: str = "testing"


class TestingDockerSettings(Settings):
elastic_url: str = "http://elasticsearch:9200"
elastic_password: str = "geophotoradar"
elastic_index: str = "testing"


settings: Settings = globals()[os.environ.get("ENVIRONMENT", "Testing") + "Settings"]()
12 changes: 12 additions & 0 deletions api/bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/../.."

docker-compose -f compose-test.yaml up -d elasticsearch
docker-compose -f compose-test.yaml build api
docker-compose -f compose-test.yaml run --rm api flake8
docker-compose -f compose-test.yaml run --rm api isort --check-only --quiet --settings pyproject.toml .
docker-compose -f compose-test.yaml run --rm api black --check .
sleep 5 # wait for elasticsearch
docker-compose -f compose-test.yaml run --rm api pytest
docker-compose -f compose-test.yaml rm -fs api
60 changes: 59 additions & 1 deletion api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ python = "^3.12"
fastapi = "^0.113.0"
uvicorn = {extras = ["standard"], version = "^0.30.6"}
pydantic-settings = "^2.4.0"
elasticsearch = "^8.15.0"

[tool.poetry.group.test.dependencies]
pytest = "^8.3.1"
Expand Down
4 changes: 4 additions & 0 deletions api/tests/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

from fastapi.testclient import TestClient

from app.elastic import Elastic
from app.server import app


class TestCase(unittest.TestCase):
def setUp(self):
super().setUp()
self.client = TestClient(app)
self.elastic = Elastic()
self.elastic.delete_index()
self.elastic.create_index()
8 changes: 8 additions & 0 deletions api/tests/test_elastic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from app.settings import settings
from tests.case import TestCase


class TestElastic(TestCase):

def test_index_exists(self):
self.assertTrue(self.elastic.connection.indices.exists(index=settings.elastic_index))
21 changes: 21 additions & 0 deletions compose-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@ services:
build:
context: api
target: test
depends_on:
elasticsearch:
condition: service_started

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.15.1
environment:
- discovery.type=single-node
- ELASTIC_PASSWORD=geophotoradar
- bootstrap.memory_lock=true
- cluster.routing.allocation.disk.threshold_enabled=false
- xpack.security.enabled=false
- xpack.security.enrollment.enabled=false
ports:
- 9200:9200
healthcheck:
test: ["CMD-SHELL", "curl -s http://localhost:9200/_cluster/health || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s

0 comments on commit f22faf2

Please sign in to comment.