Skip to content

Commit

Permalink
Add version constraint to divide into two release trains 1.x and 2.x (#…
Browse files Browse the repository at this point in the history
…130)

Add version constraint to divide into two release trains 1.x and 2.x
  • Loading branch information
pszulczewski authored Aug 7, 2023
1 parent c76fb00 commit 2f982c3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
8 changes: 8 additions & 0 deletions pynautobot/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
This file has been modified by NetworktoCode, LLC.
"""
from packaging import version
import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry
Expand Down Expand Up @@ -109,6 +110,13 @@ def __init__(
self.users = App(self, "users")
self.plugins = PluginsApp(self)
self.graphql = GraphQLQuery(self)
self._validate_version()

def _validate_version(self):
"""Validate API version if eq or ge than 2.0 raise an error."""
api_version = self.version
if api_version.replace(".", "").isnumeric() and version.parse(api_version) >= version.parse("2.0"):
raise ValueError("Nautobot version 2 detected, please upgrade pynautobot to version 2.x")

@property
def version(self):
Expand Down
9 changes: 5 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from .util import Response

api = pynautobot.api(
"http://localhost:8000",
token="abc123",
)
with patch("pynautobot.api.version", "1.999"):
api = pynautobot.api(
"http://localhost:8000",
token="abc123",
)

HEADERS = {
"accept": "application/json;",
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def pytest_configure(config):


@pytest.fixture
def pynautobot_api():
def pynautobot_api(monkeypatch):
"""Factory to create pynautobot api instance."""
monkeypatch.setattr("pynautobot.api.version", "1.999")
return Api(url="https://mocknautobot.example.com", token="1234567890abcdefg")


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestAPIVersioning:
def nb_client_1_3(self, nb_client):
"""Setup a nb_client with API v1.3."""
# Instantiate with a temp url and then replace
nb_api = pynautobot.api("http://localhost", token=nb_client.token, api_version="1.3")
nb_api = pynautobot.api("http://nautobot:8000", token=nb_client.token, api_version="1.3")
nb_api.base_url = nb_client.base_url

return nb_api
Expand Down
29 changes: 23 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class ApiTestCase(unittest.TestCase):
@patch(
"requests.sessions.Session.post",
)
@patch("pynautobot.api.version", "1.999")
def test_get(self, *_):
api = pynautobot.api(host, **def_kwargs)
self.assertTrue(api)

@patch(
"requests.sessions.Session.post",
)
@patch("pynautobot.api.version", "1.999")
def test_sanitize_url(self, *_):
api = pynautobot.api("http://localhost:8000/", **def_kwargs)
self.assertTrue(api)
Expand Down Expand Up @@ -68,6 +70,19 @@ def test_api_version_not_found(self, *_):
)
self.assertEqual(api.version, "")

class ResponseHeadersWithVersion2:
headers = {"API-Version": "2.0"}
ok = True

@patch(
"requests.sessions.Session.get",
return_value=ResponseHeadersWithVersion2(),
)
def test_api_version_2(self, *_):
with self.assertRaises(ValueError) as error:
pynautobot.api(host)
self.assertEqual(str(error.exception), "Nautobot version 2 detected, please upgrade pynautobot to version 2.x")


class ApiStatusTestCase(unittest.TestCase):
class ResponseWithStatus:
Expand All @@ -82,6 +97,7 @@ def json(self):
"requests.sessions.Session.get",
return_value=ResponseWithStatus(),
)
@patch("pynautobot.api.version", "1.999")
def test_api_status(self, *_):
api = pynautobot.api(
host,
Expand All @@ -106,8 +122,8 @@ def test_api_retry(self, getconn_mock):
"http://any.url/",
retries=2,
)

api.version
with patch("pynautobot.api.version", "1.999"):
api.version

assert getconn_mock.return_value.request.mock_calls == [
call("GET", "/api/", body=None, headers=ANY),
Expand All @@ -123,10 +139,11 @@ def test_api_retry_fails(self, getconn_mock):
Mock(status=200, msg=HTTPMessage()),
]

api = pynautobot.api(
"http://any.url/",
retries=1,
)
with patch("pynautobot.api.version", "1.999"):
api = pynautobot.api(
"http://any.url/",
retries=1,
)

with self.assertRaises(RequestErrorFromException):
api.version
5 changes: 5 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppCustomFieldsTestCase(unittest.TestCase):
"requests.sessions.Session.get",
return_value=Response(fixture="extras/custom_fields.json"),
)
@patch("pynautobot.api.version", "1.999")
def test_custom_fields(self, session_get_mock):
api = pynautobot.api(host, **def_kwargs)
cfs = api.extras.custom_fields()
Expand All @@ -42,6 +43,7 @@ class AppCustomFieldChoicesTestCase(unittest.TestCase):
"requests.sessions.Session.get",
return_value=Response(fixture="extras/custom_field_choices.json"),
)
@patch("pynautobot.api.version", "1.999")
def test_custom_field_choices(self, session_get_mock):
api = pynautobot.api(host, **def_kwargs)
choices = api.extras.custom_field_choices()
Expand All @@ -66,6 +68,7 @@ class AppConfigTestCase(unittest.TestCase):
"pynautobot.core.query.Request.get",
return_value={"tables": {"DeviceTable": {"columns": ["name", "status", "tenant", "tags"]}}},
)
@patch("pynautobot.api.version", "1.999")
def test_config(self, *_):
api = pynautobot.api(host, **def_kwargs)
config = api.users.config()
Expand All @@ -81,6 +84,7 @@ class PluginAppCustomChoicesTestCase(unittest.TestCase):
"pynautobot.core.query.Request.get",
return_value={"Testfield1": {"TF1_1": 1, "TF1_2": 2}, "Testfield2": {"TF2_1": 3, "TF2_2": 4}},
)
@patch("pynautobot.api.version", "1.999")
def test_custom_choices(self, *_):
api = pynautobot.api(host, **def_kwargs)
choices = api.plugins.test_plugin.custom_fields()
Expand All @@ -91,6 +95,7 @@ def test_custom_choices(self, *_):
"pynautobot.core.query.Request.get",
return_value=[{"name": "test_plugin", "package": "netbox_test_plugin"}],
)
@patch("pynautobot.api.version", "1.999")
def test_installed_plugins(self, *_):
api = pynautobot.api(host, **def_kwargs)
plugins = api.plugins.installed_plugins()
Expand Down

0 comments on commit 2f982c3

Please sign in to comment.