Host network configuration tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

233 lines
9.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Red Hat, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import os
  16. import os.path
  17. import random
  18. import shutil
  19. from os_net_config import sriov_config
  20. from os_net_config.tests import base
  21. from os_net_config import utils
  22. class TestSriovConfig(base.TestCase):
  23. def setUp(self):
  24. super(TestSriovConfig, self).setUp()
  25. rand = str(int(random.random() * 100000))
  26. sriov_config._SYS_CLASS_NET = '/tmp/sys_class_net'
  27. sriov_config._UDEV_RULE_FILE = '/tmp/etc_udev_rules.d_80-persistent'\
  28. '-os-net-config.rules'
  29. sriov_config._UDEV_LEGACY_RULE_FILE = '/tmp/etc_udev_rules.d_'\
  30. '70-os-net-config-sriov.rules'
  31. sriov_config._IFUP_LOCAL_FILE = '/tmp/sbin_ifup-local'
  32. sriov_config._RESET_SRIOV_RULES_FILE = '/tmp/etc_udev_rules.d_'\
  33. '70-tripleo-reset-sriov.rules'
  34. sriov_config._ALLOCATE_VFS_FILE = '/tmp/etc_sysconfig_allocate_vfs'
  35. sriov_config._SRIOV_CONFIG_FILE = '/tmp/sriov_config_' + rand + \
  36. '.yaml'
  37. os.mkdir(sriov_config._SYS_CLASS_NET)
  38. def tearDown(self):
  39. super(TestSriovConfig, self).tearDown()
  40. if os.path.isfile(sriov_config._SRIOV_CONFIG_FILE):
  41. os.remove(sriov_config._SRIOV_CONFIG_FILE)
  42. if os.path.isfile(sriov_config._IFUP_LOCAL_FILE):
  43. os.remove(sriov_config._IFUP_LOCAL_FILE)
  44. shutil.rmtree(sriov_config._SYS_CLASS_NET)
  45. def test_configure_sriov_pf(self):
  46. exp_udev_content = '# This file is autogenerated by os-net-config\n'\
  47. 'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:10"\n'\
  48. 'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:12"\n'
  49. run_cmd = []
  50. def run_ip_config_cmd_stub(*args, **kwargs):
  51. run_cmd.append(' '.join(args))
  52. self.stub_out('os_net_config.sriov_config.run_ip_config_cmd',
  53. run_ip_config_cmd_stub)
  54. def udev_monitor_setup_stub():
  55. return
  56. self.stub_out('os_net_config.sriov_config.udev_monitor_setup',
  57. udev_monitor_setup_stub)
  58. def udev_monitor_start_stub(observer):
  59. return
  60. self.stub_out('os_net_config.sriov_config.udev_monitor_start',
  61. udev_monitor_start_stub)
  62. def udev_monitor_stop_stub(observer):
  63. return
  64. self.stub_out('os_net_config.sriov_config.udev_monitor_stop',
  65. udev_monitor_stop_stub)
  66. def cleanup_puppet_config_stub():
  67. return
  68. self.stub_out('os_net_config.sriov_config.cleanup_puppet_config',
  69. cleanup_puppet_config_stub)
  70. def trigger_udev_rules_stub():
  71. return
  72. self.stub_out('os_net_config.sriov_config.trigger_udev_rules',
  73. trigger_udev_rules_stub)
  74. def reload_udev_rules_stub():
  75. return
  76. self.stub_out('os_net_config.sriov_config.reload_udev_rules',
  77. reload_udev_rules_stub)
  78. def _wait_for_vf_creation_stub(pf_name, numvfs):
  79. numvfs_pair = {"p2p1": 10, "p2p2": 12}
  80. self.assertEqual(numvfs_pair[pf_name], numvfs)
  81. self.stub_out('os_net_config.sriov_config._wait_for_vf_creation',
  82. _wait_for_vf_creation_stub)
  83. def get_vendor_id_stub(ifname):
  84. return "0x8086"
  85. self.stub_out('os_net_config.sriov_config.get_vendor_id',
  86. get_vendor_id_stub)
  87. pf_config = [{"device_type": "pf", "name": "p2p1", "numvfs": 10,
  88. "promisc": "on", "link_mode": "legacy"},
  89. {"device_type": "pf", "name": "p2p2", "numvfs": 12,
  90. "promisc": "off", "link_mode": "legacy"}]
  91. os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
  92. os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p2/device")
  93. f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
  94. "w+")
  95. f.write("0")
  96. f.close()
  97. f = open(sriov_config._SYS_CLASS_NET + "/p2p2/device/sriov_numvfs",
  98. "w+")
  99. f.write("0")
  100. f.close()
  101. utils.write_yaml_config(sriov_config._SRIOV_CONFIG_FILE, pf_config)
  102. sriov_config.configure_logger(debug=True)
  103. sriov_config.configure_sriov_pf()
  104. f = open(sriov_config._UDEV_LEGACY_RULE_FILE, 'r')
  105. self.assertEqual(exp_udev_content, f.read())
  106. self.assertEqual(10, sriov_config.get_numvfs('p2p1'))
  107. self.assertEqual(12, sriov_config.get_numvfs('p2p2'))
  108. def test_cleanup_puppet_config_deprecation(self):
  109. content = '#!/bin/bash\n'\
  110. '/etc/sysconfig/allocate_vfs $1'
  111. f = open(sriov_config._RESET_SRIOV_RULES_FILE, "w+")
  112. f.close()
  113. f = open(sriov_config._ALLOCATE_VFS_FILE, "w+")
  114. f.close()
  115. f = open(sriov_config._IFUP_LOCAL_FILE, "w+")
  116. f.write(content)
  117. f.close()
  118. sriov_config.cleanup_puppet_config()
  119. self.assertEqual(False,
  120. os.path.exists(sriov_config._RESET_SRIOV_RULES_FILE))
  121. self.assertEqual(False,
  122. os.path.exists(sriov_config._ALLOCATE_VFS_FILE))
  123. self.assertEqual(False,
  124. os.path.exists(sriov_config._IFUP_LOCAL_FILE))
  125. def test_cleanup_puppet_config_new(self):
  126. sriov_config.cleanup_puppet_config()
  127. self.assertEqual(False,
  128. os.path.exists(sriov_config._RESET_SRIOV_RULES_FILE))
  129. self.assertEqual(False,
  130. os.path.exists(sriov_config._ALLOCATE_VFS_FILE))
  131. self.assertEqual(False,
  132. os.path.exists(sriov_config._IFUP_LOCAL_FILE))
  133. def test_cleanup_puppet_config_modified(self):
  134. content = '#!/bin/bash\n'\
  135. '/etc/sysconfig/allocate_vfs $1\n'\
  136. '/usr/sbin/ifup eth0'
  137. mod_content = '#!/bin/bash\n'\
  138. '/usr/sbin/ifup eth0'
  139. f = open(sriov_config._IFUP_LOCAL_FILE, "w+")
  140. f.write(content)
  141. f.close()
  142. sriov_config.cleanup_puppet_config()
  143. self.assertEqual(False,
  144. os.path.exists(sriov_config._RESET_SRIOV_RULES_FILE))
  145. self.assertEqual(False,
  146. os.path.exists(sriov_config._ALLOCATE_VFS_FILE))
  147. self.assertEqual(True,
  148. os.path.exists(sriov_config._IFUP_LOCAL_FILE))
  149. f = open(sriov_config._IFUP_LOCAL_FILE, "r")
  150. self.assertEqual(mod_content, f.read())
  151. def test_numvfs_config(self):
  152. os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
  153. f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
  154. "w+")
  155. f.write("0")
  156. f.close()
  157. self.assertEqual(None, sriov_config.main(['ARG0', '-n', 'p2p1:15']))
  158. self.assertEqual(15, sriov_config.get_numvfs('p2p1'))
  159. def test_numvfs_invalid_params(self):
  160. os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
  161. f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
  162. "w+")
  163. f.write("0")
  164. f.close()
  165. self.assertEqual(1, sriov_config.main(['ARG0', '-n', 'p2p1:15a']))
  166. self.assertEqual(0, sriov_config.get_numvfs('p2p1'))
  167. def test_numvfs_preconfigured(self):
  168. os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
  169. f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
  170. "w+")
  171. f.write("10")
  172. f.close()
  173. self.assertEqual(None, sriov_config.main(['ARG0', '-n', 'p2p1:15']))
  174. self.assertEqual(10, sriov_config.get_numvfs('p2p1'))
  175. def test_configure_sriov_vf(self):
  176. vf_config = [{"device_type": "vf", "device": {"name": "p2p1",
  177. "vfid": 1}, "promisc": "on", "vlan_id": 101,
  178. "qos": 5, "macaddr": "AA:BB:CC:DD:EE:FF",
  179. "spoofcheck": "on", "state": "auto", "trust": "on",
  180. "name": "p2p1_1"}]
  181. exp_cmds = ["ip link set dev p2p1 vf 1 mac AA:BB:CC:DD:EE:FF",
  182. "ip link set dev p2p1 vf 1 vlan 101 qos 5",
  183. "ip link set dev p2p1_1 promisc on",
  184. "ip link set dev p2p1 vf 1 spoofchk on",
  185. "ip link set dev p2p1 vf 1 state auto",
  186. "ip link set dev p2p1 vf 1 trust on"]
  187. run_cmd = []
  188. def run_ip_config_cmd_stub(*args, **kwargs):
  189. run_cmd.append(' '.join(args))
  190. self.stub_out('os_net_config.sriov_config.run_ip_config_cmd',
  191. run_ip_config_cmd_stub)
  192. utils.write_yaml_config(sriov_config._SRIOV_CONFIG_FILE, vf_config)
  193. sriov_config.configure_sriov_vf()
  194. for cmd in exp_cmds:
  195. self.assertIn(cmd, run_cmd)