Skip to content

Commit a355050

Browse files
committed
Testcase for Time Utility
The time utility runs the specified program/command with the given arguments. When program/command finishes, time writes a message to standard error giving timing statistics about the program/command run It provides /usr/bin/time binary signedoff-By:Basheer K<[email protected]>
1 parent 04a3b55 commit a355050

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

linux-tools/test_time/control

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
AUTHOR = "Basheer Khadarsabgari <[email protected]>"
2+
NAME = "test_time"
3+
TEST_CATEGORY = "Functional"
4+
TEST_CLASS = "Linux Tools"
5+
TEST_TYPE = "client"
6+
TIME = 'SHORT'
7+
DOC = '''
8+
Test for time package.
9+
'''
10+
11+
job.run_test('linux-tools/test_time')

linux-tools/test_time/test_time.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#!/bin/python
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 2 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
#
12+
# See LICENSE for more details.
13+
# Copyright: 2016 IBM
14+
# Author: Basheer K<[email protected]>
15+
16+
import re
17+
import logging
18+
import time
19+
import os
20+
from autotest.client import test, utils
21+
from distutils.spawn import find_executable
22+
from autotest.client.shared import error
23+
24+
25+
class test_time(test.test):
26+
"""
27+
Autotest module for time utility.
28+
time - time is a simple command,it gives resource usage.
29+
The time utility runs the specified program/command with the given
30+
arguments.When program/command finishes,time writes a message
31+
to standard error giving timing statistics about the command run.
32+
These statistics consist of
33+
(i) The elapsed real time between invocation and termination,
34+
(ii) The user CPU time , and
35+
(iii) The system CPU time.
36+
37+
@author:Basheer Khadarsabgari <[email protected]>
38+
"""
39+
version = 1
40+
nfail = 0
41+
seconds = 2
42+
time_exe = ''
43+
output_file = ''
44+
45+
def install_check(self):
46+
"""
47+
Install check for time
48+
"""
49+
self.time_exe = find_executable('time')
50+
if self.time_exe is None:
51+
raise error.TestError('time binary not found')
52+
53+
def test_portability_option(self):
54+
"""
55+
This method will verify the time portability(-p) option
56+
Ex: "/usr/bin/time -p sleep 2"
57+
"""
58+
cmd = "%s -p sleep %s" % (self.time_exe, self.seconds)
59+
ret = utils.run(cmd)
60+
if re.search(
61+
"(real\s+%s.\d+)\nuser\s+\d+.\d+\nsys\s+\d.\d+" %
62+
self.seconds,
63+
ret.stderr):
64+
logging.info(
65+
"Verified '--portability' option successfully.")
66+
else:
67+
self.nfail += 1
68+
raise error.TestError(
69+
"Failed to verify --portability option of time utility")
70+
71+
def test_format_option(self, cmd, expected_status):
72+
"""
73+
This method will verify the time --format(-f) option
74+
Ex:/usr/bin/time -f "%x is Exit status of the cmd %C" <cmd>
75+
"""
76+
cmd_to_execute = self.time_exe + \
77+
" -f '%x is the exit status of the cmd %C' " + cmd
78+
search_pattern = "%s is the exit status of the cmd %s" % (
79+
expected_status, cmd)
80+
ret = utils.run(cmd_to_execute, ignore_status=True)
81+
if search_pattern not in ret.stderr or \
82+
ret.exit_status != expected_status:
83+
self.nfail += 1
84+
raise error.TestError(
85+
"Failed to verify --format option of time, " +
86+
"exit status of the %s command using time utility."
87+
% cmd)
88+
else:
89+
logging.info(
90+
"Verified --format option of time, " +
91+
"exit status of %s command using time utility."
92+
% cmd)
93+
94+
def test_output_option(self):
95+
"""
96+
This method will verify the whether output file is
97+
created or not using '-o' option
98+
Ex:/usr/bin/time -o time_out.txt <cmd>
99+
"""
100+
cmd_invoked = "sleep %s" % self.seconds
101+
self.output_file = '%s/time-output-%s' % (
102+
self.tmpdir, time.strftime('%Y-%m-%d-%H.%M.%S'))
103+
cmd = "%s -v -o %s %s" % (self.time_exe, self.output_file, cmd_invoked)
104+
utils.system(cmd)
105+
if os.path.exists(self.output_file) and \
106+
utils.file_contains_pattern(self.output_file, cmd_invoked):
107+
logging.info("Output file is created successfully using -o option")
108+
else:
109+
self.nfail += 1
110+
raise error.TestError(
111+
"Failed to create output file using -o option")
112+
113+
def test_append_option(self):
114+
"""
115+
This method will verify the --append option of time utility
116+
Ex:/usr/bin/time -v -a -o time_out.txt <cmd>
117+
"""
118+
cmd_invoked = "pwd"
119+
cmd = "%s -v -a -o %s %s" % (self.time_exe,
120+
self.output_file, cmd_invoked)
121+
utils.system(cmd)
122+
if os.path.exists(self.output_file) and \
123+
utils.file_contains_pattern(self.output_file, cmd_invoked):
124+
logging.info(
125+
"Output is appended to the output file successfully" +
126+
" using -a option of time utility")
127+
else:
128+
self.nfail += 1
129+
raise error.TestError(
130+
"Failed to append the output to a file using -a option")
131+
132+
def test_custom_script(self):
133+
"""
134+
This method will test the custom shell script with the time utility
135+
create a custom shell script and provide it as input to the time
136+
Ex: /usr/bin/time -v <custom-script>
137+
"""
138+
custom_script = "%s/custom_script.sh" % (self.tmpdir)
139+
fobj = open(custom_script, 'w')
140+
fobj.writelines("""
141+
#!/bin/bash
142+
echo "Hello, $LOGNAME"
143+
echo "Current date is `date`"
144+
echo "User is `who i am`"
145+
echo "Current directory `pwd`"
146+
""")
147+
fobj.close()
148+
cmd = "%s -v bash %s" % (self.time_exe, custom_script)
149+
ret = utils.run(cmd, ignore_status=True)
150+
if ret.exit_status and "Current date" not in ret.stdout:
151+
self.nfail += 1
152+
raise error.TestError(
153+
"Failed to execute custom script using time utility")
154+
else:
155+
logging.info(
156+
"verified execution of custom script using " +
157+
"time utility successfully")
158+
159+
def run_once(self):
160+
"""
161+
Runs the test.
162+
"""
163+
try:
164+
self.install_check()
165+
self.test_portability_option()
166+
self.test_format_option("pwd", 0)
167+
self.test_format_option("pwd1", 127)
168+
self.test_format_option("host", 1)
169+
self.test_output_option()
170+
self.test_append_option()
171+
self.test_custom_script()
172+
finally:
173+
self.cleanup()
174+
175+
def cleanup(self):
176+
"""
177+
cleanup
178+
"""
179+
pass
180+
181+
def postprocess(self):
182+
if self.nfail != 0:
183+
logging.info('nfail is non-zero')
184+
raise error.TestError('Test failed')
185+
else:
186+
logging.info('Test completed successfully')

0 commit comments

Comments
 (0)