]> granicus.if.org Git - icinga2/commitdiff
Implement more unit tests
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 23 Nov 2014 11:35:13 +0000 (12:35 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 23 Nov 2014 11:35:13 +0000 (12:35 +0100)
refs #7805

lib/cli/CMakeLists.txt
lib/cli/daemoncommand.cpp
lib/cli/replcommand.cpp [new file with mode: 0644]
lib/cli/replcommand.hpp [new file with mode: 0644]
lib/config/config_parser.yy
test/CMakeLists.txt
test/config-ops.cpp

index 121effb76d2186e99d72c4e7034120e8ba3c3b05..4eb5d72869322d474822989eecae6cf61bdb8a8f 100644 (file)
@@ -24,6 +24,7 @@ set(cli_SOURCES
   objectlistcommand.cpp
   pkinewcacommand.cpp pkinewcertcommand.cpp pkisigncsrcommand.cpp pkirequestcommand.cpp pkisavecertcommand.cpp pkiticketcommand.cpp
   pkiutility.cpp
+  replcommand.cpp
   repositoryclearchangescommand.cpp repositorycommitcommand.cpp repositoryobjectcommand.cpp repositoryutility.cpp
   variablegetcommand.cpp variablelistcommand.cpp variableutility.cpp
 )
index 7193d2d53418e041719adde3b84523f59b6ffd6c..c269214e91cfccd6e4ead3e9493fbf50c01119b6 100644 (file)
@@ -106,12 +106,14 @@ static bool LoadConfigFiles(const boost::program_options::variables_map& vm, con
        if (vm.count("config") > 0) {
                BOOST_FOREACH(const String& configPath, vm["config"].as<std::vector<std::string> >()) {
                        Expression *expression = ConfigCompiler::CompileFile(configPath);
-                       ExecuteExpression(expression);
+                       if (expression)
+                               ExecuteExpression(expression);
                        delete expression;
                }
        } else if (!vm.count("no-config")) {
                Expression *expression = ConfigCompiler::CompileFile(Application::GetSysconfDir() + "/icinga2/icinga2.conf");
-               ExecuteExpression(expression);
+               if (expression)
+                       ExecuteExpression(expression);
                delete expression;
        }
 
@@ -128,7 +130,8 @@ static bool LoadConfigFiles(const boost::program_options::variables_map& vm, con
        String name, fragment;
        BOOST_FOREACH(boost::tie(name, fragment), ConfigFragmentRegistry::GetInstance()->GetItems()) {
                Expression *expression = ConfigCompiler::CompileText(name, fragment);
-               ExecuteExpression(expression);
+               if (expression)
+                       ExecuteExpression(expression);
                delete expression;
        }
 
diff --git a/lib/cli/replcommand.cpp b/lib/cli/replcommand.cpp
new file mode 100644 (file)
index 0000000..e2f4950
--- /dev/null
@@ -0,0 +1,92 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "cli/replcommand.hpp"
+#include "config/configcompiler.hpp"
+#include "config/configcompilercontext.hpp"
+#include "base/json.hpp"
+#include <iostream>
+
+using namespace icinga;
+namespace po = boost::program_options;
+
+REGISTER_CLICOMMAND("repl", ReplCommand);
+
+String ReplCommand::GetDescription(void) const
+{
+       return "Interprets Icinga script expressions.";
+}
+
+String ReplCommand::GetShortDescription(void) const
+{
+       return "Icinga REPL console";
+}
+
+bool ReplCommand::IsHidden(void) const
+{
+       return true;
+}
+
+void ReplCommand::InitParameters(boost::program_options::options_description& visibleDesc,
+    boost::program_options::options_description& hiddenDesc) const
+{
+}
+
+/**
+ * The entry point for the "repl" CLI command.
+ *
+ * @returns An exit status.
+ */
+int ReplCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
+{
+       VMFrame frame;
+
+       while (std::cin.good()) {
+               std::cout << "=> ";
+
+               std::string line;
+               std::getline(std::cin, line);
+
+               Expression *expr;
+
+               try {
+                       ConfigCompilerContext::GetInstance()->Reset();
+
+                       expr = ConfigCompiler::CompileText("<console>", line);
+
+                       BOOST_FOREACH(const ConfigCompilerMessage& message, ConfigCompilerContext::GetInstance()->GetMessages()) {
+                               std::cout << (message.Error ? "Error" : "Warning") << ": " << message.Text << "\n";
+                       }
+
+                       if (expr) {
+                               Value result = expr->Evaluate(frame);
+                               if (!result.IsObject() || result.IsObjectType<Array>() || result.IsObjectType<Dictionary>())
+                                       std::cout << JsonEncode(result) << "\n";
+                               else
+                                       std::cout << result << "\n";
+                       }
+               } catch (const ConfigError& ex) {
+                       std::cout << ex.what() << "\n";
+               } catch (const std::exception& ex) {
+                       std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
+               }
+
+               delete expr;
+       }
+}
diff --git a/lib/cli/replcommand.hpp b/lib/cli/replcommand.hpp
new file mode 100644 (file)
index 0000000..cbef3df
--- /dev/null
@@ -0,0 +1,48 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#ifndef REPLCOMMAND_H
+#define REPLCOMMAND_H
+
+#include "cli/clicommand.hpp"
+
+namespace icinga
+{
+
+/**
+ * The "repl" CLI command.
+ *
+ * @ingroup cli
+ */
+class ReplCommand : public CLICommand
+{
+public:
+       DECLARE_PTR_TYPEDEFS(ReplCommand);
+
+       virtual String GetDescription(void) const;
+       virtual String GetShortDescription(void) const;
+       virtual bool IsHidden(void) const;
+       virtual void InitParameters(boost::program_options::options_description& visibleDesc,
+           boost::program_options::options_description& hiddenDesc) const;
+       virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
+};
+
+}
+
+#endif /* REPLCOMMAND_H */
index 341a1103531a6e4d4f1f419a03f17d85c58e5b84..9ba840a72ee276b1c47fc01125ae2269cf2bae3b 100644 (file)
@@ -255,8 +255,10 @@ Expression *ConfigCompiler::Compile(void)
        m_Expressions.push(std::vector<Expression *>());
 
        try {
-               if (yyparse(this) != 0)
-                       BOOST_THROW_EXCEPTION(ConfigError("Syntax error"));
+               if (yyparse(this) != 0) {
+                       m_Expressions.pop();
+                       return NULL;
+               }
 
                DictExpression *expr = new DictExpression(m_Expressions.top());
                m_Expressions.pop();
@@ -279,7 +281,8 @@ Expression *ConfigCompiler::Compile(void)
 %}
 
 %%
-statements: statement sep
+statements: newlines
+       | statement sep
        | statements statement sep
        ;
 
index 71fd610abcc2ea81653c43d27923a50c96c1dfdd..8d62ea2ea3db91dbaf0c5ebed3daf5061f86822b 100644 (file)
@@ -85,6 +85,7 @@ add_boost_test(base
         base_value/convert
         base_value/format
        config_ops/simple
+       config_ops/advanced
        icinga_perfdata/simple
        icinga_perfdata/multiple
        icinga_perfdata/uom
index a6c1da77882937b4ea93d4574fe653ca8f35bb97..ae0ecd179395af9806e3c98c862168a48c5c2203 100644 (file)
@@ -30,6 +30,18 @@ BOOST_AUTO_TEST_CASE(simple)
        Expression *expr;
        Dictionary::Ptr dict;
 
+       expr = ConfigCompiler::CompileText("<test>", "");
+       BOOST_CHECK(expr->Evaluate(frame) == Empty);
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "\n3");
+       BOOST_CHECK(expr->Evaluate(frame) == 3);
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "{ 3\n\n5 }");
+       BOOST_CHECK(expr->Evaluate(frame) != Empty);
+       delete expr;
+
        expr = ConfigCompiler::CompileText("<test>", "1 + 3");
        BOOST_CHECK(expr->Evaluate(frame) == 4);
        delete expr;
@@ -90,8 +102,8 @@ BOOST_AUTO_TEST_CASE(simple)
        BOOST_CHECK(expr->Evaluate(frame));
        delete expr;
 
-       expr = ConfigCompiler::CompileText("<test>", "!0 == true");
-       BOOST_CHECK(expr->Evaluate(frame));
+       expr = ConfigCompiler::CompileText("<test>", "~0");
+       BOOST_CHECK(expr->Evaluate(frame) == (double)~(long)0);
        delete expr;
 
        expr = ConfigCompiler::CompileText("<test>", "4 << 8");
@@ -156,6 +168,53 @@ BOOST_AUTO_TEST_CASE(simple)
        BOOST_CHECK(dict->GetLength() == 1);
        BOOST_CHECK(dict->Get("a") == 3);
 
+       expr = ConfigCompiler::CompileText("<test>", "test");
+       BOOST_CHECK_THROW(expr->Evaluate(frame), ConfigError);
+       delete expr;
+}
+
+BOOST_AUTO_TEST_CASE(advanced)
+{
+       VMFrame frame;
+       Expression *expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "regex(\"^Hello\", \"Hello World\")");
+       BOOST_CHECK(expr->Evaluate(frame));
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "\"regex\"(\"^Hello\", \"Hello World\")");
+       BOOST_CHECK(expr->Evaluate(frame));
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "__boost_test()");
+       BOOST_CHECK_THROW(expr->Evaluate(frame), ConfigError);
+       delete expr;
+
+       Object::Ptr self = new Object();
+       VMFrame frame2(self);
+       expr = ConfigCompiler::CompileText("<test>", "this");
+       BOOST_CHECK(expr->Evaluate(frame2) == Value(self));
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "local v = 7; v");
+       BOOST_CHECK(expr->Evaluate(frame));
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "{ a = 3 }.a");
+       BOOST_CHECK(expr->Evaluate(frame) == 3);
+       delete expr;
+
+       expr = ConfigCompiler::CompileText("<test>", "[ 2, 3 ][1]");
+       BOOST_CHECK(expr->Evaluate(frame) == 3);
+       delete expr;
+
+       /* Uncomment this once #7800 is fixed
+       expr = ConfigCompiler::CompileText("<test>", "local v = { a = 3}; v.a");
+       BOOST_CHECK(expr->Evaluate(frame) == 3);
+       delete expr;*/
+
+       expr = ConfigCompiler::CompileText("<test>", "a = 3 b = 3");
+       BOOST_CHECK(expr == NULL);
 }
 
 BOOST_AUTO_TEST_SUITE_END()