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