From 4126f47ed9d137fac51cde066a231caccdd6639f Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Wed, 2 Aug 2023 18:14:25 -0500 Subject: [PATCH 1/7] Adds some tests --- .../tests/workers/test_nautobot.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 nautobot_chatops/tests/workers/test_nautobot.py diff --git a/nautobot_chatops/tests/workers/test_nautobot.py b/nautobot_chatops/tests/workers/test_nautobot.py new file mode 100644 index 00000000..6856835e --- /dev/null +++ b/nautobot_chatops/tests/workers/test_nautobot.py @@ -0,0 +1,67 @@ +from unittest.mock import MagicMock, patch + +from django.test import TestCase +from nautobot.dcim.models import Site +from nautobot.ipam.models import VLAN +from nautobot.extras.models import Status + +from nautobot_chatops.dispatchers import Dispatcher +from nautobot_chatops.workers.nautobot import get_vlans + + +class IpamTestCase(TestCase): + """Tests related to IPAM ChatOps commands.""" + + def setUp(self): + """Per-test-case setup function.""" + self.active_status = Status.objects.get(name="Active") + self.site = Site.objects.create(name="site-1", status=self.active_status) + self.vlans, created = VLAN.objects.get_or_create(vid=1, name="vlan-1", status=self.active_status, site=self.site) + + # Mock the dispatcher + self.dispatcher = MagicMock(Dispatcher) + + def test_get_vlans_initial_prompt(self): + """Test get VLANs initial command.""" + self.assertFalse(get_vlans(self.dispatcher)) + self.dispatcher.send_error.assert_not_called() + self.dispatcher.prompt_from_menu.assert_called_with( + "nautobot get-vlans", + "select a vlan filter", + [ + ("VLAN ID", "id"), + ("Group", "group"), + ("Name", "name"), + ("Role", "role"), + ("Site", "site"), + ("Status", "status"), + ("Tenant", "tenant"), + ("All (no filter)", "all"), + ] + ) + + def test_get_vlans_filter_type_sent_filter_name(self): + """Test get VLANs with filter type Name selected.""" + self.assertFalse(get_vlans(self.dispatcher, "name")) + self.dispatcher.send_error.assert_not_called() + self.dispatcher.prompt_from_menu.assert_called_with( + "nautobot get-vlans name", + "select a vlan name", + [ + ("vlan-1", "vlan-1") + ], + offset=0 + ) + + def test_get_vlans_filter_type_sent_filter_all(self): + """Test get VLANs with filter type All selected.""" + self.assertFalse(get_vlans(self.dispatcher, "all")) + self.dispatcher.send_error.assert_not_called() + self.dispatcher.prompt_from_menu.assert_called_with( + "nautobot get-vlans name", + "select a vlan name", + [ + ("vlan-1", "vlan-1") + ], + offset=0 + ) \ No newline at end of file From 8318612408d2e28b40bd35065db2ca8bfd6169d6 Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Wed, 2 Aug 2023 18:18:08 -0500 Subject: [PATCH 2/7] Black. --- .../tests/workers/test_nautobot.py | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/nautobot_chatops/tests/workers/test_nautobot.py b/nautobot_chatops/tests/workers/test_nautobot.py index 6856835e..1bbc5ae3 100644 --- a/nautobot_chatops/tests/workers/test_nautobot.py +++ b/nautobot_chatops/tests/workers/test_nautobot.py @@ -16,7 +16,9 @@ def setUp(self): """Per-test-case setup function.""" self.active_status = Status.objects.get(name="Active") self.site = Site.objects.create(name="site-1", status=self.active_status) - self.vlans, created = VLAN.objects.get_or_create(vid=1, name="vlan-1", status=self.active_status, site=self.site) + self.vlans, created = VLAN.objects.get_or_create( + vid=1, name="vlan-1", status=self.active_status, site=self.site + ) # Mock the dispatcher self.dispatcher = MagicMock(Dispatcher) @@ -37,7 +39,7 @@ def test_get_vlans_initial_prompt(self): ("Status", "status"), ("Tenant", "tenant"), ("All (no filter)", "all"), - ] + ], ) def test_get_vlans_filter_type_sent_filter_name(self): @@ -45,12 +47,7 @@ def test_get_vlans_filter_type_sent_filter_name(self): self.assertFalse(get_vlans(self.dispatcher, "name")) self.dispatcher.send_error.assert_not_called() self.dispatcher.prompt_from_menu.assert_called_with( - "nautobot get-vlans name", - "select a vlan name", - [ - ("vlan-1", "vlan-1") - ], - offset=0 + "nautobot get-vlans name", "select a vlan name", [("vlan-1", "vlan-1")], offset=0 ) def test_get_vlans_filter_type_sent_filter_all(self): @@ -58,10 +55,5 @@ def test_get_vlans_filter_type_sent_filter_all(self): self.assertFalse(get_vlans(self.dispatcher, "all")) self.dispatcher.send_error.assert_not_called() self.dispatcher.prompt_from_menu.assert_called_with( - "nautobot get-vlans name", - "select a vlan name", - [ - ("vlan-1", "vlan-1") - ], - offset=0 - ) \ No newline at end of file + "nautobot get-vlans name", "select a vlan name", [("vlan-1", "vlan-1")], offset=0 + ) From c4f96058729e4fb82e0151bf21cfaa06bcf6c429 Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Wed, 2 Aug 2023 18:21:14 -0500 Subject: [PATCH 3/7] Changelog --- changes/227.added | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/227.added diff --git a/changes/227.added b/changes/227.added new file mode 100644 index 00000000..bf3b7412 --- /dev/null +++ b/changes/227.added @@ -0,0 +1 @@ +Added some tests for VLAN chatops. \ No newline at end of file From 09322749fd50206d9e88b8cd40e4f8060dcb7d8b Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Thu, 3 Aug 2023 08:25:23 -0500 Subject: [PATCH 4/7] Updates nautobot tests. --- nautobot_chatops/tests/workers/test_nautobot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nautobot_chatops/tests/workers/test_nautobot.py b/nautobot_chatops/tests/workers/test_nautobot.py index 1bbc5ae3..b1b925b9 100644 --- a/nautobot_chatops/tests/workers/test_nautobot.py +++ b/nautobot_chatops/tests/workers/test_nautobot.py @@ -1,4 +1,5 @@ -from unittest.mock import MagicMock, patch +"""Tests for the /nautobot chatops commands.""" +from unittest.mock import MagicMock from django.test import TestCase from nautobot.dcim.models import Site From 3103a45958b9da99c0c2417e875052ccc64f4666 Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Thu, 3 Aug 2023 12:05:43 -0500 Subject: [PATCH 5/7] Updates tests. --- nautobot_chatops/tests/workers/test_nautobot.py | 10 +++------- nautobot_chatops/workers/nautobot.py | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/nautobot_chatops/tests/workers/test_nautobot.py b/nautobot_chatops/tests/workers/test_nautobot.py index b1b925b9..a6c7db59 100644 --- a/nautobot_chatops/tests/workers/test_nautobot.py +++ b/nautobot_chatops/tests/workers/test_nautobot.py @@ -5,6 +5,7 @@ from nautobot.dcim.models import Site from nautobot.ipam.models import VLAN from nautobot.extras.models import Status +from nautobot_chatops.choices import CommandStatusChoices from nautobot_chatops.dispatchers import Dispatcher from nautobot_chatops.workers.nautobot import get_vlans @@ -17,9 +18,7 @@ def setUp(self): """Per-test-case setup function.""" self.active_status = Status.objects.get(name="Active") self.site = Site.objects.create(name="site-1", status=self.active_status) - self.vlans, created = VLAN.objects.get_or_create( - vid=1, name="vlan-1", status=self.active_status, site=self.site - ) + self.vlans, _ = VLAN.objects.get_or_create(vid=1, name="vlan-1", status=self.active_status, site=self.site) # Mock the dispatcher self.dispatcher = MagicMock(Dispatcher) @@ -53,8 +52,5 @@ def test_get_vlans_filter_type_sent_filter_name(self): def test_get_vlans_filter_type_sent_filter_all(self): """Test get VLANs with filter type All selected.""" - self.assertFalse(get_vlans(self.dispatcher, "all")) + self.assertEqual(get_vlans(self.dispatcher, "all"), CommandStatusChoices.STATUS_SUCCEEDED) self.dispatcher.send_error.assert_not_called() - self.dispatcher.prompt_from_menu.assert_called_with( - "nautobot get-vlans name", "select a vlan name", [("vlan-1", "vlan-1")], offset=0 - ) diff --git a/nautobot_chatops/workers/nautobot.py b/nautobot_chatops/workers/nautobot.py index 747a3be1..642d457a 100644 --- a/nautobot_chatops/workers/nautobot.py +++ b/nautobot_chatops/workers/nautobot.py @@ -160,7 +160,7 @@ def examine_termination_endpoints(circuit): # pylint: disable=too-many-statements @subcommand_of("nautobot") -def get_vlans(dispatcher, filter_type, filter_value_1): +def get_vlans(dispatcher, filter_type=None, filter_value_1=None): """Return a filtered list of VLANs based on filter type and/or `filter_value_1`.""" # pylint: disable=no-else-return if not filter_type: From 7647f408bc2b8e105139da2d152cfc7d7c286e4c Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Thu, 3 Aug 2023 12:06:28 -0500 Subject: [PATCH 6/7] Adds additional town crier. --- changes/227.fixed | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/227.fixed diff --git a/changes/227.fixed b/changes/227.fixed new file mode 100644 index 00000000..b446e79b --- /dev/null +++ b/changes/227.fixed @@ -0,0 +1 @@ +Fixed parameters that should be set to None if they have not been defined yet by default. \ No newline at end of file From 6de374645b7f54da46ced178b8bdfa8786c0ecd2 Mon Sep 17 00:00:00 2001 From: Josh VanDeraa Date: Thu, 3 Aug 2023 16:07:57 -0500 Subject: [PATCH 7/7] Updates unittest. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e7b14e61..b59d8947 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -104,7 +104,7 @@ jobs: fail-fast: true matrix: python-version: ["3.8", "3.9", "3.10"] - nautobot-version: ["1.5.4", "latest"] + nautobot-version: ["1.5.4", "stable"] runs-on: "ubuntu-20.04" env: INVOKE_NAUTOBOT_CHATOPS_PYTHON_VER: "${{ matrix.python-version }}"