Modify handling Ixia traffic profile parameters
[yardstick.git] / yardstick / tests / unit / network_services / libs / ixia_libs / test_ixnet_api.py
index 2416aee..91ca161 100644 (file)
@@ -16,6 +16,8 @@ import mock
 import IxNetwork
 import unittest
 
+from copy import deepcopy
+
 from yardstick.common import exceptions
 from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
 
@@ -31,11 +33,13 @@ TRAFFIC_PARAMETERS = {
         'rate': 10000.5,
         'rate_unit': 'fps',
         'outer_l2': {
-            'framesize': {'64B': '25', '256B': '75'}
+            'framesize': {'64B': '25', '256B': '75'},
+            'QinQ': None
         },
         'outer_l3': {
             'count': 512,
-            'seed': 1,
+            'srcseed': 10,
+            'dstseed': 20,
             'dscp': 0,
             'proto': 'udp',
             'ttl': 32,
@@ -61,11 +65,13 @@ TRAFFIC_PARAMETERS = {
         'rate': 75.2,
         'rate_unit': '%',
         'outer_l2': {
-            'framesize': {'128B': '35', '1024B': '65'}
+            'framesize': {'128B': '35', '1024B': '65'},
+            'QinQ': None
         },
         'outer_l3': {
             'count': 1024,
-            'seed': 1,
+            'srcseed': 30,
+            'dstseed': 40,
             'dscp': 0,
             'proto': 'udp',
             'ttl': 32,
@@ -180,6 +186,154 @@ class TestIxNextgen(unittest.TestCase):
         self.assertIn([64, 64, 75], output)
         self.assertIn([512, 512, 25], output)
 
+    def test_add_topology(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.add_topology('topology 1', 'vports')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('my_root', 'topology')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_called_once_with(
+            'obj', '-name', 'topology 1', '-vports', 'vports')
+        self.ixnet_gen.ixnet.commit.assert_called_once()
+
+    def test_add_device_group(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.add_device_group('topology', 'device group 1', '1')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('topology',
+                                                         'deviceGroup')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_called_once_with(
+            'obj', '-name', 'device group 1', '-multiplier', '1')
+        self.ixnet_gen.ixnet.commit.assert_called_once()
+
+    def test_add_ethernet(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.add_ethernet('device_group', 'ethernet 1')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('device_group',
+                                                         'ethernet')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_called_once_with(
+            'obj', '-name', 'ethernet 1')
+        self.ixnet_gen.ixnet.commit.assert_called_once()
+
+    def test_add_vlans_single(self):
+        obj = 'ethernet'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.ixnet_gen.ixnet.getList.return_value = ['vlan1', 'vlan2']
+        vlan1 = ixnet_api.Vlan(vlan_id=100, tp_id='ethertype88a8', prio=2)
+        vlan2 = ixnet_api.Vlan(vlan_id=101, tp_id='ethertype88a8', prio=3)
+        self.ixnet_gen.add_vlans(obj, [vlan1, vlan2])
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('ethernet',
+                                                               '-vlanCount', 2)
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('attr/singleValue',
+                                                               '-value', 100)
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('attr/singleValue',
+                                                               '-value', 101)
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('attr/singleValue',
+                                                               '-value', 2)
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('attr/singleValue',
+                                                               '-value', 3)
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', 'ethertype88a8')
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_vlans_increment(self):
+        obj = 'ethernet'
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.ixnet_gen.ixnet.getList.return_value = ['vlan1']
+        vlan = ixnet_api.Vlan(vlan_id=100, vlan_id_step=1, prio=3, prio_step=2)
+        self.ixnet_gen.add_vlans(obj, [vlan])
+        self.ixnet.setMultiAttribute.assert_any_call('obj', '-start', 100,
+                                                     '-step', 1,
+                                                     '-direction', 'increment')
+        self.ixnet.setMultiAttribute.assert_any_call('obj', '-start', 3,
+                                                     '-step', 2,
+                                                     '-direction', 'increment')
+
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_vlans_invalid(self):
+        vlans = []
+        self.assertRaises(RuntimeError, self.ixnet_gen.add_vlans, 'obj', vlans)
+
+    def test_add_ipv4(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.add_ipv4('ethernet 1', name='ipv4 1')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('ethernet 1', 'ipv4')
+        self.ixnet_gen.ixnet.setAttribute.assert_called_once_with('obj',
+                                                                  '-name',
+                                                                  'ipv4 1')
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_ipv4_single(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.ixnet_gen.add_ipv4('ethernet 1', name='ipv4 1', addr='100.1.1.100',
+                                prefix='24', gateway='100.1.1.200')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('ethernet 1', 'ipv4')
+        self.ixnet_gen.ixnet.setAttribute.assert_called_once_with('obj',
+                                                                  '-name',
+                                                                  'ipv4 1')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', '100.1.1.100')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', '24')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', '100.1.1.200')
+
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_ipv4_counter(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.ixnet_gen.add_ipv4('ethernet 1', name='ipv4 1',
+                                addr='100.1.1.100',
+                                addr_step='1',
+                                addr_direction='increment',
+                                prefix='24',
+                                gateway='100.1.1.200',
+                                gw_step='1',
+                                gw_direction='increment')
+        self.ixnet_gen.ixnet.add.assert_any_call('ethernet 1', 'ipv4')
+        self.ixnet_gen.ixnet.setAttribute.assert_called_once_with('obj',
+                                                                  '-name',
+                                                                  'ipv4 1')
+        self.ixnet_gen.ixnet.add.assert_any_call('attr', 'counter')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('obj', '-start',
+                                                               '100.1.1.100',
+                                                               '-step', '1',
+                                                               '-direction',
+                                                               'increment')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', '24')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call('obj', '-start',
+                                                               '100.1.1.200',
+                                                               '-step', '1',
+                                                               '-direction',
+                                                               'increment')
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_pppox_client(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.ixnet_gen.add_pppox_client('ethernet 1', 'pap', 'user', 'pwd')
+        self.ixnet_gen.ixnet.add.assert_called_once_with('ethernet 1',
+                                                         'pppoxclient')
+
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', 'pap')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', 'user')
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_any_call(
+            'attr/singleValue', '-value', 'pwd')
+
+        self.assertEqual(self.ixnet.commit.call_count, 2)
+
+    def test_add_pppox_client_invalid_auth(self):
+        self.ixnet_gen.ixnet.add.return_value = 'obj'
+        self.ixnet_gen.ixnet.getAttribute.return_value = 'attr'
+        self.assertRaises(NotImplementedError, self.ixnet_gen.add_pppox_client,
+                          'ethernet 1', 'invalid_auth', 'user', 'pwd')
+
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_not_called()
+
     @mock.patch.object(IxNetwork, 'IxNet')
     def test_connect(self, mock_ixnet):
         mock_ixnet.return_value = self.ixnet
@@ -233,8 +387,8 @@ class TestIxNextgen(unittest.TestCase):
         self.ixnet_gen._cfg = config
 
         self.assertIsNone(self.ixnet_gen.assign_ports())
-        self.assertEqual(self.ixnet.execute.call_count, 2)
-        self.assertEqual(self.ixnet.commit.call_count, 4)
+        self.assertEqual(self.ixnet.execute.call_count, 1)
+        self.assertEqual(self.ixnet.commit.call_count, 3)
         self.assertEqual(self.ixnet.getAttribute.call_count, 2)
 
     @mock.patch.object(ixnet_api, 'log')
@@ -267,17 +421,24 @@ class TestIxNextgen(unittest.TestCase):
                                 '-trackBy', 'trafficGroupId0')
 
     def test__create_flow_groups(self):
+        uplink_endpoints = ['up_endp1', 'up_endp2']
+        downlink_endpoints = ['down_endp1', 'down_endp2']
         self.ixnet_gen.ixnet.getList.side_effect = [['traffic_item'], ['1', '2']]
-        self.ixnet_gen.ixnet.add.side_effect = ['endp1', 'endp2']
-        self.ixnet_gen._create_flow_groups()
+        self.ixnet_gen.ixnet.add.side_effect = ['endp1', 'endp2', 'endp3',
+                                                'endp4']
+        self.ixnet_gen._create_flow_groups(uplink_endpoints, downlink_endpoints)
         self.ixnet_gen.ixnet.add.assert_has_calls([
             mock.call('traffic_item', 'endpointSet'),
             mock.call('traffic_item', 'endpointSet')])
         self.ixnet_gen.ixnet.setMultiAttribute.assert_has_calls([
-            mock.call('endp1', '-name', '1', '-sources', ['1/protocols'],
-                      '-destinations', ['2/protocols']),
-            mock.call('endp2', '-name', '2', '-sources', ['2/protocols'],
-                      '-destinations', ['1/protocols'])])
+            mock.call('endp1', '-name', '1', '-sources', ['up_endp1'],
+                      '-destinations', ['down_endp1']),
+            mock.call('endp2', '-name', '2', '-sources', ['down_endp1'],
+                      '-destinations', ['up_endp1']),
+            mock.call('endp3', '-name', '3', '-sources', ['up_endp2'],
+                      '-destinations', ['down_endp2']),
+            mock.call('endp4', '-name', '4', '-sources', ['down_endp2'],
+                      '-destinations', ['up_endp2'])])
 
     def test__append_protocol_to_stack(self):
 
@@ -307,12 +468,31 @@ class TestIxNextgen(unittest.TestCase):
     def test_create_traffic_model(self, mock__setup_config_elements,
                                   mock__create_flow_groups,
                                   mock__create_traffic_item):
-
-        self.ixnet_gen.create_traffic_model()
-        mock__create_traffic_item.assert_called_once()
-        mock__create_flow_groups.assert_called_once()
+        uplink_ports = ['port1', 'port3']
+        downlink_ports = ['port2', 'port4']
+        uplink_endpoints = ['port1/protocols', 'port3/protocols']
+        downlink_endpoints = ['port2/protocols', 'port4/protocols']
+        self.ixnet_gen.create_traffic_model(uplink_ports, downlink_ports)
+        mock__create_traffic_item.assert_called_once_with('raw')
+        mock__create_flow_groups.assert_called_once_with(uplink_endpoints,
+                                                         downlink_endpoints)
         mock__setup_config_elements.assert_called_once()
 
+    @mock.patch.object(ixnet_api.IxNextgen, '_create_traffic_item')
+    @mock.patch.object(ixnet_api.IxNextgen, '_create_flow_groups')
+    @mock.patch.object(ixnet_api.IxNextgen, '_setup_config_elements')
+    def test_create_ipv4_traffic_model(self, mock__setup_config_elements,
+                                       mock__create_flow_groups,
+                                       mock__create_traffic_item):
+        uplink_topologies = ['up1', 'up3']
+        downlink_topologies = ['down2', 'down4']
+        self.ixnet_gen.create_ipv4_traffic_model(uplink_topologies,
+                                                 downlink_topologies)
+        mock__create_traffic_item.assert_called_once_with('ipv4')
+        mock__create_flow_groups.assert_called_once_with(uplink_topologies,
+                                                         downlink_topologies)
+        mock__setup_config_elements.assert_called_once_with(False)
+
     def test__update_frame_mac(self):
         with mock.patch.object(self.ixnet_gen, '_get_field_in_stack_item') as \
                 mock_get_field:
@@ -339,6 +519,46 @@ class TestIxNextgen(unittest.TestCase):
         self.assertEqual(6, len(self.ixnet_gen.ixnet.setMultiAttribute.mock_calls))
         self.assertEqual(4, len(mock_update_frame.mock_calls))
 
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_has_calls([
+            mock.call('cfg_element/transmissionControl',
+                      '-type', 'continuous', '-duration', 50)
+        ])
+
+    def test_update_frame_qinq(self):
+        with mock.patch.object(self.ixnet_gen,
+                               '_get_config_element_by_flow_group_name',
+                               return_value='cfg_element'), \
+             mock.patch.object(self.ixnet_gen, '_update_frame_mac'),\
+             mock.patch.object(self.ixnet_gen, '_get_stack_item',
+                               return_value='item'), \
+             mock.patch.object(self.ixnet_gen, '_get_field_in_stack_item',
+                               return_value='field'):
+
+            traffic_parameters = deepcopy(TRAFFIC_PARAMETERS)
+            traffic_parameters[UPLINK]['outer_l2']['QinQ'] = {
+                'S-VLAN': {'id': 128,
+                           'priority': 1,
+                           'cfi': 0},
+                'C-VLAN': {'id': 512,
+                           'priority': 0,
+                           'cfi': 2}
+            }
+
+            self.ixnet_gen.update_frame(traffic_parameters, 50)
+
+        self.ixnet_gen.ixnet.setMultiAttribute.assert_has_calls([
+            mock.call('field', '-auto', 'false', '-singleValue', '0x88a8',
+                      '-fieldValue', '0x88a8', '-valueType', 'singleValue'),
+            mock.call('field', '-auto', 'false', '-singleValue', 1,
+                      '-fieldValue', 1, '-valueType', 'singleValue'),
+            mock.call('field', '-auto', 'false', '-singleValue', 128,
+                      '-fieldValue', 128, '-valueType', 'singleValue'),
+            mock.call('field', '-auto', 'false', '-singleValue', 512,
+                      '-fieldValue', 512, '-valueType', 'singleValue'),
+            mock.call('field', '-auto', 'false', '-singleValue', 2,
+                      '-fieldValue', 2, '-valueType', 'singleValue')
+        ], any_order=True)
+
     def test_update_frame_flow_not_present(self):
         with mock.patch.object(
                 self.ixnet_gen, '_get_config_element_by_flow_group_name',
@@ -423,6 +643,9 @@ class TestIxNextgen(unittest.TestCase):
                 'outer_l3': {
                     'proto': 'unsupported',
                 },
+                'outer_l4': {
+                    'seed': 1
+                }
             },
         }
         with mock.patch.object(self.ixnet_gen,
@@ -478,3 +701,16 @@ class TestIxNextgen(unittest.TestCase):
         self.assertIsNone(result)
         self.ixnet.getList.assert_called_once()
         self.assertEqual(3, self.ixnet_gen._ixnet.execute.call_count)
+
+    def test_start_protocols(self):
+        self.ixnet_gen.start_protocols()
+        self.ixnet.execute.assert_called_once_with('startAllProtocols')
+
+    def test_stop_protocols(self):
+        self.ixnet_gen.stop_protocols()
+        self.ixnet.execute.assert_called_once_with('stopAllProtocols')
+
+    def test_get_vports(self):
+        self.ixnet_gen._ixnet.getRoot.return_value = 'root'
+        self.ixnet_gen.get_vports()
+        self.ixnet.getList.assert_called_once_with('root', 'vport')