Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cached module identity #2159

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions private/bufpkg/bufmodule/bufmodulecache/cas_module_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ func TestCASModuleReaderHappyPath(t *testing.T) {
time.Now(),
)
require.NoError(t, err)
_, err = moduleReader.GetModule(context.Background(), pin)
_, err = moduleReader.GetModule(context.Background(), pin) // non-cached
require.NoError(t, err)
assert.Equal(t, 1, moduleReader.stats.Count())
assert.Equal(t, 0, moduleReader.stats.Hits())
verifyCache(t, storageBucket, pin, moduleManifest, blobs)

_, err = moduleReader.GetModule(context.Background(), pin)
cachedMod, err := moduleReader.GetModule(context.Background(), pin)
require.NoError(t, err)
assertModuleIdentity(t, cachedMod, pin.IdentityString(), pin.Commit())
assert.Equal(t, 2, moduleReader.stats.Count())
assert.Equal(t, 1, moduleReader.stats.Hits()) // We should have a cache hit the second time
verifyCache(t, storageBucket, pin, moduleManifest, blobs)
Expand Down Expand Up @@ -212,6 +213,24 @@ func verifyBlobContents(t *testing.T, bucket storage.ReadWriteBucket, basedir st
assert.Equal(t, bb.Bytes(), cachedModule)
}

func assertModuleIdentity(t *testing.T, module bufmodule.Module, expectedModuleIdentity string, expectedCommit string) {
require.NotNil(t, module)
require.NotEmpty(t, expectedCommit)
fileInfos, err := module.SourceFileInfos(context.Background())
require.NoError(t, err)
for _, fileInfo := range fileInfos {
require.NotNil(t, fileInfo.ModuleIdentity())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little surprised that there was no way to do module.Identity(), but I needed to loop over the files. I assume there's no way to access a module identity from a module with no proto files?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules always have .proto files - its invalid to have an empty module. ModuleFileSets inherit from Modules, and ModuleFileSets will have files from different Modules, meaning that you need to get identity at the file level given this inheritance.

assert.Equalf(
t, expectedModuleIdentity, fileInfo.ModuleIdentity().IdentityString(),
"unexpected module identity for file %q", fileInfo.Path(),
)
assert.Equalf(
t, expectedCommit, fileInfo.Commit(),
"unexpected commit for file %q", fileInfo.Path(),
)
}
}

type testModuleReader struct {
module bufmodule.Module
}
Expand Down