1#!/usr/bin/env python
2# Copyright 2014 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import time
7import unittest
8
9import common
10
11from autotest_lib.client.common_lib import time_utils
12from autotest_lib.site_utils import dut_status
13
14
15class TimeOptionTests(unittest.TestCase):
16    """Test the --since, --until, and --destination options.
17
18    The options are allowed in these seven combinations:
19      * No options - use the default end time and duration.
20      * --since - use the given start time and the default end time.
21      * --until - use the given end time and the default duration.
22      * --duration - use the given duration and the default end time.
23      * --since --until - use the given start and end times.
24      * --since --duration - use the given start time and duration.
25      * --until --duration - use the given end time and duration.
26
27    It's an error to use all three options together.
28
29    """
30
31    def _try_parse(self, options):
32        return dut_status._parse_command(
33                ['mumble.py'] + options + ['hostname'])
34
35
36    def _check_duration(self, arguments, duration):
37        start_time = (arguments.until - duration * 3600)
38        assert arguments.since == start_time
39
40
41    def _check_default_end(self, arguments, end_time):
42        max_end = int(time.time())
43        assert arguments.until >= end_time
44        assert arguments.until <= max_end
45
46
47    def test_default_time_bounds(self):
48        """Test time bounds when no options are supplied."""
49        end_time = int(time.time())
50        arguments = self._try_parse([])
51        self._check_default_end(arguments, end_time)
52        self._check_duration(arguments, dut_status._DEFAULT_DURATION)
53
54
55    def test_start_only(self):
56        """Test time bounds with --since only.
57
58        Also tests that --since and -s are equivalent.
59
60        """
61        for option in ['--since', '-s']:
62            end_time = int(time.time())
63            start_time = end_time - 3600
64            start_time_string = time_utils.epoch_time_to_date_string(start_time)
65            arguments = self._try_parse([option, start_time_string])
66            self._check_default_end(arguments, end_time)
67            assert arguments.since == start_time
68
69
70    def test_end_only(self):
71        """Test time bounds with --until only.
72
73        Also tests that --until and -u are equivalent.
74
75        """
76        for option in ['--until', '-u']:
77            end_time = int(time.time()) - 3600
78            end_time_string = time_utils.epoch_time_to_date_string(end_time)
79            arguments = self._try_parse([option, end_time_string])
80            assert arguments.until == end_time
81            self._check_duration(arguments, dut_status._DEFAULT_DURATION)
82
83
84    def test_duration_only(self):
85        """Test time bounds with --duration only.
86
87        Also tests that --duration and -d are equivalent.
88
89        """
90        for option in ['--duration', '-d']:
91            duration = 4
92            duration_string = '%d' % duration
93            end_time = int(time.time())
94            arguments = self._try_parse([option, duration_string])
95            self._check_default_end(arguments, end_time)
96            self._check_duration(arguments, duration)
97
98
99    def test_start_and_end(self):
100        """Test time bounds with --since and --until."""
101        start_time = int(time.time()) - 5 * 3600
102        start_time_string = time_utils.epoch_time_to_date_string(start_time)
103        end_time = start_time + 4 * 3600
104        end_time_string = time_utils.epoch_time_to_date_string(end_time)
105        arguments = self._try_parse(['--s', start_time_string,
106                                     '--u', end_time_string])
107        assert arguments.since == start_time
108        assert arguments.until == end_time
109
110
111    def test_start_and_duration(self):
112        """Test time bounds with --since and --duration."""
113        start_time = int(time.time()) - 5 * 3600
114        start_time_string = time_utils.epoch_time_to_date_string(start_time)
115        duration = 4
116        duration_string = '%d' % duration
117        arguments = self._try_parse(['--s', start_time_string,
118                                     '--d', duration_string])
119        assert arguments.since == start_time
120        self._check_duration(arguments, duration)
121
122
123    def test_end_and_duration(self):
124        """Test time bounds with --until and --duration."""
125        end_time = int(time.time()) - 5 * 3600
126        end_time_string = time_utils.epoch_time_to_date_string(end_time)
127        duration = 4
128        duration_string = '%d' % duration
129        arguments = self._try_parse(['--u', end_time_string,
130                                     '--d', duration_string])
131        assert arguments.until == end_time
132        self._check_duration(arguments, duration)
133
134
135    def test_all_options(self):
136        """Test that all three options are a fatal error."""
137        start_time = int(time.time()) - 5 * 3600
138        start_time_string = time_utils.epoch_time_to_date_string(start_time)
139        duration = 4
140        duration_string = '%d' % duration
141        end_time = start_time + duration * 3600
142        end_time_string = time_utils.epoch_time_to_date_string(end_time)
143        with self.assertRaises(SystemExit):
144            self._try_parse(['--s', start_time_string,
145                             '--u', end_time_string,
146                             '--d', duration_string])
147
148
149if __name__ == '__main__':
150    unittest.main()
151