Skip to content

Commit

Permalink
Toggle ModemReporter (#23)
Browse files Browse the repository at this point in the history
* Add new ENV var to toggle the modem reporting on/off

* Update the workflow

* Update main workflow to use matrix testing, and coverage workflow to have fix from earlier.
  • Loading branch information
erunks authored Nov 13, 2022
1 parent 848b0a8 commit 1f7c6e8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LATENCY_ADDRESSES = ""
LATENCY_RUNS = 10
LOG_FILE = "log.txt"
MAILTO = "[email protected]"
MODEM_REPORTING = 0
MODEM_ADDRESS = "http://192.168.100.1"
MODEM_PASSWORD = "admin"
MODEM_USERNAME = "admin"
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ HOST_ADDRESSES = ""
LATENCY_ADDRESSES = ""
LATENCY_RUNS = 10
MAILTO = "[email protected]"
MODEM_REPORTING = 1
MODEM_ADDRESS = "http://0.0.0.0"
MODEM_PASSWORD = "password"
MODEM_USERNAME = "username"
14 changes: 8 additions & 6 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ jobs:

steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.6
python-version: "3.6"

- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.1.15

- name: Run setup
run: |
poetry install
cp .env.sample .env
- name: Generate coverage
run: |
chmod +x $GITHUB_WORKSPACE/scripts/generate_coverage.sh
sh $GITHUB_WORKSPACE/scripts/generate_coverage.sh
- name: Upload coverage
uses: codacy/codacy-coverage-reporter-action@master
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
coverage-reports: $GITHUB_WORKSPACE/coverage.xml

42 changes: 31 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,51 @@ name: CI

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]

jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
os: [ "ubuntu-latest" ]
python-version: [ "3.6", "3.7", "3.8", "3.9", "3.10" ]

runs-on: ${{ matrix.os }}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: 3.6
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.1.15
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

- name: Create .env
run: cp .env.sample .env

- name: Run setup
run: |
poetry install
cp .env.sample .env
- name: Run tests
run: |
source .venv/bin/activate
chmod +x $GITHUB_WORKSPACE/scripts/run_tests.sh
sh $GITHUB_WORKSPACE/scripts/run_tests.sh
7 changes: 3 additions & 4 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ PYTHON_MET=false
PIP_MET=false
POETRY_MET=false

PIP3_REGEX='^pip.+(python\ 3.[6-9].+)$'
PYTHON3_6_REGEX='^Python\ 3\.[6-9].+$'
PIP3_REGEX='^pip.+(python\ 3\.([6-9]|1[0-9]).+)$'
PYTHON3_6_REGEX='^Python\ 3\.([6-9]|1[0-9]).+$'
PYTHON_VERSION="$( python --version )"
PYTHON3_VERSION="$( python3 --version )"

Expand Down Expand Up @@ -61,8 +61,7 @@ if [ "$POETRY_MET" != "true" ]; then
echo "Poetry is not installed!"
echo "Installing poetry..."

$("curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -")
exec bash
$("curl -sSL https://install.python-poetry.org | python3 - --version 1.1.15")
POETRY_MET=true
fi
fi
Expand Down
8 changes: 5 additions & 3 deletions src/ModemReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self):
'home': MODEM_ADDRESS,
'logout': MODEM_ADDRESS + '/logout.asp'
}
self.enabled = bool(int(getenv('MODEM_REPORTING')))

def __setup_browser(self):
from mechanicalsoup import StatefulBrowser
Expand All @@ -22,9 +23,10 @@ def __setup_browser(self):
self.browser.set_verbose(2)

def run(self):
self.login()
self.report_events(self.scrape_events())
self.logout()
if self.enabled:
self.login()
self.report_events(self.scrape_events())
self.logout()

def filter_event_logs(self, event_logs):
last_event = self.get_last_logged_event()
Expand Down
16 changes: 15 additions & 1 deletion src/tests/ModemReporter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,28 @@ def test_report_events_when_a_later_event_has_no_time_established(self, connect_
@patch.object(ModemReporter, 'logout')
@patch.object(ModemReporter, 'report_events')
@patch.object(ModemReporter, 'scrape_events')
def test_run(self, login_mock, logout_mock, report_events_mock, scrape_events_mock):
def test_run_when_enabled(self, login_mock, logout_mock, report_events_mock, scrape_events_mock):
self.modemReporter.enabled = True
self.modemReporter.run()

login_mock.assert_called_once()
scrape_events_mock.assert_called_once()
report_events_mock.assert_called_once()
logout_mock.assert_called_once()

@patch.object(ModemReporter, 'login')
@patch.object(ModemReporter, 'logout')
@patch.object(ModemReporter, 'report_events')
@patch.object(ModemReporter, 'scrape_events')
def test_run_when_not_enabled(self, login_mock, logout_mock, report_events_mock, scrape_events_mock):
self.modemReporter.enabled = False
self.modemReporter.run()

login_mock.assert_not_called()
scrape_events_mock.assert_not_called()
report_events_mock.assert_not_called()
logout_mock.assert_not_called()

def test_scrape_events(self):
from bs4 import BeautifulSoup

Expand Down

0 comments on commit 1f7c6e8

Please sign in to comment.