Browse Source

[3259] Parameterise hooks configuration syntax tests

Modify the tests of the hooks-libraries syntax to allow testing
of both the old and the new syntax.
Stephen Morris 9 years ago
parent
commit
ab26597d78
1 changed files with 136 additions and 36 deletions
  1. 136 36
      src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc

+ 136 - 36
src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc

@@ -822,28 +822,27 @@ TEST_F(ParseConfigTest, optionDataMinimalWithOptionDef) {
 
 }
 
-};  // Anonymous namespace
-
 /// The next set of tests check basic operation of the HooksLibrariesParser.
 
-
 // Utility function for setting up the "hooks-libraries" configuration.
 //
 // Returns a hooks-libraries configuration element that contains zero to
-// three libraries, depending on what arguments are supplied.
+// four libraries, depending on what arguments are supplied.
+//
+// This function uses the old syntax (Kea 0.9.1 and 0.9.2)
 std::string
 setHooksLibrariesConfig(const char* lib1 = NULL, const char* lib2 = NULL,
                         const char* lib3 = NULL) {
-    const std::string quote("\"");
-    const std::string comma_space(", ");
+    const string quote("\"");
+    const string comma_space(", ");
 
-    std::string config = std::string("{ \"hooks-libraries\": [");
+    string config = string("{ \"hooks-libraries\": [");
     if (lib1 != NULL) {
-        config += (quote + std::string(lib1) + quote);
+        config += (quote + string(lib1) + quote);
         if (lib2 != NULL) {
-            config += (comma_space + quote + std::string(lib2) + quote);
+            config += (comma_space + quote + string(lib2) + quote);
             if (lib3 != NULL) {
-                config += (comma_space + quote + std::string(lib3) + quote);
+                config += (comma_space + quote + string(lib3) + quote);
             }
         }
     }
@@ -852,14 +851,110 @@ setHooksLibrariesConfig(const char* lib1 = NULL, const char* lib2 = NULL,
     return (config);
 }
 
+// Set up four libraries using the new configuration syntax.  As the new syntax
+// syntax allows for parameters, each library will have a differing sets of
+// parameters:
+//
+// lib1 - No parameters
+// lib2 - Empty parameters statement
+// lib3 - Valid parameters
+std::string
+setHooksLibrariesNewConfig(const char* lib1 = NULL, const char* lib2 = NULL,
+                           const char* lib3 = NULL) {
+    const string lbrace("{");
+    const string rbrace("}");
+    const string quote("\"");
+    const string comma_space(", ");
+    const string library("\"library\": ");
+    const string parameters("\"parameters\": ");
+
+    string config = string("{ \"hooks-libraries\": [");
+    if (lib1 != NULL) {
+        // Library 1 has no parameters
+        config += lbrace;
+        config += library + quote + std::string(lib1) + quote;
+        config += rbrace;
+
+        if (lib2 != NULL) {
+            // Library 2 has an empty parameters statement
+            config += comma_space + lbrace;
+            config += library + quote + std::string(lib2) + quote + comma_space;
+            config += string("\"parameters\": {}");
+            config += rbrace;
+
+            if (lib3 != NULL) {
+                // Library 3 has valid parameters
+                config += comma_space + lbrace;
+                config += library + quote + std::string(lib3) + quote + comma_space;
+                config += string("\"parameters\": {");
+                config += string("    \"svalue\": \"string value\", ");
+                config += string("    \"ivalue\": 42, ");     // Integer value
+                config += string("    \"bvalue\": true");     // Boolean value
+                config += string("}");
+                config += rbrace;
+            }
+        }
+    }
+    config += std::string("] }");
+
+    return (config);
+}
+
+// The tests of both syntaxes are more or less identical (there is an extra
+// test for invalid parameters with the new syntax).  The following class allows
+// the configuration for the tests to be generated by either the old or new
+// syntax.
+//
+// The type of the generator function is a template parameter to a Gtest
+// parent class.  This class itself is instantiated multiple times with
+// the INSTANTIATE_TEST_CASE_P macro, where a value of the templated parameter
+// is made available via the Gtest parent class's GetParam() method.
+//
+// This class stores the address of the configuration generation function and
+// uses it to obtain the configuration string when needed.
+
+class HooksParseConfigTest : public ParseConfigTest,
+    public ::testing::WithParamInterface<
+        std::string (*)(const char*, const char*, const char*)
+    > {
+
+public:
+    // Virtual desstructor: needed as the parent has a virtual destructor.
+    virtual ~HooksParseConfigTest() {}
+
+    // SetUp - stiore the configuration generation function
+    //
+    // This follows the example in the Gtest documentation, where the generator
+    // function is set in SetUp rather than the class constructor.
+    virtual void SetUp() {
+        generator_ = GetParam();
+    };
+
+    // createConfig - create Configuration String
+    //
+    // Uses the stored generator function to generate a configuration string
+    // based on the number of libraries requested.
+    std::string
+    createConfig(const char* lib1 = NULL, const char* lib2 = NULL,
+		         const char* lib3 = NULL) {
+       return ((*generator_)(lib1, lib2, lib3));
+    }
+
+    // Pointer to the configuration string generator function.
+    std::string (*generator_)(const char* lib1, const char* lib2,
+                              const char* lib3);
+};
+
+};  // Anonymous namespace
+
 // hooks-libraries element that does not contain anything.
-TEST_F(ParseConfigTest, noHooksLibraries) {
+TEST_P(HooksParseConfigTest, noHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Create an empty hooks-libraries configuration element.
-    const string config = setHooksLibrariesConfig();
+    const string config = createConfig();
 
     // Verify that the configuration string parses.
     const int rcode = parseConfiguration(config);
@@ -878,13 +973,13 @@ TEST_F(ParseConfigTest, noHooksLibraries) {
 }
 
 // hooks-libraries element that contains a single library.
-TEST_F(ParseConfigTest, oneHooksLibrary) {
+TEST_P(HooksParseConfigTest, oneHooksLibrary) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration with hooks-libraries set to a single library.
-    const string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1);
+    const string config = createConfig(CALLOUT_LIBRARY_1);
 
     // Verify that the configuration string parses.
     const int rcode = parseConfiguration(config);
@@ -905,14 +1000,14 @@ TEST_F(ParseConfigTest, oneHooksLibrary) {
 }
 
 // hooks-libraries element that contains two libraries
-TEST_F(ParseConfigTest, twoHooksLibraries) {
+TEST_P(HooksParseConfigTest, twoHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration with hooks-libraries set to two libraries.
-    const string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
-                                                  CALLOUT_LIBRARY_2);
+    const string config = createConfig(CALLOUT_LIBRARY_1,
+                                       CALLOUT_LIBRARY_2);
 
     // Verify that the configuration string parses.
     const int rcode = parseConfiguration(config);
@@ -935,14 +1030,14 @@ TEST_F(ParseConfigTest, twoHooksLibraries) {
 }
 
 // Configure with two libraries, then reconfigure with the same libraries.
-TEST_F(ParseConfigTest, reconfigureSameHooksLibraries) {
+TEST_P(HooksParseConfigTest, reconfigureSameHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration with hooks-libraries set to two libraries.
-    const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
-                                                       CALLOUT_LIBRARY_2);
+    const std::string config = createConfig(CALLOUT_LIBRARY_1,
+                                            CALLOUT_LIBRARY_2);
 
     // Verify that the configuration string parses. The twoHooksLibraries
     // test shows that the list will be as expected.
@@ -976,14 +1071,14 @@ TEST_F(ParseConfigTest, reconfigureSameHooksLibraries) {
 
 // Configure the hooks with two libraries, then reconfigure with the same
 // libraries, but in reverse order.
-TEST_F(ParseConfigTest, reconfigureReverseHooksLibraries) {
+TEST_P(HooksParseConfigTest, reconfigureReverseHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration with hooks-libraries set to two libraries.
-    std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
-                                                 CALLOUT_LIBRARY_2);
+    std::string config = createConfig(CALLOUT_LIBRARY_1,
+                                      CALLOUT_LIBRARY_2);
 
     // Verify that the configuration string parses. The twoHooksLibraries
     // test shows that the list will be as expected.
@@ -994,7 +1089,7 @@ TEST_F(ParseConfigTest, reconfigureReverseHooksLibraries) {
     // libraries and that they loaded correctly.
 
     // Parse the reversed set of libraries.
-    config = setHooksLibrariesConfig(CALLOUT_LIBRARY_2, CALLOUT_LIBRARY_1);
+    config = createConfig(CALLOUT_LIBRARY_2, CALLOUT_LIBRARY_1);
     rcode = parseConfiguration(config);
     ASSERT_TRUE(rcode == 0) << error_text_;
 
@@ -1016,14 +1111,14 @@ TEST_F(ParseConfigTest, reconfigureReverseHooksLibraries) {
 
 // Configure the hooks with two libraries, then reconfigure with
 // no libraries.
-TEST_F(ParseConfigTest, reconfigureZeroHooksLibraries) {
+TEST_P(HooksParseConfigTest, reconfigureZeroHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration with hooks-libraries set to two libraries.
-    std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
-                                                 CALLOUT_LIBRARY_2);
+    std::string config = createConfig(CALLOUT_LIBRARY_1,
+                                      CALLOUT_LIBRARY_2);
 
     // Verify that the configuration string parses.
     int rcode = parseConfiguration(config);
@@ -1033,7 +1128,7 @@ TEST_F(ParseConfigTest, reconfigureZeroHooksLibraries) {
     // libraries and that they loaded correctly.
 
     // Parse the string again, this time without any libraries.
-    config = setHooksLibrariesConfig();
+    config = createConfig();
     rcode = parseConfiguration(config);
     ASSERT_TRUE(rcode == 0) << error_text_;
 
@@ -1050,16 +1145,16 @@ TEST_F(ParseConfigTest, reconfigureZeroHooksLibraries) {
 }
 
 // Check with a set of libraries, some of which are invalid.
-TEST_F(ParseConfigTest, invalidHooksLibraries) {
+TEST_P(HooksParseConfigTest, invalidHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configuration string.  This contains an invalid library which should
     // trigger an error in the "build" stage.
-    const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
-                                                       NOT_PRESENT_LIBRARY,
-                                                       CALLOUT_LIBRARY_2);
+    const std::string config = createConfig(CALLOUT_LIBRARY_1,
+                                            NOT_PRESENT_LIBRARY,
+                                            CALLOUT_LIBRARY_2);
 
     // Verify that the configuration fails to parse. (Syntactically it's OK,
     // but the library is invalid).
@@ -1087,13 +1182,13 @@ TEST_F(ParseConfigTest, invalidHooksLibraries) {
 }
 
 // Check that trying to reconfigure with an invalid set of libraries fails.
-TEST_F(ParseConfigTest, reconfigureInvalidHooksLibraries) {
+TEST_P(HooksParseConfigTest, reconfigureInvalidHooksLibraries) {
     // Check that no libraries are currently loaded
     vector<string> hooks_libraries = HooksManager::getLibraryNames();
     EXPECT_TRUE(hooks_libraries.empty());
 
     // Configure with a single library.
-    std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1);
+    std::string config = createConfig(CALLOUT_LIBRARY_1);
     int rcode = parseConfiguration(config);
     ASSERT_TRUE(rcode == 0) << error_text_;
 
@@ -1102,8 +1197,8 @@ TEST_F(ParseConfigTest, reconfigureInvalidHooksLibraries) {
 
     // Configuration string.  This contains an invalid library which should
     // trigger an error in the "build" stage.
-    config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1, NOT_PRESENT_LIBRARY,
-                                     CALLOUT_LIBRARY_2);
+    config = createConfig(CALLOUT_LIBRARY_1, NOT_PRESENT_LIBRARY,
+                          CALLOUT_LIBRARY_2);
 
     // Verify that the configuration fails to parse. (Syntactically it's OK,
     // but the library is invalid).
@@ -1133,6 +1228,11 @@ TEST_F(ParseConfigTest, reconfigureInvalidHooksLibraries) {
     EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
 }
 
+INSTANTIATE_TEST_CASE_P(OldSyntax, HooksParseConfigTest,
+                         ::testing::Values(setHooksLibrariesConfig));
+INSTANTIATE_TEST_CASE_P(NewSyntax, HooksParseConfigTest,
+                         ::testing::Values(setHooksLibrariesNewConfig));
+
 /// @brief Checks that a valid, enabled D2 client configuration works correctly.
 TEST_F(ParseConfigTest, validD2Config) {