]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5495] Added tests trac5491_base
authorFrancis Dupont <fdupont@isc.org>
Tue, 2 Jan 2018 13:33:17 +0000 (14:33 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 2 Jan 2018 13:33:17 +0000 (14:33 +0100)
doc/examples/agent/simple.json
doc/examples/ddns/sample1.json
src/bin/agent/tests/ca_cfg_mgr_unittests.cc
src/bin/agent/tests/get_config_unittest.cc
src/bin/agent/tests/testdata/get_config.json
src/bin/d2/tests/d2_cfg_mgr_unittests.cc
src/bin/d2/tests/get_config_unittest.cc
src/bin/d2/tests/testdata/get_config.json

index de3932ce88620c0295748ba57a3253b01e6c5497..15be619f690d48c2c87033f2174ee2d17cba47cd 100644 (file)
         // servers. At this time the only supported socket type is "unix".
         // Make sure that the Agent and respective servers configuration
         // matches exactly, otherwise they won't be able to communicate.
+        // One extra feature that requires some explanation is
+        // user-context. This is a structure that you can define at
+        // global scope, in control sockets and others. It is parsed by
+        // Kea, but not used directly.  It is intended to keep anything
+        // you may want to put there - comments, extra designations,
+        // floor or department names etc. These structures will be made
+        // available to Kea hooks. A comment entry is translated into a
+        // user-context with a "comment" property so you can include
+        // comments inside the configuration itself.
         "control-sockets":
         {
             // This is how the Agent can communicate with the DHCPv4 server.
             "dhcp4":
             {
+                "comment": "socket to DHCP4 server",
                 "socket-type": "unix",
                 "socket-name": "/path/to/the/unix/socket-v4"
             },
@@ -38,7 +48,8 @@
             "d2":
             {
                 "socket-type": "unix",
-                "socket-name": "/path/to/the/unix/socket-d2"
+                "socket-name": "/path/to/the/unix/socket-d2",
+                "user-context": { "in-use": false }
             }
         },
 
index 7f6cea0872f18bd08b73170607804206d8395541..bd283bf0a0a69d238c6d81fa42d091a6b08be3f9 100644 (file)
     "port": 53001,
     "dns-server-timeout" : 1000,
 
+// One extra feature that requires some explanation is
+// user-context. This is a structure that you can define at global scope,
+// in ddns domain, dns server, tsig key and others. It is parsed by
+// Kea, but not used directly.  It is intended to keep anything you
+// may want to put there - comments, extra designations, floor or
+// department names etc.
+// A comment entry is translated into a user-context with a "comment"
+// property so you can include comments inside the configuration itself.
+
+    "user-context": { "version": 1 },
+
+
 //
 // ----------------- Forward DDNS  ------------------
 //
@@ -36,6 +48,7 @@
         [
 //           DdnsDomain for zone "four.example.com."
             {
+                "comment": "DdnsDomain example",
                 "name": "four.example.com.",
                 "key-name": "d2.md5.key",
                 "dns-servers":
index 7bd745be1dd62dcf1e4137c9082a073f33a0fbb9..d4e3ab10c1d612caa3052a45f87e03f569d351ad 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -254,6 +254,23 @@ const char* AGENT_CONFIGS[] = {
     "            \"socket-name\": \"/tmp/socket-v6\"\n"
     "        }\n"
     "    }\n"
+    "}",
+
+    // Configuration 7: http and 2 sockets with user contexts and comments
+    "{\n"
+    "    \"user-context\": { \"comment\": \"Indirect comment\" },\n"
+    "    \"http-host\": \"betelgeuse\",\n"
+    "    \"http-port\": 8001,\n"
+    "    \"control-sockets\": {\n"
+    "        \"dhcp4\": {\n"
+    "            \"comment\": \"dhcp4 socket\",\n"
+    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "        },\n"
+    "        \"dhcp6\": {\n"
+    "            \"socket-name\": \"/tmp/socket-v6\",\n"
+    "            \"user-context\": { \"version\": 1 }\n"
+    "        }\n"
+    "   }\n"
     "}"
 };
 
@@ -395,5 +412,40 @@ TEST_F(AgentParserTest, configParseHooks) {
     EXPECT_EQ("{ \"param1\": \"foo\" }", libs[0].second->str());
 }
 
+// This test checks comments.
+TEST_F(AgentParserTest, comments) {
+    configParse(AGENT_CONFIGS[7], 0);
+    CtrlAgentCfgContextPtr agent_ctx = cfg_mgr_.getCtrlAgentCfgContext();
+    ASSERT_TRUE(agent_ctx);
+
+    // Check global user context.
+    ConstElementPtr ctx = agent_ctx->getContext();
+    ASSERT_TRUE(ctx);
+    ASSERT_EQ(1, ctx->size());
+    ASSERT_TRUE(ctx->get("comment"));
+    EXPECT_EQ("\"Indirect comment\"", ctx->get("comment")->str());
+
+    // There is a DHCP4 control socket.
+    ConstElementPtr socket4 = agent_ctx->getControlSocketInfo("dhcp4");
+    ASSERT_TRUE(socket4);
+
+    // Check DHCP4 control socket user context.
+    ConstElementPtr ctx4 = socket4->get("user-context");
+    ASSERT_TRUE(ctx4);
+    ASSERT_EQ(1, ctx4->size());
+    ASSERT_TRUE(ctx4->get("comment"));
+    EXPECT_EQ("\"dhcp4 socket\"", ctx4->get("comment")->str());
+
+    // There is a DHCP6 control socket.
+    ConstElementPtr socket6 = agent_ctx->getControlSocketInfo("dhcp6");
+    ASSERT_TRUE(socket6);
+
+    // Check DHCP6 control socket user context.
+    ConstElementPtr ctx6 = socket6->get("user-context");
+    ASSERT_TRUE(ctx6);
+    ASSERT_EQ(1, ctx6->size());
+    ASSERT_TRUE(ctx6->get("version"));
+    EXPECT_EQ("1", ctx6->get("version")->str());
+}
 
 }; // end of anonymous namespace
index 0b24ce29b36a40621242661078b8f886fae4fd4f..f20343c6062f4b074f363cf94ed0ad8efa202765 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,6 +8,7 @@
 
 #include <cc/data.h>
 #include <cc/command_interpreter.h>
+#include <testutils/user_context_utils.h>
 #include <process/testutils/d_test_stubs.h>
 #include <agent/ca_cfg_mgr.h>
 #include <agent/parser_context.h>
@@ -26,6 +27,7 @@ using namespace isc::agent;
 using namespace isc::config;
 using namespace isc::data;
 using namespace isc::process;
+using namespace isc::test;
 
 namespace {
 
@@ -255,15 +257,18 @@ TEST_F(CtrlAgentGetCfgTest, simple) {
         prettyPrint(unparsed, std::cerr, 0, 4);
         std::cerr << "\n";
     } else {
-        ConstElementPtr json;
-        ASSERT_NO_THROW(json = parseAGENT(expected, true));
+        ElementPtr jsond;
+        ASSERT_NO_THROW(jsond = parseAGENT(expected, true));
+        ElementPtr jsonj;
+        ASSERT_NO_THROW(jsonj = parseJSON(expected));
+        EXPECT_TRUE(isEquivalent(jsond, moveComments(jsonj)));
         ConstElementPtr ca;
-        ASSERT_NO_THROW(ca = json->get("Control-agent"));
+        ASSERT_NO_THROW(ca = jsonj->get("Control-agent"));
         ASSERT_TRUE(ca);
         pathReplacer(ca);
-        EXPECT_TRUE(isEquivalent(unparsed, json));
+        EXPECT_TRUE(isEquivalent(unparsed, jsonj));
         std::string current = prettyPrint(unparsed, 0, 4);
-        std::string expected2 = prettyPrint(json, 0, 4);
+        std::string expected2 = prettyPrint(jsonj, 0, 4);
         EXPECT_EQ(expected2, current);
         if (expected2 != current) {
             expected = current + "\n";
index ba56901abb5b3e622f2d04fc6cab8ea7db9b0adf..5b14d26005effb59d41f45cb4df6ff3535292ccb 100644 (file)
@@ -3,9 +3,13 @@
         "control-sockets": {
             "d2": {
                 "socket-name": "/path/to/the/unix/socket-d2",
-                "socket-type": "unix"
+                "socket-type": "unix",
+                "user-context": {
+                    "in-use": false
+                }
             },
             "dhcp4": {
+                "comment": "socket to DHCP4 server",
                 "socket-name": "/path/to/the/unix/socket-v4",
                 "socket-type": "unix"
             },
index 50eee04efac84c59628a64ad2478bc7093c45e18..eaf9ba37aff376229717e4a5d1f40e588598ef80 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -935,5 +935,92 @@ TEST_F(D2CfgMgrTest, configPermutations) {
     }
 }
 
+/// @brief Tests comments.
+TEST_F(D2CfgMgrTest, comments) {
+    std::string config = "{ "
+                        "\"comment\": \"D2 config\" , "
+                        "\"ip-address\" : \"192.168.1.33\" , "
+                        "\"port\" : 88 , "
+                        "\"tsig-keys\": ["
+                        "{"
+                        "  \"user-context\": { "
+                        "    \"comment\": \"Indirect comment\" } , "
+                        "  \"name\": \"d2_key.example.com\" , "
+                        "  \"algorithm\": \"hmac-md5\" , "
+                        "  \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
+                        "}"
+                        "],"
+                        "\"forward-ddns\" : {"
+                        "\"ddns-domains\": [ "
+                        "{ \"comment\": \"A DDNS domain\" , "
+                        "  \"name\": \"example.com\" , "
+                        "  \"key-name\": \"d2_key.example.com\" , "
+                        "  \"dns-servers\" : [ "
+                        "  { \"ip-address\": \"127.0.0.1\" , "
+                        "    \"user-context\": { \"version\": 1 } } "
+                        "  ] } "
+                        "] } }";
+
+    // Should parse without error.
+    RUN_CONFIG_OK(config);
+
+    // Check the D2 context.
+    D2CfgContextPtr d2_context;
+    ASSERT_NO_THROW(d2_context = cfg_mgr_->getD2CfgContext());
+    ASSERT_TRUE(d2_context);
+
+    // Check global user context.
+    ConstElementPtr ctx = d2_context->getContext();
+    ASSERT_TRUE(ctx);
+    ASSERT_EQ(1, ctx->size());
+    ASSERT_TRUE(ctx->get("comment"));
+    EXPECT_EQ("\"D2 config\"", ctx->get("comment")->str());
+
+    // Check TSIG keys.
+    TSIGKeyInfoMapPtr keys = d2_context->getKeys();
+    ASSERT_TRUE(keys);
+    ASSERT_EQ(1, keys->size());
+
+    // Check the TSIG key.
+    TSIGKeyInfoMap::iterator gotkey = keys->find("d2_key.example.com");
+    ASSERT_TRUE(gotkey != keys->end());
+    TSIGKeyInfoPtr key = gotkey->second;
+    ASSERT_TRUE(key);
+
+    // Check the TSIG key user context.
+    ConstElementPtr key_ctx = key->getContext();
+    ASSERT_TRUE(key_ctx);
+    ASSERT_EQ(1, key_ctx->size());
+    ASSERT_TRUE(key_ctx->get("comment"));
+    EXPECT_EQ("\"Indirect comment\"", key_ctx->get("comment")->str());
+
+    // Check the forward manager.
+    DdnsDomainListMgrPtr mgr = d2_context->getForwardMgr();
+    ASSERT_TRUE(mgr);
+    EXPECT_EQ("forward-ddns", mgr->getName());
+    DdnsDomainMapPtr domains = mgr->getDomains();
+    ASSERT_TRUE(domains);
+    ASSERT_EQ(1, domains->size());
+
+    // Check the DDNS domain.
+    DdnsDomainMap::iterator gotdns = domains->find("example.com");
+    ASSERT_TRUE(gotdns != domains->end());
+    DdnsDomainPtr domain = gotdns->second;
+    ASSERT_TRUE(domain);
+
+    // Check the DNS server.
+    DnsServerInfoStoragePtr servers = domain->getServers();
+    ASSERT_TRUE(servers);
+    ASSERT_EQ(1, servers->size());
+    DnsServerInfoPtr server = (*servers)[0];
+    ASSERT_TRUE(server);
+
+    // Check the DNS server user context.
+    ConstElementPtr srv_ctx = server->getContext();
+    ASSERT_TRUE(srv_ctx);
+    ASSERT_EQ(1, srv_ctx->size());
+    ASSERT_TRUE(srv_ctx->get("version"));
+    EXPECT_EQ("1", srv_ctx->get("version")->str());
+}
 
 } // end of anonymous namespace
index 0cded96a08569c098ac1e7f52defa69aa4a0487b..008af6b7e7e9bb4d29c0be0c74742335323c3efa 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,6 +8,7 @@
 
 #include <cc/data.h>
 #include <cc/command_interpreter.h>
+#include <testutils/user_context_utils.h>
 #include <process/testutils/d_test_stubs.h>
 #include <d2/d2_config.h>
 #include <d2/d2_cfg_mgr.h>
@@ -25,6 +26,7 @@ using namespace isc::config;
 using namespace isc::d2;
 using namespace isc::data;
 using namespace isc::process;
+using namespace isc::test;
 
 namespace {
 
@@ -238,9 +240,12 @@ TEST_F(D2GetConfigTest, sample1) {
         prettyPrint(unparsed, std::cerr, 0, 4);
         std::cerr << "\n";
     } else {
-        ConstElementPtr json;
-        ASSERT_NO_THROW(json = parseDHCPDDNS(expected, true));
-        EXPECT_TRUE(isEquivalent(unparsed, json));
+        ElementPtr jsond;
+        ASSERT_NO_THROW(jsond = parseDHCPDDNS(expected, true));
+        ElementPtr jsonj;
+        ASSERT_NO_THROW(jsonj = parseJSON(expected));
+        EXPECT_TRUE(isEquivalent(jsond, moveComments(jsonj)));
+        EXPECT_TRUE(isEquivalent(unparsed, jsonj));
         std::string current = prettyPrint(unparsed, 0, 4) + "\n";
         EXPECT_EQ(expected, current);
         if (expected != current) {
index 89e7326b4e506515bd7b9e1335bf067e81375aab..b040c058edf54ce31f631f0da07102eb4cbb3414 100644 (file)
@@ -4,6 +4,7 @@
         "forward-ddns": {
             "ddns-domains": [
                 {
+                    "comment": "DdnsDomain example",
                     "dns-servers": [
                         {
                             "hostname": "",
@@ -69,6 +70,9 @@
                 "name": "d2.sha512.key",
                 "secret": "/4wklkm04jeH4anx2MKGJLcya+ZLHldL5d6mK+4q6UXQP7KJ9mS2QG29hh0SJR4LA0ikxNJTUMvir42gLx6fGQ=="
             }
-        ]
+        ],
+        "user-context": {
+            "version": 1
+        }
     }
 }