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 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 38 | // Tests that we can read and merge a multinode configuration. |
| 39 | TEST_F(ConfigurationTest, ConfigMergeMultinode) { |
| 40 | FlatbufferDetachedBuffer<Configuration> config = |
| 41 | ReadConfig("aos/testdata/config1_multinode.json"); |
| 42 | LOG(INFO) << "Read: " << FlatbufferToJson(config, true); |
| 43 | |
| 44 | EXPECT_EQ( |
| 45 | std::string(absl::StripSuffix( |
| 46 | util::ReadFileToStringOrDie("aos/testdata/expected_multinode.json"), |
| 47 | "\n")), |
| 48 | FlatbufferToJson(config, true)); |
| 49 | } |
| 50 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | // Tests that we sort the entries in a config so we can look entries up. |
| 52 | TEST_F(ConfigurationTest, UnsortedConfig) { |
| 53 | FlatbufferDetachedBuffer<Configuration> config = |
| 54 | ReadConfig("aos/testdata/backwards.json"); |
| 55 | |
| 56 | LOG(INFO) << "Read: " << FlatbufferToJson(config, true); |
| 57 | |
| 58 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, ".aos.robot_state", |
| 59 | "aos.RobotState", "app1")), |
| 60 | "{ \"name\": \".aos.robot_state\", \"type\": \"aos.RobotState\", " |
| 61 | "\"max_size\": 5 }"); |
| 62 | } |
| 63 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 64 | // Tests that we die when a file is imported twice. |
| 65 | TEST_F(ConfigurationDeathTest, DuplicateFile) { |
| 66 | EXPECT_DEATH( |
| 67 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 68 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 69 | ReadConfig("aos/testdata/config1_bad.json"); |
| 70 | }, |
| 71 | "aos/testdata/config1_bad.json"); |
| 72 | } |
| 73 | |
| 74 | // Tests that we can lookup a location, complete with maps, from a merged |
| 75 | // config. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 76 | TEST_F(ConfigurationTest, GetChannel) { |
| 77 | FlatbufferDetachedBuffer<Configuration> config = |
| 78 | ReadConfig("aos/testdata/config1.json"); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 79 | |
| 80 | // Test a basic lookup first. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 81 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 82 | kExpectedLocation); |
| 83 | |
| 84 | // Test that an invalid name results in nullptr back. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 85 | EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1"), nullptr); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 86 | |
| 87 | // Tests that a root map/rename works. And that they get processed from the |
| 88 | // bottom up. |
| 89 | EXPECT_EQ( |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 90 | FlatbufferToJson(GetChannel(config, "/batman", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 91 | kExpectedLocation); |
| 92 | |
| 93 | // And then test that an application specific map/rename works. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 94 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/bar", ".aos.bar", "app1")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 95 | kExpectedLocation); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 96 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/baz", ".aos.bar", "app2")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 97 | kExpectedLocation); |
| 98 | |
| 99 | // And then test that an invalid application name gets properly ignored. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 100 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app3")), |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 101 | kExpectedLocation); |
| 102 | } |
| 103 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 104 | // Tests that we reject a configuration which has a nodes list, but has channels |
| 105 | // withoout source_node filled out. |
| 106 | TEST_F(ConfigurationDeathTest, InvalidSourceNode) { |
| 107 | EXPECT_DEATH( |
| 108 | { |
| 109 | FlatbufferDetachedBuffer<Configuration> config = |
| 110 | ReadConfig("aos/testdata/invalid_nodes.json"); |
| 111 | }, |
| 112 | "source_node"); |
| 113 | |
| 114 | EXPECT_DEATH( |
| 115 | { |
| 116 | FlatbufferDetachedBuffer<Configuration> config = |
| 117 | ReadConfig("aos/testdata/invalid_source_node.json"); |
| 118 | }, |
| 119 | "source_node"); |
| 120 | |
| 121 | EXPECT_DEATH( |
| 122 | { |
| 123 | FlatbufferDetachedBuffer<Configuration> config = |
| 124 | ReadConfig("aos/testdata/invalid_destination_node.json"); |
| 125 | }, |
| 126 | "destination_nodes"); |
| 127 | |
| 128 | EXPECT_DEATH( |
| 129 | { |
| 130 | FlatbufferDetachedBuffer<Configuration> config = |
| 131 | ReadConfig("aos/testdata/self_forward.json"); |
| 132 | }, |
| 133 | "forwarding data to itself"); |
| 134 | } |
| 135 | |
| 136 | // Tests that our node writeable helpers work as intended. |
| 137 | TEST_F(ConfigurationTest, ChannelIsSendableOnNode) { |
| 138 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
| 139 | R"channel({ |
| 140 | "name": "/test", |
| 141 | "type": "aos.examples.Ping", |
| 142 | "source_node": "foo" |
| 143 | })channel", |
| 144 | Channel::MiniReflectTypeTable())); |
| 145 | |
| 146 | FlatbufferDetachedBuffer<Channel> bad_channel(JsonToFlatbuffer( |
| 147 | R"channel({ |
| 148 | "name": "/test", |
| 149 | "type": "aos.examples.Ping", |
| 150 | "source_node": "bar" |
| 151 | })channel", |
| 152 | Channel::MiniReflectTypeTable())); |
| 153 | |
| 154 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
| 155 | R"node({ |
| 156 | "name": "foo" |
| 157 | })node", |
| 158 | Node::MiniReflectTypeTable())); |
| 159 | |
| 160 | EXPECT_TRUE( |
| 161 | ChannelIsSendableOnNode(&good_channel.message(), &node.message())); |
| 162 | EXPECT_FALSE( |
| 163 | ChannelIsSendableOnNode(&bad_channel.message(), &node.message())); |
| 164 | } |
| 165 | |
| 166 | // Tests that our node readable and writeable helpers work as intended. |
| 167 | TEST_F(ConfigurationTest, ChannelIsReadableOnNode) { |
| 168 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
| 169 | R"channel({ |
| 170 | "name": "/test", |
| 171 | "type": "aos.examples.Ping", |
| 172 | "source_node": "bar", |
| 173 | "destination_nodes": [ |
| 174 | "baz", |
| 175 | "foo", |
| 176 | ] |
| 177 | })channel", |
| 178 | Channel::MiniReflectTypeTable())); |
| 179 | |
| 180 | FlatbufferDetachedBuffer<Channel> bad_channel1(JsonToFlatbuffer( |
| 181 | R"channel({ |
| 182 | "name": "/test", |
| 183 | "type": "aos.examples.Ping", |
| 184 | "source_node": "bar" |
| 185 | })channel", |
| 186 | Channel::MiniReflectTypeTable())); |
| 187 | |
| 188 | FlatbufferDetachedBuffer<Channel> bad_channel2(JsonToFlatbuffer( |
| 189 | R"channel({ |
| 190 | "name": "/test", |
| 191 | "type": "aos.examples.Ping", |
| 192 | "source_node": "bar", |
| 193 | "destination_nodes": [ |
| 194 | "baz" |
| 195 | ] |
| 196 | })channel", |
| 197 | Channel::MiniReflectTypeTable())); |
| 198 | |
| 199 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
| 200 | R"node({ |
| 201 | "name": "foo" |
| 202 | })node", |
| 203 | Node::MiniReflectTypeTable())); |
| 204 | |
| 205 | EXPECT_TRUE( |
| 206 | ChannelIsReadableOnNode(&good_channel.message(), &node.message())); |
| 207 | EXPECT_FALSE( |
| 208 | ChannelIsReadableOnNode(&bad_channel1.message(), &node.message())); |
| 209 | EXPECT_FALSE( |
| 210 | ChannelIsReadableOnNode(&bad_channel2.message(), &node.message())); |
| 211 | } |
| 212 | |
| 213 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 214 | } // namespace testing |
| 215 | } // namespace configuration |
| 216 | } // namespace aos |