已合并
[fix]add schema_version #5198
sjtulxh创建于 18 天前
[fix]add schema_version #5198
已合并
共 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 | 19 | ||
| 20 | 20 | ||
| 21 | + | ||
| 21 | 22 | ||
| 22 | 23 | ||
| 23 | namespace ascendc { | 24 | namespace ascendc { |
| @@ -26,6 +27,7 @@ namespace { | |||
| 26 | 27 | ||
| 27 | constexpr size_t TEST_MAX_DIRECTORY_DEPTH = 64U; | 28 | constexpr size_t TEST_MAX_DIRECTORY_DEPTH = 64U; |
| 28 | 29 | ||
| 30 | +using Json = nlohmann::json; | ||
| 29 | using asc_compile_exporter_test::ModuleTest; | 31 | using asc_compile_exporter_test::ModuleTest; |
| 30 | using asc_compile_exporter_test::ReadTestFile; | 32 | using asc_compile_exporter_test::ReadTestFile; |
| 31 | using asc_compile_exporter_test::WriteTestFile; | 33 | using 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 | + | ||
| 58 | TEST_F(ModuleTest, RepositoryReportsManifestJsonAndResourcePathErrors) | 90 | TEST_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 | ||