To run tests for the screenly/balena-prometheus-exporter
project, follow the outlined steps below. Ensure you have the required dependencies and Docker installed.
Step 1: Set Up the Environment
Begin by setting up your environment to run the tests. You may want to create a virtual environment and install the required dependencies:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Verify that all dependencies are correctly installed:
pip list
Step 2: Build the Docker Image
Before running the tests, build the Docker image using the provided Dockerfile
:
docker build -t balena-exporter .
Step 3: Run the Docker Container
Once the image has been built, you can run it in a detached mode:
docker run -d \
--name balena-exporter \
-p 8000:8000 \
-e BALENA_TOKEN=<YOUR_BALENA_TOKEN> \
balena-exporter
This command starts the exporter and maps port 8000 on your host to port 8000 in the container. Replace <YOUR_BALENA_TOKEN>
with your actual Balena token.
Step 4: Verify the Exporter
Ensure the exporter is running correctly by querying the /metrics
endpoint:
curl http://localhost:8000/metrics
You should see the Prometheus metrics being exposed.
Step 5: Running the Tests
With the environment prepared and the Docker container running, navigate to the tests directory and run the tests using the following command:
python -m unittest discover -s tests
This command will discover and run all the test cases located in the tests
directory.
Step 6: Reviewing the Test Code
The tests are written using the unittest
framework and utilize mock
to simulate and assert behaviors. Below is an example of a test that verifies the functionality of the BalenaCollector
class.
import unittest
from unittest import mock
from main import BalenaCollector
class TestBalenaCollector(unittest.TestCase):
def setUp(self):
self.collector = BalenaCollector()
@mock.patch("main.BalenaCollector.get_fleet_metrics")
def test_collect(self, mock_metrics):
with mock.patch.object(self.collector, "get_balena_fleets") as mock_fleets:
mock_fleets.return_value = ["fleet1", "fleet2"]
mock_metrics.side_effect = [("test_fleet", 3), ("test_fleet", 3)]
result = list(self.collector.collect()[0].samples)
expected = [('balena_devices_online', {'fleet_name': 'test_fleet'}, 3),
('balena_devices_online', {'fleet_name': 'test_fleet'}, 3)]
result = [(s.name, s.labels, s.value) for s in result]
self.assertEqual(result, expected)
# Additional test methods...
In this example, the test_collect
method uses mock.patch
to simulate the responses from the various methods for the BalenaCollector
. This ensures that the tests focus on testing the logic without making real API calls.
Conclusion
After executing the tests, you will see the test results in the terminal. If all tests pass, your setup is functioning correctly. If any tests fail, inspect the output for details on which tests did not pass and adjust your code accordingly.
Useful resources for further information on Python’s unittest
module can be found in the official documentation at unittest documentation.