about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/mock-binary-cache-store.hh
blob: 419077b6bbb03104c866a8c47f8a03eaf5991a1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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