blob: e20b30873cd43dfc846dc86878cff25cf59a768b [file] [log] [blame]
Austin Schuhcb108412019-10-13 16:09:54 -07001#include "aos/configuration.h"
2
3#include "absl/strings/strip.h"
4#include "aos/json_to_flatbuffer.h"
5#include "aos/testing/test_logging.h"
6#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "flatbuffers/reflection.h"
8#include "glog/logging.h"
Austin Schuhcb108412019-10-13 16:09:54 -07009#include "gtest/gtest.h"
10
11namespace aos {
12namespace configuration {
13namespace testing {
14
15class ConfigurationTest : public ::testing::Test {
16 public:
17 ConfigurationTest() { ::aos::testing::EnableTestLogging(); }
18};
19
20typedef ConfigurationTest ConfigurationDeathTest;
21
22// *the* expected location for all working tests.
23const char *kExpectedLocation =
24 "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }";
25
26// Tests that we can read and merge a configuration.
27TEST_F(ConfigurationTest, ConfigMerge) {
Austin Schuh40485ed2019-10-26 21:51:44 -070028 FlatbufferDetachedBuffer<Configuration> config =
29 ReadConfig("aos/testdata/config1.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 LOG(INFO) << "Read: " << FlatbufferToJson(config, true);
Austin Schuhcb108412019-10-13 16:09:54 -070031
32 EXPECT_EQ(
33 absl::StripSuffix(
34 util::ReadFileToStringOrDie("aos/testdata/expected.json"), "\n"),
35 FlatbufferToJson(config, true));
36}
37
Alex Perrycb7da4b2019-08-28 19:35:56 -070038// Tests that we sort the entries in a config so we can look entries up.
39TEST_F(ConfigurationTest, UnsortedConfig) {
40 FlatbufferDetachedBuffer<Configuration> config =
41 ReadConfig("aos/testdata/backwards.json");
42
43 LOG(INFO) << "Read: " << FlatbufferToJson(config, true);
44
45 EXPECT_EQ(FlatbufferToJson(GetChannel(config, ".aos.robot_state",
46 "aos.RobotState", "app1")),
47 "{ \"name\": \".aos.robot_state\", \"type\": \"aos.RobotState\", "
48 "\"max_size\": 5 }");
49}
50
Austin Schuhcb108412019-10-13 16:09:54 -070051// Tests that we die when a file is imported twice.
52TEST_F(ConfigurationDeathTest, DuplicateFile) {
53 EXPECT_DEATH(
54 {
Austin Schuh40485ed2019-10-26 21:51:44 -070055 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuhcb108412019-10-13 16:09:54 -070056 ReadConfig("aos/testdata/config1_bad.json");
57 },
58 "aos/testdata/config1_bad.json");
59}
60
61// Tests that we can lookup a location, complete with maps, from a merged
62// config.
Austin Schuh40485ed2019-10-26 21:51:44 -070063TEST_F(ConfigurationTest, GetChannel) {
64 FlatbufferDetachedBuffer<Configuration> config =
65 ReadConfig("aos/testdata/config1.json");
Austin Schuhcb108412019-10-13 16:09:54 -070066
67 // Test a basic lookup first.
Austin Schuh40485ed2019-10-26 21:51:44 -070068 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070069 kExpectedLocation);
70
71 // Test that an invalid name results in nullptr back.
Austin Schuh40485ed2019-10-26 21:51:44 -070072 EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1"), nullptr);
Austin Schuhcb108412019-10-13 16:09:54 -070073
74 // Tests that a root map/rename works. And that they get processed from the
75 // bottom up.
76 EXPECT_EQ(
Austin Schuh40485ed2019-10-26 21:51:44 -070077 FlatbufferToJson(GetChannel(config, "/batman", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070078 kExpectedLocation);
79
80 // And then test that an application specific map/rename works.
Austin Schuh40485ed2019-10-26 21:51:44 -070081 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/bar", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070082 kExpectedLocation);
Austin Schuh40485ed2019-10-26 21:51:44 -070083 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/baz", ".aos.bar", "app2")),
Austin Schuhcb108412019-10-13 16:09:54 -070084 kExpectedLocation);
85
86 // And then test that an invalid application name gets properly ignored.
Austin Schuh40485ed2019-10-26 21:51:44 -070087 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app3")),
Austin Schuhcb108412019-10-13 16:09:54 -070088 kExpectedLocation);
89}
90
91} // namespace testing
92} // namespace configuration
93} // namespace aos