Browse Source

Remove day validation, add basic typer testing just to start with

master
Teran McKinney 2 weeks ago
parent
commit
d1b845363b
  1. 2
      Makefile
  2. 16
      src/sporestack/api_client.py
  3. 9
      src/sporestack/client.py
  4. 21
      src/sporestack/validate.py
  5. 16
      tests/client_test.py
  6. 23
      tests/validate_test.py

2
Makefile

@ -7,7 +7,7 @@ test:
isort --check .
flake8 .
mypy --strict .
pytest --cov=sporestack --cov-fail-under=40 --cov-report=term --durations=3 --cache-clear
pytest --cov=sporestack --cov-fail-under=39 --cov-report=term --durations=3 --cache-clear
install:
python3 -m pip install -r requirements.txt

16
src/sporestack/api_client.py

@ -1,6 +1,6 @@
import logging
from time import sleep
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, Optional
import requests
@ -116,20 +116,6 @@ def api_request(
raise Exception("Stuff broke strangely.")
def normalize_argument(argument: Union[str, None, bool]) -> Union[str, None, bool]:
"""
Helps normalize arguments from aaargh that may not be what we want.
"""
if argument == "False":
return False
elif argument == "True":
return True
elif argument == "None":
return None
else:
return argument
def launch(
machine_id: str,
days: int,

9
src/sporestack/client.py

@ -349,13 +349,6 @@ def list() -> None:
print()
def remove(hostname: str) -> None:
"""
Removes a server's .json file.
"""
machine_info_path().joinpath(f"{hostname}.json").unlink()
def machine_exists(hostname: str) -> bool:
"""
Check if the VM's JSON exists locally.
@ -424,7 +417,7 @@ def delete(hostname: str, api_endpoint: Optional[str] = None) -> None:
api_endpoint = machine_info["api_endpoint"]
api_client.delete(machine_id=machine_id, api_endpoint=api_endpoint)
# Also remove the .json file
remove(hostname)
machine_info_path().joinpath(f"{hostname}.json").unlink()
print(f"{hostname} was deleted.")

21
src/sporestack/validate.py

@ -76,27 +76,6 @@ def ssh_key(ssh_key: str) -> None:
raise ValueError("ssh_key is too big.")
def days(days: int, zero_allowed: bool = False) -> None:
"""
Makes sure our argument is valid.
0-28
Keep in mind that 0 days implies no expiration,
may be used in the future with settlement tokens.
0 is invalid unless zero_allowed is True.
"""
if not isinstance(days, int):
raise TypeError("days must be an integer type")
if zero_allowed is True:
minimum_days = 0
else:
minimum_days = 1
if (days < minimum_days) or (days > 28):
raise ValueError(f"days must be {minimum_days}-28")
def organization(organization: Optional[str]) -> None:
if organization is None:
return

16
tests/client_test.py

@ -0,0 +1,16 @@
from typer.testing import CliRunner
from sporestack import __version__ as version
from sporestack import client
runner = CliRunner()
def test_random_machine_id() -> None:
assert client.random_machine_id() != client.random_machine_id()
def test_version() -> None:
result = runner.invoke(client.cli, ["version"])
assert result.output == f"{version}\n"
assert result.exit_code == 0

23
tests/validate_test.py

@ -28,29 +28,6 @@ def test_settlement_token() -> None:
validate.settlement_token(invalid_id)
def test_days() -> None:
with pytest.raises(ValueError):
validate.days(0)
validate.days(1)
validate.days(28)
validate.days(0, zero_allowed=True)
validate.days(1, zero_allowed=True)
validate.days(28, zero_allowed=True)
with pytest.raises(ValueError):
validate.days(29)
with pytest.raises(ValueError):
validate.days(29, zero_allowed=True)
with pytest.raises(ValueError):
validate.days(-1)
with pytest.raises(ValueError):
validate.days(-1, zero_allowed=True)
with pytest.raises(TypeError):
validate.days("one") # type: ignore
def test_is_unsigned_int() -> None:
assert validate.is_unsigned_int(0) is True
assert validate.is_unsigned_int(1) is True

Loading…
Cancel
Save