feat(3p/nix): add MockBinaryCacheStore
This adds an implementation of BinaryCacheStore to be used by tests exercising both the logic inherent to BinaryCacheStore and more general Store tests. A new library target, nixstoremock, is created to indicate that this file is intended only for use in tests and not in user-facing code. Change-Id: Ib68f777238843a4f3a2303db8a69735fbc22d161 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1645 Tested-by: BuildkiteCI Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
parent
28c7e926ea
commit
3c3527e16b
3 changed files with 170 additions and 1 deletions
59
third_party/nix/src/libstore/mock-binary-cache-store.hh
vendored
Normal file
59
third_party/nix/src/libstore/mock-binary-cache-store.hh
vendored
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#pragma once
|
||||
|
||||
#include <absl/container/btree_map.h>
|
||||
#include <absl/container/flat_hash_map.h>
|
||||
|
||||
#include "libstore/binary-cache-store.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
// MockBinaryCacheStore implements a memory-based BinaryCacheStore, for use in
|
||||
// tests.
|
||||
class MockBinaryCacheStore : public BinaryCacheStore {
|
||||
public:
|
||||
MockBinaryCacheStore(const Params& params);
|
||||
|
||||
// Store API
|
||||
|
||||
std::string getUri() override;
|
||||
|
||||
bool fileExists(const std::string& path) override;
|
||||
|
||||
void upsertFile(const std::string& path, const std::string& data,
|
||||
const std::string& mimeType) override;
|
||||
|
||||
void getFile(
|
||||
const std::string& path,
|
||||
Callback<std::shared_ptr<std::string>> callback) noexcept override;
|
||||
|
||||
PathSet queryAllValidPaths() override;
|
||||
|
||||
// Test API
|
||||
|
||||
// Remove a file from the store.
|
||||
void DeleteFile(const std::string& path);
|
||||
|
||||
// Same as upsert, but bypasses injected errors.
|
||||
void SetFileContentsForTest(const std::string& path, const std::string& data,
|
||||
const std::string& mimeType);
|
||||
|
||||
void PrepareErrorInjection(const std::string& path,
|
||||
std::function<void()> throw_func);
|
||||
|
||||
void CancelErrorInjection(const std::string& path);
|
||||
|
||||
// Internals
|
||||
|
||||
private:
|
||||
void ThrowInjectedErrors(const std::string& path);
|
||||
|
||||
struct MemoryFile {
|
||||
std::string data;
|
||||
std::string mimeType;
|
||||
};
|
||||
|
||||
absl::btree_map<std::string, MemoryFile> contents_;
|
||||
absl::flat_hash_map<std::string, std::function<void()>> errorInjections_;
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
Loading…
Add table
Add a link
Reference in a new issue