buffet: Add command visibility option to command schema
Add the ability to specify command visibility by using 'visibility'
property in command definition JSON file.
This CL adds parsing code for command visibility as well as the storage
of the property inside CommandDefinition class. The actual usage of this
property will be implemented in subsequent CLs.
BUG=brillo:797
TEST=`FEATURES=test emerge-link buffet`
Change-Id: I3f9b49f8b57f6b63e7c4ae73d042e414c9b6b801
Reviewed-on: https://chromium-review.googlesource.com/266208
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/commands/command_dictionary_unittest.cc b/buffet/commands/command_dictionary_unittest.cc
index 0519d88..143b53e 100644
--- a/buffet/commands/command_dictionary_unittest.cc
+++ b/buffet/commands/command_dictionary_unittest.cc
@@ -251,3 +251,167 @@
"'robot':{'_jump':{'parameters':{'_height':{'type':'integer'}}}}}",
buffet::unittests::ValueToString(json.get()));
}
+
+TEST(CommandDictionary, LoadCommandsWithVisibility) {
+ buffet::CommandDictionary dict;
+ auto json = CreateDictionaryValue(R"({
+ 'base': {
+ 'command1': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':''
+ },
+ 'command2': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'local'
+ },
+ 'command3': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'cloud'
+ },
+ 'command4': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'all'
+ },
+ 'command5': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'cloud,local'
+ }
+ }
+ })");
+ EXPECT_TRUE(dict.LoadCommands(*json, "testd", nullptr, nullptr));
+ auto cmd = dict.FindCommand("base.command1");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("none", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command2");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("local", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command3");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("cloud", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command4");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("all", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command5");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("all", cmd->GetVisibility().ToString());
+}
+
+TEST(CommandDictionary, LoadCommandsWithVisibility_Inheritance) {
+ buffet::CommandDictionary base_dict;
+ auto json = CreateDictionaryValue(R"({
+ 'base': {
+ 'command1': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':''
+ },
+ 'command2': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'local'
+ },
+ 'command3': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'cloud'
+ },
+ 'command4': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'all'
+ },
+ 'command5': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'local,cloud'
+ }
+ }
+ })");
+ EXPECT_TRUE(base_dict.LoadCommands(*json, "testd", nullptr, nullptr));
+
+ buffet::CommandDictionary dict;
+ json = CreateDictionaryValue(R"({
+ 'base': {
+ 'command1': {
+ 'parameters': {},
+ 'results': {}
+ },
+ 'command2': {
+ 'parameters': {},
+ 'results': {}
+ },
+ 'command3': {
+ 'parameters': {},
+ 'results': {}
+ },
+ 'command4': {
+ 'parameters': {},
+ 'results': {}
+ },
+ 'command5': {
+ 'parameters': {},
+ 'results': {}
+ },
+ '_command6': {
+ 'parameters': {},
+ 'results': {}
+ }
+ }
+ })");
+ EXPECT_TRUE(dict.LoadCommands(*json, "testd", &base_dict, nullptr));
+
+ auto cmd = dict.FindCommand("base.command1");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("none", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command2");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("local", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command3");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("cloud", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command4");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("all", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base.command5");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("all", cmd->GetVisibility().ToString());
+
+ cmd = dict.FindCommand("base._command6");
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_EQ("all", cmd->GetVisibility().ToString());
+}
+
+TEST(CommandDictionary, LoadCommandsWithVisibility_Failures) {
+ buffet::CommandDictionary dict;
+ chromeos::ErrorPtr error;
+
+ auto json = CreateDictionaryValue(R"({
+ 'base': {
+ 'jump': {
+ 'parameters': {},
+ 'results': {},
+ 'visibility':'foo'
+ }
+ }
+ })");
+ EXPECT_FALSE(dict.LoadCommands(*json, "testd", nullptr, &error));
+ EXPECT_EQ("invalid_command_visibility", error->GetCode());
+ EXPECT_EQ("Error parsing command 'base.jump'", error->GetMessage());
+ EXPECT_EQ("invalid_parameter_value", error->GetInnerError()->GetCode());
+ EXPECT_EQ("Invalid command visibility value 'foo'",
+ error->GetInnerError()->GetMessage());
+ error.reset();
+}