Skip to content

Commit

Permalink
Merge pull request #148 from atlanticwave-sdx/147.use-tox-expose-dire…
Browse files Browse the repository at this point in the history
…ctive

Use tox-docker `expose` directive
  • Loading branch information
sajith authored Jun 14, 2024
2 parents 3fdd855 + 1fb12bb commit 7d6e15b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ jobs:
MQ_PORT: '5672'
MQ_USER: 'guest'
MQ_PASS: 'guest'
MONGODB_CONNSTRING: 'mongodb://guest:guest@localhost:27017/'
MONGO_HOST: 'localhost'
MONGO_PORT: '27017'
MONGO_USER: 'guest'
MONGO_PASS: 'guest'
DB_NAME: 'test-db'
DB_CONFIG_TABLE_NAME: 'test-config-table'

Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,46 @@ $ python3 -m sdx_lc

## Running tests

To run tests, use [tox] and [tox-docker]:
### With tox

An easy way to run tests is using [tox] and [tox-docker]. You will
need Docker installed and running.

```console
$ python3 -m venv venv --upgrade-deps
$ source venv/bin/activate
$ pip install 'tox>=4' tox-docker
$ pip install 'tox>=4' 'tox-docker>=5'
$ tox
```

### With pytest

If you want to avoid tox and run [pytest] directly, that is possible
too. You will need to run RabbitMQ and MongoDB, with Docker perhaps.
See above for the steps.

Some environment variables are expected to be set for the tests to
work as expected, so you may want to copy `env.template` to `.env` and
edit it according to your environment, and make sure the env vars are
present in your shell:

```console
$ cp env.template .env
$ # and then edit .env to suit your environment
$ source .env
```

And now, activate a virtual environment, install the requirements, and
then run `pytest`:

```
$ python3 -m venv venv --upgrade-deps
$ source ./venv/bin/activate
$ pip3 install --editable .[test]
$ pytest
```
`

## Communication between SDX Controller and Local Controller

The SDX controller and local controller communicate using
Expand Down Expand Up @@ -131,3 +162,4 @@ Local controller sends domain information to SDX controller:

[tox]: https://tox.wiki/en/latest/
[tox-docker]: https://tox-docker.readthedocs.io/
[pytest]: https://docs.pytest.org/
15 changes: 9 additions & 6 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ services:
expose:
- ${MONGO_PORT}
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-guest}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-guest}
volumes:
# See https://docs.docker.com/storage/volumes/
#
Expand Down Expand Up @@ -44,14 +44,17 @@ services:
- SDXLC_PORT=${SDXLC_PORT}
# MongoDB settings. When using compose, we connect to the
# mongodb service started by compose.
- MONGODB_CONNSTRING=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:${MONGO_PORT}/
- MONGO_HOST=mongodb
- MONGO_PORT=${MONGO_PORT:-27017}
- MONGO_USER=${MONGO_USER:-guest}
- MONGO_PASS=${MONGO_PASS:-guest}
- DB_NAME=${DB_NAME}
- DB_CONFIG_TABLE_NAME=${DB_CONFIG_TABLE_NAME}
# Message queue settings.
- MQ_HOST=${MQ_HOST}
- MQ_PORT=${MQ_PORT}
- MQ_USER=${MQ_USER}
- MQ_PASS=${MQ_PASS}
- MQ_PORT=${MQ_PORT:-5672}
- MQ_USER=${MQ_USER:-guest}
- MQ_PASS=${MQ_PASS:-guest}
# OXP settings.
- OXP_PROVISION_URL=${OXP_PROVISION_URL}
- OXP_PULL_URL=${OXP_PULL_URL}
Expand Down
15 changes: 7 additions & 8 deletions env.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ export SDXLC_DOMAIN='amlight.net'
export SDXLC_HOST="aw-sdx-lc-1.renci.org"
export SDXLC_PORT="8080"

# To run tests locally (with tox or pytest) or to run the server
# without Docker Compose, localhost is convenient as MONGO_HOST; for
# Docker Compose, we hard-code the container name in the compose file.
export MONGO_HOST="localhost"
export MONGO_PORT="27017"
# Used in initial MongoDB container setup.
export MONGO_INITDB_ROOT_USERNAME="guest"
export MONGO_INITDB_ROOT_PASSWORD="guest"
export MONGODB_CONNSTRING="mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/"
export SDXLC_MONGO_DB="sdx-controllder-test-db"
export SDXLC_MONGO_COLLECTION="sdx-controller-test-table"

# Used when accessing MongoDB.
export MONGO_HOST="localhost"
export MONGO_PORT="27017"
export MONGO_USER="guest"
export MONGO_PASS="guest"

export DB_NAME='test-db'
export DB_CONFIG_TABLE_NAME='test-1'
Expand Down
25 changes: 23 additions & 2 deletions sdx_lc/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@

DB_NAME = os.environ.get("DB_NAME")
DB_CONFIG_TABLE_NAME = os.environ.get("DB_CONFIG_TABLE_NAME")
MONGODB_CONNSTRING = os.environ.get("MONGODB_CONNSTRING")


class DbUtils(object):
def __init__(self):
self.db_name = DB_NAME
self.config_table_name = DB_CONFIG_TABLE_NAME
self.mongo_client = pymongo.MongoClient(MONGODB_CONNSTRING)

self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)

mongo_user = os.getenv("MONGO_USER") or "guest"
mongo_pass = os.getenv("MONGO_PASS") or "guest"
mongo_host = os.getenv("MONGO_HOST")
mongo_port = os.getenv("MONGO_PORT")

if mongo_host is None:
raise Exception("MONGO_HOST environment variable is not set")

if mongo_port is None:
raise Exception("MONGO_PORT environment variable is not set")

mongo_connstring = (
f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/"
)

# Log DB URI, without a password.
self.logger.info(
f"[DB] Using mongodb://{mongo_user}@{mongo_host}:{mongo_port}/"
)

self.mongo_client = pymongo.MongoClient(mongo_connstring)

def initialize_db(self):
"""Init database"""
self.logger.debug("Trying to load {} from DB".format(self.db_name))
Expand Down
22 changes: 16 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ setenv =
MQ_PORT = 5672
MQ_USER = guest
MQ_PASS = guest
MONGODB_CONNSTRING = mongodb://guest:guest@localhost:27017/
MONGO_USER = guest
MONGO_PASS = guest
MONGO_HOST = localhost
MONGO_PORT = 27017
DB_NAME = test-db
DB_CONFIG_TABLE_NAME = test-table

Expand All @@ -31,20 +34,27 @@ docker =
rabbitmq
mongo


[docker:rabbitmq]
image = rabbitmq:latest

ports =
5672:5672/tcp
# This will update MQ_HOST in testenv.
host_var = MQ_HOST

# This will update MQ_PORT in testenv.
expose =
MQ_PORT=5672/tcp

healthcheck_cmd = rabbitmq-diagnostics -q ping

[docker:mongo]
image = mongo:7.0.11

ports =
27017:27017/tcp
# This will update MONGO_HOST in testenv.
host_var = MONGO_HOST

# This will update MONGO_PORT in testenv.
expose =
MONGO_PORT=27017/tcp

environment =
MONGO_INITDB_ROOT_USERNAME=guest
Expand Down

0 comments on commit 7d6e15b

Please sign in to comment.