mirror of
https://github.com/NixOS/nix.git
synced 2025-11-21 09:49:36 +01:00
The problem with old code was that it used getUri for both the `diskCache` as well as logging. This is really bad because it mixes the textual human readable representation with the caching. Also using getUri for the cache key is really problematic for the S3 store, since it doesn't include the `endpoint` in the cache key, so it's totally broken. This starts separating the logging / cache concerns by introducing a `getHumanReadableURI` that should only be used for logging. The caching logic now instead uses `getReference().render(/*withParams=*/false)` exclusively. This would need to be fixed in follow-ups, because that's really fragile and broken for some store types (but it was already broken before).
202 lines
6.2 KiB
C++
202 lines
6.2 KiB
C++
#include "nix_api_util.h"
|
|
#include "nix_api_util_internal.h"
|
|
#include "nix_api_store.h"
|
|
#include "nix_api_store_internal.h"
|
|
|
|
#include "nix/store/tests/nix_api_store.hh"
|
|
#include "nix/util/tests/string_callback.hh"
|
|
#include "nix/util/url.hh"
|
|
|
|
#include "store-tests-config.hh"
|
|
|
|
namespace nixC {
|
|
|
|
std::string PATH_SUFFIX = "/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-name";
|
|
|
|
TEST_F(nix_api_util_context, nix_libstore_init)
|
|
{
|
|
auto ret = nix_libstore_init(ctx);
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, nix_store_get_uri)
|
|
{
|
|
std::string str;
|
|
auto ret = nix_store_get_uri(ctx, store, OBSERVE_STRING(str));
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
auto expectedStoreURI = "local://?"
|
|
+ nix::encodeQuery({
|
|
{"log", nixLogDir},
|
|
{"state", nixStateDir},
|
|
{"store", nixStoreDir},
|
|
});
|
|
ASSERT_EQ(expectedStoreURI, str);
|
|
}
|
|
|
|
TEST_F(nix_api_util_context, nix_store_get_storedir_default)
|
|
{
|
|
nix_libstore_init(ctx);
|
|
Store * store = nix_store_open(ctx, nullptr, nullptr);
|
|
assert_ctx_ok();
|
|
ASSERT_NE(store, nullptr);
|
|
|
|
std::string str;
|
|
auto ret = nix_store_get_storedir(ctx, store, OBSERVE_STRING(str));
|
|
assert_ctx_ok();
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
|
|
// These tests run with a unique storeDir, but not a relocated store
|
|
ASSERT_STREQ(NIX_STORE_DIR, str.c_str());
|
|
|
|
nix_store_free(store);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, nix_store_get_storedir)
|
|
{
|
|
std::string str;
|
|
auto ret = nix_store_get_storedir(ctx, store, OBSERVE_STRING(str));
|
|
assert_ctx_ok();
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
|
|
// These tests run with a unique storeDir, but not a relocated store
|
|
ASSERT_STREQ(nixStoreDir.c_str(), str.c_str());
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, InvalidPathFails)
|
|
{
|
|
nix_store_parse_path(ctx, store, "invalid-path");
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, ReturnsValidStorePath)
|
|
{
|
|
StorePath * result = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
|
ASSERT_NE(result, nullptr);
|
|
ASSERT_STREQ("name", result->path.name().data());
|
|
ASSERT_STREQ(PATH_SUFFIX.substr(1).c_str(), result->path.to_string().data());
|
|
nix_store_path_free(result);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk)
|
|
{
|
|
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
|
ASSERT_EQ(ctx->last_err_code, NIX_OK);
|
|
nix_store_path_free(path);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
|
|
{
|
|
StorePath * path = nullptr;
|
|
ASSERT_NO_THROW(path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()));
|
|
nix_store_path_free(path);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, get_version)
|
|
{
|
|
std::string str;
|
|
auto ret = nix_store_get_version(ctx, store, OBSERVE_STRING(str));
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
ASSERT_STREQ(PACKAGE_VERSION, str.c_str());
|
|
}
|
|
|
|
TEST_F(nix_api_util_context, nix_store_open_dummy)
|
|
{
|
|
nix_libstore_init(ctx);
|
|
Store * store = nix_store_open(ctx, "dummy://", nullptr);
|
|
ASSERT_EQ(NIX_OK, ctx->last_err_code);
|
|
ASSERT_STREQ("dummy://", store->ptr->config.getReference().render(/*withParams=*/true).c_str());
|
|
|
|
std::string str;
|
|
nix_store_get_version(ctx, store, OBSERVE_STRING(str));
|
|
ASSERT_STREQ("", str.c_str());
|
|
|
|
nix_store_free(store);
|
|
}
|
|
|
|
TEST_F(nix_api_util_context, nix_store_open_invalid)
|
|
{
|
|
nix_libstore_init(ctx);
|
|
Store * store = nix_store_open(ctx, "invalid://", nullptr);
|
|
ASSERT_EQ(NIX_ERR_NIX_ERROR, ctx->last_err_code);
|
|
ASSERT_EQ(nullptr, store);
|
|
nix_store_free(store);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store)
|
|
{
|
|
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
|
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, path));
|
|
nix_store_path_free(path);
|
|
}
|
|
|
|
TEST_F(nix_api_store_test, nix_store_real_path)
|
|
{
|
|
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
|
std::string rp;
|
|
auto ret = nix_store_real_path(ctx, store, path, OBSERVE_STRING(rp));
|
|
assert_ctx_ok();
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
// Assumption: we're not testing with a relocated store
|
|
ASSERT_STREQ((nixStoreDir + PATH_SUFFIX).c_str(), rp.c_str());
|
|
|
|
nix_store_path_free(path);
|
|
}
|
|
|
|
TEST_F(nix_api_util_context, nix_store_real_path_relocated)
|
|
{
|
|
auto tmp = nix::createTempDir();
|
|
std::string storeRoot = tmp + "/store";
|
|
std::string stateDir = tmp + "/state";
|
|
std::string logDir = tmp + "/log";
|
|
const char * rootkv[] = {"root", storeRoot.c_str()};
|
|
const char * statekv[] = {"state", stateDir.c_str()};
|
|
const char * logkv[] = {"log", logDir.c_str()};
|
|
// const char * rokv[] = {"read-only", "true"};
|
|
const char ** kvs[] = {rootkv, statekv, logkv, NULL};
|
|
|
|
nix_libstore_init(ctx);
|
|
assert_ctx_ok();
|
|
|
|
Store * store = nix_store_open(ctx, "local", kvs);
|
|
assert_ctx_ok();
|
|
ASSERT_NE(store, nullptr);
|
|
|
|
std::string nixStoreDir;
|
|
auto ret = nix_store_get_storedir(ctx, store, OBSERVE_STRING(nixStoreDir));
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
ASSERT_STREQ(NIX_STORE_DIR, nixStoreDir.c_str());
|
|
|
|
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
|
assert_ctx_ok();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
std::string rp;
|
|
ret = nix_store_real_path(ctx, store, path, OBSERVE_STRING(rp));
|
|
assert_ctx_ok();
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
|
|
// Assumption: we're not testing with a relocated store
|
|
ASSERT_STREQ((storeRoot + NIX_STORE_DIR + PATH_SUFFIX).c_str(), rp.c_str());
|
|
|
|
nix_store_path_free(path);
|
|
}
|
|
|
|
TEST_F(nix_api_util_context, nix_store_real_path_binary_cache)
|
|
{
|
|
Store * store = nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir()).c_str(), nullptr);
|
|
assert_ctx_ok();
|
|
ASSERT_NE(store, nullptr);
|
|
|
|
std::string path_raw = std::string(NIX_STORE_DIR) + PATH_SUFFIX;
|
|
StorePath * path = nix_store_parse_path(ctx, store, path_raw.c_str());
|
|
assert_ctx_ok();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
std::string rp;
|
|
auto ret = nix_store_real_path(ctx, store, path, OBSERVE_STRING(rp));
|
|
assert_ctx_ok();
|
|
ASSERT_EQ(NIX_OK, ret);
|
|
ASSERT_STREQ(path_raw.c_str(), rp.c_str());
|
|
}
|
|
|
|
} // namespace nixC
|