Skip to content

Commit

Permalink
Test cached module identity (#2159)
Browse files Browse the repository at this point in the history
Test for #2152
  • Loading branch information
unmultimedio committed Jun 5, 2023
1 parent 206dd16 commit 78537dd
Showing 1 changed file with 21 additions and 2 deletions.
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())
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

0 comments on commit 78537dd

Please sign in to comment.