blob: c4df812616612e807fb4e69e22af3c3f31d0d501 [file] [log] [blame]
Austin Schuhcb108412019-10-13 16:09:54 -07001#include "aos/configuration.h"
2
3#include "absl/strings/strip.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07004#include "flatbuffers/reflection.h"
5#include "glog/logging.h"
6#include "gmock/gmock.h"
7#include "gtest/gtest.h"
8
Nathan Leong307c9692022-10-08 15:25:03 -07009#include "aos/events/ping_generated.h"
Austin Schuhcb108412019-10-13 16:09:54 -070010#include "aos/json_to_flatbuffer.h"
Austin Schuh1ef01ef2021-02-07 20:40:36 -080011#include "aos/testing/flatbuffer_eq.h"
Austin Schuh373f1762021-06-02 21:07:09 -070012#include "aos/testing/path.h"
Austin Schuhcb108412019-10-13 16:09:54 -070013#include "aos/testing/test_logging.h"
14#include "aos/util/file.h"
Austin Schuhcb108412019-10-13 16:09:54 -070015
16namespace aos {
17namespace configuration {
18namespace testing {
19
Austin Schuh373f1762021-06-02 21:07:09 -070020using aos::testing::ArtifactPath;
Austin Schuhfb37c612022-08-11 15:24:51 -070021namespace chrono = std::chrono;
Austin Schuh66602132020-02-28 13:38:37 -080022
Austin Schuhcb108412019-10-13 16:09:54 -070023class ConfigurationTest : public ::testing::Test {
24 public:
25 ConfigurationTest() { ::aos::testing::EnableTestLogging(); }
26};
27
28typedef ConfigurationTest ConfigurationDeathTest;
29
30// *the* expected location for all working tests.
Austin Schuh1ef01ef2021-02-07 20:40:36 -080031aos::FlatbufferDetachedBuffer<Channel> ExpectedLocation() {
32 return JsonToFlatbuffer<Channel>(
33 "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }");
34}
35
Austin Schuhbca6cf02019-12-22 17:28:34 -080036// And for multinode setups
Austin Schuh1ef01ef2021-02-07 20:40:36 -080037aos::FlatbufferDetachedBuffer<Channel> ExpectedMultinodeLocation() {
38 return JsonToFlatbuffer<Channel>(
39 "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5, "
40 "\"source_node\": \"pi1\" }");
41}
Austin Schuhcb108412019-10-13 16:09:54 -070042
43// Tests that we can read and merge a configuration.
44TEST_F(ConfigurationTest, ConfigMerge) {
Austin Schuh40485ed2019-10-26 21:51:44 -070045 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -070046 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Ravago Jonescf453ab2020-05-06 21:14:53 -070047 LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true});
Austin Schuhcb108412019-10-13 16:09:54 -070048
Austin Schuh373f1762021-06-02 21:07:09 -070049 EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie(
50 ArtifactPath("aos/testdata/expected.json")),
51 "\n"),
52 FlatbufferToJson(config, {.multi_line = true}));
Austin Schuhcb108412019-10-13 16:09:54 -070053}
54
Austin Schuhc9e10ec2020-01-26 16:08:28 -080055// Tests that we can get back a ChannelIndex.
56TEST_F(ConfigurationTest, ChannelIndex) {
57 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -070058 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -080059
60 EXPECT_EQ(
61 ChannelIndex(&config.message(), config.message().channels()->Get(1u)),
62 1u);
63}
64
Austin Schuh217a9782019-12-21 23:02:50 -080065// Tests that we can read and merge a multinode configuration.
66TEST_F(ConfigurationTest, ConfigMergeMultinode) {
67 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -070068 ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json"));
Ravago Jonescf453ab2020-05-06 21:14:53 -070069 LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true});
Austin Schuh217a9782019-12-21 23:02:50 -080070
Ravago Jonescf453ab2020-05-06 21:14:53 -070071 EXPECT_EQ(std::string(absl::StripSuffix(
Austin Schuh373f1762021-06-02 21:07:09 -070072 util::ReadFileToStringOrDie(
73 ArtifactPath("aos/testdata/expected_multinode.json")),
Ravago Jonescf453ab2020-05-06 21:14:53 -070074 "\n")),
75 FlatbufferToJson(config, {.multi_line = true}));
Austin Schuh217a9782019-12-21 23:02:50 -080076}
77
Alex Perrycb7da4b2019-08-28 19:35:56 -070078// Tests that we sort the entries in a config so we can look entries up.
79TEST_F(ConfigurationTest, UnsortedConfig) {
80 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -070081 ReadConfig(ArtifactPath("aos/testdata/backwards.json"));
Alex Perrycb7da4b2019-08-28 19:35:56 -070082
Ravago Jonescf453ab2020-05-06 21:14:53 -070083 LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070084
Austin Schuhf1fff282020-03-28 16:57:32 -070085 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/aos/robot_state",
Austin Schuhbca6cf02019-12-22 17:28:34 -080086 "aos.RobotState", "app1", nullptr)),
Austin Schuhf1fff282020-03-28 16:57:32 -070087 "{ \"name\": \"/aos/robot_state\", \"type\": \"aos.RobotState\", "
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 "\"max_size\": 5 }");
89}
90
Austin Schuhcb108412019-10-13 16:09:54 -070091// Tests that we die when a file is imported twice.
92TEST_F(ConfigurationDeathTest, DuplicateFile) {
93 EXPECT_DEATH(
94 {
Austin Schuh40485ed2019-10-26 21:51:44 -070095 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -070096 ReadConfig(ArtifactPath("aos/testdata/config1_bad.json"));
Austin Schuhcb108412019-10-13 16:09:54 -070097 },
Austin Schuh373f1762021-06-02 21:07:09 -070098 "aos/testdata/config1_bad.json");
Austin Schuhcb108412019-10-13 16:09:54 -070099}
100
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700101// Tests that we die when we give an invalid path.
102TEST_F(ConfigurationDeathTest, NonexistentFile) {
103 EXPECT_DEATH(
104 {
105 FlatbufferDetachedBuffer<Configuration> config =
106 ReadConfig("nonexistent/config.json");
107 },
108 "above error");
109}
110
111// Tests that we return std::nullopt when we give an invalid path.
112TEST_F(ConfigurationTest, NonexistentFileOptional) {
113 std::optional<FlatbufferDetachedBuffer<Configuration>> config =
114 MaybeReadConfig("nonexistent/config.json");
115 EXPECT_FALSE(config.has_value());
116}
117
Austin Schuhf1fff282020-03-28 16:57:32 -0700118// Tests that we reject invalid channel names. This means any channels with //
119// in their name, a trailing /, or regex characters.
120TEST_F(ConfigurationDeathTest, InvalidChannelName) {
121 EXPECT_DEATH(
122 {
123 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700124 ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name1.json"));
Austin Schuhf1fff282020-03-28 16:57:32 -0700125 },
126 "Channel names can't end with '/'");
127 EXPECT_DEATH(
128 {
129 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700130 ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name2.json"));
Austin Schuhf1fff282020-03-28 16:57:32 -0700131 },
132 "Invalid channel name");
133 EXPECT_DEATH(
134 {
135 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700136 ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name3.json"));
Austin Schuhf1fff282020-03-28 16:57:32 -0700137 LOG(FATAL) << "Foo";
138 },
139 "Invalid channel name");
Austin Schuh47e382e2023-05-28 11:20:56 -0700140 EXPECT_DEATH(
141 {
142 FlatbufferDetachedBuffer<Configuration> config =
143 ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name4.json"));
144 LOG(FATAL) << "Foo";
145 },
146 "Channel names must start with '/'");
Austin Schuhf1fff282020-03-28 16:57:32 -0700147}
148
Austin Schuh8d6cea82020-02-28 12:17:16 -0800149// Tests that we can modify a config with a json snippet.
150TEST_F(ConfigurationTest, MergeWithConfig) {
151 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700152 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Ravago Jonescf453ab2020-05-06 21:14:53 -0700153 LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true});
Austin Schuh8d6cea82020-02-28 12:17:16 -0800154
155 FlatbufferDetachedBuffer<Configuration> updated_config =
156 MergeWithConfig(&config.message(),
157 R"channel({
158 "channels": [
159 {
160 "name": "/foo",
161 "type": ".aos.bar",
162 "max_size": 100
163 }
164 ]
165})channel");
166
Austin Schuh373f1762021-06-02 21:07:09 -0700167 EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie(ArtifactPath(
168 "aos/testdata/expected_merge_with.json")),
Ravago Jonescf453ab2020-05-06 21:14:53 -0700169 "\n"),
170 FlatbufferToJson(updated_config, {.multi_line = true}));
Austin Schuh8d6cea82020-02-28 12:17:16 -0800171}
172
Austin Schuhcb108412019-10-13 16:09:54 -0700173// Tests that we can lookup a location, complete with maps, from a merged
174// config.
Austin Schuh40485ed2019-10-26 21:51:44 -0700175TEST_F(ConfigurationTest, GetChannel) {
176 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700177 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Austin Schuhcb108412019-10-13 16:09:54 -0700178
179 // Test a basic lookup first.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800180 EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", nullptr),
181 aos::testing::FlatbufferEq(ExpectedLocation()));
Austin Schuhcb108412019-10-13 16:09:54 -0700182
183 // Test that an invalid name results in nullptr back.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800184 EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1", nullptr),
185 nullptr);
Austin Schuhcb108412019-10-13 16:09:54 -0700186
187 // Tests that a root map/rename works. And that they get processed from the
188 // bottom up.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800189 EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", nullptr),
190 aos::testing::FlatbufferEq(ExpectedLocation()));
Austin Schuhcb108412019-10-13 16:09:54 -0700191
192 // And then test that an application specific map/rename works.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800193 EXPECT_THAT(GetChannel(config, "/bar", ".aos.bar", "app1", nullptr),
194 aos::testing::FlatbufferEq(ExpectedLocation()));
195 EXPECT_THAT(GetChannel(config, "/baz", ".aos.bar", "app2", nullptr),
196 aos::testing::FlatbufferEq(ExpectedLocation()));
Austin Schuhcb108412019-10-13 16:09:54 -0700197
198 // And then test that an invalid application name gets properly ignored.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800199 EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app3", nullptr),
200 aos::testing::FlatbufferEq(ExpectedLocation()));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800201}
202
James Kuszmaulc8503f32022-06-25 16:17:12 -0700203// Tests that we can do reverse-lookups of channel names.
204TEST_F(ConfigurationTest, GetChannelAliases) {
205 FlatbufferDetachedBuffer<Configuration> config =
206 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
207
208 // Test a basic lookup first.
209 EXPECT_THAT(
210 GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app1", nullptr),
211 ::testing::UnorderedElementsAre("/foo", "/batman", "/bar"));
212 EXPECT_THAT(
213 GetChannelAliases(&config.message(), "/bar", ".aos.bar", "app1", nullptr),
214 ::testing::UnorderedElementsAre("/batman", "/bar"));
215 EXPECT_THAT(GetChannelAliases(&config.message(), "/batman", ".aos.bar",
216 "app1", nullptr),
217 ::testing::UnorderedElementsAre("/batman"));
218 // /bar (deliberately) does not get included because of the ordering in the
219 // map.
220 EXPECT_THAT(
221 GetChannelAliases(&config.message(), "/foo", ".aos.bar", "", nullptr),
222 ::testing::UnorderedElementsAre("/foo", "/batman"));
223 EXPECT_THAT(
224 GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app2", nullptr),
225 ::testing::UnorderedElementsAre("/foo", "/batman", "/baz"));
226}
227
Austin Schuhbca6cf02019-12-22 17:28:34 -0800228// Tests that we can lookup a location with node specific maps.
229TEST_F(ConfigurationTest, GetChannelMultinode) {
230 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700231 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800232 const Node *pi1 = GetNode(&config.message(), "pi1");
233 const Node *pi2 = GetNode(&config.message(), "pi2");
234
235 // Test a basic lookup first.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800236 EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi1),
237 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
238 EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi2),
239 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800240
241 // Tests that a root map/rename works with a node specific map.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800242 EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1),
243 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800244
245 // Tests that a root map/rename fails with a node specific map for the wrong
246 // node.
247 EXPECT_EQ(GetChannel(config, "/batman", ".aos.bar", "app1", pi2), nullptr);
248
249 // And then test that an application specific map/rename works.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800250 EXPECT_THAT(GetChannel(config, "/batman2", ".aos.bar", "app1", pi1),
251 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
252 EXPECT_THAT(GetChannel(config, "/batman3", ".aos.bar", "app1", pi1),
253 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800254
255 // And then that it fails when the node changes.
256 EXPECT_EQ(GetChannel(config, "/batman3", ".aos.bar", "app1", pi2), nullptr);
257}
258
James Kuszmaulc8503f32022-06-25 16:17:12 -0700259// Tests that reverse channel lookup on a multi-node config (including with
260// wildcards) works.
261TEST_F(ConfigurationTest, GetChannelAliasesMultinode) {
262 FlatbufferDetachedBuffer<Configuration> config =
263 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
264
265 const Node *pi1 = GetNode(&config.message(), "pi1");
266 const Node *pi2 = GetNode(&config.message(), "pi2");
267
268 EXPECT_THAT(
269 GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app1", pi1),
270 ::testing::UnorderedElementsAre("/foo", "/batman", "/batman2", "/batman3",
271 "/magic/string"));
272
273 EXPECT_THAT(
274 GetChannelAliases(&config.message(), "/foo", ".aos.baz", "app1", pi1),
275 ::testing::UnorderedElementsAre("/foo", "/batman3", "/magic/string"));
276
277 EXPECT_THAT(
278 GetChannelAliases(&config.message(), "/foo/testing", ".aos.bar", "", pi1),
279 ::testing::UnorderedElementsAre("/foo/testing", "/magic/string/testing"));
280
281 EXPECT_THAT(
282 GetChannelAliases(&config.message(), "/foo/testing", ".aos.bar", "app1",
283 pi2),
284 ::testing::UnorderedElementsAre("/foo/testing", "/magic/string/testing"));
285}
286
Austin Schuhbca6cf02019-12-22 17:28:34 -0800287// Tests that we can lookup a location with type specific maps.
288TEST_F(ConfigurationTest, GetChannelTypedMultinode) {
289 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700290 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800291 const Node *pi1 = GetNode(&config.message(), "pi1");
292
293 // Test a basic lookup first.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800294 EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1),
295 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
Austin Schuhbca6cf02019-12-22 17:28:34 -0800296
297 // Now confirm that a second message on the same name doesn't get remapped.
298 const char *kExpectedBazMultinodeLocation =
299 "{ \"name\": \"/batman\", \"type\": \".aos.baz\", \"max_size\": 5, "
300 "\"source_node\": \"pi1\" }";
301 EXPECT_EQ(
302 FlatbufferToJson(GetChannel(config, "/batman", ".aos.baz", "app1", pi1)),
303 kExpectedBazMultinodeLocation);
Austin Schuhcb108412019-10-13 16:09:54 -0700304}
305
Austin Schuhf1fff282020-03-28 16:57:32 -0700306// Tests that we can lookup a location with a glob
307TEST_F(ConfigurationTest, GetChannelGlob) {
308 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700309 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhf1fff282020-03-28 16:57:32 -0700310 const Node *pi1 = GetNode(&config.message(), "pi1");
311
312 // Confirm that a glob with nothing after it matches.
Austin Schuh1ef01ef2021-02-07 20:40:36 -0800313 EXPECT_THAT(GetChannel(config, "/magic/string", ".aos.bar", "app7", pi1),
314 aos::testing::FlatbufferEq(ExpectedMultinodeLocation()));
Austin Schuhf1fff282020-03-28 16:57:32 -0700315
316 // Now confirm that glob with something following it matches and renames
317 // correctly.
318 const char *kExpectedSubfolderMultinodeLocation =
319 "{ \"name\": \"/foo/subfolder\", \"type\": \".aos.bar\", \"max_size\": "
320 "5, \"source_node\": \"pi1\" }";
321 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/magic/string/subfolder",
322 ".aos.bar", "app7", pi1)),
323 kExpectedSubfolderMultinodeLocation);
324}
325
Austin Schuh217a9782019-12-21 23:02:50 -0800326// Tests that we reject a configuration which has a nodes list, but has channels
327// withoout source_node filled out.
328TEST_F(ConfigurationDeathTest, InvalidSourceNode) {
329 EXPECT_DEATH(
330 {
331 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700332 ReadConfig(ArtifactPath("aos/testdata/invalid_nodes.json"));
Austin Schuh217a9782019-12-21 23:02:50 -0800333 },
334 "source_node");
335
336 EXPECT_DEATH(
337 {
338 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700339 ReadConfig(ArtifactPath("aos/testdata/invalid_source_node.json"));
Austin Schuh217a9782019-12-21 23:02:50 -0800340 },
341 "source_node");
342
343 EXPECT_DEATH(
344 {
Austin Schuh373f1762021-06-02 21:07:09 -0700345 FlatbufferDetachedBuffer<Configuration> config = ReadConfig(
346 ArtifactPath("aos/testdata/invalid_destination_node.json"));
Austin Schuh217a9782019-12-21 23:02:50 -0800347 },
348 "destination_nodes");
349
350 EXPECT_DEATH(
351 {
352 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700353 ReadConfig(ArtifactPath("aos/testdata/self_forward.json"));
Austin Schuh217a9782019-12-21 23:02:50 -0800354 },
355 "forwarding data to itself");
356}
357
358// Tests that our node writeable helpers work as intended.
359TEST_F(ConfigurationTest, ChannelIsSendableOnNode) {
360 FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800361 R"channel({
Austin Schuh217a9782019-12-21 23:02:50 -0800362 "name": "/test",
363 "type": "aos.examples.Ping",
364 "source_node": "foo"
365})channel",
366 Channel::MiniReflectTypeTable()));
367
368 FlatbufferDetachedBuffer<Channel> bad_channel(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800369 R"channel({
Austin Schuh217a9782019-12-21 23:02:50 -0800370 "name": "/test",
371 "type": "aos.examples.Ping",
372 "source_node": "bar"
373})channel",
374 Channel::MiniReflectTypeTable()));
375
376 FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800377 R"node({
Austin Schuh217a9782019-12-21 23:02:50 -0800378 "name": "foo"
379})node",
380 Node::MiniReflectTypeTable()));
381
382 EXPECT_TRUE(
383 ChannelIsSendableOnNode(&good_channel.message(), &node.message()));
384 EXPECT_FALSE(
385 ChannelIsSendableOnNode(&bad_channel.message(), &node.message()));
386}
387
388// Tests that our node readable and writeable helpers work as intended.
389TEST_F(ConfigurationTest, ChannelIsReadableOnNode) {
390 FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800391 R"channel({
Austin Schuh217a9782019-12-21 23:02:50 -0800392 "name": "/test",
393 "type": "aos.examples.Ping",
394 "source_node": "bar",
395 "destination_nodes": [
Austin Schuh719946b2019-12-28 14:51:01 -0800396 {
397 "name": "baz"
398 },
399 {
400 "name": "foo"
401 }
Austin Schuh217a9782019-12-21 23:02:50 -0800402 ]
403})channel",
404 Channel::MiniReflectTypeTable()));
405
406 FlatbufferDetachedBuffer<Channel> bad_channel1(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800407 R"channel({
Austin Schuh217a9782019-12-21 23:02:50 -0800408 "name": "/test",
409 "type": "aos.examples.Ping",
410 "source_node": "bar"
411})channel",
412 Channel::MiniReflectTypeTable()));
413
414 FlatbufferDetachedBuffer<Channel> bad_channel2(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800415 R"channel({
Austin Schuh217a9782019-12-21 23:02:50 -0800416 "name": "/test",
417 "type": "aos.examples.Ping",
418 "source_node": "bar",
419 "destination_nodes": [
Austin Schuh719946b2019-12-28 14:51:01 -0800420 {
421 "name": "baz"
422 }
Austin Schuh217a9782019-12-21 23:02:50 -0800423 ]
424})channel",
425 Channel::MiniReflectTypeTable()));
426
427 FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer(
Austin Schuh719946b2019-12-28 14:51:01 -0800428 R"node({
Austin Schuh217a9782019-12-21 23:02:50 -0800429 "name": "foo"
430})node",
431 Node::MiniReflectTypeTable()));
432
433 EXPECT_TRUE(
434 ChannelIsReadableOnNode(&good_channel.message(), &node.message()));
435 EXPECT_FALSE(
436 ChannelIsReadableOnNode(&bad_channel1.message(), &node.message()));
437 EXPECT_FALSE(
438 ChannelIsReadableOnNode(&bad_channel2.message(), &node.message()));
439}
440
Austin Schuh719946b2019-12-28 14:51:01 -0800441// Tests that our node message is logged helpers work as intended.
442TEST_F(ConfigurationTest, ChannelMessageIsLoggedOnNode) {
443 FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer(
444 R"channel({
445 "name": "/test",
446 "type": "aos.examples.Ping",
447 "source_node": "bar",
448 "destination_nodes": [
449 {
450 "name": "baz"
451 }
452 ]
453})channel",
454 Channel::MiniReflectTypeTable()));
Austin Schuh217a9782019-12-21 23:02:50 -0800455
Austin Schuh719946b2019-12-28 14:51:01 -0800456 FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer(
457 R"channel({
458 "name": "/test",
459 "type": "aos.examples.Ping",
460 "source_node": "bar",
461 "logger": "NOT_LOGGED",
462 "destination_nodes": [
463 {
464 "name": "baz",
465 "timestamp_logger": "LOCAL_LOGGER"
466 }
467 ]
468})channel",
469 Channel::MiniReflectTypeTable()));
470
471 FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer(
472 R"channel({
473 "name": "/test",
474 "type": "aos.examples.Ping",
475 "source_node": "bar",
476 "logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700477 "logger_nodes": ["baz"],
Austin Schuh719946b2019-12-28 14:51:01 -0800478 "destination_nodes": [
479 {
480 "name": "baz"
481 }
482 ]
483})channel",
484 Channel::MiniReflectTypeTable()));
485
486 FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel(
487 JsonToFlatbuffer(
488 R"channel({
489 "name": "/test",
490 "type": "aos.examples.Ping",
491 "source_node": "bar",
492 "logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700493 "logger_nodes": ["foo"],
Austin Schuh719946b2019-12-28 14:51:01 -0800494 "destination_nodes": [
495 {
496 "name": "baz"
497 }
498 ]
499})channel",
500 Channel::MiniReflectTypeTable()));
501
Ravago Jonescf453ab2020-05-06 21:14:53 -0700502 FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer(
503 R"channel({
Austin Schuh719946b2019-12-28 14:51:01 -0800504 "name": "/test",
505 "type": "aos.examples.Ping",
506 "source_node": "bar",
507 "logger": "LOCAL_AND_REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700508 "logger_nodes": ["baz"],
Austin Schuh719946b2019-12-28 14:51:01 -0800509 "destination_nodes": [
510 {
511 "name": "baz"
512 }
513 ]
514})channel",
Ravago Jonescf453ab2020-05-06 21:14:53 -0700515 Channel::MiniReflectTypeTable()));
Austin Schuh719946b2019-12-28 14:51:01 -0800516
517 FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer(
518 R"node({
519 "name": "foo"
520})node",
521 Node::MiniReflectTypeTable()));
522
523 FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer(
524 R"node({
525 "name": "bar"
526})node",
527 Node::MiniReflectTypeTable()));
528
529 FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer(
530 R"node({
531 "name": "baz"
532})node",
533 Node::MiniReflectTypeTable()));
534
535 // Local logger.
536 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(),
537 &foo_node.message()));
538 EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(),
539 &bar_node.message()));
540 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(),
541 &baz_node.message()));
Austin Schuh48e94502021-06-18 18:35:53 -0700542 EXPECT_TRUE(
543 ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), nullptr));
Austin Schuh719946b2019-12-28 14:51:01 -0800544
545 // No logger.
546 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&not_logged_channel.message(),
547 &foo_node.message()));
548 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&not_logged_channel.message(),
Ravago Jonescf453ab2020-05-06 21:14:53 -0700549 &bar_node.message()));
Austin Schuh719946b2019-12-28 14:51:01 -0800550 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&not_logged_channel.message(),
551 &baz_node.message()));
Austin Schuh48e94502021-06-18 18:35:53 -0700552 EXPECT_FALSE(
553 ChannelMessageIsLoggedOnNode(&not_logged_channel.message(), nullptr));
Austin Schuh719946b2019-12-28 14:51:01 -0800554
555 // Remote logger.
556 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(),
557 &foo_node.message()));
558 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(),
559 &bar_node.message()));
560 EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(),
561 &baz_node.message()));
562
563 // Separate logger.
564 EXPECT_TRUE(ChannelMessageIsLoggedOnNode(
565 &logged_on_separate_logger_node_channel.message(), &foo_node.message()));
566 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(
567 &logged_on_separate_logger_node_channel.message(), &bar_node.message()));
568 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(
569 &logged_on_separate_logger_node_channel.message(), &baz_node.message()));
570
571 // Logged in multiple places.
572 EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(),
573 &foo_node.message()));
574 EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(),
575 &bar_node.message()));
576 EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(),
577 &baz_node.message()));
578}
579
Austin Schuh48e94502021-06-18 18:35:53 -0700580// Tests that our node message is logged helpers work as intended.
581TEST_F(ConfigurationDeathTest, ChannelMessageIsLoggedOnNode) {
582 FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer(
583 R"channel({
584 "name": "/test",
585 "type": "aos.examples.Ping",
586 "source_node": "bar",
587 "logger": "LOCAL_AND_REMOTE_LOGGER",
588 "logger_nodes": ["baz"],
589 "destination_nodes": [
590 {
591 "name": "baz"
592 }
593 ]
594})channel",
595 Channel::MiniReflectTypeTable()));
596
597 FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel(
598 JsonToFlatbuffer(
599 R"channel({
600 "name": "/test",
601 "type": "aos.examples.Ping",
602 "source_node": "bar",
603 "logger": "REMOTE_LOGGER",
604 "logger_nodes": ["foo"],
605 "destination_nodes": [
606 {
607 "name": "baz"
608 }
609 ]
610})channel",
611 Channel::MiniReflectTypeTable()));
612
613 EXPECT_DEATH(
614 {
615 ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(),
616 nullptr);
617 },
618 "Unsupported logging configuration in a single node world");
619 EXPECT_DEATH(
620 {
621 ChannelMessageIsLoggedOnNode(
622 &logged_on_separate_logger_node_channel.message(), nullptr);
623 },
624 "Unsupported logging configuration in a single node world");
625}
626
Austin Schuh719946b2019-12-28 14:51:01 -0800627// Tests that our forwarding timestamps are logged helpers work as intended.
628TEST_F(ConfigurationTest, ConnectionDeliveryTimeIsLoggedOnNode) {
629 FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer(
630 R"channel({
631 "name": "/test",
632 "type": "aos.examples.Ping",
633 "source_node": "bar",
634 "logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700635 "logger_nodes": ["baz"],
Austin Schuh719946b2019-12-28 14:51:01 -0800636 "destination_nodes": [
637 {
638 "name": "baz"
639 }
640 ]
641})channel",
642 Channel::MiniReflectTypeTable()));
643
644 FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer(
645 R"channel({
646 "name": "/test",
647 "type": "aos.examples.Ping",
648 "source_node": "bar",
649 "logger": "NOT_LOGGED",
650 "destination_nodes": [
651 {
652 "name": "baz",
653 "timestamp_logger": "NOT_LOGGED"
654 }
655 ]
656})channel",
657 Channel::MiniReflectTypeTable()));
658
659 FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer(
660 R"channel({
661 "name": "/test",
662 "type": "aos.examples.Ping",
663 "source_node": "bar",
664 "destination_nodes": [
665 {
666 "name": "baz",
667 "timestamp_logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700668 "timestamp_logger_nodes": ["bar"]
Austin Schuh719946b2019-12-28 14:51:01 -0800669 }
670 ]
671})channel",
672 Channel::MiniReflectTypeTable()));
673
674 FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel(
675 JsonToFlatbuffer(
676 R"channel({
677 "name": "/test",
678 "type": "aos.examples.Ping",
679 "source_node": "bar",
680 "logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700681 "logger_nodes": ["foo"],
Austin Schuh719946b2019-12-28 14:51:01 -0800682 "destination_nodes": [
683 {
684 "name": "baz",
685 "timestamp_logger": "REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700686 "timestamp_logger_nodes": ["foo"]
Austin Schuh719946b2019-12-28 14:51:01 -0800687 }
688 ]
689})channel",
690 Channel::MiniReflectTypeTable()));
691
Ravago Jonescf453ab2020-05-06 21:14:53 -0700692 FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer(
693 R"channel({
Austin Schuh719946b2019-12-28 14:51:01 -0800694 "name": "/test",
695 "type": "aos.examples.Ping",
696 "source_node": "bar",
697 "destination_nodes": [
698 {
699 "name": "baz",
700 "timestamp_logger": "LOCAL_AND_REMOTE_LOGGER",
Austin Schuhda40e472020-03-28 15:15:29 -0700701 "timestamp_logger_nodes": ["bar"]
Austin Schuh719946b2019-12-28 14:51:01 -0800702 }
703 ]
704})channel",
Ravago Jonescf453ab2020-05-06 21:14:53 -0700705 Channel::MiniReflectTypeTable()));
Austin Schuh719946b2019-12-28 14:51:01 -0800706
707 FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer(
708 R"node({
709 "name": "foo"
710})node",
711 Node::MiniReflectTypeTable()));
712
713 FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer(
714 R"node({
715 "name": "bar"
716})node",
717 Node::MiniReflectTypeTable()));
718
719 FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer(
720 R"node({
721 "name": "baz"
722})node",
723 Node::MiniReflectTypeTable()));
724
725 // Local logger.
726 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
727 &logged_on_self_channel.message(), &baz_node.message(),
728 &foo_node.message()));
729 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
730 &logged_on_self_channel.message(), &baz_node.message(),
731 &bar_node.message()));
732 EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode(
733 &logged_on_self_channel.message(), &baz_node.message(),
734 &baz_node.message()));
735
736 // No logger means.
737 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
738 &not_logged_channel.message(), &baz_node.message(), &foo_node.message()));
739 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
740 &not_logged_channel.message(), &baz_node.message(), &bar_node.message()));
741 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
742 &not_logged_channel.message(), &baz_node.message(), &baz_node.message()));
743
744 // Remote logger.
745 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
746 &logged_on_remote_channel.message(), &baz_node.message(),
747 &foo_node.message()));
748 EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode(
749 &logged_on_remote_channel.message(), &baz_node.message(),
750 &bar_node.message()));
751 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
752 &logged_on_remote_channel.message(), &baz_node.message(),
753 &baz_node.message()));
754
755 // Separate logger.
756 EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode(
757 &logged_on_separate_logger_node_channel.message(), &baz_node.message(),
758 &foo_node.message()));
759 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
760 &logged_on_separate_logger_node_channel.message(), &baz_node.message(),
761 &bar_node.message()));
762 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
763 &logged_on_separate_logger_node_channel.message(), &baz_node.message(),
764 &baz_node.message()));
765
766 // Logged on both the node and a remote node.
767 EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode(
768 &logged_on_both_channel.message(), &baz_node.message(),
769 &foo_node.message()));
770 EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode(
771 &logged_on_both_channel.message(), &baz_node.message(),
772 &bar_node.message()));
773 EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode(
774 &logged_on_both_channel.message(), &baz_node.message(),
775 &baz_node.message()));
776}
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800777
778// Tests that we can deduce source nodes from a multinode config.
779TEST_F(ConfigurationTest, SourceNodeNames) {
780 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700781 ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json"));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800782
783 // This is a bit simplistic in that it doesn't test deduplication, but it does
784 // exercise a lot of the logic.
785 EXPECT_THAT(
786 SourceNodeNames(&config.message(), config.message().nodes()->Get(0)),
787 ::testing::ElementsAreArray({"pi2"}));
788 EXPECT_THAT(
789 SourceNodeNames(&config.message(), config.message().nodes()->Get(1)),
790 ::testing::ElementsAreArray({"pi1"}));
791}
792
793// Tests that we can deduce destination nodes from a multinode config.
794TEST_F(ConfigurationTest, DestinationNodeNames) {
795 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700796 ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json"));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800797
798 // This is a bit simplistic in that it doesn't test deduplication, but it does
799 // exercise a lot of the logic.
800 EXPECT_THAT(
801 DestinationNodeNames(&config.message(), config.message().nodes()->Get(0)),
802 ::testing::ElementsAreArray({"pi2"}));
803 EXPECT_THAT(
804 DestinationNodeNames(&config.message(), config.message().nodes()->Get(1)),
805 ::testing::ElementsAreArray({"pi1"}));
806}
807
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800808// Tests that we can pull out all the nodes.
809TEST_F(ConfigurationTest, GetNodes) {
810 {
811 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700812 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800813 const Node *pi1 = GetNode(&config.message(), "pi1");
814 const Node *pi2 = GetNode(&config.message(), "pi2");
815
816 EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(pi1, pi2));
817 }
818
819 {
820 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700821 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800822 EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(nullptr));
823 }
824}
825
Austin Schuh65465332020-11-05 17:36:53 -0800826// Tests that we can pull out all the nodes with a tag.
827TEST_F(ConfigurationTest, GetNodesWithTag) {
828 {
829 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700830 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuh65465332020-11-05 17:36:53 -0800831 const Node *pi1 = GetNode(&config.message(), "pi1");
832 const Node *pi2 = GetNode(&config.message(), "pi2");
833
834 EXPECT_THAT(GetNodesWithTag(&config.message(), "a"),
835 ::testing::ElementsAre(pi1));
836 EXPECT_THAT(GetNodesWithTag(&config.message(), "b"),
837 ::testing::ElementsAre(pi2));
838 EXPECT_THAT(GetNodesWithTag(&config.message(), "c"),
839 ::testing::ElementsAre(pi1, pi2));
840 }
841
842 {
843 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700844 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Austin Schuh65465332020-11-05 17:36:53 -0800845 EXPECT_THAT(GetNodesWithTag(&config.message(), "arglfish"),
846 ::testing::ElementsAre(nullptr));
847 }
848}
849
Brian Silverman631b6262021-11-10 12:25:08 -0800850// Tests that we can check if a node has a tag.
851TEST_F(ConfigurationTest, NodeHasTag) {
852 {
853 FlatbufferDetachedBuffer<Configuration> config =
854 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
855 const Node *pi1 = GetNode(&config.message(), "pi1");
856 const Node *pi2 = GetNode(&config.message(), "pi2");
857
858 EXPECT_TRUE(NodeHasTag(pi1, "a"));
859 EXPECT_FALSE(NodeHasTag(pi2, "a"));
860 EXPECT_FALSE(NodeHasTag(pi1, "b"));
861 EXPECT_TRUE(NodeHasTag(pi2, "b"));
862 EXPECT_TRUE(NodeHasTag(pi1, "c"));
863 EXPECT_TRUE(NodeHasTag(pi2, "c"));
864 EXPECT_FALSE(NodeHasTag(pi1, "nope"));
865 EXPECT_FALSE(NodeHasTag(pi2, "nope"));
866 }
867
868 EXPECT_TRUE(NodeHasTag(nullptr, "arglfish"));
869}
870
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800871// Tests that we can extract a node index from a config.
872TEST_F(ConfigurationTest, GetNodeIndex) {
873 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700874 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuh04408fc2020-02-16 21:48:54 -0800875 FlatbufferDetachedBuffer<Configuration> config2 =
Austin Schuh373f1762021-06-02 21:07:09 -0700876 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800877 const Node *pi1 = GetNode(&config.message(), "pi1");
878 const Node *pi2 = GetNode(&config.message(), "pi2");
879
Austin Schuh04408fc2020-02-16 21:48:54 -0800880 // Try the normal case.
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800881 EXPECT_EQ(GetNodeIndex(&config.message(), pi1), 0);
882 EXPECT_EQ(GetNodeIndex(&config.message(), pi2), 1);
Austin Schuh04408fc2020-02-16 21:48:54 -0800883
884 // Now try if we have node pointers from a different message.
885 EXPECT_EQ(GetNodeIndex(&config2.message(), pi1), 0);
886 EXPECT_EQ(GetNodeIndex(&config2.message(), pi2), 1);
887
888 // And now try string names.
889 EXPECT_EQ(GetNodeIndex(&config2.message(), pi1->name()->string_view()), 0);
890 EXPECT_EQ(GetNodeIndex(&config2.message(), pi2->name()->string_view()), 1);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800891}
892
893// Tests that GetNodeOrDie handles both single and multi-node worlds and returns
894// valid nodes.
895TEST_F(ConfigurationDeathTest, GetNodeOrDie) {
896 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700897 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800898 FlatbufferDetachedBuffer<Configuration> config2 =
Austin Schuh373f1762021-06-02 21:07:09 -0700899 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800900 {
901 // Simple case, nullptr -> nullptr
902 FlatbufferDetachedBuffer<Configuration> single_node_config =
Austin Schuh373f1762021-06-02 21:07:09 -0700903 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800904 EXPECT_EQ(nullptr, GetNodeOrDie(&single_node_config.message(), nullptr));
905
906 // Confirm that we die when a node is passed in.
907 EXPECT_DEATH(
908 {
909 GetNodeOrDie(&single_node_config.message(),
910 config.message().nodes()->Get(0));
911 },
912 "Provided a node in a single node world.");
913 }
914
915 const Node *pi1 = GetNode(&config.message(), "pi1");
916 // Now try a lookup using a node from a different instance of the config.
917 EXPECT_EQ(pi1,
918 GetNodeOrDie(&config.message(), config2.message().nodes()->Get(0)));
919}
920
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800921TEST_F(ConfigurationTest, GetNodeFromHostname) {
922 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700923 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800924 EXPECT_EQ("pi1",
925 CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi"))
926 ->name()
927 ->string_view());
928 EXPECT_EQ("pi2", CHECK_NOTNULL(
929 GetNodeFromHostname(&config.message(), "raspberrypi2"))
930 ->name()
931 ->string_view());
932 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi3"));
933 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost"));
934 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3"));
935}
936
937TEST_F(ConfigurationTest, GetNodeFromHostnames) {
938 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuh373f1762021-06-02 21:07:09 -0700939 ReadConfig(ArtifactPath("aos/testdata/good_multinode_hostnames.json"));
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800940 EXPECT_EQ("pi1",
941 CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi"))
942 ->name()
943 ->string_view());
944 EXPECT_EQ("pi2", CHECK_NOTNULL(
945 GetNodeFromHostname(&config.message(), "raspberrypi2"))
946 ->name()
947 ->string_view());
948 EXPECT_EQ("pi2", CHECK_NOTNULL(
949 GetNodeFromHostname(&config.message(), "raspberrypi3"))
950 ->name()
951 ->string_view());
Ravago Jonescf453ab2020-05-06 21:14:53 -0700952 EXPECT_EQ("pi2",
953 CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "other"))
954 ->name()
955 ->string_view());
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800956 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi4"));
957 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost"));
958 EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3"));
959}
960
Austin Schuhfc7b6a02021-07-12 21:19:07 -0700961// Tests that SourceNodeIndex reasonably handles a multi-node log file.
962TEST_F(ConfigurationTest, SourceNodeIndex) {
963 FlatbufferDetachedBuffer<Configuration> config =
964 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
965 std::vector<size_t> result = SourceNodeIndex(&config.message());
966
967 EXPECT_THAT(result, ::testing::ElementsAreArray({0, 1, 0, 0}));
968}
969
Austin Schuh5e95bd62021-10-11 18:40:22 -0700970// Tests that we reject invalid logging configurations.
971TEST_F(ConfigurationDeathTest, InvalidLoggerConfig) {
972 EXPECT_DEATH(
973 {
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700974 FlatbufferDetachedBuffer<Configuration> config = ReadConfig(
975 ArtifactPath("aos/testdata/invalid_logging_configuration.json"));
Austin Schuh5e95bd62021-10-11 18:40:22 -0700976 },
977 "Logging timestamps without data");
978}
979
Austin Schuha156fb22021-10-11 19:23:21 -0700980// Tests that we reject duplicate timestamp destination node configurations.
981TEST_F(ConfigurationDeathTest, DuplicateTimestampDestinationNodes) {
982 EXPECT_DEATH(
983 {
984 FlatbufferDetachedBuffer<Configuration> config = ReadConfig(
985 ArtifactPath("aos/testdata/duplicate_destination_nodes.json"));
986 },
987 "Found duplicate timestamp_logger_nodes in");
988}
989
990// Tests that we reject duplicate logger node configurations for a channel's
991// data.
992TEST_F(ConfigurationDeathTest, DuplicateLoggerNodes) {
993 EXPECT_DEATH(
994 {
995 FlatbufferDetachedBuffer<Configuration> config = ReadConfig(
996 ArtifactPath("aos/testdata/duplicate_logger_nodes.json"));
997 },
998 "Found duplicate logger_nodes in");
999}
1000
Austin Schuhfb37c612022-08-11 15:24:51 -07001001// Tests that we properly compute the queue size for the provided duration.
1002TEST_F(ConfigurationTest, QueueSize) {
1003 EXPECT_EQ(QueueSize(100, chrono::seconds(2)), 200);
1004 EXPECT_EQ(QueueSize(200, chrono::seconds(2)), 400);
1005 EXPECT_EQ(QueueSize(100, chrono::seconds(6)), 600);
1006 EXPECT_EQ(QueueSize(100, chrono::milliseconds(10)), 1);
1007 EXPECT_EQ(QueueSize(100, chrono::milliseconds(10) - chrono::nanoseconds(1)),
1008 1);
1009 EXPECT_EQ(QueueSize(100, chrono::milliseconds(10) - chrono::nanoseconds(2)),
1010 1);
1011}
1012
1013// Tests that we compute scratch buffer size correctly too.
1014TEST_F(ConfigurationTest, QueueScratchBufferSize) {
1015 const aos::FlatbufferDetachedBuffer<Channel> channel =
1016 JsonToFlatbuffer<Channel>(
1017 "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"num_readers\": 5, "
1018 "\"num_senders\": 10 }");
Austin Schuhfb37c612022-08-11 15:24:51 -07001019 EXPECT_EQ(QueueScratchBufferSize(&channel.message()), 15);
1020}
1021
Nathan Leong307c9692022-10-08 15:25:03 -07001022// Tests that GetSchema returns schema of specified type
1023TEST_F(ConfigurationTest, GetSchema) {
1024 FlatbufferDetachedBuffer<Configuration> config =
1025 ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
1026 FlatbufferVector<reflection::Schema> expected_schema =
1027 FileToFlatbuffer<reflection::Schema>(
1028 ArtifactPath("aos/events/ping.bfbs"));
1029 EXPECT_EQ(FlatbufferToJson(GetSchema(&config.message(), "aos.examples.Ping")),
1030 FlatbufferToJson(expected_schema));
1031 EXPECT_EQ(GetSchema(&config.message(), "invalid_name"), nullptr);
1032}
1033
1034// Tests that GetSchema template returns schema of specified type
1035TEST_F(ConfigurationTest, GetSchemaTemplate) {
1036 FlatbufferDetachedBuffer<Configuration> config =
1037 ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
1038 FlatbufferVector<reflection::Schema> expected_schema =
1039 FileToFlatbuffer<reflection::Schema>(
1040 ArtifactPath("aos/events/ping.bfbs"));
1041 EXPECT_EQ(FlatbufferToJson(GetSchema<aos::examples::Ping>(&config.message())),
1042 FlatbufferToJson(expected_schema));
1043}
1044
1045// Tests that GetSchemaDetachedBuffer returns detached buffer of specified type
1046TEST_F(ConfigurationTest, GetSchemaDetachedBuffer) {
1047 FlatbufferDetachedBuffer<Configuration> config =
1048 ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
1049 FlatbufferVector<reflection::Schema> expected_schema =
1050 FileToFlatbuffer<reflection::Schema>(
1051 ArtifactPath("aos/events/ping.bfbs"));
1052 EXPECT_EQ(FlatbufferToJson(
1053 GetSchemaDetachedBuffer(&config.message(), "aos.examples.Ping")
1054 .value()),
1055 FlatbufferToJson(expected_schema));
1056 EXPECT_EQ(GetSchemaDetachedBuffer(&config.message(), "invalid_name"),
1057 std::nullopt);
1058}
1059
James Kuszmaul741a4d02023-01-05 14:59:21 -08001060// Tests that we can use a utility to add individual channels to a single-node
1061// config.
1062TEST_F(ConfigurationTest, AddChannelToConfigSingleNode) {
1063 FlatbufferDetachedBuffer<Configuration> base_config =
1064 ReadConfig(ArtifactPath("aos/testdata/config1.json"));
1065
1066 FlatbufferVector<reflection::Schema> schema =
1067 FileToFlatbuffer<reflection::Schema>(
1068 ArtifactPath("aos/events/ping.bfbs"));
1069
1070 FlatbufferDetachedBuffer<Configuration> new_config =
1071 AddChannelToConfiguration(&base_config.message(), "/new", schema);
1072
1073 ASSERT_EQ(new_config.message().channels()->size(),
1074 base_config.message().channels()->size() + 1);
1075
1076 const Channel *channel =
1077 GetChannel(new_config, "/new", "aos.examples.Ping", "", nullptr);
1078 ASSERT_TRUE(channel != nullptr);
1079 ASSERT_TRUE(channel->has_schema());
1080 // Check that we don't populate channel settings that we don't override the
1081 // defaults of.
1082 ASSERT_FALSE(channel->has_frequency());
1083}
1084
1085// Tests that we can use a utility to add individual channels to a multi-node
1086// config.
1087TEST_F(ConfigurationTest, AddChannelToConfigMultiNode) {
1088 FlatbufferDetachedBuffer<Configuration> base_config =
1089 ReadConfig(ArtifactPath("aos/testdata/good_multinode.json"));
1090
1091 FlatbufferVector<reflection::Schema> schema =
1092 FileToFlatbuffer<reflection::Schema>(
1093 ArtifactPath("aos/events/ping.bfbs"));
1094
1095 aos::ChannelT channel_overrides;
1096 channel_overrides.frequency = 971;
1097 FlatbufferDetachedBuffer<Configuration> new_config =
1098 AddChannelToConfiguration(&base_config.message(), "/new", schema,
1099 GetNode(&base_config.message(), "pi1"),
1100 channel_overrides);
1101
1102 ASSERT_EQ(new_config.message().channels()->size(),
1103 base_config.message().channels()->size() + 1);
1104
1105 const Channel *channel =
1106 GetChannel(new_config, "/new", "aos.examples.Ping", "", nullptr);
1107 ASSERT_TRUE(channel != nullptr);
1108 ASSERT_TRUE(channel->has_schema());
1109 ASSERT_TRUE(channel->has_source_node());
1110 ASSERT_EQ("pi1", channel->source_node()->string_view());
1111 ASSERT_EQ(971, channel->frequency());
1112}
1113
Austin Schuhcb108412019-10-13 16:09:54 -07001114} // namespace testing
1115} // namespace configuration
1116} // namespace aos