diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-06 11:35:32 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-11 14:52:07 +0200 |
commit | 8e652e5d7b6fbe0abf07df62814c655759564247 (patch) | |
tree | cd338f2f3425cf6305e0c2647ae6cc6369998d45 /test | |
parent | 15c54bf96fbc8ca19af7ff5edf0faa37228c4c00 (diff) | |
download | justbuild-8e652e5d7b6fbe0abf07df62814c655759564247.tar.gz |
Use ArtifactDigestFactory in tests
...to create ArtifactDigests.
In some tests ArtifactDigests were constructed using non-hexadecimal string identifiers. These tests were adjusted so that ArtifactDigest contained a valid hash.
Diffstat (limited to 'test')
-rw-r--r-- | test/buildtool/common/TARGETS | 2 | ||||
-rw-r--r-- | test/buildtool/common/artifact_description.test.cpp | 23 | ||||
-rw-r--r-- | test/buildtool/execution_engine/executor/TARGETS | 1 | ||||
-rw-r--r-- | test/buildtool/execution_engine/executor/executor.test.cpp | 50 | ||||
-rw-r--r-- | test/buildtool/file_system/TARGETS | 2 | ||||
-rw-r--r-- | test/buildtool/file_system/file_root.test.cpp | 54 |
6 files changed, 82 insertions, 50 deletions
diff --git a/test/buildtool/common/TARGETS b/test/buildtool/common/TARGETS index 90d3742c..dc6b3056 100644 --- a/test/buildtool/common/TARGETS +++ b/test/buildtool/common/TARGETS @@ -8,6 +8,8 @@ , ["@", "json", "", "json"] , ["@", "src", "src/buildtool/common", "artifact_description"] , ["@", "src", "src/buildtool/common", "common"] + , ["@", "src", "src/buildtool/common", "artifact_digest_factory"] + , ["@", "src", "src/buildtool/crypto", "hash_function"] , ["@", "src", "src/buildtool/file_system", "object_type"] ] , "stage": ["test", "buildtool", "common"] diff --git a/test/buildtool/common/artifact_description.test.cpp b/test/buildtool/common/artifact_description.test.cpp index c6d3a8d5..f7af5159 100644 --- a/test/buildtool/common/artifact_description.test.cpp +++ b/test/buildtool/common/artifact_description.test.cpp @@ -20,8 +20,19 @@ #include "catch2/catch_test_macros.hpp" #include "nlohmann/json.hpp" #include "src/buildtool/common/artifact.hpp" +#include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/common/artifact_digest_factory.hpp" +#include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/file_system/object_type.hpp" +static auto NamedDigest(std::string const& str) -> ArtifactDigest { + HashFunction const hash_function{Compatibility::IsCompatible() + ? HashFunction::Type::PlainSHA256 + : HashFunction::Type::GitSHA1}; + return ArtifactDigestFactory::HashDataAs<ObjectType::File>(hash_function, + str); +} + [[nodiscard]] auto operator==(Artifact const& lhs, Artifact const& rhs) -> bool { return lhs.Id() == rhs.Id() and lhs.FilePath() == rhs.FilePath() and @@ -38,22 +49,19 @@ TEST_CASE("Local artifact", "[artifact_description]") { TEST_CASE("Known artifact", "[artifact_description]") { SECTION("File object") { auto known_desc = ArtifactDescription::CreateKnown( - ArtifactDigest{std::string{"f_fake_hash"}, 0, /*is_tree=*/false}, - ObjectType::File); + NamedDigest("f_fake_hash"), ObjectType::File); auto from_json = ArtifactDescription::FromJson(known_desc.ToJson()); CHECK(known_desc == *from_json); } SECTION("Executable object") { auto known_desc = ArtifactDescription::CreateKnown( - ArtifactDigest{std::string{"x_fake_hash"}, 1, /*is_tree=*/false}, - ObjectType::Executable); + NamedDigest("x_fake_hash"), ObjectType::Executable); auto from_json = ArtifactDescription::FromJson(known_desc.ToJson()); CHECK(known_desc == *from_json); } SECTION("Symlink object") { auto known_desc = ArtifactDescription::CreateKnown( - ArtifactDigest{std::string{"l_fake_hash"}, 2, /*is_tree=*/false}, - ObjectType::Symlink); + NamedDigest("l_fake_hash"), ObjectType::Symlink); auto from_json = ArtifactDescription::FromJson(known_desc.ToJson()); CHECK(known_desc == *from_json); } @@ -69,8 +77,7 @@ TEST_CASE("Action artifact", "[artifact_description]") { TEST_CASE("From JSON", "[artifact_description]") { auto local = ArtifactDescription::CreateLocal("local", "repo").ToJson(); auto known = - ArtifactDescription::CreateKnown( - ArtifactDigest{"hash", 0, /*is_tree=*/false}, ObjectType::File) + ArtifactDescription::CreateKnown(NamedDigest("hash"), ObjectType::File) .ToJson(); auto action = ArtifactDescription::CreateAction("id", "output").ToJson(); diff --git a/test/buildtool/execution_engine/executor/TARGETS b/test/buildtool/execution_engine/executor/TARGETS index cfe6ec23..a3176e91 100644 --- a/test/buildtool/execution_engine/executor/TARGETS +++ b/test/buildtool/execution_engine/executor/TARGETS @@ -32,6 +32,7 @@ [ ["@", "src", "src/buildtool/auth", "auth"] , ["@", "src", "src/buildtool/common", "artifact_description"] , ["@", "src", "src/buildtool/common", "common"] + , ["@", "src", "src/buildtool/common", "artifact_digest_factory"] , ["@", "src", "src/buildtool/common", "config"] , ["@", "src", "src/buildtool/common/remote", "retry_config"] , ["@", "src", "src/buildtool/execution_api/common", "common"] diff --git a/test/buildtool/execution_engine/executor/executor.test.cpp b/test/buildtool/execution_engine/executor/executor.test.cpp index 22cd2cc4..ee950ec0 100644 --- a/test/buildtool/execution_engine/executor/executor.test.cpp +++ b/test/buildtool/execution_engine/executor/executor.test.cpp @@ -27,6 +27,8 @@ #include "gsl/gsl" #include "src/buildtool/auth/authentication.hpp" #include "src/buildtool/common/artifact_description.hpp" +#include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/common/artifact_digest_factory.hpp" #include "src/buildtool/common/repository_config.hpp" #include "src/buildtool/common/statistics.hpp" #include "src/buildtool/compatibility/compatibility.hpp" @@ -61,6 +63,14 @@ struct TestApiConfig { TestResponseConfig response; }; +static auto NamedDigest(std::string const& str) -> ArtifactDigest { + HashFunction const hash_function{Compatibility::IsCompatible() + ? HashFunction::Type::PlainSHA256 + : HashFunction::Type::GitSHA1}; + return ArtifactDigestFactory::HashDataAs<ObjectType::File>(hash_function, + str); +} + // forward declarations class TestApi; class TestAction; @@ -123,9 +133,8 @@ class TestResponse : public IExecutionResponse { try { artifacts.emplace( path, - Artifact::ObjectInfo{ - .digest = ArtifactDigest{path, 0, /*is_tree=*/false}, - .type = ObjectType::File}); + Artifact::ObjectInfo{.digest = NamedDigest(path), + .type = ObjectType::File}); } catch (...) { return; } @@ -261,24 +270,25 @@ class TestApi : public IExecutionApi { auto const local_cpp_desc = ArtifactDescription::CreateLocal(path{"local.cpp"}, ""); - auto const known_cpp_desc = ArtifactDescription::CreateKnown( - ArtifactDigest{"known.cpp", 0, /*is_tree=*/false}, ObjectType::File); + auto const known_digest = NamedDigest("known.cpp"); + auto const known_cpp_desc = + ArtifactDescription::CreateKnown(known_digest, ObjectType::File); auto const test_action_desc = ActionDescription{ {"output1.exe", "output2.exe"}, {}, Action{"test_action", {"cmd", "line"}, {}}, - {{"local.cpp", local_cpp_desc}, {"known.cpp", known_cpp_desc}}}; + {{"local.cpp", local_cpp_desc}, {known_digest.hash(), known_cpp_desc}}}; CHECK(g->AddAction(test_action_desc)); CHECK(FileSystemManager::WriteFile("local.cpp", ws / "local.cpp")); TestApiConfig config{}; - config.artifacts["local.cpp"].uploads = true; - config.artifacts["known.cpp"].available = true; - config.artifacts["output1.exe"].available = true; - config.artifacts["output2.exe"].available = true; + config.artifacts[NamedDigest("local.cpp").hash()].uploads = true; + config.artifacts[NamedDigest("known.cpp").hash()].available = true; + config.artifacts[NamedDigest("output1.exe").hash()].available = true; + config.artifacts[NamedDigest("output2.exe").hash()].available = true; config.execution.failed = false; config.execution.outputs = {"output1.exe", "output2.exe"}; @@ -302,10 +312,9 @@ TEST_CASE("Executor: Process artifact", "[executor]") { auto const local_cpp_id = ArtifactDescription::CreateLocal("local.cpp", "").Id(); - auto const known_cpp_id = - ArtifactDescription::CreateKnown( - ArtifactDigest{"known.cpp", 0, /*is_tree=*/false}, ObjectType::File) - .Id(); + auto const known_cpp_id = ArtifactDescription::CreateKnown( + NamedDigest("known.cpp"), ObjectType::File) + .Id(); Auth auth{}; RetryConfig retry_config{}; // default retry config @@ -331,7 +340,7 @@ TEST_CASE("Executor: Process artifact", "[executor]") { } SECTION("Processing fails if uploading local artifact failed") { - config.artifacts["local.cpp"].uploads = false; + config.artifacts[NamedDigest("local.cpp").hash()].uploads = false; auto api = TestApi::Ptr{new TestApi{config}}; Statistics stats{}; @@ -349,7 +358,7 @@ TEST_CASE("Executor: Process artifact", "[executor]") { } SECTION("Processing fails if known artifact is not available") { - config.artifacts["known.cpp"].available = false; + config.artifacts[NamedDigest("known.cpp").hash()].available = false; auto api = TestApi::Ptr{new TestApi{config}}; Statistics stats{}; @@ -381,10 +390,9 @@ TEST_CASE("Executor: Process action", "[executor]") { auto const local_cpp_id = ArtifactDescription::CreateLocal("local.cpp", "").Id(); - auto const known_cpp_id = - ArtifactDescription::CreateKnown( - ArtifactDigest{"known.cpp", 0, /*is_tree=*/false}, ObjectType::File) - .Id(); + auto const known_cpp_id = ArtifactDescription::CreateKnown( + NamedDigest("known.cpp"), ObjectType::File) + .Id(); ActionIdentifier const action_id{"test_action"}; auto const output1_id = @@ -441,7 +449,7 @@ TEST_CASE("Executor: Process action", "[executor]") { } SECTION("Processing succeeds even if output is not available in CAS") { - config.artifacts["output2.exe"].available = false; + config.artifacts[NamedDigest("output2.exe").hash()].available = false; auto api = TestApi::Ptr{new TestApi{config}}; Statistics stats{}; diff --git a/test/buildtool/file_system/TARGETS b/test/buildtool/file_system/TARGETS index a8e998a6..a7342221 100644 --- a/test/buildtool/file_system/TARGETS +++ b/test/buildtool/file_system/TARGETS @@ -61,6 +61,8 @@ , ["utils", "container_matchers"] , ["@", "src", "src/buildtool/common", "artifact_description"] , ["@", "src", "src/buildtool/common", "common"] + , ["@", "src", "src/buildtool/common", "artifact_digest_factory"] + , ["@", "src", "src/buildtool/crypto", "hash_function"] , ["@", "src", "src/buildtool/file_system", "file_root"] , ["@", "src", "src/buildtool/file_system", "file_system_manager"] , ["utils", "shell_quoting"] diff --git a/test/buildtool/file_system/file_root.test.cpp b/test/buildtool/file_system/file_root.test.cpp index 2f21dfab..f709aa7a 100644 --- a/test/buildtool/file_system/file_root.test.cpp +++ b/test/buildtool/file_system/file_root.test.cpp @@ -24,6 +24,8 @@ #include "catch2/catch_test_macros.hpp" #include "src/buildtool/common/artifact_description.hpp" #include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/common/artifact_digest_factory.hpp" +#include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" #include "test/utils/container_matchers.hpp" #include "test/utils/shell_quoting.hpp" @@ -411,37 +413,47 @@ static void CheckGitRoot(bool ignore_special) noexcept { auto const foo = root->ToArtifactDescription("baz/foo", "repo"); REQUIRE(foo); if (Compatibility::IsCompatible()) { + auto const digest = + ArtifactDigestFactory::Create(HashFunction::Type::PlainSHA256, + kFooIdSha256, + kFooContentLength, + /*is_tree=*/false); + REQUIRE(digest); CHECK(*foo == - ArtifactDescription::CreateKnown( - ArtifactDigest{ - kFooIdSha256, kFooContentLength, /*is_tree=*/false}, - ObjectType::File)); + ArtifactDescription::CreateKnown(*digest, ObjectType::File)); } else { - CHECK(*foo == - ArtifactDescription::CreateKnown( - ArtifactDigest{ - kFooIdGitSha1, kFooContentLength, /*is_tree=*/false}, - ObjectType::File, - "repo")); + auto const digest = + ArtifactDigestFactory::Create(HashFunction::Type::GitSHA1, + kFooIdGitSha1, + kFooContentLength, + /*is_tree=*/false); + REQUIRE(digest); + CHECK(*foo == ArtifactDescription::CreateKnown( + *digest, ObjectType::File, "repo")); } auto const bar = root->ToArtifactDescription("baz/bar", "repo"); REQUIRE(bar); if (Compatibility::IsCompatible()) { - CHECK(*bar == - ArtifactDescription::CreateKnown( - ArtifactDigest{ - kBarIdSha256, kBarContentLength, /*is_tree=*/false}, - ObjectType::Executable)); + auto const digest = + ArtifactDigestFactory::Create(HashFunction::Type::PlainSHA256, + kBarIdSha256, + kBarContentLength, + /*is_tree=*/false); + REQUIRE(digest); + CHECK(*bar == ArtifactDescription::CreateKnown(*digest, + ObjectType::Executable)); } else { - CHECK(*bar == - ArtifactDescription::CreateKnown( - ArtifactDigest{ - kBarIdGitSha1, kBarContentLength, /*is_tree=*/false}, - ObjectType::Executable, - "repo")); + auto const digest = + ArtifactDigestFactory::Create(HashFunction::Type::GitSHA1, + kBarIdGitSha1, + kBarContentLength, + /*is_tree=*/false); + REQUIRE(digest); + CHECK(*bar == ArtifactDescription::CreateKnown( + *digest, ObjectType::Executable, "repo")); } CHECK_FALSE(root->ToArtifactDescription("baz", "repo")); |