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 55@pytest.mark.parametrize('name,expected', [ 56 ('scheduler.models', 'autotest_lib.scheduler.models'), 57]) 58def test_deferred_load(name, expected): 59 """Test deferred_load().""" 60 with mock.patch('importlib.import_module', autospec=True) as import_module: 61 module = autotest.deferred_load(name) 62 assert import_module.call_count == 0 63 autotest.monkeypatch() 64 # Force module import by accessing an attribute. 65 getattr(module, '__dict__') 66 import_module.assert_called_with(expected) 67 68 69def test_load_without_patch_fails(): 70 """Test load() without patch.""" 71 with mock.patch.object(autotest, '_setup_done', False): 72 with pytest.raises(ImportError): 73 autotest.load('asdf') 74 75 76@pytest.mark.parametrize('name,expected', [ 77 ('constants', 'chromite.lib.constants'), 78]) 79def test_chromite_load(name, expected): 80 """Test load().""" 81 with mock.patch('importlib.import_module', autospec=True) \ 82 as import_module, \ 83 mock.patch.object(autotest, '_setup_done', True): 84 autotest.chromite_load(name) 85 import_module.assert_called_once_with(expected) 86 87 88@pytest.mark.parametrize('name', [ 89 'google.protobuf.internal.well_known_types', 90]) 91def test_deps_load(name): 92 """Test load().""" 93 with mock.patch('importlib.import_module', autospec=True) \ 94 as import_module, \ 95 mock.patch.object(autotest, '_setup_done', True): 96 autotest.deps_load(name) 97 import_module.assert_called_once_with(name) 98