buffet: Allow tests to pass in a path to search for command definitions
This allows us to dynamically generate commands to test Buffet with at
runtime. The read only nature of the file system makes this difficult
with just the statically configured files.
BUG=brillo:172
TEST=unittests, buffet_Registration passes with this change.
Change-Id: I54a92edadbb5c8a5ebad63b8464d87bb21ba5aa2
Reviewed-on: https://chromium-review.googlesource.com/249440
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/buffet/commands/command_manager.cc b/buffet/commands/command_manager.cc
index 1676cdc..78e0dee 100644
--- a/buffet/commands/command_manager.cc
+++ b/buffet/commands/command_manager.cc
@@ -60,26 +60,32 @@
return LoadCommands(*json, category, error);
}
-void CommandManager::Startup() {
+void CommandManager::Startup(const base::FilePath& definitions_path,
+ const base::FilePath& test_definitions_path) {
LOG(INFO) << "Initializing CommandManager.";
// Load global standard GCD command dictionary.
- base::FilePath base_command_file("/etc/buffet/gcd.json");
+ base::FilePath base_command_file{definitions_path.Append("gcd.json")};
LOG(INFO) << "Loading standard commands from " << base_command_file.value();
CHECK(LoadBaseCommands(base_command_file, nullptr))
<< "Failed to load the standard command definitions.";
- // Load static device command definitions.
- base::FilePath device_command_dir("/etc/buffet/commands");
- base::FileEnumerator enumerator(device_command_dir, false,
- base::FileEnumerator::FILES,
- FILE_PATH_LITERAL("*.json"));
- base::FilePath json_file_path = enumerator.Next();
- while (!json_file_path.empty()) {
- LOG(INFO) << "Loading command schema from " << json_file_path.value();
- CHECK(LoadCommands(json_file_path, nullptr))
- << "Failed to load the command definition file.";
- json_file_path = enumerator.Next();
- }
+ auto LoadPackages = [this](const base::FilePath& root,
+ const base::FilePath::StringType& pattern) {
+ base::FilePath device_command_dir{root.Append("commands")};
+ VLOG(2) << "Looking for commands in " << root.value();
+ base::FileEnumerator enumerator(device_command_dir, false,
+ base::FileEnumerator::FILES,
+ pattern);
+ base::FilePath json_file_path = enumerator.Next();
+ while (!json_file_path.empty()) {
+ LOG(INFO) << "Loading command schema from " << json_file_path.value();
+ CHECK(this->LoadCommands(json_file_path, nullptr))
+ << "Failed to load the command definition file.";
+ json_file_path = enumerator.Next();
+ }
+ };
+ LoadPackages(definitions_path, FILE_PATH_LITERAL("*.json"));
+ LoadPackages(test_definitions_path, FILE_PATH_LITERAL("*test.json"));
}
void CommandManager::AddCommand(