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