Skip to content

Commit

Permalink
tests: run MQTT test using container
Browse files Browse the repository at this point in the history
**Asana Ticket**: [🍎 make MQTT tests work on MBTA network](https://app.asana.com/0/1204137169527258/1206661431136934/f)

Runs the Mosquitto broker as a container with `docker compose`. Publishes a test message in the MQTT test.
  • Loading branch information
bklebe committed Feb 27, 2024
1 parent b55a1a5 commit 6382198
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,18 @@ jobs:
image: amazon/dynamodb-local:2.0.0
ports:
- 8000:8000
mosquitto:
image: eclipse-mosquitto:2.0
ports:
- 1883:1883
volumes:
- ./mosquitto:/mosquitto/config/
options: --name mqtt

steps:
- uses: actions/checkout@v3
- name: Restart MQTT to load mosquitto/mosquitto.conf from checkout
run: docker restart mqtt
- name: ASDF cache
uses: actions/cache@v3
with:
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ To start your Phoenix app:

Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.

## Tests

To run the tests, first install and setup Colima, Docker, and docker-compose:

```shell
brew install docker docker-compose colima
colima start
mkdir -p ${DOCKER_CONFIG:-"~/.docker"}/cli-plugins
ln -sfn /opt/homebrew/opt/docker-compose/bin/docker-compose ${DOCKER_CONFIG:-"~/.docker"}/cli-plugins/docker-compose
```

Then, start the Compose configuration in a separate window or tab and run the tests:
1. `docker compose up`
2. `mix test`

## Environment Variables

In addition to the Elixir config files, the V3 API allows runtime configuration through a collection of environment variables.
Expand Down
2 changes: 1 addition & 1 deletion apps/api_accounts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end

## Setting Up DynamoDB Local

Make sure you have Docker installed, and then run
Make sure you have Colima and Docker installed and configured and then run:

```shell
docker compose up
Expand Down
20 changes: 18 additions & 2 deletions apps/state_mediator/test/state_mediator/mqtt_mediator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ defmodule StateMediator.MqttMediatorTest do

# @moduletag capture_log: true
@opts [
url: "mqtt://test.mosquitto.org",
topic: "home/#",
url: "mqtt://localhost:1883",
topic: "home/test",
state: __MODULE__.StateModule
]

Expand All @@ -55,6 +55,22 @@ defmodule StateMediator.MqttMediatorTest do

test "are sent to the state module" do
{:ok, _pid} = start_link(@opts)

{:ok, _client} =
EmqttFailover.Connection.start_link(
configs: [@opts[:url]],
handler: {EmqttFailover.ConnectionHandler.Parent, parent: self()}
)

assert_receive {:connected, client}, 5_000
Process.sleep(100)

:ok =
EmqttFailover.Connection.publish(client, %EmqttFailover.Message{
topic: @opts[:topic],
payload: "online"
})

assert_receive {:updated, contents}, 5_000
assert <<_::binary>> = contents
end
Expand Down
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ services:
container_name: dynamodb-local
ports:
- "8000:8000"
working_dir: /home/dynamodblocal
mosquitto:
image: "eclipse-mosquitto:2.0"
container_name: mosquitto
ports:
- "1883:1883"
volumes:
- ./mosquitto:/mosquitto/config/
2 changes: 2 additions & 0 deletions mosquitto/mosquitto.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
listener 1883
allow_anonymous true

0 comments on commit 6382198

Please sign in to comment.