Skip to content

Commit

Permalink
Throw exception when not use swsssdk in UT. (sonic-net#126)
Browse files Browse the repository at this point in the history
#### Why I did it
swsssdk been deprecated after 202205 branch, all use should switch to sonic-swss-common.

#### How I did it
Throw exception when load swsssdk for none UT usage.

#### How to verify it
Pass all UT.

#### Which release branch to backport (provide reason below if selected)

#### Description for the changelog
Throw exception when load swsssdk for none UT usage.

#### Link to config_db schema for YANG module changes

#### A picture of a cute animal (not mandatory but encouraged)
  • Loading branch information
liuh-80 authored and EdenGri committed Oct 30, 2022
1 parent cc847a2 commit 157f30a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
'hiredis>=0.1.4'
]

console_scripts={
'sonic-db-load = swsssdk:sonic_db_dump_load',
'sonic-db-dump = swsssdk:sonic_db_dump_load',
}

setup(
name='swsssdk',
version='2.0.1',
Expand All @@ -26,12 +31,7 @@
extras_require={
'high_perf': high_performance_deps
},
entry_points={
'console_scripts': [
'sonic-db-load = swsssdk:sonic_db_dump_load',
'sonic-db-dump = swsssdk:sonic_db_dump_load',
],
},
entry_points={},
classifiers=[
'Intended Audience :: Developers',
'Operating System :: Linux',
Expand Down
8 changes: 8 additions & 0 deletions src/swsssdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
"""
Utility library for Switch-state Redis database access and syslog reporting.
"""
import sys
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.NullHandler())

if ('unittest' not in sys.modules.keys() and
'mockredis' not in sys.modules.keys() and
'mock' not in sys.modules.keys()):
msg = "sonic-py-swsssdk been deprecated, please switch to sonic-swss-common."
logger.exception(msg)
raise ImportError("sonic-py-swsssdk been deprecated, please switch to sonic-swss-common.")

try:
from .dbconnector import SonicDBConfig, SonicV2Connector
from .configdb import ConfigDBConnector, ConfigDBPipeConnector
Expand Down
9 changes: 8 additions & 1 deletion test/test_moduleLoad.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
sys.path.insert(0, os.path.join(modules_path, 'src'))

from unittest import TestCase

import subprocess

class Test_load_sonic_db_config(TestCase):
def test__db_map_attributes(self):
Expand Down Expand Up @@ -34,3 +34,10 @@ def test__dbConfig(self):
for namespace in list(dbConfig.get_ns_list()):
self.assertEqual(dbConfig.get_dbid('PFC_WD_DB', namespace), 5)
self.assertEqual(dbConfig.get_dbid('APPL_DB', namespace), 0)

def test_BlockUseSwsssdk():
# Import swsssdk will throw exception with deprecated message.
swsssdk_path = os.path.join(modules_path, 'src')
result = subprocess.run(["python", "-c", "import swsssdk;exit()"], capture_output=True, cwd=swsssdk_path)

assert "deprecated" in result.stderr.decode("utf-8")

0 comments on commit 157f30a

Please sign in to comment.