已合并
[fix]add schema_version #5198
sjtulxh创建于 18 天前
[fix]add schema_version #5198
已合并
sjtulxh创建于 18 天前
4 个文件变更+42-5
@@ -212,8 +212,7 @@ protected:
212 {212 {
213 WriteFile(213 WriteFile(
214 JoinTestPath(unit, name + "_manifest.json"),214 JoinTestPath(unit, name + "_manifest.json"),
215- "{\"schema_version\":\"1.0\",\"resource_id\":\"" + name +215+ "{\"resource_id\":\"" + name + "\",\"soc_version\":\"test\",\"resource_path\":\"" + resourcePath +
216- "\",\"soc_version\":\"test\",\"resource_path\":\"" + resourcePath +
217 "\",\"kernels\":[{\"kernel_name\":\"" + name +216 "\",\"kernels\":[{\"kernel_name\":\"" + name +
218 "\",\"objects\":[{\"object_name\":\"kernel.o\",\"object_type\":\"basic\",\"commands\":[{\"type\":"217 "\",\"objects\":[{\"object_name\":\"kernel.o\",\"object_type\":\"basic\",\"commands\":[{\"type\":"
219 "\"compile\",\"stage\":0,\"cmd\":[\"${output}/kernel.o\"]}],\"outputs\":[\"${output}/kernel.o\"]}]}]}");218 "\"compile\",\"stage\":0,\"cmd\":[\"${output}/kernel.o\"]}],\"outputs\":[\"${output}/kernel.o\"]}]}]}");
@@ -18,6 +18,7 @@
18 18 
19#include "collected_manifest_repository.h"19#include "collected_manifest_repository.h"
20#include "file_utils.h"20#include "file_utils.h"
21+#include "nlohmann/json.hpp"
21#include "test_support.h"22#include "test_support.h"
22 23 
23namespace ascendc {24namespace ascendc {
@@ -26,6 +27,7 @@ namespace {
26 27 
27constexpr size_t TEST_MAX_DIRECTORY_DEPTH = 64U;28constexpr size_t TEST_MAX_DIRECTORY_DEPTH = 64U;
28 29 
30+using Json = nlohmann::json;
29using asc_compile_exporter_test::ModuleTest;31using asc_compile_exporter_test::ModuleTest;
30using asc_compile_exporter_test::ReadTestFile;32using asc_compile_exporter_test::ReadTestFile;
31using asc_compile_exporter_test::WriteTestFile;33using asc_compile_exporter_test::WriteTestFile;
@@ -55,6 +57,36 @@ TEST_F(ModuleTest, RepositorySortsManifestsAndResourcesAndSkipsEmptyFiles)
55 EXPECT_EQ(std::string(units[1].files[0].data.begin(), units[1].files[0].data.end()), "a");57 EXPECT_EQ(std::string(units[1].files[0].data.begin(), units[1].files[0].data.end()), "a");
56}58}
57 59 
60+TEST_F(ModuleTest, RepositoryAddsSchemaVersionToManifestJson)
61+{
62+ const std::string unit = FileUtils::JoinPath(root_, "collection-schema-version/Unit");
63+ WriteTestFile(FileUtils::JoinPath(unit, "resources/kernel.cpp"), "source");
64+ CreateManifest(unit, "Unit");
65+ 
66+ std::vector<ManifestUnit> units;
67+ ASSERT_TRUE(CollectedManifestRepository(FileUtils::JoinPath(root_, "collection-schema-version")).Load(units));
68+ 
69+ ASSERT_EQ(units.size(), 1U);
70+ const Json manifest = Json::parse(units[0].json);
71+ ASSERT_TRUE(manifest.contains("schema_version"));
72+ EXPECT_EQ(manifest.at("schema_version"), "1.0");
73+}
74+ 
75+TEST_F(ModuleTest, RepositoryRejectsManifestWithSchemaVersion)
76+{
77+ const std::string collection = FileUtils::JoinPath(root_, "collection-existing-schema-version");
78+ const std::string unit = FileUtils::JoinPath(collection, "Unit");
79+ WriteTestFile(FileUtils::JoinPath(unit, "resources/kernel.cpp"), "source");
80+ CreateManifest(unit, "Unit");
81+ const std::string manifestPath = FileUtils::JoinPath(unit, "Unit_manifest.json");
82+ Json manifest = Json::parse(ReadTestFile(manifestPath));
83+ manifest["schema_version"] = "2.0";
84+ WriteTestFile(manifestPath, manifest.dump());
85+ 
86+ std::vector<ManifestUnit> units;
87+ EXPECT_FALSE(CollectedManifestRepository(collection).Load(units));
88+}
89+ 
58TEST_F(ModuleTest, RepositoryReportsManifestJsonAndResourcePathErrors)90TEST_F(ModuleTest, RepositoryReportsManifestJsonAndResourcePathErrors)
59{91{
60 const std::string malformed = FileUtils::JoinPath(root_, "malformed/Unit");92 const std::string malformed = FileUtils::JoinPath(root_, "malformed/Unit");
@@ -118,7 +118,7 @@ protected:
118 {118 {
119 WriteTestFile(119 WriteTestFile(
120 ascendc::FileUtils::JoinPath(unit, name + "_manifest.json"),120 ascendc::FileUtils::JoinPath(unit, name + "_manifest.json"),
121- "{\"schema_version\":\"1.0\",\"resource_id\":\"" + name +121+ "{\"resource_id\":\"" + name +
122 "\",\"soc_version\":\"test\",\"resource_path\":\"resources\",\"kernels\":[{\"kernel_name\":\"" + name +122 "\",\"soc_version\":\"test\",\"resource_path\":\"resources\",\"kernels\":[{\"kernel_name\":\"" + name +
123 "\",\"objects\":[{\"object_name\":\"kernel.o\",\"object_type\":\"basic\",\"commands\":[{\"type\":"123 "\",\"objects\":[{\"object_name\":\"kernel.o\",\"object_type\":\"basic\",\"commands\":[{\"type\":"
124 "\"compile\",\"stage\":0,\"cmd\":[\"${output}/kernel.o\"]}],\"outputs\":[\"${output}/kernel.o\"]}]}]}");124 "\"compile\",\"stage\":0,\"cmd\":[\"${output}/kernel.o\"]}],\"outputs\":[\"${output}/kernel.o\"]}]}]}");
@@ -69,13 +69,17 @@ private:
69 size_t fileCount_{0U};69 size_t fileCount_{0U};
70};70};
71 71 
72-bool ValidateAndExtractResourcePath(const std::string& text, const std::string& manifestPath, std::string& resourcePath)72+bool ValidateAndExtractResourcePath(std::string& text, const std::string& manifestPath, std::string& resourcePath)
73{73{
74- const Json manifest = Json::parse(text, nullptr, false);74+ Json manifest = Json::parse(text, nullptr, false);
75 if (manifest.is_discarded()) {75 if (manifest.is_discarded()) {
76 ASCENDLOGE("Manifest is not valid JSON: %s", manifestPath.c_str());76 ASCENDLOGE("Manifest is not valid JSON: %s", manifestPath.c_str());
77 return false;77 return false;
78 }78 }
79+ if (manifest.contains("schema_version")) {
80+ ASCENDLOGE("Manifest must not contain schema_version: %s", manifestPath.c_str());
81+ return false;
82+ }
79 83 
80 if (!ValidateResourceManifest(manifest, manifestPath)) {84 if (!ValidateResourceManifest(manifest, manifestPath)) {
81 return false;85 return false;
@@ -87,6 +91,8 @@ bool ValidateAndExtractResourcePath(const std::string& text, const std::string&
87 return false;91 return false;
88 }92 }
89 resourcePath = resourcePathValue->get<std::string>();93 resourcePath = resourcePathValue->get<std::string>();
94+ manifest["schema_version"] = "1.0";
95+ text = manifest.dump();
90 return true;96 return true;
91}97}
92 98