Skip to content

Commit

Permalink
Use an HTTP registry in Bzlmod tests
Browse files Browse the repository at this point in the history
This is in preparation for tracking the hashes of remote (non-`file:` URL) files in the lockfile. If the tests use local registries, they wouldn't be representative of the default situation.

Work towards bazelbuild#20369

Closes bazelbuild#21906.

PiperOrigin-RevId: 622290283
Change-Id: Ibe825d2bede84c1b0672dbb699aaf3ee5168a813
  • Loading branch information
fmeum authored and bazel-io committed Apr 5, 2024
1 parent 8fe5532 commit 9df8ddd
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 77 deletions.
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_fetch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.ScratchFile(
'.bazelrc',
[
Expand All @@ -48,6 +49,10 @@ def setUp(self):
self.ScratchFile('MODULE.bazel')
self.generatBuiltinModules()

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def generatBuiltinModules(self):
self.ScratchFile('platforms_mock/BUILD')
self.ScratchFile(
Expand Down
127 changes: 68 additions & 59 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'aaa', '1.1'
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
Expand Down Expand Up @@ -59,6 +60,10 @@ def setUp(self):
# deterministic?
os.remove(self.Path('MODULE.bazel.lock'))

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def testChangeModuleInRegistryWithoutLockfile(self):
# Add module 'sss' to the registry with dep on 'aaa'
self.main_registry.createCcModule('sss', '1.3', {'aaa': '1.1'})
Expand Down Expand Up @@ -1277,66 +1282,70 @@ def testExtensionEvaluationOnlyRerunOnRelevantUsagesChanges(self):

def testLockfileWithNoUserSpecificPath(self):
self.my_registry = BazelRegistry(os.path.join(self._test_cwd, 'registry'))
self.my_registry.setModuleBasePath('projects')
patch_file = self.ScratchFile(
'ss.patch',
[
'--- a/aaa.cc',
'+++ b/aaa.cc',
'@@ -1,6 +1,6 @@',
' #include <stdio.h>',
' #include "aaa.h"',
' void hello_aaa(const std::string& caller) {',
'- std::string lib_name = "[email protected]";',
'+ std::string lib_name = "[email protected] (remotely patched)";',
' printf("%s => %s\\n", caller.c_str(), lib_name.c_str());',
' }',
],
)
# Module with a local patch & extension
self.my_registry.createCcModule(
'ss',
'1.3-1',
{'ext': '1.0'},
patches=[patch_file],
patch_strip=1,
extra_module_file_contents=[
'my_ext = use_extension("@ext//:ext.bzl", "ext")',
'use_repo(my_ext, "justRepo")',
],
)
ext_src = [
'def _repo_impl(ctx): ctx.file("BUILD")',
'repo = repository_rule(_repo_impl)',
'def _ext_impl(ctx): repo(name=justRepo)',
'ext=module_extension(_ext_impl)',
]
self.my_registry.createLocalPathModule('ext', '1.0', 'ext')
scratchFile(self.my_registry.projects.joinpath('ext', 'BUILD'))
scratchFile(self.my_registry.projects.joinpath('ext', 'ext.bzl'), ext_src)

self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "ss", version = "1.3-1")',
],
)
self.ScratchFile('BUILD.bazel', ['filegroup(name = "lala")'])
self.RunBazel(
['build', '--registry=file:///%workspace%/registry', '//:lala']
)
self.my_registry.start()
try:
self.my_registry.setModuleBasePath('projects')
patch_file = self.ScratchFile(
'ss.patch',
[
'--- a/aaa.cc',
'+++ b/aaa.cc',
'@@ -1,6 +1,6 @@',
' #include <stdio.h>',
' #include "aaa.h"',
' void hello_aaa(const std::string& caller) {',
'- std::string lib_name = "[email protected]";',
'+ std::string lib_name = "[email protected] (remotely patched)";',
' printf("%s => %s\\n", caller.c_str(), lib_name.c_str());',
' }',
],
)
# Module with a local patch & extension
self.my_registry.createCcModule(
'ss',
'1.3-1',
{'ext': '1.0'},
patches=[patch_file],
patch_strip=1,
extra_module_file_contents=[
'my_ext = use_extension("@ext//:ext.bzl", "ext")',
'use_repo(my_ext, "justRepo")',
],
)
ext_src = [
'def _repo_impl(ctx): ctx.file("BUILD")',
'repo = repository_rule(_repo_impl)',
'def _ext_impl(ctx): repo(name=justRepo)',
'ext=module_extension(_ext_impl)',
]
self.my_registry.createLocalPathModule('ext', '1.0', 'ext')
scratchFile(self.my_registry.projects.joinpath('ext', 'BUILD'))
scratchFile(self.my_registry.projects.joinpath('ext', 'ext.bzl'), ext_src)

self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "ss", version = "1.3-1")',
],
)
self.ScratchFile('BUILD.bazel', ['filegroup(name = "lala")'])
self.RunBazel(
['build', '--registry=file:///%workspace%/registry', '//:lala']
)

with open('MODULE.bazel.lock', 'r') as json_file:
lockfile = json.load(json_file)
ss_dep = lockfile['moduleDepGraph']['[email protected]']
remote_patches = ss_dep['repoSpec']['attributes']['remote_patches']
ext_usage_location = ss_dep['extensionUsages'][0]['location']['file']

self.assertNotIn(self.my_registry.getURL(), ext_usage_location)
self.assertIn('%workspace%', ext_usage_location)
for key in remote_patches.keys():
self.assertNotIn(self.my_registry.getURL(), key)
self.assertIn('%workspace%', key)
with open('MODULE.bazel.lock', 'r') as json_file:
lockfile = json.load(json_file)
ss_dep = lockfile['moduleDepGraph']['[email protected]']
remote_patches = ss_dep['repoSpec']['attributes']['remote_patches']
ext_usage_location = ss_dep['extensionUsages'][0]['location']['file']

self.assertNotIn(self.my_registry.getURL(), ext_usage_location)
self.assertIn('%workspace%', ext_usage_location)
for key in remote_patches.keys():
self.assertNotIn(self.my_registry.getURL(), key)
self.assertIn('%workspace%', key)
finally:
self.my_registry.stop()

def testExtensionEvaluationRerunsIfDepGraphOrderChanges(self):
self.ScratchFile(
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setUp(self):
self.registries_work_dir = tempfile.mkdtemp(dir=self._test_cwd)
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main'))
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'aaa', '1.1'
).createCcModule(
Expand Down Expand Up @@ -60,6 +61,10 @@ def setUp(self):
],
)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def writeMainProjectFiles(self):
self.ScratchFile('aaa.patch', [
'--- a/aaa.cc',
Expand Down
41 changes: 25 additions & 16 deletions src/test/py/bazel/bzlmod/bazel_overrides_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'aaa', '1.1'
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
Expand All @@ -54,6 +55,10 @@ def setUp(self):
],
)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def writeMainProjectFiles(self):
self.ScratchFile(
'aaa.patch',
Expand Down Expand Up @@ -122,22 +127,26 @@ def testRegistryOverride(self):
os.path.join(self.registries_work_dir, 'another'),
' from another registry',
)
another_registry.createCcModule('aaa', '1.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "aaa", version = "1.0")',
'bazel_dep(name = "bbb", version = "1.0")',
'single_version_override(',
' module_name = "aaa",',
' registry = "%s",' % another_registry.getURL(),
')',
],
)
_, stdout, _ = self.RunBazel(['run', '//:main'])
self.assertIn('main function => [email protected] from another registry', stdout)
self.assertIn('main function => [email protected]', stdout)
self.assertIn('[email protected] => [email protected] from another registry', stdout)
another_registry.start()
try:
another_registry.createCcModule('aaa', '1.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "aaa", version = "1.0")',
'bazel_dep(name = "bbb", version = "1.0")',
'single_version_override(',
' module_name = "aaa",',
' registry = "%s",' % another_registry.getURL(),
')',
],
)
_, stdout, _ = self.RunBazel(['run', '//:main'])
self.assertIn('main function => [email protected] from another registry', stdout)
self.assertIn('main function => [email protected]', stdout)
self.assertIn('[email protected] => [email protected] from another registry', stdout)
finally:
another_registry.stop()

def testArchiveOverride(self):
self.writeMainProjectFiles()
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'aaa', '1.1'
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
Expand All @@ -56,6 +57,10 @@ def setUp(self):
],
)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def testRunfilesRepoMappingManifest(self):
self.main_registry.setModuleBasePath('projects')
projects_dir = self.main_registry.projects
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_vendor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.ScratchFile(
'.bazelrc',
[
Expand All @@ -49,6 +50,10 @@ def setUp(self):
self.ScratchFile('MODULE.bazel')
self.generateBuiltinModules()

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def generateBuiltinModules(self):
self.ScratchFile('platforms_mock/BUILD')
self.ScratchFile(
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'aaa', '1.1'
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
Expand All @@ -50,6 +51,10 @@ def setUp(self):
)
self.writeBazelrcFile()

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def writeBazelrcFile(self, allow_yanked_versions=True):
self.ScratchFile(
'.bazelrc',
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/bzlmod_query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
self.registries_work_dir = tempfile.mkdtemp(dir=self._test_cwd)
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main'))
self.main_registry.start()
self.main_registry.createCcModule('aaa', '1.0', {'ccc': '1.2'}) \
.createCcModule('aaa', '1.1') \
.createCcModule('bbb', '1.0', {'aaa': '1.0'}, {'aaa': 'com_foo_bar_aaa'}) \
Expand All @@ -50,6 +51,10 @@ def setUp(self):
],
)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def testQueryModuleRepoTargetsBelow(self):
self.ScratchFile('MODULE.bazel', [
'bazel_dep(name = "aaa", version = "1.0", repo_name = "my_repo")',
Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/external_repo_completion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.setModuleBasePath('projects')
self.projects_dir = self.main_registry.projects
self.maxDiff = None # there are some long diffs in this test
Expand Down Expand Up @@ -157,6 +158,10 @@ def setUp(self):
)
scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def complete(self, bazel_args):
"""Get the bash completions for the given "bazel" command line."""

Expand Down
5 changes: 5 additions & 0 deletions src/test/py/bazel/bzlmod/mod_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setUp(self):
self.main_registry = BazelRegistry(
os.path.join(self.registries_work_dir, 'main')
)
self.main_registry.start()
self.main_registry.setModuleBasePath('projects')
self.projects_dir = self.main_registry.projects
self.maxDiff = None # there are some long diffs in this test
Expand Down Expand Up @@ -133,6 +134,10 @@ def setUp(self):
scratchFile(self.projects_dir.joinpath('ext2', 'BUILD'))
scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src)

def tearDown(self):
self.main_registry.stop()
test_base.TestBase.tearDown(self)

def testFailWithoutBzlmod(self):
_, _, stderr = self.RunBazel(
['mod', 'graph', '--noenable_bzlmod'], allow_failure=True
Expand Down
15 changes: 13 additions & 2 deletions src/test/py/bazel/bzlmod/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,28 @@ def __init__(self, root, registry_suffix=''):
self.archives = self.root.joinpath('archives')
self.archives.mkdir(parents=True, exist_ok=True)
self.registry_suffix = registry_suffix
self.http_server = StaticHTTPServer(self.root)

def setModuleBasePath(self, module_base_path):
bazel_registry = {
'module_base_path': module_base_path,
'module_base_path': (
self.root.joinpath(module_base_path).resolve().as_posix()
),
}
with self.root.joinpath('bazel_registry.json').open('w') as f:
json.dump(bazel_registry, f, indent=4, sort_keys=True)

def start(self):
"""Start an HTTP server serving the registry."""
self.http_server.__enter__()

def stop(self):
"""Stop the HTTP server."""
self.http_server.__exit__(None, None, None)

def getURL(self):
"""Return the URL of this registry."""
return self.root.resolve().as_uri()
return self.http_server.getURL()

def generateCcSource(
self,
Expand Down

0 comments on commit 9df8ddd

Please sign in to comment.