liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/python -u
#
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
 
#pylint: disable-msg=C0111
 
import mox, os, shutil, tempfile, unittest
 
import common
from django.conf import settings
from autotest_lib.client.common_lib import global_config
from autotest_lib.frontend import database_settings_helper
from autotest_lib.frontend import setup_django_environment
from autotest_lib.frontend import setup_test_environment
from autotest_lib.frontend.afe import frontend_test_utils
from autotest_lib.frontend.afe import models as django_afe_models
from autotest_lib.frontend.tko import models as django_tko_models
from autotest_lib.tko import db as tko_db
from autotest_lib.tko.site_parse import StackTrace
 
# Have to import this after setup_django_environment and setup_test_environment.
# It creates a database connection, so the mocking has to be done first.
from django.db import connections
 
class stack_trace_test(unittest.TestCase):
 
 
    def setUp(self):
        setup_test_environment.set_up()
        self._fake_results = tempfile.mkdtemp()
        self._cros_src_dir = global_config.global_config.get_config_value(
            'CROS', 'source_tree', default=None)
 
        if not self._cros_src_dir:
            self.fail('No Chrome OS source tree defined in global_config.ini')
 
        self._stack_trace = StackTrace(
            self._fake_results, self._cros_src_dir)
 
        self._cache_dir = os.path.join(
            self._cros_src_dir, 'chroot', self._stack_trace._CACHE_DIR)
 
        # Ensure we don't obliterate a live cache directory by accident.
        if os.path.exists(self._cache_dir):
            self.fail(
                'Symbol cache directory already exists. Cowardly refusing to'
                ' run. Please remove this directory manually to continue.')
 
 
    def tearDown(self):
        setup_test_environment.tear_down()
        shutil.rmtree(self._fake_results)
        if os.path.exists(self._cache_dir):
            shutil.rmtree(self._cache_dir)
 
 
    def _setup_basic_cache(self,
                           job_name='x86-alex-r16-R16-1166.0.0-a1-b1118_bvt',
                           mkdir=True):
        # Ensure cache directory is present.
        self._stack_trace._get_cache_dir()
        board, rev, version = self._stack_trace._parse_job_name(job_name)
 
        symbols_dir = os.path.join(
            self._cache_dir, '-'.join([board, rev, version]))
        if mkdir:
            os.mkdir(symbols_dir)
 
        chroot_symbols_dir = os.sep + os.path.relpath(
            symbols_dir, self._stack_trace._chroot_dir)
 
        return job_name, symbols_dir, chroot_symbols_dir
 
 
    def test_get_job_name(self):
        job_name = 'x86-alex-r16-R16-1166.0.0-a1-b1118_regression'
        with open(os.path.join(self._fake_results, 'keyval'), 'w') as f:
            f.write('label=%s' % job_name)
 
        self.assertEqual(self._stack_trace._get_job_name(), job_name)
 
 
    def test_parse_3_tuple_job_name(self):
        job_name = 'x86-alex-r16-R16-1166.0.0-a1-b1118_regression'
        board, rev, version = self._stack_trace._parse_job_name(job_name)
        self.assertEqual(board, 'x86-alex')
        self.assertEqual(rev, 'r16')
        self.assertEqual(version, '1166.0.0')
 
 
    def test_parse_4_tuple_job_name(self):
        job_name = 'x86-mario-r15-0.15.1011.74-a1-b61_bvt'
        board, rev, version = self._stack_trace._parse_job_name(job_name)
        self.assertEqual(board, 'x86-mario')
        self.assertEqual(rev, 'r15')
        self.assertEqual(version, '0.15.1011.74')
 
 
    def test_parse_4_tuple_au_job_name(self):
        job_name = 'x86-alex-r15-0.15.1011.81_to_0.15.1011.82-a1-b69_mton_au'
        board, rev, version = self._stack_trace._parse_job_name(job_name)
        self.assertEqual(board, 'x86-alex')
        self.assertEqual(rev, 'r15')
        self.assertEqual(version, '0.15.1011.82')
 
 
    def test_parse_3_tuple_au_job_name(self):
        job_name = 'x86-alex-r16-1165.0.0_to_R16-1166.0.0-a1-b69_mton_au'
        board, rev, version = self._stack_trace._parse_job_name(job_name)
        self.assertEqual(board, 'x86-alex')
        self.assertEqual(rev, 'r16')
        self.assertEqual(version, '1166.0.0')
 
 
class database_selection_test(mox.MoxTestBase,
                              frontend_test_utils.FrontendTestMixin):
 
    def setUp(self):
        super(database_selection_test, self).setUp()
        self._frontend_common_setup(fill_data=False)
 
 
    def tearDown(self):
        super(database_selection_test, self).tearDown()
        self._frontend_common_teardown()
        global_config.global_config.reset_config_values()
 
 
    def assertQueries(self, database, assert_in, assert_not_in):
        assert_in_found = False
        for query in connections[database].queries:
            sql = query['sql']
            # Ignore CREATE TABLE statements as they are always executed
            if 'INSERT INTO' in sql or 'SELECT' in sql:
                self.assertNotIn(assert_not_in, sql)
                if assert_in in sql:
                    assert_in_found = True
        self.assertTrue(assert_in_found)
 
 
    def testDjangoModels(self):
        # If DEBUG=False connection.query will be empty
        settings.DEBUG = True
 
        afe_job = django_afe_models.Job.objects.create(created_on='2014-08-12')
        # Machine has less dependencies than tko Job so it's easier to create
        tko_job = django_tko_models.Machine.objects.create()
 
        django_afe_models.Job.objects.get(pk=afe_job.id)
        django_tko_models.Machine.objects.get(pk=tko_job.pk)
 
        self.assertQueries('global', 'tko_machines', 'afe_jobs')
        self.assertQueries('default', 'afe_jobs', 'tko_machines')
 
        # Avoid unnecessary debug output from other tests
        settings.DEBUG = True
 
 
    def testRunOnShardWithoutGlobalConfigsFails(self):
        global_config.global_config.override_config_value(
                'SHARD', 'shard_hostname', 'host1')
        from autotest_lib.frontend import settings
        # settings module was already loaded during the imports of this file,
        # so before the configuration setting was made, therefore reload it:
        reload(database_settings_helper)
        self.assertRaises(global_config.ConfigError,
                          reload, settings)
 
 
    def testRunOnMasterWithoutGlobalConfigsWorks(self):
        global_config.global_config.override_config_value(
                'SHARD', 'shard_hostname', '')
        from autotest_lib.frontend import settings
        # settings module was already loaded during the imports of this file,
        # so before the configuration setting was made, therefore reload it:
        reload(database_settings_helper)
        reload(settings)
 
 
    def testTkoDatabase(self):
        global_host = 'GLOBAL_HOST'
        global_user = 'GLOBAL_USER'
        global_db = 'GLOBAL_DB'
        global_pw = 'GLOBAL_PW'
        global_port = ''
        local_host = 'LOCAL_HOST'
 
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'global_db_type', '')
 
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'global_db_host', global_host)
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'global_db_database', global_db)
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'global_db_user', global_user)
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'global_db_password', global_pw)
        global_config.global_config.override_config_value(
                'AUTOTEST_WEB', 'host', local_host)
 
        class ConnectCalledException(Exception):
            pass
 
        # We're only interested in the parameters connect is called with here.
        # Take the fast path out so we don't have to mock all the other calls
        # that will later be made on the connection
        def fake_connect(*args, **kwargs):
            raise ConnectCalledException
 
        tko_db.db_sql.connect = None
        self.mox.StubOutWithMock(tko_db.db_sql, 'connect')
        tko_db.db_sql.connect(
                global_host, global_db, global_user, global_pw,
                global_port).WithSideEffects(fake_connect)
 
        self.mox.ReplayAll()
 
        self.assertRaises(ConnectCalledException, tko_db.db_sql)
 
 
if __name__ == "__main__":
    unittest.main()