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" |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 5 | #include "aos/testing/flatbuffer_eq.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 6 | #include "aos/testing/path.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 7 | #include "aos/testing/test_logging.h" |
| 8 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "flatbuffers/reflection.h" |
| 10 | #include "glog/logging.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 11 | #include "gmock/gmock.h" |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | namespace configuration { |
| 16 | namespace testing { |
| 17 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 18 | using aos::testing::ArtifactPath; |
Austin Schuh | 6660213 | 2020-02-28 13:38:37 -0800 | [diff] [blame] | 19 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 20 | class ConfigurationTest : public ::testing::Test { |
| 21 | public: |
| 22 | ConfigurationTest() { ::aos::testing::EnableTestLogging(); } |
| 23 | }; |
| 24 | |
| 25 | typedef ConfigurationTest ConfigurationDeathTest; |
| 26 | |
| 27 | // *the* expected location for all working tests. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 28 | aos::FlatbufferDetachedBuffer<Channel> ExpectedLocation() { |
| 29 | return JsonToFlatbuffer<Channel>( |
| 30 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }"); |
| 31 | } |
| 32 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 33 | // And for multinode setups |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 34 | aos::FlatbufferDetachedBuffer<Channel> ExpectedMultinodeLocation() { |
| 35 | return JsonToFlatbuffer<Channel>( |
| 36 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5, " |
| 37 | "\"source_node\": \"pi1\" }"); |
| 38 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 39 | |
| 40 | // Tests that we can read and merge a configuration. |
| 41 | TEST_F(ConfigurationTest, ConfigMerge) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 42 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 43 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 44 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 45 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 46 | EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie( |
| 47 | ArtifactPath("aos/testdata/expected.json")), |
| 48 | "\n"), |
| 49 | FlatbufferToJson(config, {.multi_line = true})); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 52 | // Tests that we can get back a ChannelIndex. |
| 53 | TEST_F(ConfigurationTest, ChannelIndex) { |
| 54 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 55 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 56 | |
| 57 | EXPECT_EQ( |
| 58 | ChannelIndex(&config.message(), config.message().channels()->Get(1u)), |
| 59 | 1u); |
| 60 | } |
| 61 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 62 | // Tests that we can read and merge a multinode configuration. |
| 63 | TEST_F(ConfigurationTest, ConfigMergeMultinode) { |
| 64 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 65 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 66 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 67 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 68 | EXPECT_EQ(std::string(absl::StripSuffix( |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 69 | util::ReadFileToStringOrDie( |
| 70 | ArtifactPath("aos/testdata/expected_multinode.json")), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 71 | "\n")), |
| 72 | FlatbufferToJson(config, {.multi_line = true})); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | // Tests that we sort the entries in a config so we can look entries up. |
| 76 | TEST_F(ConfigurationTest, UnsortedConfig) { |
| 77 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 78 | ReadConfig(ArtifactPath("aos/testdata/backwards.json")); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 80 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 82 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/aos/robot_state", |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 83 | "aos.RobotState", "app1", nullptr)), |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 84 | "{ \"name\": \"/aos/robot_state\", \"type\": \"aos.RobotState\", " |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | "\"max_size\": 5 }"); |
| 86 | } |
| 87 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 88 | // Tests that we die when a file is imported twice. |
| 89 | TEST_F(ConfigurationDeathTest, DuplicateFile) { |
| 90 | EXPECT_DEATH( |
| 91 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 92 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 93 | ReadConfig(ArtifactPath("aos/testdata/config1_bad.json")); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 94 | }, |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 95 | "aos/testdata/config1_bad.json"); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 98 | // Tests that we reject invalid channel names. This means any channels with // |
| 99 | // in their name, a trailing /, or regex characters. |
| 100 | TEST_F(ConfigurationDeathTest, InvalidChannelName) { |
| 101 | EXPECT_DEATH( |
| 102 | { |
| 103 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 104 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name1.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 105 | }, |
| 106 | "Channel names can't end with '/'"); |
| 107 | EXPECT_DEATH( |
| 108 | { |
| 109 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 110 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name2.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 111 | }, |
| 112 | "Invalid channel name"); |
| 113 | EXPECT_DEATH( |
| 114 | { |
| 115 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 116 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name3.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 117 | LOG(FATAL) << "Foo"; |
| 118 | }, |
| 119 | "Invalid channel name"); |
| 120 | } |
| 121 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 122 | // Tests that we can modify a config with a json snippet. |
| 123 | TEST_F(ConfigurationTest, MergeWithConfig) { |
| 124 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 125 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 126 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 127 | |
| 128 | FlatbufferDetachedBuffer<Configuration> updated_config = |
| 129 | MergeWithConfig(&config.message(), |
| 130 | R"channel({ |
| 131 | "channels": [ |
| 132 | { |
| 133 | "name": "/foo", |
| 134 | "type": ".aos.bar", |
| 135 | "max_size": 100 |
| 136 | } |
| 137 | ] |
| 138 | })channel"); |
| 139 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 140 | EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie(ArtifactPath( |
| 141 | "aos/testdata/expected_merge_with.json")), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 142 | "\n"), |
| 143 | FlatbufferToJson(updated_config, {.multi_line = true})); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 146 | // Tests that we can lookup a location, complete with maps, from a merged |
| 147 | // config. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 148 | TEST_F(ConfigurationTest, GetChannel) { |
| 149 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 150 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 151 | |
| 152 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 153 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", nullptr), |
| 154 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 155 | |
| 156 | // Test that an invalid name results in nullptr back. |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 157 | EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1", nullptr), |
| 158 | nullptr); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 159 | |
| 160 | // Tests that a root map/rename works. And that they get processed from the |
| 161 | // bottom up. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 162 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", nullptr), |
| 163 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 164 | |
| 165 | // And then test that an application specific map/rename works. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 166 | EXPECT_THAT(GetChannel(config, "/bar", ".aos.bar", "app1", nullptr), |
| 167 | aos::testing::FlatbufferEq(ExpectedLocation())); |
| 168 | EXPECT_THAT(GetChannel(config, "/baz", ".aos.bar", "app2", nullptr), |
| 169 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 170 | |
| 171 | // And then test that an invalid application name gets properly ignored. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 172 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app3", nullptr), |
| 173 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Tests that we can lookup a location with node specific maps. |
| 177 | TEST_F(ConfigurationTest, GetChannelMultinode) { |
| 178 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 179 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 180 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 181 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 182 | |
| 183 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 184 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi1), |
| 185 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
| 186 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi2), |
| 187 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 188 | |
| 189 | // Tests that a root map/rename works with a node specific map. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 190 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1), |
| 191 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 192 | |
| 193 | // Tests that a root map/rename fails with a node specific map for the wrong |
| 194 | // node. |
| 195 | EXPECT_EQ(GetChannel(config, "/batman", ".aos.bar", "app1", pi2), nullptr); |
| 196 | |
| 197 | // And then test that an application specific map/rename works. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 198 | EXPECT_THAT(GetChannel(config, "/batman2", ".aos.bar", "app1", pi1), |
| 199 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
| 200 | EXPECT_THAT(GetChannel(config, "/batman3", ".aos.bar", "app1", pi1), |
| 201 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 202 | |
| 203 | // And then that it fails when the node changes. |
| 204 | EXPECT_EQ(GetChannel(config, "/batman3", ".aos.bar", "app1", pi2), nullptr); |
| 205 | } |
| 206 | |
| 207 | // Tests that we can lookup a location with type specific maps. |
| 208 | TEST_F(ConfigurationTest, GetChannelTypedMultinode) { |
| 209 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 210 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 211 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 212 | |
| 213 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 214 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1), |
| 215 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 216 | |
| 217 | // Now confirm that a second message on the same name doesn't get remapped. |
| 218 | const char *kExpectedBazMultinodeLocation = |
| 219 | "{ \"name\": \"/batman\", \"type\": \".aos.baz\", \"max_size\": 5, " |
| 220 | "\"source_node\": \"pi1\" }"; |
| 221 | EXPECT_EQ( |
| 222 | FlatbufferToJson(GetChannel(config, "/batman", ".aos.baz", "app1", pi1)), |
| 223 | kExpectedBazMultinodeLocation); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 226 | // Tests that we can lookup a location with a glob |
| 227 | TEST_F(ConfigurationTest, GetChannelGlob) { |
| 228 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 229 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 230 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 231 | |
| 232 | // Confirm that a glob with nothing after it matches. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 233 | EXPECT_THAT(GetChannel(config, "/magic/string", ".aos.bar", "app7", pi1), |
| 234 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 235 | |
| 236 | // Now confirm that glob with something following it matches and renames |
| 237 | // correctly. |
| 238 | const char *kExpectedSubfolderMultinodeLocation = |
| 239 | "{ \"name\": \"/foo/subfolder\", \"type\": \".aos.bar\", \"max_size\": " |
| 240 | "5, \"source_node\": \"pi1\" }"; |
| 241 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/magic/string/subfolder", |
| 242 | ".aos.bar", "app7", pi1)), |
| 243 | kExpectedSubfolderMultinodeLocation); |
| 244 | } |
| 245 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 246 | // Tests that we reject a configuration which has a nodes list, but has channels |
| 247 | // withoout source_node filled out. |
| 248 | TEST_F(ConfigurationDeathTest, InvalidSourceNode) { |
| 249 | EXPECT_DEATH( |
| 250 | { |
| 251 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 252 | ReadConfig(ArtifactPath("aos/testdata/invalid_nodes.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 253 | }, |
| 254 | "source_node"); |
| 255 | |
| 256 | EXPECT_DEATH( |
| 257 | { |
| 258 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 259 | ReadConfig(ArtifactPath("aos/testdata/invalid_source_node.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 260 | }, |
| 261 | "source_node"); |
| 262 | |
| 263 | EXPECT_DEATH( |
| 264 | { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 265 | FlatbufferDetachedBuffer<Configuration> config = ReadConfig( |
| 266 | ArtifactPath("aos/testdata/invalid_destination_node.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 267 | }, |
| 268 | "destination_nodes"); |
| 269 | |
| 270 | EXPECT_DEATH( |
| 271 | { |
| 272 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 273 | ReadConfig(ArtifactPath("aos/testdata/self_forward.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 274 | }, |
| 275 | "forwarding data to itself"); |
| 276 | } |
| 277 | |
| 278 | // Tests that our node writeable helpers work as intended. |
| 279 | TEST_F(ConfigurationTest, ChannelIsSendableOnNode) { |
| 280 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 281 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 282 | "name": "/test", |
| 283 | "type": "aos.examples.Ping", |
| 284 | "source_node": "foo" |
| 285 | })channel", |
| 286 | Channel::MiniReflectTypeTable())); |
| 287 | |
| 288 | FlatbufferDetachedBuffer<Channel> bad_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 289 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 290 | "name": "/test", |
| 291 | "type": "aos.examples.Ping", |
| 292 | "source_node": "bar" |
| 293 | })channel", |
| 294 | Channel::MiniReflectTypeTable())); |
| 295 | |
| 296 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 297 | R"node({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 298 | "name": "foo" |
| 299 | })node", |
| 300 | Node::MiniReflectTypeTable())); |
| 301 | |
| 302 | EXPECT_TRUE( |
| 303 | ChannelIsSendableOnNode(&good_channel.message(), &node.message())); |
| 304 | EXPECT_FALSE( |
| 305 | ChannelIsSendableOnNode(&bad_channel.message(), &node.message())); |
| 306 | } |
| 307 | |
| 308 | // Tests that our node readable and writeable helpers work as intended. |
| 309 | TEST_F(ConfigurationTest, ChannelIsReadableOnNode) { |
| 310 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 311 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 312 | "name": "/test", |
| 313 | "type": "aos.examples.Ping", |
| 314 | "source_node": "bar", |
| 315 | "destination_nodes": [ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 316 | { |
| 317 | "name": "baz" |
| 318 | }, |
| 319 | { |
| 320 | "name": "foo" |
| 321 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 322 | ] |
| 323 | })channel", |
| 324 | Channel::MiniReflectTypeTable())); |
| 325 | |
| 326 | FlatbufferDetachedBuffer<Channel> bad_channel1(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 327 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 328 | "name": "/test", |
| 329 | "type": "aos.examples.Ping", |
| 330 | "source_node": "bar" |
| 331 | })channel", |
| 332 | Channel::MiniReflectTypeTable())); |
| 333 | |
| 334 | FlatbufferDetachedBuffer<Channel> bad_channel2(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 335 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 336 | "name": "/test", |
| 337 | "type": "aos.examples.Ping", |
| 338 | "source_node": "bar", |
| 339 | "destination_nodes": [ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 340 | { |
| 341 | "name": "baz" |
| 342 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 343 | ] |
| 344 | })channel", |
| 345 | Channel::MiniReflectTypeTable())); |
| 346 | |
| 347 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 348 | R"node({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 349 | "name": "foo" |
| 350 | })node", |
| 351 | Node::MiniReflectTypeTable())); |
| 352 | |
| 353 | EXPECT_TRUE( |
| 354 | ChannelIsReadableOnNode(&good_channel.message(), &node.message())); |
| 355 | EXPECT_FALSE( |
| 356 | ChannelIsReadableOnNode(&bad_channel1.message(), &node.message())); |
| 357 | EXPECT_FALSE( |
| 358 | ChannelIsReadableOnNode(&bad_channel2.message(), &node.message())); |
| 359 | } |
| 360 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 361 | // Tests that our node message is logged helpers work as intended. |
| 362 | TEST_F(ConfigurationTest, ChannelMessageIsLoggedOnNode) { |
| 363 | FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer( |
| 364 | R"channel({ |
| 365 | "name": "/test", |
| 366 | "type": "aos.examples.Ping", |
| 367 | "source_node": "bar", |
| 368 | "destination_nodes": [ |
| 369 | { |
| 370 | "name": "baz" |
| 371 | } |
| 372 | ] |
| 373 | })channel", |
| 374 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 375 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 376 | FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer( |
| 377 | R"channel({ |
| 378 | "name": "/test", |
| 379 | "type": "aos.examples.Ping", |
| 380 | "source_node": "bar", |
| 381 | "logger": "NOT_LOGGED", |
| 382 | "destination_nodes": [ |
| 383 | { |
| 384 | "name": "baz", |
| 385 | "timestamp_logger": "LOCAL_LOGGER" |
| 386 | } |
| 387 | ] |
| 388 | })channel", |
| 389 | Channel::MiniReflectTypeTable())); |
| 390 | |
| 391 | FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer( |
| 392 | R"channel({ |
| 393 | "name": "/test", |
| 394 | "type": "aos.examples.Ping", |
| 395 | "source_node": "bar", |
| 396 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 397 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 398 | "destination_nodes": [ |
| 399 | { |
| 400 | "name": "baz" |
| 401 | } |
| 402 | ] |
| 403 | })channel", |
| 404 | Channel::MiniReflectTypeTable())); |
| 405 | |
| 406 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 407 | JsonToFlatbuffer( |
| 408 | R"channel({ |
| 409 | "name": "/test", |
| 410 | "type": "aos.examples.Ping", |
| 411 | "source_node": "bar", |
| 412 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 413 | "logger_nodes": ["foo"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 414 | "destination_nodes": [ |
| 415 | { |
| 416 | "name": "baz" |
| 417 | } |
| 418 | ] |
| 419 | })channel", |
| 420 | Channel::MiniReflectTypeTable())); |
| 421 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 422 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 423 | R"channel({ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 424 | "name": "/test", |
| 425 | "type": "aos.examples.Ping", |
| 426 | "source_node": "bar", |
| 427 | "logger": "LOCAL_AND_REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 428 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 429 | "destination_nodes": [ |
| 430 | { |
| 431 | "name": "baz" |
| 432 | } |
| 433 | ] |
| 434 | })channel", |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 435 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 436 | |
| 437 | FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer( |
| 438 | R"node({ |
| 439 | "name": "foo" |
| 440 | })node", |
| 441 | Node::MiniReflectTypeTable())); |
| 442 | |
| 443 | FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer( |
| 444 | R"node({ |
| 445 | "name": "bar" |
| 446 | })node", |
| 447 | Node::MiniReflectTypeTable())); |
| 448 | |
| 449 | FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer( |
| 450 | R"node({ |
| 451 | "name": "baz" |
| 452 | })node", |
| 453 | Node::MiniReflectTypeTable())); |
| 454 | |
| 455 | // Local logger. |
| 456 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 457 | &foo_node.message())); |
| 458 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 459 | &bar_node.message())); |
| 460 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 461 | &baz_node.message())); |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame^] | 462 | EXPECT_TRUE( |
| 463 | ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), nullptr)); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 464 | |
| 465 | // No logger. |
| 466 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
| 467 | &foo_node.message())); |
| 468 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 469 | &bar_node.message())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 470 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
| 471 | &baz_node.message())); |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame^] | 472 | EXPECT_FALSE( |
| 473 | ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), nullptr)); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 474 | |
| 475 | // Remote logger. |
| 476 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 477 | &foo_node.message())); |
| 478 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 479 | &bar_node.message())); |
| 480 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 481 | &baz_node.message())); |
| 482 | |
| 483 | // Separate logger. |
| 484 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode( |
| 485 | &logged_on_separate_logger_node_channel.message(), &foo_node.message())); |
| 486 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode( |
| 487 | &logged_on_separate_logger_node_channel.message(), &bar_node.message())); |
| 488 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode( |
| 489 | &logged_on_separate_logger_node_channel.message(), &baz_node.message())); |
| 490 | |
| 491 | // Logged in multiple places. |
| 492 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 493 | &foo_node.message())); |
| 494 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 495 | &bar_node.message())); |
| 496 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 497 | &baz_node.message())); |
| 498 | } |
| 499 | |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame^] | 500 | // Tests that our node message is logged helpers work as intended. |
| 501 | TEST_F(ConfigurationDeathTest, ChannelMessageIsLoggedOnNode) { |
| 502 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 503 | R"channel({ |
| 504 | "name": "/test", |
| 505 | "type": "aos.examples.Ping", |
| 506 | "source_node": "bar", |
| 507 | "logger": "LOCAL_AND_REMOTE_LOGGER", |
| 508 | "logger_nodes": ["baz"], |
| 509 | "destination_nodes": [ |
| 510 | { |
| 511 | "name": "baz" |
| 512 | } |
| 513 | ] |
| 514 | })channel", |
| 515 | Channel::MiniReflectTypeTable())); |
| 516 | |
| 517 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 518 | JsonToFlatbuffer( |
| 519 | R"channel({ |
| 520 | "name": "/test", |
| 521 | "type": "aos.examples.Ping", |
| 522 | "source_node": "bar", |
| 523 | "logger": "REMOTE_LOGGER", |
| 524 | "logger_nodes": ["foo"], |
| 525 | "destination_nodes": [ |
| 526 | { |
| 527 | "name": "baz" |
| 528 | } |
| 529 | ] |
| 530 | })channel", |
| 531 | Channel::MiniReflectTypeTable())); |
| 532 | |
| 533 | EXPECT_DEATH( |
| 534 | { |
| 535 | ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 536 | nullptr); |
| 537 | }, |
| 538 | "Unsupported logging configuration in a single node world"); |
| 539 | EXPECT_DEATH( |
| 540 | { |
| 541 | ChannelMessageIsLoggedOnNode( |
| 542 | &logged_on_separate_logger_node_channel.message(), nullptr); |
| 543 | }, |
| 544 | "Unsupported logging configuration in a single node world"); |
| 545 | } |
| 546 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 547 | // Tests that our forwarding timestamps are logged helpers work as intended. |
| 548 | TEST_F(ConfigurationTest, ConnectionDeliveryTimeIsLoggedOnNode) { |
| 549 | FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer( |
| 550 | R"channel({ |
| 551 | "name": "/test", |
| 552 | "type": "aos.examples.Ping", |
| 553 | "source_node": "bar", |
| 554 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 555 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 556 | "destination_nodes": [ |
| 557 | { |
| 558 | "name": "baz" |
| 559 | } |
| 560 | ] |
| 561 | })channel", |
| 562 | Channel::MiniReflectTypeTable())); |
| 563 | |
| 564 | FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer( |
| 565 | R"channel({ |
| 566 | "name": "/test", |
| 567 | "type": "aos.examples.Ping", |
| 568 | "source_node": "bar", |
| 569 | "logger": "NOT_LOGGED", |
| 570 | "destination_nodes": [ |
| 571 | { |
| 572 | "name": "baz", |
| 573 | "timestamp_logger": "NOT_LOGGED" |
| 574 | } |
| 575 | ] |
| 576 | })channel", |
| 577 | Channel::MiniReflectTypeTable())); |
| 578 | |
| 579 | FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer( |
| 580 | R"channel({ |
| 581 | "name": "/test", |
| 582 | "type": "aos.examples.Ping", |
| 583 | "source_node": "bar", |
| 584 | "destination_nodes": [ |
| 585 | { |
| 586 | "name": "baz", |
| 587 | "timestamp_logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 588 | "timestamp_logger_nodes": ["bar"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 589 | } |
| 590 | ] |
| 591 | })channel", |
| 592 | Channel::MiniReflectTypeTable())); |
| 593 | |
| 594 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 595 | JsonToFlatbuffer( |
| 596 | R"channel({ |
| 597 | "name": "/test", |
| 598 | "type": "aos.examples.Ping", |
| 599 | "source_node": "bar", |
| 600 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 601 | "logger_nodes": ["foo"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 602 | "destination_nodes": [ |
| 603 | { |
| 604 | "name": "baz", |
| 605 | "timestamp_logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 606 | "timestamp_logger_nodes": ["foo"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 607 | } |
| 608 | ] |
| 609 | })channel", |
| 610 | Channel::MiniReflectTypeTable())); |
| 611 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 612 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 613 | R"channel({ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 614 | "name": "/test", |
| 615 | "type": "aos.examples.Ping", |
| 616 | "source_node": "bar", |
| 617 | "destination_nodes": [ |
| 618 | { |
| 619 | "name": "baz", |
| 620 | "timestamp_logger": "LOCAL_AND_REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 621 | "timestamp_logger_nodes": ["bar"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 622 | } |
| 623 | ] |
| 624 | })channel", |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 625 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 626 | |
| 627 | FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer( |
| 628 | R"node({ |
| 629 | "name": "foo" |
| 630 | })node", |
| 631 | Node::MiniReflectTypeTable())); |
| 632 | |
| 633 | FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer( |
| 634 | R"node({ |
| 635 | "name": "bar" |
| 636 | })node", |
| 637 | Node::MiniReflectTypeTable())); |
| 638 | |
| 639 | FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer( |
| 640 | R"node({ |
| 641 | "name": "baz" |
| 642 | })node", |
| 643 | Node::MiniReflectTypeTable())); |
| 644 | |
| 645 | // Local logger. |
| 646 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 647 | &logged_on_self_channel.message(), &baz_node.message(), |
| 648 | &foo_node.message())); |
| 649 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 650 | &logged_on_self_channel.message(), &baz_node.message(), |
| 651 | &bar_node.message())); |
| 652 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 653 | &logged_on_self_channel.message(), &baz_node.message(), |
| 654 | &baz_node.message())); |
| 655 | |
| 656 | // No logger means. |
| 657 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 658 | ¬_logged_channel.message(), &baz_node.message(), &foo_node.message())); |
| 659 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 660 | ¬_logged_channel.message(), &baz_node.message(), &bar_node.message())); |
| 661 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 662 | ¬_logged_channel.message(), &baz_node.message(), &baz_node.message())); |
| 663 | |
| 664 | // Remote logger. |
| 665 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 666 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 667 | &foo_node.message())); |
| 668 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 669 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 670 | &bar_node.message())); |
| 671 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 672 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 673 | &baz_node.message())); |
| 674 | |
| 675 | // Separate logger. |
| 676 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 677 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 678 | &foo_node.message())); |
| 679 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 680 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 681 | &bar_node.message())); |
| 682 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 683 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 684 | &baz_node.message())); |
| 685 | |
| 686 | // Logged on both the node and a remote node. |
| 687 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 688 | &logged_on_both_channel.message(), &baz_node.message(), |
| 689 | &foo_node.message())); |
| 690 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 691 | &logged_on_both_channel.message(), &baz_node.message(), |
| 692 | &bar_node.message())); |
| 693 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 694 | &logged_on_both_channel.message(), &baz_node.message(), |
| 695 | &baz_node.message())); |
| 696 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 697 | |
| 698 | // Tests that we can deduce source nodes from a multinode config. |
| 699 | TEST_F(ConfigurationTest, SourceNodeNames) { |
| 700 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 701 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 702 | |
| 703 | // This is a bit simplistic in that it doesn't test deduplication, but it does |
| 704 | // exercise a lot of the logic. |
| 705 | EXPECT_THAT( |
| 706 | SourceNodeNames(&config.message(), config.message().nodes()->Get(0)), |
| 707 | ::testing::ElementsAreArray({"pi2"})); |
| 708 | EXPECT_THAT( |
| 709 | SourceNodeNames(&config.message(), config.message().nodes()->Get(1)), |
| 710 | ::testing::ElementsAreArray({"pi1"})); |
| 711 | } |
| 712 | |
| 713 | // Tests that we can deduce destination nodes from a multinode config. |
| 714 | TEST_F(ConfigurationTest, DestinationNodeNames) { |
| 715 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 716 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 717 | |
| 718 | // This is a bit simplistic in that it doesn't test deduplication, but it does |
| 719 | // exercise a lot of the logic. |
| 720 | EXPECT_THAT( |
| 721 | DestinationNodeNames(&config.message(), config.message().nodes()->Get(0)), |
| 722 | ::testing::ElementsAreArray({"pi2"})); |
| 723 | EXPECT_THAT( |
| 724 | DestinationNodeNames(&config.message(), config.message().nodes()->Get(1)), |
| 725 | ::testing::ElementsAreArray({"pi1"})); |
| 726 | } |
| 727 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 728 | // Tests that we can pull out all the nodes. |
| 729 | TEST_F(ConfigurationTest, GetNodes) { |
| 730 | { |
| 731 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 732 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 733 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 734 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 735 | |
| 736 | EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(pi1, pi2)); |
| 737 | } |
| 738 | |
| 739 | { |
| 740 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 741 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 742 | EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(nullptr)); |
| 743 | } |
| 744 | } |
| 745 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 746 | // Tests that we can pull out all the nodes with a tag. |
| 747 | TEST_F(ConfigurationTest, GetNodesWithTag) { |
| 748 | { |
| 749 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 750 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 751 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 752 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 753 | |
| 754 | EXPECT_THAT(GetNodesWithTag(&config.message(), "a"), |
| 755 | ::testing::ElementsAre(pi1)); |
| 756 | EXPECT_THAT(GetNodesWithTag(&config.message(), "b"), |
| 757 | ::testing::ElementsAre(pi2)); |
| 758 | EXPECT_THAT(GetNodesWithTag(&config.message(), "c"), |
| 759 | ::testing::ElementsAre(pi1, pi2)); |
| 760 | } |
| 761 | |
| 762 | { |
| 763 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 764 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 765 | EXPECT_THAT(GetNodesWithTag(&config.message(), "arglfish"), |
| 766 | ::testing::ElementsAre(nullptr)); |
| 767 | } |
| 768 | } |
| 769 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 770 | // Tests that we can extract a node index from a config. |
| 771 | TEST_F(ConfigurationTest, GetNodeIndex) { |
| 772 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 773 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 774 | FlatbufferDetachedBuffer<Configuration> config2 = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 775 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 776 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 777 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 778 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 779 | // Try the normal case. |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 780 | EXPECT_EQ(GetNodeIndex(&config.message(), pi1), 0); |
| 781 | EXPECT_EQ(GetNodeIndex(&config.message(), pi2), 1); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 782 | |
| 783 | // Now try if we have node pointers from a different message. |
| 784 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi1), 0); |
| 785 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi2), 1); |
| 786 | |
| 787 | // And now try string names. |
| 788 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi1->name()->string_view()), 0); |
| 789 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi2->name()->string_view()), 1); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // Tests that GetNodeOrDie handles both single and multi-node worlds and returns |
| 793 | // valid nodes. |
| 794 | TEST_F(ConfigurationDeathTest, GetNodeOrDie) { |
| 795 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 796 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 797 | FlatbufferDetachedBuffer<Configuration> config2 = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 798 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 799 | { |
| 800 | // Simple case, nullptr -> nullptr |
| 801 | FlatbufferDetachedBuffer<Configuration> single_node_config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 802 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 803 | EXPECT_EQ(nullptr, GetNodeOrDie(&single_node_config.message(), nullptr)); |
| 804 | |
| 805 | // Confirm that we die when a node is passed in. |
| 806 | EXPECT_DEATH( |
| 807 | { |
| 808 | GetNodeOrDie(&single_node_config.message(), |
| 809 | config.message().nodes()->Get(0)); |
| 810 | }, |
| 811 | "Provided a node in a single node world."); |
| 812 | } |
| 813 | |
| 814 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 815 | // Now try a lookup using a node from a different instance of the config. |
| 816 | EXPECT_EQ(pi1, |
| 817 | GetNodeOrDie(&config.message(), config2.message().nodes()->Get(0))); |
| 818 | } |
| 819 | |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 820 | TEST_F(ConfigurationTest, GetNodeFromHostname) { |
| 821 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 822 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 823 | EXPECT_EQ("pi1", |
| 824 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi")) |
| 825 | ->name() |
| 826 | ->string_view()); |
| 827 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 828 | GetNodeFromHostname(&config.message(), "raspberrypi2")) |
| 829 | ->name() |
| 830 | ->string_view()); |
| 831 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi3")); |
| 832 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost")); |
| 833 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3")); |
| 834 | } |
| 835 | |
| 836 | TEST_F(ConfigurationTest, GetNodeFromHostnames) { |
| 837 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 838 | ReadConfig(ArtifactPath("aos/testdata/good_multinode_hostnames.json")); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 839 | EXPECT_EQ("pi1", |
| 840 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi")) |
| 841 | ->name() |
| 842 | ->string_view()); |
| 843 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 844 | GetNodeFromHostname(&config.message(), "raspberrypi2")) |
| 845 | ->name() |
| 846 | ->string_view()); |
| 847 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 848 | GetNodeFromHostname(&config.message(), "raspberrypi3")) |
| 849 | ->name() |
| 850 | ->string_view()); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 851 | EXPECT_EQ("pi2", |
| 852 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "other")) |
| 853 | ->name() |
| 854 | ->string_view()); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 855 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi4")); |
| 856 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost")); |
| 857 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3")); |
| 858 | } |
| 859 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 860 | } // namespace testing |
| 861 | } // namespace configuration |
| 862 | } // namespace aos |