diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /configutil/src/tests/config_status/config_status_test.cpp |
Publish
Diffstat (limited to 'configutil/src/tests/config_status/config_status_test.cpp')
-rw-r--r-- | configutil/src/tests/config_status/config_status_test.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/configutil/src/tests/config_status/config_status_test.cpp b/configutil/src/tests/config_status/config_status_test.cpp new file mode 100644 index 00000000000..af3f343a0b7 --- /dev/null +++ b/configutil/src/tests/config_status/config_status_test.cpp @@ -0,0 +1,109 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/fastos/fastos.h> +#include <vespa/vespalib/testkit/testapp.h> +#include <lib/configstatus.h> +#include <iostream> +#include <vespa/fastlib/net/httpserver.h> +#include <vespa/config-model.h> +#include <vespa/config/config.h> +#include <vespa/config/subscription/sourcespec.h> +#include <vespa/vespalib/stllike/string.h> + +using namespace config; + +class HTTPStatus : private Fast_HTTPServer { +private: + std::string _reply; + bool _fail; + + virtual void onGetRequest(const string &, const string &, + Fast_HTTPConnection &conn) { + if (_fail) { + conn.Output(conn.GetHTTPVersion().c_str()); + conn.Output(" 500 Error\r\n"); + conn.Output("Connection: close\r\n"); + conn.Output("\r\n"); + } else { + conn.Output(conn.GetHTTPVersion().c_str()); + conn.Output(" 200 OK\r\n"); + conn.Output("Content-Type: application/json\r\n\r\n"); + conn.Output(_reply.c_str()); + } + }; + + +public: + HTTPStatus(std::string reply) + : Fast_HTTPServer(0), _reply(reply), _fail(false) + { + Start(); + }; + HTTPStatus(bool fail) + : Fast_HTTPServer(0), _reply(""), _fail(fail) + { + Start(); + }; + + int getListenPort() { return Fast_HTTPServer::getListenPort(); } + + ~HTTPStatus() { + Stop(); + }; +}; + +class Status { +public: + ConfigStatus::Flags flags; + std::unique_ptr<ConfigStatus> status; + + Status(int httpport) : flags() { + flags.verbose = true; + ConfigSet set; + ConfigContext::SP ctx(new ConfigContext(set)); + cloud::config::ModelConfigBuilder builder; + + cloud::config::ModelConfigBuilder::Hosts::Services::Ports port; + port.number = httpport; + port.tags = "http state"; + + cloud::config::ModelConfigBuilder::Hosts::Services service; + service.name = "qrserver"; + service.type = "qrserver"; + service.configid = "qrserver/cluster.default"; + service.clustertype = "qrserver"; + service.clustername = "default"; + service.ports.push_back(port); + + cloud::config::ModelConfigBuilder::Hosts host; + host.services.push_back(service); + host.name = "localhost"; + + builder.hosts.push_back(host); + + set.addBuilder("admin/model", &builder); + config::ConfigUri uri("admin/model", ctx); + std::unique_ptr<ConfigStatus> s(new ConfigStatus(flags, uri)); + status = std::move(s); + }; + + ~Status() { + }; +}; + +TEST_FF("all ok", HTTPStatus(std::string("{\"config\": { \"all\": { \"generation\": 1 } }}")), Status(f1.getListenPort())) { + ASSERT_EQUAL(0, f2.status->action()); +} + +TEST_FF("generation too old", HTTPStatus(std::string("{\"config\": { \"all\": { \"generation\": 0 } }}")), Status(f1.getListenPort())) { + ASSERT_EQUAL(1, f2.status->action()); +} + +TEST_FF("bad json", HTTPStatus(std::string("{")), Status(f1.getListenPort())) { + ASSERT_EQUAL(1, f2.status->action()); +} + +TEST_FF("http failure", HTTPStatus(true), Status(f1.getListenPort())) { + ASSERT_EQUAL(1, f2.status->action()); +} + +TEST_MAIN() { TEST_RUN_ALL(); } |