Files
spark/src/lib/config/tests/config_tests.cpp
T

583 lines
15 KiB
C++
Executable File

#include "lib/utils/TempFile.h"
#include "../config.h"
#include "../ConfigurationState.h"
#include "../ConfigurationException.h"
#include "lib/expression/ParserExceptions.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/is_unittest_environment.h"
#include "HTTPServer.h"
#include "lib/config/invalid/invalid.pb.h"
#include "lib/config/protos/system.pb.h"
#include "lib/config/protos/testkeys.pb.h"
#include <boost/filesystem.hpp>
#include <iostream>
#include <memory>
#define URDL_HEADER_ONLY 1
#define URDL_DISALE_SSL 1
#include <urdl/url.hpp>
#include <urdl/test/http_server.hpp>
#include <catch/catch.hpp>
using namespace bts::config;
using namespace bts::config::internal;
namespace proto = google::protobuf;
using bts::config::test::Foo;
using bts::config::test::FooGroup;
using bts::config::test::StringTable;
namespace bts {
int config_tests = 0;
TEST_CASE("Configuration Tests","[config]") {
bts::utils::internal::force_unittest_environment();
SECTION("basic_get_value_test")
{
initialize_for_test(
"Foo { \n"
" rule: \"true\"\n"
" value {\n"
" name: \"Name A\"\n"
" id: 65\n"
" email: \"test@example.com\"\n"
" }\n"
"}\n");
const Foo& foo = get<Foo>();
REQUIRE(foo.name() == "Name A");
REQUIRE(foo.id() == 65);
REQUIRE(foo.email().Get(0) == "test@example.com");
}
SECTION("default_value_test")
{
initialize_for_test(""); // Only loads defaults.conf
// Should load a Foo from testkeys.conf
const Foo& foo = get<Foo>();
REQUIRE(foo.name() == "Blue Foo"); // from testkeys.proto
REQUIRE(foo.id() == 1234); // from testkeys.proto
REQUIRE(foo.email().size() > 0);
REQUIRE(foo.email().Get(0) == "default@example.com"); // from testkeys.conf
}
SECTION("default_include_test")
{
// test defaults.conf with an include
}
SECTION("invalid_key_type_test")
{
// Load an initialize with no files - OK all values default
const Foo& foo = get<Foo>(); // OK
REQUIRE(foo.name() == "Blue Foo");
bool error = false;
try
{
get<config::test::InvalidMessageType>(); // BAD
}
catch(...)
{
error = true;
}
REQUIRE(error == true);
}
SECTION("invalid_config_test2")
{
utils::TempFile config_b;
*config_b.out() <<
"Foo {\n"
" value { \n"
" name: \"bob\"\n"
" id: 7319 \n"
" }";
bool error = false;
try
{
initialize_for_test("include: \"" + config_b.path() + "\"", false);
}
catch(const ConfigurationException& err)
{
std::string what = err.what();
bts_assert(what.find("Expected identifier.") != std::string::npos);
error = true;
}
REQUIRE(error == true);
}
// Tests that we can load using a File:/// url
// Tests complex value load
SECTION("file_url_test")
{
utils::TempFile config_a;
utils::TempFile config_b;
*config_a.out() << "include: \"" << config_b.path() << "\"" << std::endl <<
"Foo { \n"
" rule: \"false\"\n"
" value {\n"
" name: \"Name A\"\n"
" id: 65\n"
" email: \"A@bluecg.com\"\n"
" }\n"
"}\n";
*config_b.out() <<
"Foo {\n"
" rule: \"true\"\n"
" value {\n"
" name: \"Name B\"\n"
" id: 66 "
" email: \"A@bluecg.com\" \n"
" }}";
// Get key Foo without loading any config files - this will show the default value
Foo foo = bts::config::get<Foo>();
REQUIRE(foo.name() == "Blue Foo");
REQUIRE(foo.id() == 1234);
// Parse the config file and file:/// include
initialize_for_test("include: \"" + config_a.path() + "\"");
// Get the new value of Foo - this time it will come from config_b
foo = bts::config::get<Foo>();
REQUIRE(foo.name() == "Name B");
REQUIRE(foo.id() == 66);
}
SECTION("http_test")
{
HTTPServer server;
std::string port = boost::lexical_cast<std::string>(server.port());
std::string path = "/spark.conf";
std::string response =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n\r\n";
std::string content =
"FooGroup {\n"
" rule: \"true\"\n"
" value {\n"
" group: \"http_group\"\n"
" foo {\n"
" name: \"http\"\n"
" id: 80\n"
"}}}\n";
server.add_response(path, response, content);
server.start();
std::string config_url = "http://127.0.0.1:" + port;
// Initialize with a configuration which references the http url
initialize_for_test("include: \"" + config_url + "\"");
server.stop();
const FooGroup& foogrp = get<FooGroup>();
REQUIRE(foogrp.group() == "http_group");
REQUIRE(foogrp.foo().name() == "http");
REQUIRE(foogrp.foo().id() == 80);
REQUIRE(get<System>().config_host() == "127.0.0.1");
}
SECTION("http_folder_test")
{
HTTPServer server;
std::string port = boost::lexical_cast<std::string>(server.port());
std::string config_url = "http://localhost:" + port + "/";
std::string response =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n\r\n";
server.add_response("/spark.conf", response, "include: \"" + config_url + "other/\"");
server.add_response("/testkeys.conf", response,
"FooGroup {\n"
" rule: \"true\"\n"
" value {\n"
" group: \"http_group\"\n"
" foo {\n"
" name: \"http\"\n"
" id: 80\n"
"}}}\n");
server.add_response("/other/spark.conf", response,
"Foo {\n"
" rule: \"true\"\n"
" value: {\n"
" name: \"included_value\"\n"
" id: 4096\n"
" }}\n");
server.start();
initialize_for_test("include: \"" + config_url + "\"");
server.stop();
const FooGroup& foogrp = get<FooGroup>();
REQUIRE(foogrp.group() == "http_group");
REQUIRE(foogrp.foo().name() == "http");
REQUIRE(foogrp.foo().id() == 80);
REQUIRE(get<Foo>().name() == "included_value");
REQUIRE(get<Foo>().id() == 4096);
}
SECTION("test_rules")
{
initialize_for_test(
"StringTable {\n"
" value { \n"
" entries: [\"foo\", \"bar\", \"baz\"] \n"
" }\n"
" }\n"
"RuleRef { \n"
" # use this rule of StringTable has a cow \n"
" rule: \"'cow' in StringTable.entries\" \n"
" value { \n"
" a_string: \"AAA\"\n"
" a_int: -1\n"
" }\n"
"}\n"
"RuleRef { \n"
" rule: \"'baz' in StringTable.entries\" \n"
" value { \n"
" a_string: \"BBB\"\n"
" a_int: 99\n"
" }\n"
"}\n"
"Foo { \n"
" rule: \"RuleRef.a_int == 789\"\n"
" value {\n"
" name: \"A\"\n"
" }\n"
"}\n"
"Foo { \n"
" rule: \"RuleRef.a_string == 'BBB'\"\n"
" value {\n"
" name: \"B\"\n"
" }\n"
"}\n"
);
// What should happen:
// Foo A
// eval rule
// find RuleRef AAA
// eval rule
// fail - StringTable.entries does not have 'cow'
// find RuleRef BBB
// eval rule
// success
// Fail - RuleRef.a_int != 789
// Foo B
// eval rule
// - same as above
// success a_string == 'BBB'
// return Foo B
const Foo& foo = get<Foo>();
REQUIRE(foo.name() == "B");
}
SECTION("test_recursive_rules")
{
initialize_for_test(
"StringTable {\n"
" rule: \"Foo.name == 'foo'\"\n"
" value { \n"
" entries: [\"foo\", \"bar\", \"baz\"] \n"
" }\n"
" }\n"
"RuleRef { \n"
" rule: \"'foo' in StringTable.entries\" \n"
" value { \n"
" a_string: \"AAA\"\n"
" a_int: -1\n"
" }\n"
"}\n"
"Foo { \n"
" rule: \"RuleRef.a_string == 'BBB'\"\n"
" value {\n"
" name: \"foo\"\n"
" }\n"
"}\n"
);
// Foo cannot be queried because RuleRef's rule requests StringTable which requests RuleRef
bool error = false;
try
{
get<Foo>();
}
catch(const expression::RecursivePropertyGetException&)
{
error = true;
}
REQUIRE(error);
}
SECTION("test_nested_properties")
{
initialize_for_test(
"RuleRef { \n"
" value { \n"
" nested {\n"
" list_int: [7,8,9] \n"
" nested {\n"
" a_string: 'ABC' \n"
" }\n"
" }\n"
" }\n"
"}\n"
"Foo { \n"
" rule: \"RuleRef.nested.nested.a_string == 'ABC' and 9 in RuleRef.nested.list_int\"\n"
" value {\n"
" name: \"foo\"\n"
" }\n"
"}\n"
);
REQUIRE(get<Foo>().name() == "foo");
}
SECTION("built_in_properties_test")
{
initialize_for_test(
"Foo { \n"
" rule: \"System.unittest_environment == true \"\n"
" value {\n"
" name: \"unit tests\"\n"
" }\n"
"}\n"
);
REQUIRE(get<Foo>().name() == "unit tests");
}
SECTION("nonbreaking_space_test")
{
bool parse_error = false;
try
{
initialize_for_test(
"Foo { "
" value {\xA0\xA0 "
" name: \"valid name\""
" \xA0 }"
"} ", false);
}
catch(const bts::config::ConfigurationException)
{
parse_error = true;
}
REQUIRE(parse_error);
}
SECTION("macro_bool_test")
{
initialize_for_test(
"macro"
"{"
" name: \"bool\""
" expression: \"'c' in ['a', 'b', 'c', 'd']\""
"}"
""
"Foo"
"{"
" rule: \"$bool\""
" value {"
" name: 'good'"
" }"
"}"
""
"Foo"
"{"
" value {"
" name: 'bad'"
" }"
"}"
);
REQUIRE(config::get<Foo>().name() == "good");
}
SECTION("macro_list_test")
{
initialize_for_test(
"macro"
"{"
" name: \"list\""
" expression: \"['a', 'b', 'c', 'd']\""
"}"
""
"Foo"
"{"
" rule: \"'a' in $list\""
" value {"
" name: 'good'"
" }"
"}"
""
"Foo"
"{"
" value {"
" name: 'bad'"
" }"
"}"
);
REQUIRE(config::get<Foo>().name() == "good");
}
SECTION("fallback_test") {
//HTTPServer main;
HTTPServer fallback;
//std::string main_url = "http://localhost:" + boost::lexical_cast<std::string>(main.port()) + "/";
std::string main_url = "http://does_not_exist:12345/";
//main.stop();
std::string fallback_url = "http://127.0.0.1:" + boost::lexical_cast<std::string>(fallback.port()) + "/";
std::string response =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n\r\n";
std::string content =
"FooGroup {\n"
" value {\n"
" group: \"http_group\"\n"
" foo {\n"
" name: \"http\"\n"
" id: 80\n"
"}}}\n";
fallback.add_response("/spark.conf", response, content);
fallback.start();
initialize_for_test(
"include: '" + main_url + "'\n"
"fallback: '" + fallback_url + "'\n",false
);
fallback.stop();
REQUIRE(get<FooGroup>().foo().name() == "http");
REQUIRE(get<FooGroup>().foo().id() == 80);
}
SECTION("macro_definition_order_test") {
// The $is_crusible test
// o2_cme_enabled depends on Foo which has a rule which depends on is_crucible.
// Eager evaluation caused a bug which would cause loading o2_cme_enabled to set is_crucible false. Case 3993
initialize_for_test(
"Foo {"
" rule: '$is_crucible'"
" value { name: 'cme_1u7_cert' }"
"}"
""
"Foo {"
" value { name: '' }"
"}"
""
"macro {"
" name: 'o2_cme_enabled'"
" expression: \"Foo.name != ''\" "
"}"
""
"macro {"
" name: 'is_crucible'"
" expression: 'true'"
"}"
);
REQUIRE(get<Foo>().name() == "cme_1u7_cert");
}
SECTION("repeated_value_order_preserved") {
initialize_for_test(
"Foo {"
" value { name: 'edge-eharding' id: 80 }"
"}"
"StringTable {"
" value {"
" entries: 'http://'"
" entries: @Foo.name"
" entries: ':'"
" entries: @Foo.id"
" entries: '/risk'"
" }"
"}");
std::string result;
for (const auto& part : get<StringTable>().entries()) {
result += part;
}
REQUIRE(result == "http://edge-eharding:80/risk");
}
SECTION("string_concat_test") {
initialize_for_test(
"Ports {\n"
" value { \n"
" config_port: 80 \n"
" }\n"
"}\n"
"RuleRef { \n"
" value { \n"
" a_string: \"localhost\"\n"
" }\n"
"}\n"
"macro{\n"
" name: 'list'\n"
" expression: \"['http://', RuleRef.a_string, ':', Ports.config_port, '/risk']\"\n"
"}"
"StringTable {\n"
"value {\n"
" entries: 'http://'\n"
" entries: @RuleRef.a_string\n"
" entries: ':'\n"
" entries: @Ports.config_port\n"
" entries: '/risk'\n"
"}}\n"
"Foo { \n"
" value {\n"
" name: $list\n"
" url: @StringTable.entries \n"
" }\n"
"}"
);
auto foo = config::get<Foo>();
auto expected = "http://localhost:80/risk";
REQUIRE(foo.name() == expected);
REQUIRE(foo.url() == expected);
}
reset();
}
}