]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1848] pgsql_cb_dhcp6_mgr_unittest.cc added
authorTomek Mrugalski <tomek@isc.org>
Thu, 1 Jul 2021 14:26:03 +0000 (16:26 +0200)
committerTomek Mrugalski <tomek@isc.org>
Wed, 17 Nov 2021 14:35:18 +0000 (15:35 +0100)
src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_dhcp6_mgr_unittest.cc [new file with mode: 0644]

diff --git a/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_dhcp6_mgr_unittest.cc b/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_dhcp6_mgr_unittest.cc
new file mode 100644 (file)
index 0000000..9345bd3
--- /dev/null
@@ -0,0 +1,88 @@
+// Copyright (C) 2021 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
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <cc/stamped_value.h>
+#include <dhcpsrv/config_backend_dhcp6_mgr.h>
+#include <pgsql_cb_dhcp6.h>
+#include <pgsql/testutils/pgsql_schema.h>
+#include <dhcpsrv/testutils/generic_backend_unittest.h>
+#include <boost/shared_ptr.hpp>
+#include <gtest/gtest.h>
+
+using namespace isc::data;
+using namespace isc::dhcp;
+using namespace isc::dhcp::test;
+using namespace isc::db;
+using namespace isc::db::test;
+
+namespace {
+
+/// @brief Test fixture class for @c PgSqlConfigBackendDHCPv6Mgr.
+class PgSqlConfigBackendDHCPv6MgrTest : public GenericBackendTest {
+public:
+    /// @brief Constructor.
+    PgSqlConfigBackendDHCPv6MgrTest() {
+        // Recreate a fresh mgr.
+        ConfigBackendDHCPv6Mgr::create();
+
+        // Ensure we have the proper schema with no transient data.
+        createPgSQLSchema();
+    }
+
+    /// @brief Destructor.
+    virtual ~PgSqlConfigBackendDHCPv6MgrTest() {
+        // Destroy the mgr.
+        ConfigBackendDHCPv6Mgr::destroy();
+
+        // If data wipe enabled, delete transient data otherwise destroy the schema.
+        destroyPgSQLSchema();
+    }
+};
+
+// This test verifies that Postgres backend can be registered with and
+// unregistered from the Config Backend Manager.
+TEST_F(PgSqlConfigBackendDHCPv6MgrTest, factoryRegistration) {
+
+    // Get the mgr singleton.
+    ConfigBackendDHCPv6Mgr& mgr = ConfigBackendDHCPv6Mgr::instance();
+
+    // With no factory registered, attempting to add a Postgres db should fail.
+    ASSERT_THROW(mgr.addBackend(validPgSQLConnectionString()), InvalidType);
+
+    // Now we'll register the Postgres factory.
+    ASSERT_NO_THROW(PgSqlConfigBackendDHCPv6::registerBackendType());
+
+    // With the factory registered, attempting to add a Postgres db should succeed.
+    ASSERT_NO_THROW(mgr.addBackend(validPgSQLConnectionString()));
+
+    // Create a Postgres backend selector for convenience.
+    BackendSelector pgsql(BackendSelector::Type::PGSQL);
+
+    // Should be able to create a global parameter.
+    StampedValuePtr server_tag = StampedValue::create("server-tag", "whale");
+    ASSERT_NO_THROW(mgr.getPool()->createUpdateGlobalParameter6(pgsql, ServerSelector::ALL(),
+                                                                server_tag));
+    // Verify parameter can be fetched.
+    server_tag.reset();
+    ASSERT_NO_THROW(server_tag = mgr.getPool()->getGlobalParameter6(pgsql, ServerSelector::ALL(),
+                                                                    "server-tag"));
+    ASSERT_TRUE(server_tag);
+    EXPECT_EQ("server-tag", server_tag->getName());
+    EXPECT_EQ("whale", server_tag->getValue());
+
+    // Now we'll unregister PgSQL.
+    ASSERT_NO_THROW(PgSqlConfigBackendDHCPv6::unregisterBackendType());
+
+    // With no factory registered, attempting to add a Postgres db should fail.
+    ASSERT_THROW(mgr.addBackend(validPgSQLConnectionString()), InvalidType);
+
+    // Attempting to read the global parameter should fail.
+    ASSERT_THROW(mgr.getPool()->getGlobalParameter6(pgsql, ServerSelector::ALL(), "server-tag"),
+                 NoSuchDatabase);
+}
+
+}