no more start_bumper

remove start_bumper
- built into __main__ + __init__
- add tests
This commit is contained in:
Brian Martin 2019-06-08 16:45:52 -04:00
parent 7637dfca6e
commit 1f0e417760
8 changed files with 316 additions and 133 deletions

View file

@ -1,5 +1,5 @@
[pytest]
env =
BUMPER_CA=tests/test_certs/ca.crt
BUMPER_CERT=tests/test_certs/bumper.crt
BUMPER_KEY=tests/test_certs/bumper.key
D:BUMPER_CA=tests/test_certs/ca.crt
D:BUMPER_CERT=tests/test_certs/bumper.crt
D:BUMPER_KEY=tests/test_certs/bumper.key

View file

@ -1,4 +1,5 @@
import mock
from mock import patch
import pytest
from tinydb.storages import MemoryStorage
from tinydb import TinyDB, Query
@ -6,6 +7,9 @@ import bumper
import os
import datetime, time
import platform
import json
import asyncio
from testfixtures import LogCapture
def test_get_milli_time():
@ -19,9 +23,61 @@ def test_get_milli_time():
)
def test_strtobool():
assert bumper.strtobool("t") == True
assert bumper.strtobool("f") == False
assert bumper.strtobool(0) == False
async def test_start_stop():
with LogCapture() as l:
if os.path.exists("tests/tmp.db"):
os.remove("tests/tmp.db") # Remove existing db
b = bumper
b.db = "tests/tmp.db" # Set db location for testing
b.conf1_listen_address = "0.0.0.0"
b.conf1_listen_port = 443
asyncio.create_task(b.start())
await asyncio.sleep(0.1)
l.check_present(("bumper", "INFO", "Starting Bumper"))
l.clear()
assert b.shutting_down == False
asyncio.create_task(b.shutdown())
await asyncio.sleep(0.1)
l.check_present(
("bumper", "INFO", "Shutting down"), ("bumper", "INFO", "Shutdown complete")
)
assert b.shutting_down == True
async def test_start_stop_debug():
with LogCapture() as l:
if os.path.exists("tests/tmp.db"):
os.remove("tests/tmp.db") # Remove existing db
b = bumper
b.db = "tests/tmp.db" # Set db location for testing
b.bumper_listen = "0.0.0.0"
b.bumper_debug = True
asyncio.create_task(b.start())
await asyncio.sleep(0.1)
asyncio.create_task(b.shutdown())
l.check_present(("bumper", "INFO", "Starting Bumper"))
l.clear()
await asyncio.sleep(0.1)
l.check_present(
("bumper", "INFO", "Shutting down"), ("bumper", "INFO", "Shutdown complete")
)
assert b.shutting_down == True
def test_db_path():
bumper.db = None
assert bumper.db_file() == os.path.join(bumper.data_dir, "bumper.db")
def test_user_db():
if os.path.exists("tests/tmp.db"):
os.remove("tests/tmp.db") # Remove existing db
bumper.db = "tests/tmp.db" # Set db location for testing
bumper.user_add("testuser") # Add testuser

111
tests/test_z_problem.py Normal file
View file

@ -0,0 +1,111 @@
import mock
from mock import patch
import pytest
from tinydb.storages import MemoryStorage
from tinydb import TinyDB, Query
import bumper
import os
import datetime, time
import platform
import json
import asyncio
from testfixtures import LogCapture
import sys
@patch("bumper.firstrun_input")
@patch("bumper.create_certs")
def test_firstrun(mock_input, mock_create):
with LogCapture() as l:
bumper.firstrun_input.return_value = "n"
bumper.first_run()
l.check_present(
(
"root",
"CRITICAL",
"Can't continue without certificates, please create some then try again.",
)
)
bumper.firstrun_input.return_value = "y"
bumper.first_run()
assert mock_create.called == True
def mock_subrun(*args):
return args
@patch("bumper.start")
def test_argparse(mock_start):
bumper.ca_cert = "tests/test_certs/ca.crt"
bumper.server_cert = "tests/test_certs/bumper.crt"
bumper.server_key = "tests/test_certs/bumper.key"
bumper.main(["--debug"])
assert bumper.bumper_debug == True
assert mock_start.called == True
bumper.main(["--listen", "127.0.0.1"])
assert bumper.bumper_listen == "127.0.0.1"
assert mock_start.called == True
bumper.main(["--announce", "127.0.0.1"])
assert bumper.bumper_announce_ip == "127.0.0.1"
assert mock_start.called == True
bumper.main(["--debug", "--listen", "127.0.0.1", "--announce", "127.0.0.1"])
assert bumper.bumper_debug == True
assert bumper.bumper_announce_ip == "127.0.0.1"
assert bumper.bumper_listen == "127.0.0.1"
assert mock_start.called == True
@patch("subprocess.run")
@patch("platform.system.lower")
@patch("platform.machine")
@patch("os.execv")
def test_createcert(mock_run, mock_platform, mock_machine, mock_exec):
mock_run.side_effect = mock_subrun
platform.system.return_value = "darwin"
bumper.create_certs()
assert mock_run.called == True
assert "../create_certs/create_certs_osx" in mock_exec.call_args.args[0]
platform.system.return_value = "windows"
bumper.create_certs()
assert mock_run.called == True
assert "../create_certs/create_certs_windows.exe" in mock_exec.call_args.args[0]
platform.system.return_value = "linux"
bumper.create_certs()
assert mock_run.called == True
assert "../create_certs/create_certs_linux" in mock_exec.call_args.args[0]
platform.system.return_value = "linux"
platform.machine.return_value = "arm"
bumper.create_certs()
assert mock_run.called == True
assert "../create_certs/create_certs_rpi" in mock_exec.call_args.args[0]
with LogCapture() as l:
platform.system.return_value = "nixbad"
bumper.create_certs()
l.check_present(
(
"root",
"CRITICAL",
"Can't determine platform. Create certs manually and try again.",
)
)
@patch("bumper.first_run")
def test_main(mock_firstrun):
bumper.ca_cert = "sf"
bumper.main()
assert mock_firstrun.called == True
bumper.ca_cert = "tests/test_certs/ca.crt"