ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
 
import os, unittest, time, datetime, itertools
 
import common
from autotest_lib.client.common_lib.test_utils import mock
from autotest_lib.tko import utils
 
 
class get_timestamp_test(unittest.TestCase):
    def test_zero_time(self):
        date = utils.get_timestamp({"key": "0"}, "key")
        timezone = datetime.timedelta(seconds=time.timezone)
        utc_date = date + timezone
        # should be equal to epoch, i.e. Jan 1, 1970
        self.assertEquals(utc_date.year, 1970)
        self.assertEquals(utc_date.month, 1)
        self.assertEquals(utc_date.day, 1)
        self.assertEquals(utc_date.hour, 0)
        self.assertEquals(utc_date.minute, 0)
        self.assertEquals(utc_date.second, 0)
        self.assertEquals(utc_date.microsecond, 0)
 
 
    def test_returns_none_on_missing_value(self):
        date = utils.get_timestamp({}, "missing_key")
        self.assertEquals(date, None)
 
 
    def test_fails_on_non_integer_values(self):
        self.assertRaises(ValueError, utils.get_timestamp,
                          {"key": "zero"}, "key")
 
 
    def test_date_can_be_string_or_integer(self):
        int_times = [1, 12, 123, 1234, 12345, 123456]
        str_times = [str(t) for t in int_times]
        for int_t, str_t in itertools.izip(int_times, str_times):
            date_int = utils.get_timestamp({"key": int_t}, "key")
            date_str = utils.get_timestamp({"key": str_t}, "key")
            self.assertEquals(date_int, date_str)
 
 
class find_toplevel_job_dir_test(unittest.TestCase):
    def setUp(self):
        self.god = mock.mock_god()
        self.god.stub_function(os.path, "exists")
 
 
    def tearDown(self):
        self.god.unstub_all()
 
 
    def test_start_is_toplevel(self):
        jobdir = "/results/job1"
        os.path.exists.expect_call(
            jobdir + "/.autoserv_execute").and_return(True)
        self.assertEqual(utils.find_toplevel_job_dir(jobdir), jobdir)
 
 
    def test_parent_is_toplevel(self):
        jobdir = "/results/job2"
        os.path.exists.expect_call(
            jobdir + "/sub/.autoserv_execute").and_return(False)
        os.path.exists.expect_call(
            jobdir + "/.autoserv_execute").and_return(True)
        self.assertEqual(utils.find_toplevel_job_dir(jobdir + "/sub"), jobdir)
 
 
    def test_grandparent_is_toplevel(self):
        jobdir = "/results/job3"
        os.path.exists.expect_call(
            jobdir + "/sub/sub/.autoserv_execute").and_return(False)
        os.path.exists.expect_call(
            jobdir + "/sub/.autoserv_execute").and_return(False)
        os.path.exists.expect_call(
            jobdir + "/.autoserv_execute").and_return(True)
        self.assertEqual(utils.find_toplevel_job_dir(jobdir + "/sub/sub"),
                         jobdir)
 
    def test_root_is_toplevel(self):
        jobdir = "/results/job4"
        os.path.exists.expect_call(
            jobdir + "/.autoserv_execute").and_return(False)
        os.path.exists.expect_call(
            "/results/.autoserv_execute").and_return(False)
        os.path.exists.expect_call("/.autoserv_execute").and_return(True)
        self.assertEqual(utils.find_toplevel_job_dir(jobdir), "/")
 
 
    def test_no_toplevel(self):
        jobdir = "/results/job5"
        os.path.exists.expect_call(
            jobdir + "/.autoserv_execute").and_return(False)
        os.path.exists.expect_call(
            "/results/.autoserv_execute").and_return(False)
        os.path.exists.expect_call("/.autoserv_execute").and_return(False)
        self.assertEqual(utils.find_toplevel_job_dir(jobdir), None)
 
 
class drop_redundant_messages(unittest.TestCase):
    def test_empty_set(self):
        self.assertEqual(utils.drop_redundant_messages(set()), set())
 
 
    def test_singleton(self):
        self.assertEqual(utils.drop_redundant_messages(set(["abc"])),
                         set(["abc"]))
 
 
    def test_distinct_messages(self):
        self.assertEqual(utils.drop_redundant_messages(set(["abc", "def"])),
                         set(["abc", "def"]))
 
 
    def test_one_unique_message(self):
        self.assertEqual(
                utils.drop_redundant_messages(set(["abc", "abcd", "abcde"])),
                set(["abcde"]))
 
 
    def test_some_unique_some_not(self):
        self.assertEqual(
                utils.drop_redundant_messages(set(["abc", "def", "abcdef",
                                                   "defghi", "cd"])),
                set(["abcdef", "defghi"]))
 
 
if __name__ == "__main__":
    unittest.main()