• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Test methods in module sponge_utils.
6
7This test has dependency on sponge module, therefore it's not a unit test.
8User can run this module manually to verify test results can be compiled into
9Sponge XML and uploaded to test server correctly.
10"""
11
12import datetime
13import mox
14import os
15import shutil
16import tempfile
17import time
18import unittest
19
20import common
21
22from autotest_lib.site_utils.sponge_lib import sponge_utils
23from autotest_lib.tko import models
24
25ACTS_SUMMARY_JSON = """
26{
27    "Results": [
28        {
29            "Begin Time": 1464054883744,
30            "Details": "setup_class failed for FilteringTest.",
31            "End Time": 1464054883744,
32            "Extras": null,
33            "Result": "FAIL",
34            "Test Class": "FilteringTest",
35            "Test Name": "",
36            "UID": null,
37            "Extra Errors": {"on_fail": "I also failed for whatever reason"}
38        },
39        {
40            "Begin Time": 1464054888355,
41            "Details": null,
42            "End Time": 1464054888644,
43            "Extras": null,
44            "Result": "PASS",
45            "Test Class": "UniqueFilteringTest",
46            "Test Name": "test_scan_flush_pending_scan_results",
47            "UID": null,
48            "Extra Errors": {}
49        }
50    ],
51    "Summary": {
52        "Executed": 2,
53        "Failed": 1,
54        "Passed": 1,
55        "Requested": 10,
56        "Skipped": 0,
57        "Unknown": 8
58    }
59}
60"""
61
62class SpongeUtilsUnitTests(mox.MoxTestBase):
63    """Test functions in sponge_utils.
64    """
65
66    def setUp(self):
67        """Set up test."""
68        super(SpongeUtilsUnitTests, self).setUp()
69        self.acts_summary = tempfile.NamedTemporaryFile(delete=False)
70        self.acts_summary.write(ACTS_SUMMARY_JSON)
71        self.acts_summary.close()
72        self.tmp_dir = tempfile.mkdtemp()
73        self.resultsdir = os.path.join(self.tmp_dir,
74                                       '123-debug_user/host1/dummy_PassServer')
75        os.makedirs(self.resultsdir)
76        with open(os.path.join(self.tmp_dir, '123-debug_user/host1',
77                               '.autoserv_execute'), 'w') as f:
78            f.write('')
79
80
81    def tearDown(self):
82        """Delete temporary file.
83        """
84        super(SpongeUtilsUnitTests, self).tearDown()
85        os.unlink(self.acts_summary.name)
86        shutil.rmtree(self.tmp_dir)
87
88
89    def test_upload_results_in_test(self):
90        """Test function upload_results_in_test.
91        """
92        test = self.mox.CreateMockAnything()
93        test.resultsdir = os.path.join(self.tmp_dir,
94                                       '123-debug_user/host1/dummy_PassServer')
95        test.tagged_testname = 'dummy_PassServer'
96
97        test.job = self.mox.CreateMockAnything()
98        test.job.user = 'debug_user'
99        test.job.machines = ['host1']
100
101        job_keyvals = {'drone': 'localhost',
102                       'job_started': time.time()-1000}
103        self.mox.StubOutWithMock(models.test, 'parse_job_keyval')
104        models.test.parse_job_keyval(test.resultsdir).AndReturn(job_keyvals)
105
106        self.mox.ReplayAll()
107
108        invocation_url = sponge_utils.upload_results_in_test(
109                test, test_pass=True, acts_summary=self.acts_summary.name)
110        print 'Invocation URL: %s' % invocation_url
111        self.assertIsNotNone(invocation_url)
112
113
114    def test_upload_results_in_parsing(self):
115        """Test function upload_results.
116        """
117        job = self.mox.CreateMockAnything()
118        job.started_time = datetime.datetime(2016, 8, 15, 0, 0, 0)
119        job.finished_time = datetime.datetime(2016, 8, 15, 1, 0, 0)
120        job.keyval_dict = {'drone': 'server1',
121                           'hostname': 'host1',
122                           'job_finished': 1471284056,
123                           'job_queued': 1471283461,
124                           'job_started': 1471283480,
125                           'label': 'dummy',
126                           'status_version': 1,
127                           'suite': 'dummy',
128                           'parent_job_id': 100,
129                           'user': 'debug_user'}
130
131        job.dir = os.path.join(self.tmp_dir, '123-debug_user/host1')
132        job.label = 'dummy_PassServer'
133
134        job.tests = []
135        test = self.mox.CreateMockAnything()
136        test.attributes = {'host-labels': 'board%3Aveyron'}
137        test.status = 'GOOD'
138        test.started_time = datetime.datetime(2016, 8, 15, 0, 0, 0)
139        test.finished_time = datetime.datetime(2016, 8, 15, 1, 0, 0)
140        test.testname = 'dummy_PassServer'
141        job.tests.append(test)
142        job.user = 'debug_user'
143        job.machine = 'host1'
144
145        invocation_url = sponge_utils.upload_results(job)
146        print 'Invocation URL: %s' % invocation_url
147        self.assertIsNotNone(invocation_url)
148
149
150if __name__ == '__main__':
151    unittest.main()
152