Using tempfile instead of fixed path in unit tests.

Change-Id: Ib4d40bac8e25600be97cdae3715c87449aad573b
This commit is contained in:
Karthik S 2019-11-25 05:41:05 +00:00
parent 04bea45124
commit 3e938d82f5

View File

@ -18,6 +18,8 @@ import os
import os.path
import random
import shutil
import tempfile
from os_net_config import sriov_config
from os_net_config.tests import base
@ -25,23 +27,26 @@ from os_net_config import utils
class TestSriovConfig(base.TestCase):
"""Unit tests for methods defined in sriov_config.py"""
def setUp(self):
super(TestSriovConfig, self).setUp()
rand = str(int(random.random() * 100000))
sriov_config._SYS_CLASS_NET = '/tmp/sys_class_net'
sriov_config._UDEV_RULE_FILE = '/tmp/etc_udev_rules.d_80-persistent'\
'-os-net-config.rules'
sriov_config._UDEV_LEGACY_RULE_FILE = '/tmp/etc_udev_rules.d_'\
'70-os-net-config-sriov.rules'
sriov_config._IFUP_LOCAL_FILE = '/tmp/sbin_ifup-local'
sriov_config._RESET_SRIOV_RULES_FILE = '/tmp/etc_udev_rules.d_'\
'70-tripleo-reset-sriov.rules'
sriov_config._ALLOCATE_VFS_FILE = '/tmp/etc_sysconfig_allocate_vfs'
sriov_config._SRIOV_CONFIG_FILE = '/tmp/sriov_config_' + rand + \
tmpdir = tempfile.mkdtemp()
self.stub_out('os_net_config.sriov_config._SYS_CLASS_NET', tmpdir)
sriov_config._UDEV_RULE_FILE = '/tmp/' + rand + 'etc_udev_rules.d'\
'80-persistent-os-net-config.rules'
sriov_config._UDEV_LEGACY_RULE_FILE = '/tmp/' + rand + 'etc_udev_'\
'rules.d_70-os-net-config-sriov.rules'
sriov_config._IFUP_LOCAL_FILE = '/tmp/' + rand + 'sbin_ifup-local'
sriov_config._RESET_SRIOV_RULES_FILE = '/tmp/' + rand + 'etc_udev_'\
'rules.d_70-tripleo-reset-sriov.rules'
sriov_config._ALLOCATE_VFS_FILE = '/tmp/' + rand + 'etc_sysconfig_'\
'allocate_vfs'
sriov_config._SRIOV_CONFIG_FILE = '/tmp/' + rand + 'sriov_config'\
'.yaml'
os.mkdir(sriov_config._SYS_CLASS_NET)
def tearDown(self):
super(TestSriovConfig, self).tearDown()
@ -50,8 +55,19 @@ class TestSriovConfig(base.TestCase):
if os.path.isfile(sriov_config._IFUP_LOCAL_FILE):
os.remove(sriov_config._IFUP_LOCAL_FILE)
shutil.rmtree(sriov_config._SYS_CLASS_NET)
if os.path.isfile(sriov_config._RESET_SRIOV_RULES_FILE):
os.remove(sriov_config._RESET_SRIOV_RULES_FILE)
if os.path.isfile(sriov_config._ALLOCATE_VFS_FILE):
os.remove(sriov_config._ALLOCATE_VFS_FILE)
if os.path.isfile(sriov_config._UDEV_LEGACY_RULE_FILE):
os.remove(sriov_config._UDEV_LEGACY_RULE_FILE)
def test_configure_sriov_pf(self):
"""Test the numvfs setting for SR-IOV PF
Test the udev rules created for legacy mode of SR-IOV PF
"""
exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:10"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:12"\n'
@ -131,6 +147,12 @@ class TestSriovConfig(base.TestCase):
self.assertEqual(12, sriov_config.get_numvfs('p2p2'))
def test_cleanup_puppet_config_deprecation(self):
"""Test the cleanup of puppet-tripleo generated config file.
Usecase: The ifup-local has the default content generated by
puppet-tripleo
"""
content = '#!/bin/bash\n'\
'/etc/sysconfig/allocate_vfs $1'
f = open(sriov_config._RESET_SRIOV_RULES_FILE, "w+")
@ -150,6 +172,12 @@ class TestSriovConfig(base.TestCase):
os.path.exists(sriov_config._IFUP_LOCAL_FILE))
def test_cleanup_puppet_config_new(self):
"""Test the cleanup of puppet-tripleo generated config file.
Usecase: When os-net-config is run on fresh deployments, all these
files will not exist.
"""
sriov_config.cleanup_puppet_config()
self.assertEqual(False,
os.path.exists(sriov_config._RESET_SRIOV_RULES_FILE))
@ -159,6 +187,13 @@ class TestSriovConfig(base.TestCase):
os.path.exists(sriov_config._IFUP_LOCAL_FILE))
def test_cleanup_puppet_config_modified(self):
"""Test the cleanup of puppet-tripleo generated config file
Usecase: When os-net-config is run first time after the deprecation
of NeutronSriovNumVFs and ifup-local has contents other than invoking
allocate_vfs.
"""
content = '#!/bin/bash\n'\
'/etc/sysconfig/allocate_vfs $1\n'\
'/usr/sbin/ifup eth0'
@ -180,6 +215,8 @@ class TestSriovConfig(base.TestCase):
self.assertEqual(mod_content, f.read())
def test_numvfs_config(self):
"""Test the numvfs config with valid arguments"""
os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
"w+")
@ -189,6 +226,8 @@ class TestSriovConfig(base.TestCase):
self.assertEqual(15, sriov_config.get_numvfs('p2p1'))
def test_numvfs_invalid_params(self):
"""Test the numvfs config with invalid arguments"""
os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
"w+")
@ -198,6 +237,8 @@ class TestSriovConfig(base.TestCase):
self.assertEqual(0, sriov_config.get_numvfs('p2p1'))
def test_numvfs_preconfigured(self):
"""Test the numvfs config while its already configured"""
os.makedirs(sriov_config._SYS_CLASS_NET + "/p2p1/device")
f = open(sriov_config._SYS_CLASS_NET + "/p2p1/device/sriov_numvfs",
"w+")
@ -207,6 +248,8 @@ class TestSriovConfig(base.TestCase):
self.assertEqual(10, sriov_config.get_numvfs('p2p1'))
def test_configure_sriov_vf(self):
"""Test configuration of SR-IOV VF settings"""
vf_config = [{"device_type": "vf", "device": {"name": "p2p1",
"vfid": 1}, "promisc": "on", "vlan_id": 101,
"qos": 5, "macaddr": "AA:BB:CC:DD:EE:FF",