1#!/usr/bin/env python3

2# -*- coding: utf-8 -*-

3#

4# hdd_parking

5#

6# This file is part of Checkbox.

7#

8# Copyright 2014 Canonical Ltd.

9#

10# Authors: Brendan Donegan <brendan.donegan@canonical.com>

11#

12# Checkbox is free software: you can redistribute it and/or modify

13# it under the terms of the GNU General Public License version 3,

14# as published by the Free Software Foundation.

15

16#

17# Checkbox is distributed in the hope that it will be useful,

18# but WITHOUT ANY WARRANTY; without even the implied warranty of

19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

20# GNU General Public License for more details.

21#

22# You should have received a copy of the GNU General Public License

23# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

24

25"""

26 This script verifies that a systems HDD protection capabilities

27 are triggered when appropriate. There are many implementations

28 of HDD protection from different OEMs, each implemented in a

29 different way, so this script can only support implementations

30 which are known to work and are testable. Currently the list

31 of supported implementations is:

32

33- HDAPS (Lenovo)

34"""

35

36

37 import sys

38 import time

39

40 from argparse import ArgumentParser

41 from subprocess import Popen, PIPE

42

43 TIMEOUT = 15.0

44

45

46 def hdaps_test(run_time):

47 try:

48 hdapsd = Popen(

49 ["/usr/sbin/hdapsd"],

50 stdout=PIPE,

51 stderr=PIPE,

52 universal_newlines=True,

53 )

54 except OSError as err:

55 print("Unable to start hdapsd: {}".format(err))

56 return 1

57 time.sleep(float(run_time))

58 hdapsd.terminate()

59 # Look for parking message in hdapsd output.

60 stdout = hdapsd.communicate()[0]

61 print(stdout)

62 for line in stdout.split("\n"):

63 if line.endswith("parking"):

64 return 0

65 return 1

66

67

68 def main():

69 # First establish the driver used

70 parser = ArgumentParser(

71 "Tests a systems HDD protection capabilities. "

72 "Requires the system to be moved by the tester."

73 )

74 parser.add_argument(

75 "-t",

76 "--timeout",

77 default=TIMEOUT,

78 help="The time allowed before the test fails.",

79 )

80 print(

81 "Starting HDD protection test - move the system around on "

82 "all axis. No particular force should be required."

83 )

84 return hdaps_test(parser.parse_args().timeout)

85

86

87 if __name__ == "__main__":

88 sys.exit(main())