Repository: softlayer/softlayer-python
Pull request #1973 (2023-06-07 10:22:57)

#1968
Vlan Unit Test

Hi Chris,

I have written the unit test cases for vlans and got 100 % coverage. Please review and approve this pull request.

Attaching the screenshot of coverage report for further reference.
<img width="1317" alt="image" src="https://github.com/softlayer/softlayer-python/assets/134699156/c4ed74cb-4899-49d6-9232-baaae6d66970">

Title  - Vlan unit tests 100% (Issue #1968)

Linked Issue #1968

Thanks,
Jayasilan


Patch:
diff --git a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py
index 61e70a56..996c610b 100644
--- a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py
+++ b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py
@@ -47,6 +47,51 @@
     'tagReferences': [],
 }
 
+getObject_no_hard_no_vs_no_trunk = {'primaryRouter': {'fullyQualifiedDomainName': 'bcr02a.fra02.softlayer.com',
+                                                      'id': 1186647,
+                                                      'datacenter': {'id': 449506,
+                                                                     'longName': 'Frankfurt 2',
+                                                                     'name': 'fra02'}
+                                                      },
+                                    'id': 1186647,
+                                    'vlanNumber': 932,
+                                    'networkVlanFirewall': {
+                                        'datacenter': {'id': 449506, 'longName': 'Frankfurt 2'},
+                                        'fullyQualifiedDomainName': 'bcr02a.fra02.softlayer.com'
+                                        },
+                                    'subnets': [
+                                        {'gateway': '10.85.154.1',
+                                         'id': 1688651,
+                                         'netmask': '255.255.255.192',
+                                         'networkIdentifier': '10.85.154.0',
+                                         'subnetType': 'ADDITIONAL_PRIMARY',
+                                         'usableIpAddressCount': '61'}],
+                                    'hardware': [
+                                        {'domain': 'vmware.chechu.com',
+                                         'hostname': 'host15',
+                                         'primaryBackendIpAddress': '10.177.122.170',
+                                         'primaryIpAddress': '169.46.48.107'}],
+                                    'virtualGuests': [
+                                        {'domain': 'ocp-virt.chechu.me',
+                                         'hostname': 'haproxy',
+                                         'primaryBackendIpAddress': '10.85.154.44',
+                                         'primaryIpAddress': '158.177.72.67'}],
+                                    'networkComponentTrunks': [
+                                        {'networkComponent':
+                                            {'hardwareId': 1483895, 'networkVlanId': 3285524, 'port': 12,
+                                             'downlinkComponent': {'networkVlanId': '', 'port': 0,
+                                                                   'primaryIpAddress': '10.194.250.148',
+                                                                   'hardware':
+                                                                       {
+                                                                           'domain': 'chechu.me',
+                                                                           'hostname': 'fra02-ocp-virt',
+                                                                           'tagReferences': []
+                                                                           },
+                                                                       'networkComponentGroup': {
+                                                                           'groupTypeId': 1,
+                                                                           'membersDescription': 'eth0/2'}}}}],
+                                    'tagReferences': []}
+
 editObject = True
 setTags = True
 getList = [getObject]


Test patch:
diff --git a/tests/CLI/modules/vlan_tests.py b/tests/CLI/modules/vlan_tests.py
index b7660fb4..4021aa2e 100644
--- a/tests/CLI/modules/vlan_tests.py
+++ b/tests/CLI/modules/vlan_tests.py
@@ -7,6 +7,9 @@
 import json
 from unittest import mock as mock
 
+from SoftLayer.CLI import exceptions
+from SoftLayer import exceptions as ex
+from SoftLayer.fixtures import SoftLayer_Network_Vlan
 from SoftLayer.fixtures import SoftLayer_Product_Order
 from SoftLayer.fixtures import SoftLayer_Product_Package
 from SoftLayer import testing
@@ -32,27 +35,15 @@ def test_detail_no_hardware(self):
 
     def test_subnet_list(self):
         vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
-        getObject = {
-            'primaryRouter': {
-                'datacenter': {'id': 1234, 'longName': 'TestDC'},
-                'fullyQualifiedDomainName': 'fcr01.TestDC'
-            },
-            'id': 1234,
-            'vlanNumber': 4444,
-            'firewallInterfaces': None,
-            'subnets': [
-                {
-                    'id': 99,
-                    'networkIdentifier': 1111111,
-                    'netmask': '255.255.255.0',
-                    'gateway': '12.12.12.12',
-                    'subnetType': 'TEST',
-                    'usableIpAddressCount': 1
-
-                }
-
-            ]
-        }
+        getObject = {'primaryRouter': {'datacenter': {'id': 1234, 'longName': 'TestDC'},
+                                       'fullyQualifiedDomainName': 'fcr01.TestDC'},
+                     'id': 1234,
+                     'vlanNumber': 4444,
+                     'firewallInterfaces': None,
+                     'subnets': [{'id': 99, 'networkIdentifier': 1111111, 'netmask': '255.255.255.0',
+                                  'gateway': '12.12.12.12', 'subnetType': 'TEST', 'usableIpAddressCount': 1}
+                                 ]
+                     }
         vlan_mock.return_value = getObject
         result = self.run_command(['vlan', 'detail', '1234'])
         self.assert_no_fail(result)
@@ -93,6 +84,110 @@ def test_vlan_edit(self, click):
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)
 
+    @mock.patch('SoftLayer.CLI.vlan.edit.click')
+    def test_vlan_edit_not_any(self, click):
+        result = self.run_command(['vlan', 'edit', '100'])
+        self.assertEqual(result.exit_code, 2)
+        self.assertEqual("At least one option is required", result.exception.message)
+
+    def test_vlan_create_not_data_pod(self):
+        _mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
+        _mock.return_value = SoftLayer_Product_Package.getItemsVLAN
+        order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
+        order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
+
+        result = self.run_command(['vlan', 'create', '--name', 'my_vlan', '--datacenter', '', '--pod', ''])
+        self.assertIsInstance(result.exception, exceptions.CLIAbort)
+        self.assertIn("--datacenter or --pod is required to", result.exception.message)
+
+    def test_vlan_create_network_extra_route_exception(self):
+        _mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
+        _mock.return_value = SoftLayer_Product_Package.getItemsVLAN
+        order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
+        order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
+        result = self.run_command(['vlan', 'create',
+                                   '--name', 'TEST001',
+                                   '--datacenter', 'dal09',
+                                   '--network', 'private',
+                                   '--pod', 'dal09.pod01'
+                                   ])
+        self.assertIsInstance(result.exception, exceptions.CLIAbort)
+        self.assertIn("Unable to find pod name", result.exception.message)
+
+    @mock.patch('SoftLayer.CLI.formatting.confirm')
+    def test_vlan_create_network_priv(self, confirm_mock):
+        confirm_mock.return_value = True
+        get_pods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
+        get_pods_mock.return_value = [{
+            'name': 'ams03.pod01',
+            'frontendRouterId': 468136,
+            'backendRouterId': 468132}]
+        result = self.run_command(['vlan', 'create',
+                                   '--name', 'TEST001',
+                                   '--datacenter', 'ams03',
+                                   '--network', 'private',
+                                   '--pod', 'ams03.pod01'
+                                   ])
+
+        self.assert_called_with('SoftLayer_Network_Pod', 'getAllObjects')
+        self.assertIsInstance(result.exception, ex.SoftLayerError)
+
+    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
+    def test_vlan_create_option(self, confirm_mock):
+        confirm_mock.return_value = True
+        datacenter_mock = self.set_mock('SoftLayer_Location_Datacenter', 'getDatacenters')
+        datacenter_mock.return_value = [{'id': 814994, 'name': 'ams03'}]
+        router_mock = self.set_mock('SoftLayer_Location_Datacenter', 'getHardwareRouters')
+        router_mock.return_value = [{'hostname': 'bcr01a.ams03'}]
+        result = self.run_command(['vlan', 'create-options'])
+        self.assert_no_fail(result)
+
+    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
+    def test_vlan_list_get_pod_with_closed_announcement(self, ngb_mock):
+        ngb_mock.return_value = True
+        vlan_mock = self.set_mock('SoftLayer_Account', 'getNetworkVlans')
+        gpods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
+        vlan_mock.return_value = {'primaryRouter': {'fullyQualifiedDomainName': 'bcr01a.fra02.softlayer.com',
+                                                    'id': 462912},
+                                  'tagReferences': ''}
+        gpods_mock.return_value = [{'backendRouterId': 462912,
+                                    'backendRouterName': 'bcr01a.fra02',
+                                    'datacenterId': 449506,
+                                    'datacenterLongName': 'Frankfurt 2',
+                                    'frontendRouterId': 335916,
+                                    'frontendRouterName': 'fcr01a.fra02',
+                                    'name': 'fra02.pod01',
+                                    'capabilities': ['SUPPORTS_SECURITY_GROUP', 'CLOSURE_ANNOUNCED']}]
+        result = self.run_command(['vlan', 'list'])
+        self.assert_no_fail(result)
+
+    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
+    def test_vlan_list_get_pod_with_closed_announcement_no_closure(self, ngb_mock):
+        ngb_mock.return_value = True
+        vlan_mock = self.set_mock('SoftLayer_Account', 'getNetworkVlans')
+        gpods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
+        vlan_mock.return_value = {'primaryRouter': {'fullyQualifiedDomainName': 'bcr01a.fra02.softlayer.com',
+                                                    'id': 462912},
+                                  'tagReferences': ''}
+        gpods_mock.return_value = [{'backendRouterId': 462912,
+                                    'backendRouterName': 'bcr01a.fra02',
+                                    'datacenterId': 449506,
+                                    'datacenterLongName': 'Frankfurt 2',
+                                    'frontendRouterId': 335916,
+                                    'frontendRouterName': 'fcr01a.fra02',
+                                    'name': 'fra02.pod01',
+                                    'capabilities': ['SUPPORTS_SECURITY_GROUP']}]
+        result = self.run_command(['vlan', 'list'])
+        self.assert_no_fail(result)
+
+    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
+    def test_vlan_details_no_vs_no_hard_no_trunk(self, ngb_mock):
+        ngb_mock.return_value = True
+        vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
+        vlan_mock.return_value = SoftLayer_Network_Vlan.getObject_no_hard_no_vs_no_trunk
+        result = self.run_command(['vlan', 'detail', '3284824'])
+        self.assert_no_fail(result)
+
     @mock.patch('SoftLayer.CLI.vlan.edit.click')
     def test_vlan_edit_failure(self, click):
         mock = self.set_mock('SoftLayer_Network_Vlan', 'editObject')
@@ -114,7 +209,7 @@ def test_vlan_detail_firewall(self):
             'networkVlanFirewall': {
                 'datacenter': {'id': 1234, 'longName': 'TestDC'},
                 'fullyQualifiedDomainName': 'fcr01.TestDC'
-            },
+            }
         }
         vlan_mock.return_value = get_object
         result = self.run_command(['vlan', 'detail', '1234'])
@@ -132,7 +227,7 @@ def test_vlan_detail_gateway(self):
             'attachedNetworkGateway': {
                 'id': 54321,
                 "name": 'support'
-            },
+            }
         }
         vlan_mock.return_value = get_object
         result = self.run_command(['vlan', 'detail', '1234'])
@@ -164,8 +259,7 @@ def test_create_vlan(self):
                                    ])
 
         self.assert_no_fail(result)
-        self.assertEqual(json.loads(result.output),
-                         {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
+        self.assertEqual(json.loads(result.output), {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
 
     def test_create_vlan_pod(self):
         _mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
@@ -182,8 +276,7 @@ def test_create_vlan_pod(self):
                                    ])
 
         self.assert_no_fail(result)
-        self.assertEqual(json.loads(result.output),
-                         {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
+        self.assertEqual(json.loads(result.output), {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
 
     @mock.patch('SoftLayer.CLI.formatting.no_going_back')
     def test_vlan_cancel(self, confirm_mock):
@@ -204,3 +297,15 @@ def test_vlan_cancel_fail(self, confirm_mock):
         confirm_mock.return_value = False
         result = self.run_command(['vlan', 'cancel', '1234'])
         self.assertTrue(result.exit_code, 2)
+
+    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
+    def test_vlan_cancel_exception(self, confirm_mock):
+        confirm_mock.return_value = True
+        cancel_failure_mock = self.set_mock('SoftLayer_Network_Vlan', 'getCancelFailureReasons')
+        cancel_failure_mock.return_value = []
+        vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
+        vlan_mock.return_value = {}
+        result = self.run_command(['vlan', 'cancel', '1234'])
+        self.assertEqual(result.exit_code, 2)
+        self.assertIsInstance(result.exception, exceptions.CLIAbort)
+        self.assertIn("VLAN is an automatically assigned", result.exception.message)