From 37d3ede7cc6acc16680153669e3046448b763c81 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 16:42:11 -0500 Subject: [PATCH 1/8] Add a pytest fixture that creates a FABRIC slice --- tests/integration/conftest.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/integration/conftest.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py new file mode 100644 index 00000000..43df98cb --- /dev/null +++ b/tests/integration/conftest.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# MIT License +# +# Copyright (c) 2023 FABRIC Testbed +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socket +import time +import unittest +import pytest + +from fabrictestbed_extensions.fablib.fablib import FablibManager + + +@pytest.fixture(scope="session") +def fabric_slice(): + fablib = FablibManager() + + time_stamp = time.strftime("%Y-%m-%d %H:%M:%S") + host = socket.gethostname() + slice_name = f"integration test @ {time_stamp} on {host}" + + print(f"Creating slice '{slice_name}'..") + slice = fablib.new_slice(name=slice_name) + + yield slice + + slice.delete() From 4a56a207689b20670300c2df51d866dfa5315811 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 16:46:50 -0500 Subject: [PATCH 2/8] Use the slice fixture in "hello fabric" test --- tests/integration/test_hello_fabric.py | 59 +++++++++----------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index 80b10a8a..0c405bdc 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -12,58 +12,39 @@ class HelloFabricTests(unittest.TestCase): Run some basic tests against the testbed. """ - def test_fablib_hello(self): + def test_fablib_hello(fabric_slice): """ Create a slice with a single node, and echo a message from the node. """ fablib = FablibManager() - fablib.show_config() + self.assertIsInstance(fabric_slice, Slice) - # fablib.list_sites() + # Add a node. + node_name = "node-1" + site_name = fablib.get_random_site() - # Give the slice a unique name so that slice creation will not - # fail (because there is an existing slice with the same name) and - # we will have some hints about the test that created the slice. - time_stamp = time.strftime("%Y-%m-%d %H:%M:%S") - host = socket.gethostname() - slice_name = f"integration test @ {time_stamp} on {host}" + print( + f"Adding node '{node_name}' at site '{site_name}' to slice '{slice_name}'.." + ) + node = fabric_slice.add_node(name=node_name, site=site_name) - print(f"Creating slice '{slice_name}'..") - slice = fablib.new_slice(name=slice_name) + self.assertIsInstance(node, Node) - self.assertIsInstance(slice, Slice) + # Submit the slice. + print(f"Submitting slice '{slice_name}'..") + fabric_slice.submit() - try: - # Add a node. - node_name = "node-1" - site_name = fablib.get_random_site() + print(f"Slice '{slice_name}' status:") + fabric_slice.show() - print( - f"Adding node '{node_name}' at site '{site_name}' to slice '{slice_name}'.." - ) - node = slice.add_node(name=node_name, site=site_name) + print(f"Testing node '{node_name}' on slice '{slice_name}'...") - self.assertIsInstance(node, Node) + for node in fabric_slice.get_nodes(): + stdout, stderr = node.execute("echo Hello, FABRIC from node `hostname -s`") - # Submit the slice. - print(f"Submitting slice '{slice_name}'..") - slice.submit() - - print(f"Slice '{slice_name}' status:") - slice.show() - - print(f"Testing node '{node_name}' on slice '{slice_name}'...") - for node in slice.get_nodes(): - stdout, stderr = node.execute( - "echo Hello, FABRIC from node `hostname -s`" - ) - - self.assertEqual(stdout, f"Hello, FABRIC from node {node_name}\n") - self.assertEqual(stderr, "") - - finally: - slice.delete() + self.assertEqual(stdout, f"Hello, FABRIC from node {node_name}\n") + self.assertEqual(stderr, "") if __name__ == "__main__": From febb5ca316c45589f5293df135f95f389e67a80c Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 16:49:26 -0500 Subject: [PATCH 3/8] Reinstate comment about slice name --- tests/integration/conftest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 43df98cb..e6f666e0 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -33,6 +33,9 @@ def fabric_slice(): fablib = FablibManager() + # Give the slice a unique name so that slice creation will not + # fail (because there is an existing slice with the same name) and + # we will have some hints about the test that created the slice. time_stamp = time.strftime("%Y-%m-%d %H:%M:%S") host = socket.gethostname() slice_name = f"integration test @ {time_stamp} on {host}" From 4ea7f0387a85e5c2f555d6d811915e8dc2c8c840 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 17:05:44 -0500 Subject: [PATCH 4/8] Make fablib a fixture as well --- tests/integration/conftest.py | 7 +++++-- tests/integration/test_hello_fabric.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index e6f666e0..5c76c32f 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -30,9 +30,12 @@ @pytest.fixture(scope="session") -def fabric_slice(): - fablib = FablibManager() +def fablib(): + return FablibManager() + +@pytest.fixture(scope="session") +def fabric_slice(fablib): # Give the slice a unique name so that slice creation will not # fail (because there is an existing slice with the same name) and # we will have some hints about the test that created the slice. diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index 0c405bdc..c7eb4964 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -1,3 +1,5 @@ +import pytest + import socket import time import unittest @@ -12,12 +14,10 @@ class HelloFabricTests(unittest.TestCase): Run some basic tests against the testbed. """ - def test_fablib_hello(fabric_slice): + def test_fablib_hello(self, fablib, fabric_slice): """ Create a slice with a single node, and echo a message from the node. """ - fablib = FablibManager() - self.assertIsInstance(fabric_slice, Slice) # Add a node. From bb69691786128f348387ea297a80c9eccd149aa8 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 17:08:35 -0500 Subject: [PATCH 5/8] Get slice name from fixture --- tests/integration/test_hello_fabric.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index c7eb4964..63bcdb80 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -23,6 +23,7 @@ def test_fablib_hello(self, fablib, fabric_slice): # Add a node. node_name = "node-1" site_name = fablib.get_random_site() + slice_name = fabric_slice.get_name() print( f"Adding node '{node_name}' at site '{site_name}' to slice '{slice_name}'.." From 70cb67dd687baf70e420c07d2ac50a57ff70cd4b Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 17:09:52 -0500 Subject: [PATCH 6/8] Remove unused imports --- tests/integration/conftest.py | 1 - tests/integration/test_hello_fabric.py | 5 ----- 2 files changed, 6 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5c76c32f..67b44ef4 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -23,7 +23,6 @@ import socket import time -import unittest import pytest from fabrictestbed_extensions.fablib.fablib import FablibManager diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index 63bcdb80..25c269b5 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -1,10 +1,5 @@ -import pytest - -import socket -import time import unittest -from fabrictestbed_extensions.fablib.fablib import FablibManager from fabrictestbed_extensions.fablib.node import Node from fabrictestbed_extensions.fablib.slice import Slice From 26315d4d8694035be281e1924eb5f9597e1e4c00 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 17:10:25 -0500 Subject: [PATCH 7/8] Add copyright header --- tests/integration/test_hello_fabric.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index 25c269b5..c5f178b0 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -1,3 +1,26 @@ +#!/usr/bin/env python3 +# MIT License +# +# Copyright (c) 2023 FABRIC Testbed +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + import unittest from fabrictestbed_extensions.fablib.node import Node From cdf4f5a9d14eecc012e0baec5ffef11914666239 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Fri, 8 Sep 2023 17:39:52 -0500 Subject: [PATCH 8/8] Avoid mixing pytest fixtures and unittest --- tests/integration/test_hello_fabric.py | 54 +++++++++++--------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/tests/integration/test_hello_fabric.py b/tests/integration/test_hello_fabric.py index c5f178b0..92ea2071 100644 --- a/tests/integration/test_hello_fabric.py +++ b/tests/integration/test_hello_fabric.py @@ -21,50 +21,40 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import unittest - +from fabrictestbed_extensions.fablib.fablib import FablibManager from fabrictestbed_extensions.fablib.node import Node from fabrictestbed_extensions.fablib.slice import Slice -class HelloFabricTests(unittest.TestCase): +def test_fablib_hello(fablib, fabric_slice): """ - Run some basic tests against the testbed. + Create a slice with a single node, and echo a message from the node. """ - def test_fablib_hello(self, fablib, fabric_slice): - """ - Create a slice with a single node, and echo a message from the node. - """ - self.assertIsInstance(fabric_slice, Slice) - - # Add a node. - node_name = "node-1" - site_name = fablib.get_random_site() - slice_name = fabric_slice.get_name() - - print( - f"Adding node '{node_name}' at site '{site_name}' to slice '{slice_name}'.." - ) - node = fabric_slice.add_node(name=node_name, site=site_name) + assert isinstance(fablib, FablibManager) + assert isinstance(fabric_slice, Slice) - self.assertIsInstance(node, Node) + # Add a node. + node_name = "node-1" + site_name = fablib.get_random_site() + slice_name = fabric_slice.get_name() - # Submit the slice. - print(f"Submitting slice '{slice_name}'..") - fabric_slice.submit() + print(f"Adding node '{node_name}' at site '{site_name}' to slice '{slice_name}'..") - print(f"Slice '{slice_name}' status:") - fabric_slice.show() + node = fabric_slice.add_node(name=node_name, site=site_name) + assert isinstance(node, Node) - print(f"Testing node '{node_name}' on slice '{slice_name}'...") + # Submit the slice. + print(f"Submitting slice '{slice_name}'..") + fabric_slice.submit() - for node in fabric_slice.get_nodes(): - stdout, stderr = node.execute("echo Hello, FABRIC from node `hostname -s`") + print(f"Slice '{slice_name}' status:") + fabric_slice.show() - self.assertEqual(stdout, f"Hello, FABRIC from node {node_name}\n") - self.assertEqual(stderr, "") + print(f"Testing node '{node_name}' on slice '{slice_name}'...") + for node in fabric_slice.get_nodes(): + stdout, stderr = node.execute("echo Hello, FABRIC from node `hostname -s`") -if __name__ == "__main__": - unittest.main() + assert stdout == f"Hello, FABRIC from node {node_name}\n" + assert stderr == ""