Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 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 | """Unittests for loman.py""" |
| 6 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 7 | import os |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 8 | import xml.etree.ElementTree as ElementTree |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 9 | |
| 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import osutils |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 12 | from chromite.lib import partial_mock |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 13 | from chromite.scripts import loman |
| 14 | |
| 15 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 16 | class RunGitMock(partial_mock.PartialCmdMock): |
| 17 | """Partial mock for git.RunGit.""" |
| 18 | TARGET = 'chromite.lib.git' |
| 19 | ATTRS = ('RunGit',) |
| 20 | DEFAULT_ATTR = 'RunGit' |
| 21 | |
| 22 | def RunGit(self, _git_repo, cmd, _retry=True, **kwargs): |
| 23 | return self._results['RunGit'].LookupResult( |
| 24 | (cmd,), hook_args=(cmd,), hook_kwargs=kwargs) |
| 25 | |
| 26 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 27 | class ParserTest(cros_test_lib.OutputTestCase): |
| 28 | """Tests for the CLI parser.""" |
| 29 | |
| 30 | def setUp(self): |
| 31 | self.parser = loman.GetParser() |
| 32 | |
| 33 | def testNoCommand(self): |
| 34 | """Require a command at least.""" |
| 35 | with self.OutputCapturer(): |
| 36 | self.assertRaises(SystemExit, self.parser.parse_args, []) |
| 37 | |
| 38 | def testBadCommand(self): |
| 39 | """Reject unknown commands.""" |
| 40 | with self.OutputCapturer(): |
| 41 | self.assertRaises(SystemExit, self.parser.parse_args, ['flyaway']) |
| 42 | |
| 43 | def testAddCommand(self): |
| 44 | """Verify basic add command behavior.""" |
| 45 | with self.OutputCapturer(): |
| 46 | self.parser.parse_args(['add', '--workon', 'project']) |
| 47 | self.parser.parse_args(['add', 'project', 'path', '--remote', 'foo']) |
| 48 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 49 | |
| 50 | class ManifestTest(cros_test_lib.TempDirTestCase): |
| 51 | """Tests that need a real .repo/ manifest layout.""" |
| 52 | |
| 53 | def setUp(self): |
| 54 | # The loman code looks for the repo root, so make one, and chdir there. |
| 55 | os.chdir(self.tempdir) |
| 56 | |
| 57 | for d in ('repo', 'manifests', 'manifests.git'): |
| 58 | osutils.SafeMakedirs(os.path.join('.repo', d)) |
| 59 | |
| 60 | for m in ('default.xml', 'full.xml', 'minilayout.xml'): |
| 61 | osutils.Touch(os.path.join('.repo', 'manifests', m)) |
| 62 | |
| 63 | self._SetManifest('default.xml') |
| 64 | |
| 65 | def _SetManifest(self, manifest): |
| 66 | """Set active manifest to point to |manifest|.""" |
| 67 | source = os.path.join('.repo', 'manifest.xml') |
Gwendal Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame] | 68 | target = os.path.join('manifests', manifest) |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 69 | osutils.SafeUnlink(source) |
| 70 | os.symlink(target, source) |
| 71 | |
| 72 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 73 | class AddTest(cros_test_lib.MockOutputTestCase, ManifestTest): |
| 74 | """Tests for the add command.""" |
| 75 | |
| 76 | def testRejectBadCommands(self): |
| 77 | """Reject bad invocations.""" |
| 78 | bad_cmds = ( |
| 79 | # Missing path. |
| 80 | ['add'], |
| 81 | # Extra project. |
| 82 | ['add', '--workon', 'path', 'project'], |
| 83 | # Missing --remote. |
| 84 | ['add', 'path', 'project'], |
| 85 | # Missing project. |
| 86 | ['add', 'path', '--remote', 'remote'], |
| 87 | ) |
| 88 | with self.OutputCapturer(): |
| 89 | for cmd in bad_cmds: |
| 90 | self.assertRaises(SystemExit, loman.main, cmd) |
Gwendal Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | class NoMiniayoutTest(cros_test_lib.MockOutputTestCase, ManifestTest): |
| 94 | """Check deprecated minilayout setups are detected.""" |
| 95 | |
| 96 | def setUp(self): |
| 97 | self._SetManifest('minilayout.xml') |
| 98 | |
| 99 | def testMiniLayoutDetected(self): |
| 100 | """Check error is raised when repo is setup with minilayout.""" |
| 101 | |
| 102 | class _Error(Exception): |
| 103 | """Stub for test.""" |
| 104 | |
| 105 | self.PatchObject(loman, '_AssertNotMiniLayout', side_effect=_Error) |
| 106 | cmd = ['add', '-w', 'foo'] |
| 107 | with self.OutputCapturer(): |
| 108 | self.assertRaises(_Error, loman.main, cmd) |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class IncludeXmlTest(cros_test_lib.MockOutputTestCase, ManifestTest): |
| 112 | """End to End tests for reading and producing XML trees.""" |
| 113 | |
| 114 | PROJECT = 'chromiumos/repohooks' |
| 115 | |
| 116 | def setUp(self): |
| 117 | INCLUDING_XML = 'including.xml' |
| 118 | INCLUDED_XML = 'included.xml' |
| 119 | osutils.WriteFile(os.path.join('.repo', 'manifests', INCLUDING_XML), |
| 120 | """ |
| 121 | <manifest> |
| 122 | <include name="%s" /> |
| 123 | <project remote="cros-internal" path="crostools" groups="br" name="ct" /> |
| 124 | </manifest>""" % (INCLUDED_XML,)) |
| 125 | |
| 126 | osutils.WriteFile(os.path.join('.repo', 'manifests', INCLUDED_XML), |
| 127 | """ |
| 128 | <manifest> |
| 129 | <default remote="cros" revision="HEAD" /> |
| 130 | <remote name="cros" /> |
| 131 | <remote name="cros-internal" /> |
| 132 | <project path="src/repohooks" name="%s" groups="minilayout,bt" /> |
| 133 | </manifest>""" % (self.PROJECT,)) |
| 134 | self._SetManifest(INCLUDING_XML) |
| 135 | |
| 136 | self.git_mock = self.StartPatcher(RunGitMock()) |
| 137 | self.git_mock.AddCmdResult( |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 138 | ['symbolic-ref', '-q', 'HEAD'], stdout='default') |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 139 | self.git_mock.AddCmdResult( |
| 140 | ['config', '--get-regexp', 'branch\\.default\\.(remote|merge)'], |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 141 | stdout='branch.default.merge firmware-branch') |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 142 | |
| 143 | self.git_mock.AddCmdResult( |
| 144 | ['config', |
| 145 | '-f', os.path.join(self.tempdir, '.repo', 'manifests.git', 'config'), |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 146 | '--get', 'manifest.groups'], stdout='group1,group2') |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 147 | |
| 148 | def testAddExistingProject(self): |
| 149 | """Add an existing project, check no local_manifest.xml are created.""" |
| 150 | self.git_mock.AddCmdResult( |
| 151 | ['config', |
| 152 | '-f', os.path.join(self.tempdir, '.repo', 'manifests.git', 'config'), |
| 153 | 'manifest.groups', |
| 154 | 'minilayout,platform-linux,group1,group2,name:%s' % (self.PROJECT,)]) |
| 155 | cmd = ['add', '-w', self.PROJECT] |
| 156 | with self.OutputCapturer(): |
| 157 | self.assertEqual(loman.main(cmd), 0) |
| 158 | self.assertNotExists(os.path.join('.repo', 'local_manifest.xml')) |
| 159 | |
| 160 | def testAddNewProject(self): |
| 161 | """Add new project to the repo. |
| 162 | |
| 163 | Check local_manifest.xml is created and valid. |
| 164 | """ |
| 165 | new_project = 'project' |
| 166 | self.git_mock.AddCmdResult( |
| 167 | ['config', |
| 168 | '-f', os.path.join(self.tempdir, '.repo', 'manifests.git', 'config'), |
| 169 | 'manifest.groups', |
| 170 | 'minilayout,platform-linux,group1,group2,name:%s' % (new_project,)],) |
| 171 | cmd = ['add', new_project, 'path', '-r', 'remote'] |
| 172 | with self.OutputCapturer(): |
| 173 | self.assertEqual(loman.main(cmd), 0) |
| 174 | expected_local_manifest_nodes = ElementTree.fromstring(""" |
| 175 | <manifest> |
| 176 | <project name="project" path="path" remote="remote" workon="False" /> |
| 177 | </manifest>""") |
| 178 | with open(os.path.join('.repo', 'local_manifest.xml')) as f: |
| 179 | local_manifest_nodes = ElementTree.fromstring(f.read()) |
| 180 | |
| 181 | # Read project, check for failure. |
| 182 | self.assertEqual(ElementTree.tostring(expected_local_manifest_nodes), |
| 183 | ElementTree.tostring(local_manifest_nodes)) |
| 184 | |
| 185 | # Check that re-adding triggers error. |
| 186 | cmd = ['add', new_project, 'path', '-r', 'remote'] |
| 187 | with self.OutputCapturer() as output: |
| 188 | self.assertRaises(SystemExit, loman.main, cmd) |
| 189 | self.assertIn('conflicts with', '\n'.join(output.GetStderrLines())) |