Skip to content

Commit 97f0fd9

Browse files
authored
[Platform API][pytest] Handle both 'str' and 'unicode' string types in Python 2 (#2210)
Python 2 has two string types, `str` and `unicode`. Both share a `basestring` superclass. In Python 3, there is only one `str` type. This patch allows the platform API tests to consider a string type valid whether it is ASCII (`str`) or Unicode in Python 2. It also future-proofs the inevitable transition to Python 3 by checking the executing Python version. Note that test_chassis.py is not modified here, as the change is being applied as part of #2209
1 parent 590562b commit 97f0fd9

7 files changed

Lines changed: 99 additions & 28 deletions

File tree

tests/platform_tests/api/test_component.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
from platform_api_test_base import PlatformApiTestBase
1111

12+
###################################################
13+
# TODO: Remove this after we transition to Python 3
14+
import sys
15+
if sys.version_info.major == 3:
16+
STRING_TYPE = str
17+
else:
18+
STRING_TYPE = basestring
19+
# END Remove this after we transition to Python 3
20+
###################################################
21+
22+
1223
logger = logging.getLogger(__name__)
1324

1425
pytestmark = [
@@ -48,7 +59,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
4859
for i in range(self.num_components):
4960
name = component.get_name(platform_api_conn, i)
5061
if self.expect(name is not None, "Component {}: Unable to retrieve name".format(i)):
51-
self.expect(isinstance(name, str), "Component {}: Name appears incorrect".format(i))
62+
self.expect(isinstance(name, STRING_TYPE), "Component {}: Name appears incorrect".format(i))
5263
self.assert_expectations()
5364

5465
def test_get_presence(self, duthost, localhost, platform_api_conn):
@@ -70,7 +81,7 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
7081
for i in range(self.num_components):
7182
model = component.get_model(platform_api_conn, i)
7283
if self.expect(model is not None, "Component {}: Unable to retrieve model".format(i)):
73-
self.expect(isinstance(model, str), "Component {}: Model appears incorrect".format(i))
84+
self.expect(isinstance(model, STRING_TYPE), "Component {}: Model appears incorrect".format(i))
7485
self.assert_expectations()
7586

7687
def test_get_serial(self, duthost, localhost, platform_api_conn):
@@ -80,7 +91,7 @@ def test_get_serial(self, duthost, localhost, platform_api_conn):
8091
for i in range(self.num_components):
8192
serial = component.get_serial(platform_api_conn, i)
8293
if self.expect(serial is not None, "Component {}: Unable to retrieve serial number".format(i)):
83-
self.expect(isinstance(serial, str), "Component {}: Serial number appears incorrect".format(i))
94+
self.expect(isinstance(serial, STRING_TYPE), "Component {}: Serial number appears incorrect".format(i))
8495
self.assert_expectations()
8596

8697
def test_get_status(self, duthost, localhost, platform_api_conn):
@@ -105,7 +116,7 @@ def test_get_description(self, duthost, localhost, platform_api_conn):
105116
for i in range(self.num_components):
106117
description = component.get_description(platform_api_conn, i)
107118
if self.expect(description is not None, "Component {}: Failed to retrieve description".format(i)):
108-
self.expect(isinstance(description, str), "Component {}: Description appears to be incorrect".format(i))
119+
self.expect(isinstance(description, STRING_TYPE), "Component {}: Description appears to be incorrect".format(i))
109120
self.assert_expectations()
110121

111122
def test_get_firmware_version(self, duthost, localhost, platform_api_conn):
@@ -115,7 +126,7 @@ def test_get_firmware_version(self, duthost, localhost, platform_api_conn):
115126
for i in range(self.num_components):
116127
fw_version = component.get_firmware_version(platform_api_conn, i)
117128
if self.expect(fw_version is not None, "Component {}: Failed to retrieve firmware version".format(i)):
118-
self.expect(isinstance(fw_version, str), "Component {}: Firmware version appears to be incorrect".format(i))
129+
self.expect(isinstance(fw_version, STRING_TYPE), "Component {}: Firmware version appears to be incorrect".format(i))
119130
self.assert_expectations()
120131

121132
def test_get_available_firmware_version(self, duthost, localhost, platform_api_conn):
@@ -126,7 +137,7 @@ def test_get_available_firmware_version(self, duthost, localhost, platform_api_c
126137
for image in range(image_list):
127138
avail_fw_version = component.get_available_firmware_version(platform_api_conn, i, image)
128139
if self.expect(avail_fw_version is not None, "Component {}: Failed to retrieve available firmware version from image {}".format(i, image)):
129-
self.expect(isinstance(avail_fw_version, str), "Component {}: Available Firmware version appears to be incorrect from image {}".format(i, image))
140+
self.expect(isinstance(avail_fw_version, STRING_TYPE), "Component {}: Available Firmware version appears to be incorrect from image {}".format(i, image))
130141
self.assert_expectations()
131142

132143
def test_get_firmware_update_notification(self, duthost, localhost, platform_api_conn):
@@ -137,7 +148,7 @@ def test_get_firmware_update_notification(self, duthost, localhost, platform_api
137148
for image in range(image_list):
138149
notif = component.get_firmware_update_notification(platform_api_conn, i, image)
139150
# Can return "None" if no update required.
140-
pytest_assert(isinstance(notif, str), "Component {}: Firmware update notification appears to be incorrect from image {}".format(i, image))
151+
pytest_assert(isinstance(notif, STRING_TYPE), "Component {}: Firmware update notification appears to be incorrect from image {}".format(i, image))
141152

142153
def test_install_firmware(self, duthost, localhost, platform_api_conn):
143154
if self.num_components == 0:

tests/platform_tests/api/test_fan.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
from platform_api_test_base import PlatformApiTestBase
1313

14+
###################################################
15+
# TODO: Remove this after we transition to Python 3
16+
import sys
17+
if sys.version_info.major == 3:
18+
STRING_TYPE = str
19+
else:
20+
STRING_TYPE = basestring
21+
# END Remove this after we transition to Python 3
22+
###################################################
23+
1424
logger = logging.getLogger(__name__)
1525

1626
pytestmark = [
@@ -52,7 +62,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
5262
name = fan.get_name(platform_api_conn, i)
5363

5464
if self.expect(name is not None, "Unable to retrieve Fan {} name".format(i)):
55-
self.expect(isinstance(name, str), "Fan {} name appears incorrect".format(i))
65+
self.expect(isinstance(name, STRING_TYPE), "Fan {} name appears incorrect".format(i))
5666

5767
self.assert_expectations()
5868

@@ -71,7 +81,7 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
7181
model = fan.get_model(platform_api_conn, i)
7282

7383
if self.expect(model is not None, "Unable to retrieve fan {} model".format(i)):
74-
self.expect(isinstance(model, str), "Fan {} model appears incorrect".format(i))
84+
self.expect(isinstance(model, STRING_TYPE), "Fan {} model appears incorrect".format(i))
7585

7686
self.assert_expectations()
7787

@@ -80,7 +90,7 @@ def test_get_serial(self, duthost, localhost, platform_api_conn):
8090
serial = fan.get_serial(platform_api_conn, i)
8191

8292
if self.expect(serial is not None, "Unable to retrieve fan {} serial number".format(i)):
83-
self.expect(isinstance(serial, str), "Fan {} serial number appears incorrect".format(i))
93+
self.expect(isinstance(serial, STRING_TYPE), "Fan {} serial number appears incorrect".format(i))
8494

8595
self.assert_expectations()
8696

@@ -180,7 +190,7 @@ def test_set_fans_led(self, duthost, localhost, platform_api_conn):
180190
color_actual = fan.get_status_led(platform_api_conn, i)
181191

182192
if self.expect(color_actual is not None, "Failed to retrieve status_led"):
183-
if self.expect(isinstance(color_actual, str), "Status LED color appears incorrect"):
193+
if self.expect(isinstance(color_actual, STRING_TYPE), "Status LED color appears incorrect"):
184194
self.expect(color == color_actual, "Status LED color incorrect (expected: {}, actual: {} for fan {})".format(
185195
color, color_actual, i))
186196

tests/platform_tests/api/test_fan_drawer.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
from platform_api_test_base import PlatformApiTestBase
1313

14+
###################################################
15+
# TODO: Remove this after we transition to Python 3
16+
import sys
17+
if sys.version_info.major == 3:
18+
STRING_TYPE = str
19+
else:
20+
STRING_TYPE = basestring
21+
# END Remove this after we transition to Python 3
22+
###################################################
23+
1424
logger = logging.getLogger(__name__)
1525

1626
pytestmark = [
@@ -57,7 +67,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
5767
name = fan_drawer.get_name(platform_api_conn, i)
5868

5969
if self.expect(name is not None, "Unable to retrieve Fan_drawer {} name".format(i)):
60-
self.expect(isinstance(name, str), "Fan_drawer {} name appears incorrect".format(i))
70+
self.expect(isinstance(name, STRING_TYPE), "Fan_drawer {} name appears incorrect".format(i))
6171
if self.fan_drawer_truth:
6272
self.expect(name == self.fan_drawer_truth[i]['name'], "Fan_drawer {} name does not match, expected name {}".format(i, self.fan_drawer_truth[i]['name']))
6373

@@ -78,7 +88,7 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
7888
model = fan_drawer.get_model(platform_api_conn, i)
7989

8090
if self.expect(model is not None, "Unable to retrieve fan_drawer {} model".format(i)):
81-
self.expect(isinstance(model, str), "Fan_drawer {} model appears incorrect".format(i))
91+
self.expect(isinstance(model, STRING_TYPE), "Fan_drawer {} model appears incorrect".format(i))
8292

8393
self.assert_expectations()
8494

@@ -87,7 +97,7 @@ def test_get_serial(self, duthost, localhost, platform_api_conn):
8797
serial = fan_drawer.get_serial(platform_api_conn, i)
8898

8999
if self.expect(serial is not None, "Unable to retrieve fan_drawer {} serial number".format(i)):
90-
self.expect(isinstance(serial, str), "Fan_drawer {} serial number appears incorrect".format(i))
100+
self.expect(isinstance(serial, STRING_TYPE), "Fan_drawer {} serial number appears incorrect".format(i))
91101

92102
self.assert_expectations()
93103

@@ -139,7 +149,7 @@ def test_set_fan_drawers_led(self, duthost, localhost, platform_api_conn):
139149
color_actual = fan_drawer.get_status_led(platform_api_conn, i)
140150

141151
if self.expect(color_actual is not None, "Failed to retrieve status_led"):
142-
if self.expect(isinstance(color_actual, str), "Status LED color appears incorrect"):
152+
if self.expect(isinstance(color_actual, STRING_TYPE), "Status LED color appears incorrect"):
143153
self.expect(color == color_actual, "Status LED color incorrect (expected: {}, actual: {} for fan_drawer {})".format(
144154
color, color_actual, i))
145155

tests/platform_tests/api/test_module.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
from platform_api_test_base import PlatformApiTestBase
1010

11+
###################################################
12+
# TODO: Remove this after we transition to Python 3
13+
import sys
14+
if sys.version_info.major == 3:
15+
STRING_TYPE = str
16+
else:
17+
STRING_TYPE = basestring
18+
# END Remove this after we transition to Python 3
19+
###################################################
20+
1121
logger = logging.getLogger(__name__)
1222

1323
pytestmark = [
@@ -67,7 +77,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
6777
for i in range(self.num_modules):
6878
name = module.get_name(platform_api_conn, i)
6979
if self.expect(name is not None, "Unable to retrieve module {} name".format(i)):
70-
self.expect(isinstance(name, str), "Module {} name appears incorrect".format(i))
80+
self.expect(isinstance(name, STRING_TYPE), "Module {} name appears incorrect".format(i))
7181
self.assert_expectations()
7282

7383
def test_get_presence(self, duthost, localhost, platform_api_conn):
@@ -88,7 +98,7 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
8898
for i in range(self.num_modules):
8999
model = module.get_model(platform_api_conn, i)
90100
if self.expect(model is not None, "Unable to retrieve module {} model".format(i)):
91-
self.expect(isinstance(model, str), "Module {} model appears incorrect".format(i))
101+
self.expect(isinstance(model, STRING_TYPE), "Module {} model appears incorrect".format(i))
92102
self.assert_expectations()
93103

94104
def test_get_serial(self, duthost, localhost, platform_api_conn):
@@ -98,7 +108,7 @@ def test_get_serial(self, duthost, localhost, platform_api_conn):
98108
for i in range(self.num_modules):
99109
serial = module.get_serial(platform_api_conn, i)
100110
if self.expect(serial is not None, "Unable to retrieve module {} serial number".format(i)):
101-
self.expect(isinstance(serial, str), "Module {} serial number appears incorrect".format(i))
111+
self.expect(isinstance(serial, STRING_TYPE), "Module {} serial number appears incorrect".format(i))
102112
self.assert_expectations()
103113

104114
def test_get_status(self, duthost, localhost, platform_api_conn):

tests/platform_tests/api/test_psu.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
from platform_api_test_base import PlatformApiTestBase
1111

12+
###################################################
13+
# TODO: Remove this after we transition to Python 3
14+
import sys
15+
if sys.version_info.major == 3:
16+
STRING_TYPE = str
17+
else:
18+
STRING_TYPE = basestring
19+
# END Remove this after we transition to Python 3
20+
###################################################
21+
1222

1323
logger = logging.getLogger(__name__)
1424

@@ -43,7 +53,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
4353
for i in range(self.num_psus):
4454
name = psu.get_name(platform_api_conn, i)
4555
if self.expect(name is not None, "Unable to retrieve PSU {} name".format(i)):
46-
self.expect(isinstance(name, str), "PSU {} name appears incorrect".format(i))
56+
self.expect(isinstance(name, STRING_TYPE), "PSU {} name appears incorrect".format(i))
4757
self.assert_expectations()
4858

4959
def test_get_presence(self, duthost, localhost, platform_api_conn):
@@ -58,14 +68,14 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
5868
for i in range(self.num_psus):
5969
model = psu.get_model(platform_api_conn, i)
6070
if self.expect(model is not None, "Unable to retrieve PSU {} model".format(i)):
61-
self.expect(isinstance(model, str), "PSU {} model appears incorrect".format(i))
71+
self.expect(isinstance(model, STRING_TYPE), "PSU {} model appears incorrect".format(i))
6272
self.assert_expectations()
6373

6474
def test_get_serial(self, duthost, localhost, platform_api_conn):
6575
for i in range(self.num_psus):
6676
serial = psu.get_serial(platform_api_conn, i)
6777
if self.expect(serial is not None, "Unable to retrieve PSU {} serial number".format(i)):
68-
self.expect(isinstance(serial, str), "PSU {} serial number appears incorrect".format(i))
78+
self.expect(isinstance(serial, STRING_TYPE), "PSU {} serial number appears incorrect".format(i))
6979
self.assert_expectations()
7080

7181
def test_get_status(self, duthost, localhost, platform_api_conn):
@@ -160,7 +170,7 @@ def test_led(self, duthost, localhost, platform_api_conn):
160170

161171
color_actual = psu.get_status_led(platform_api_conn, psu_id)
162172
if self.expect(color_actual is not None, "Failed to retrieve status_led of PSU {}".format(psu_id)):
163-
if self.expect(isinstance(color_actual, str), "PSU {} status LED color appears incorrect".format(psu_id)):
173+
if self.expect(isinstance(color_actual, STRING_TYPE), "PSU {} status LED color appears incorrect".format(psu_id)):
164174
self.expect(color == color_actual, "Status LED color incorrect (expected: {}, actual: {}) from PSU {}".format(color, color_actual, psu_id))
165175
self.assert_expectations()
166176

tests/platform_tests/api/test_sfp.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
from platform_api_test_base import PlatformApiTestBase
1111

12+
###################################################
13+
# TODO: Remove this after we transition to Python 3
14+
import sys
15+
if sys.version_info.major == 3:
16+
STRING_TYPE = str
17+
else:
18+
STRING_TYPE = basestring
19+
# END Remove this after we transition to Python 3
20+
###################################################
21+
1222
logger = logging.getLogger(__name__)
1323

1424
pytestmark = [
@@ -104,7 +114,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
104114
for i in range(self.num_sfps):
105115
name = sfp.get_name(platform_api_conn, i)
106116
if self.expect(name is not None, "Unable to retrieve transceiver {} name".format(i)):
107-
self.expect(isinstance(name, str), "Transceiver {} name appears incorrect".format(i))
117+
self.expect(isinstance(name, STRING_TYPE), "Transceiver {} name appears incorrect".format(i))
108118
self.assert_expectations()
109119

110120
def test_get_presence(self, duthost, localhost, platform_api_conn):
@@ -119,14 +129,14 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
119129
for i in range(self.num_sfps):
120130
model = sfp.get_model(platform_api_conn, i)
121131
if self.expect(model is not None, "Unable to retrieve transceiver {} model".format(i)):
122-
self.expect(isinstance(model, str), "Transceiver {} model appears incorrect".format(i))
132+
self.expect(isinstance(model, STRING_TYPE), "Transceiver {} model appears incorrect".format(i))
123133
self.assert_expectations()
124134

125135
def test_get_serial(self, duthost, localhost, platform_api_conn):
126136
for i in range(self.num_sfps):
127137
serial = sfp.get_serial(platform_api_conn, i)
128138
if self.expect(serial is not None, "Unable to retrieve transceiver {} serial number".format(i)):
129-
self.expect(isinstance(serial, str), "Transceiver {} serial number appears incorrect".format(i))
139+
self.expect(isinstance(serial, STRING_TYPE), "Transceiver {} serial number appears incorrect".format(i))
130140
self.assert_expectations()
131141

132142
def test_get_status(self, duthost, localhost, platform_api_conn):

tests/platform_tests/api/test_thermal.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
from platform_api_test_base import PlatformApiTestBase
1313

14+
###################################################
15+
# TODO: Remove this after we transition to Python 3
16+
import sys
17+
if sys.version_info.major == 3:
18+
STRING_TYPE = str
19+
else:
20+
STRING_TYPE = basestring
21+
# END Remove this after we transition to Python 3
22+
###################################################
23+
1424
logger = logging.getLogger(__name__)
1525

1626
pytestmark = [
@@ -43,7 +53,7 @@ def test_get_name(self, duthost, localhost, platform_api_conn):
4353
name = thermal.get_name(platform_api_conn, i)
4454

4555
if self.expect(name is not None, "Unable to retrieve Thermal {} name".format(i)):
46-
self.expect(isinstance(name, str), "Thermal {} name appears incorrect".format(i))
56+
self.expect(isinstance(name, STRING_TYPE), "Thermal {} name appears incorrect".format(i))
4757

4858
self.assert_expectations()
4959

@@ -62,7 +72,7 @@ def test_get_model(self, duthost, localhost, platform_api_conn):
6272
model = thermal.get_model(platform_api_conn, i)
6373

6474
if self.expect(model is not None, "Unable to retrieve thermal {} model".format(i)):
65-
self.expect(isinstance(model, str), "Thermal {} model appears incorrect".format(i))
75+
self.expect(isinstance(model, STRING_TYPE), "Thermal {} model appears incorrect".format(i))
6676

6777
self.assert_expectations()
6878

@@ -71,7 +81,7 @@ def test_get_serial(self, duthost, localhost, platform_api_conn):
7181
serial = thermal.get_serial(platform_api_conn, i)
7282

7383
if self.expect(serial is not None, "Unable to retrieve thermal {} serial number".format(i)):
74-
self.expect(isinstance(serial, str), "Thermal {} serial number appears incorrect".format(i))
84+
self.expect(isinstance(serial, STRING_TYPE), "Thermal {} serial number appears incorrect".format(i))
7585

7686
self.assert_expectations()
7787

0 commit comments

Comments
 (0)