about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/mock-binary-cache-store.hh
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-08-04T23·44-0700
committerkanepyork <rikingcoding@gmail.com>2020-08-05T04·11+0000
commit3c3527e16bf1d0bf57207dcc54975534f5252225 (patch)
tree43eae1e4f72dee522fedbb2375da4f8b7103e8ca /third_party/nix/src/libstore/mock-binary-cache-store.hh
parent28c7e926ea48e4f5e57b9b8d1a70ccc494b2c188 (diff)
feat(3p/nix): add MockBinaryCacheStore r/1595
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>
Diffstat (limited to '')
-rw-r--r--third_party/nix/src/libstore/mock-binary-cache-store.hh59
1 files changed, 59 insertions, 0 deletions
diff --git a/third_party/nix/src/libstore/mock-binary-cache-store.hh b/third_party/nix/src/libstore/mock-binary-cache-store.hh
new file mode 100644
index 0000000000..419077b6bb
--- /dev/null
+++ b/third_party/nix/src/libstore/mock-binary-cache-store.hh
@@ -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