1# Copyright 2017 The Chromium 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"""Tests for autotest.py."""
6
7from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10
11import sys
12
13import mock
14import pytest
15import subprocess32
16
17from lucifer import autotest
18
19
20@pytest.mark.skip('crbug.com/787081')
21@pytest.mark.slow
22def test_monkeypatch():
23    """Test monkeypatch()."""
24    common_file = subprocess32.check_output(
25            [sys.executable, '-m',
26             'lucifer.cmd.test.autotest_monkeypatcher'])
27    assert common_file.rstrip() == '<removed>'
28
29
30@pytest.mark.parametrize('fullname,expected', [
31        ('autotest_lib.common', True),
32        ('autotest_lib.server.common', True),
33        ('autotest_lib.server', False),
34        ('some_lib.common', False),
35])
36def test__CommonRemovingFinder_find_module(fullname, expected):
37    """Test _CommonRemovingFinder.find_module()."""
38    finder = autotest._CommonRemovingFinder()
39    got = finder.find_module(fullname)
40    assert got == (finder if expected else None)
41
42
43@pytest.mark.parametrize('name,expected', [
44        ('scheduler.models', 'autotest_lib.scheduler.models'),
45])
46def test_load(name, expected):
47    """Test load()."""
48    with mock.patch('importlib.import_module', autospec=True) \
49         as import_module, \
50         mock.patch.object(autotest, '_setup_done', True):
51        autotest.load(name)
52        import_module.assert_called_once_with(expected)
53
54
55def test_load_without_patch_fails():
56    """Test load() without patch."""
57    with mock.patch.object(autotest, '_setup_done', False):
58        with pytest.raises(ImportError):
59            autotest.load('asdf')
60
61
62@pytest.mark.parametrize('name,expected', [
63        ('constants', 'chromite.lib.constants'),
64])
65def test_chromite_load(name, expected):
66    """Test load()."""
67    with mock.patch('importlib.import_module', autospec=True) \
68         as import_module, \
69         mock.patch.object(autotest, '_setup_done', True):
70        autotest.chromite_load(name)
71        import_module.assert_called_once_with(expected)
72
73
74@pytest.mark.parametrize('name', [
75        'google.protobuf.internal.well_known_types',
76])
77def test_deps_load(name):
78    """Test load()."""
79    with mock.patch('importlib.import_module', autospec=True) \
80         as import_module, \
81         mock.patch.object(autotest, '_setup_done', True):
82        autotest.deps_load(name)
83        import_module.assert_called_once_with(name)
84