Skip to content

Commit 8934479

Browse files
[fanshow] Display other fan status, such as Updating (sonic-net#1014)
The original fan status can be one of "OK", "Not OK", "N/A". This PR allows a new fan status "Updating". If fan status is not "true" or "false", display the status field value in CLI output.
1 parent 3e52604 commit 8934479

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

scripts/fanshow

100644100755
+18-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@
44
"""
55
from __future__ import print_function
66

7+
import os
8+
import sys
79
from tabulate import tabulate
810
from swsssdk import SonicV2Connector
911
from natsort import natsorted
1012

13+
# mock the redis for unit test purposes #
14+
try:
15+
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
16+
modules_path = os.path.join(os.path.dirname(__file__), "..")
17+
test_path = os.path.join(modules_path, "tests")
18+
sys.path.insert(0, modules_path)
19+
sys.path.insert(0, test_path)
20+
import mock_tables.dbconnector
21+
except KeyError:
22+
pass
23+
1124

1225
header = ['Drawer', 'LED', 'FAN', 'Speed', 'Direction', 'Presence', 'Status', 'Timestamp']
1326

@@ -52,13 +65,13 @@ class FanShow(object):
5265

5366
presence = data_dict[PRESENCE_FIELD_NAME].lower()
5467
presence = 'Present' if presence == 'true' else 'Not Present'
55-
status = data_dict[STATUS_FIELD_NAME].lower()
56-
if status == 'true':
68+
69+
status = data_dict[STATUS_FIELD_NAME]
70+
status_lower = status.lower()
71+
if status_lower == 'true':
5772
status = 'OK'
58-
elif status == 'false':
73+
elif status_lower == 'false':
5974
status = 'Not OK'
60-
else:
61-
status = 'N/A'
6275

6376
table.append((data_dict[DRAWER_FIELD_NAME], data_dict[LED_STATUS_FIELD_NAME], name, speed, data_dict[DIRECTION_FIELD_NAME], presence, status,
6477
data_dict[TIMESTAMP_FIELD_NAME]))

tests/fan_test.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
import os
3+
from click.testing import CliRunner
4+
5+
test_path = os.path.dirname(os.path.abspath(__file__))
6+
modules_path = os.path.dirname(test_path)
7+
scripts_path = os.path.join(modules_path, "scripts")
8+
sys.path.insert(0, modules_path)
9+
10+
import show.main as show
11+
12+
class TestFan(object):
13+
@classmethod
14+
def setup_class(cls):
15+
print("SETUP")
16+
os.environ["PATH"] += os.pathsep + scripts_path
17+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
18+
19+
def test_show_platform_fan(self):
20+
runner = CliRunner()
21+
result = runner.invoke(show.cli.commands["platform"].commands["fan"])
22+
print(result.output)
23+
expected = """\
24+
Drawer LED FAN Speed Direction Presence Status Timestamp
25+
-------- ----- ----- ------- ----------- ---------- -------- -----------------
26+
drawer1 red fan1 30% intake Present OK 20200813 01:32:30
27+
drawer2 green fan2 50% intake Present Not OK 20200813 01:32:30
28+
drawer3 green fan3 50% intake Present Updating 20200813 01:32:30
29+
"""
30+
31+
assert result.output == expected
32+
33+
@classmethod
34+
def teardown_class(cls):
35+
print("TEARDOWN")
36+
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
37+
os.environ["UTILITIES_UNIT_TESTING"] = "0"
38+

tests/mock_tables/state_db.json

+39
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,44 @@
164164
"runner.fast_rate": "false",
165165
"setup.kernel_team_mode_name": "loadbalance",
166166
"runner.active": "true"
167+
},
168+
"FAN_INFO|fan1": {
169+
"drawer_name": "drawer1",
170+
"presence": "True",
171+
"model": "N/A",
172+
"serial": "N/A",
173+
"status": "True",
174+
"direction": "intake",
175+
"speed": "30",
176+
"speed_tolerance": "50",
177+
"speed_target": "20",
178+
"led_status": "red",
179+
"timestamp": "20200813 01:32:30"
180+
},
181+
"FAN_INFO|fan2": {
182+
"drawer_name": "drawer2",
183+
"presence": "True",
184+
"model": "N/A",
185+
"serial": "N/A",
186+
"status": "False",
187+
"direction": "intake",
188+
"speed": "50",
189+
"speed_tolerance": "50",
190+
"speed_target": "50",
191+
"led_status": "green",
192+
"timestamp": "20200813 01:32:30"
193+
},
194+
"FAN_INFO|fan3": {
195+
"drawer_name": "drawer3",
196+
"presence": "True",
197+
"model": "N/A",
198+
"serial": "N/A",
199+
"status": "Updating",
200+
"direction": "intake",
201+
"speed": "50",
202+
"speed_tolerance": "50",
203+
"speed_target": "50",
204+
"led_status": "green",
205+
"timestamp": "20200813 01:32:30"
167206
}
168207
}

0 commit comments

Comments
 (0)