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