Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 1 | #include "aos/configuration.h" |
| 2 | |
| 3 | #include "absl/strings/strip.h" |
| 4 | #include "aos/json_to_flatbuffer.h" |
| 5 | #include "aos/testing/test_logging.h" |
| 6 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "flatbuffers/reflection.h" |
| 8 | #include "glog/logging.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 9 | #include "gtest/gtest.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | namespace configuration { |
| 13 | namespace testing { |
| 14 | |
| 15 | class ConfigurationTest : public ::testing::Test { |
| 16 | public: |
| 17 | ConfigurationTest() { ::aos::testing::EnableTestLogging(); } |
| 18 | }; |
| 19 | |
| 20 | typedef ConfigurationTest ConfigurationDeathTest; |
| 21 | |
| 22 | // *the* expected location for all working tests. |
| 23 | const char *kExpectedLocation = |
| 24 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }"; |
| 25 | |
| 26 | // Tests that we can read and merge a configuration. |
| 27 | TEST_F(ConfigurationTest, ConfigMerge) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 28 | FlatbufferDetachedBuffer<Configuration> config = |
| 29 | ReadConfig("aos/testdata/config1.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | LOG(INFO) << "Read: " << FlatbufferToJson(config, true); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 31 | |
| 32 | EXPECT_EQ( |
| 33 | absl::StripSuffix( |
| 34 | util::ReadFileToStringOrDie("aos/testdata/expected.json"), "\n"), |
| 35 | FlatbufferToJson(config, true)); |
| 36 | } |
| 37 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | // Tests that we sort the entries in a config so we can look entries up. |
| 39 | TEST_F(ConfigurationTest, UnsortedConfig) { |
| 40 | FlatbufferDetachedBuffer<Configuration> config = |
| 41 | ReadConfig("aos/testdata/backwards.json"); |
| 42 | |
| 43 | LOG(INFO) << "Read: " << FlatbufferToJson(config, true); |
| 44 | |
| 45 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, ".aos.robot_state", |
| 46 | "aos.RobotState", "app1")), |
| 47 | "{ \"name\": \".aos.robot_state\", \"type\": \"aos.RobotState\", " |
| 48 | "\"max_size\": 5 }"); |
| 49 | } |
| 50 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 51 | // Tests that we die when a file is imported twice. |
| 52 | TEST_F(ConfigurationDeathTest, DuplicateFile) { |
| 53 | EXPECT_DEATH( |
| 54 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 55 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 56 | ReadConfig("aos/testdata/config1_bad.json"); |
| 57 | }, |
| 58 | "aos/testdata/config1_bad.json"); |
| 59 | } |
| 60 | |
| 61 | // Tests that we can lookup a location, complete with maps, from a merged |
| 62 | // config. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 63 | TEST_F(ConfigurationTest, GetChannel) { |
| 64 | FlatbufferDetachedBuffer<Configuration> config = |
| 65 | ReadConfig("aos/testdata/config1.json"); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 66 | |
| 67 | // Test a basic lookup first. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 68 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 69 | kExpectedLocation); |
| 70 | |
| 71 | // Test that an invalid name results in nullptr back. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 72 | EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1"), nullptr); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 73 | |
| 74 | // Tests that a root map/rename works. And that they get processed from the |
| 75 | // bottom up. |
| 76 | EXPECT_EQ( |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 77 | FlatbufferToJson(GetChannel(config, "/batman", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 78 | kExpectedLocation); |
| 79 | |
| 80 | // And then test that an application specific map/rename works. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 81 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/bar", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 82 | kExpectedLocation); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 83 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/baz", ".aos.bar", "app2")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 84 | kExpectedLocation); |
| 85 | |
| 86 | // And then test that an invalid application name gets properly ignored. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 87 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app3")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 88 | kExpectedLocation); |
| 89 | } |
| 90 | |
| 91 | } // namespace testing |
| 92 | } // namespace configuration |
| 93 | } // namespace aos |