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