Skip to content

Commit

Permalink
tests: add unit tests for new inheritance behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura authored and BareosBot committed Feb 3, 2025
1 parent 6275b64 commit 02770ba
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 1 deletion.
95 changes: 95 additions & 0 deletions core/src/tests/configs/runscript-inheritance.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Director {
Name = bareos-dir
QueryFile = "/tmp/scripts/query.sql"
Password = "dir_password" # Console password
DirPort = 42001
Working Directory = "/tmp"
}

Catalog {
Name = DummyCatalog
dbname = "dummy"
dbuser = "user"
dbpassword = "password"
}

Pool {
Name = "Incremental"
}

Messages {
Name = Standard
}

JobDefs {
Name = "Level1"
Type = Copy
Messages = Standard
Pool = Incremental
RunScript {
Command = "echo 1"
Runs On Client = No
Runs When = Before
}
}

JobDefs {
Name = "Level2"
JobDefs = "Level1"
Type = Copy
Messages = Standard
Pool = Incremental
RunScript {
Command = "echo 2"
Runs On Client = No
Runs When = Before
}
}

Job {
Name = "CopyJob"
Type = Copy
Messages = Standard
Pool = Incremental
}

Job {
Name = "JobWithRunscript"
Type = Copy
Messages = Standard
Pool = Incremental
RunScript {
Command = "echo 3"
Runs On Client = No
Runs When = Before
}
}

Job {
Name = "JobWithLevel1"
Type = Copy
Messages = Standard
Pool = Incremental
JobDefs = Level1
}

Job {
Name = "JobWithLevel2"
Type = Copy
Messages = Standard
Pool = Incremental
JobDefs = Level2
}

Job {
Name = "JobWithRunscriptAndLevel2"
Type = Copy
Messages = Standard
Pool = Incremental
JobDefs = Level2
RunScript {
Command = "echo 3"
Runs On Client = No
Runs When = Before
}
}
101 changes: 100 additions & 1 deletion core/src/tests/test_config_parser_dir.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2019-2024 Bareos GmbH & Co. KG
Copyright (C) 2019-2025 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -18,11 +18,16 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include <algorithm>
#include "dird/reload.h"
#include "lib/runscript.h"
#if defined(HAVE_MINGW)
# include "include/bareos.h"
# include "gtest/gtest.h"
# include "gmock/gmock.h"
#else
# include "gtest/gtest.h"
# include "gmock/gmock.h"
# include "include/bareos.h"
#endif

Expand Down Expand Up @@ -353,4 +358,98 @@ TEST_F(ConfigParser_Dir, CFG_TYPE_TIME)
{
test_config_directive_type(test_CFG_TYPE_TIME);
}

static std::vector<std::string> GetCommands(const alist<RunScript*>* scripts)
{
std::vector<std::string> commands;
for (auto* script : scripts) { commands.push_back(script->command); }

std::sort(std::begin(commands), std::end(commands));

return commands;
}

TEST_F(ConfigParser_Dir, RunScriptInheritance)
{
std::string path_to_config_file
= std::string("configs/runscript-inheritance.conf");
std::unique_ptr<ConfigurationParser> dir_conf{
InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM)};
my_config = dir_conf.get();
ASSERT_TRUE(my_config->ParseConfig());
ASSERT_TRUE(PopulateDefs());

auto jobdef1 = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOBDEFS, "Level1"));
ASSERT_NE(jobdef1, nullptr);
ASSERT_NE(jobdef1->RunScripts, nullptr);
ASSERT_EQ(jobdef1->RunScripts->size(), 1);
std::string command1 = jobdef1->RunScripts->first()->command;
EXPECT_EQ(command1, "echo 1");

auto jobdef2 = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOBDEFS, "Level2"));
ASSERT_NE(jobdef2, nullptr);
ASSERT_NE(jobdef2->RunScripts, nullptr);
ASSERT_EQ(jobdef2->RunScripts->size(), 2);
std::string command2;
{
auto* r1 = jobdef2->RunScripts->first();
auto* r2 = jobdef2->RunScripts->last();

if (r1->command == command1) {
ASSERT_TRUE(r1->from_jobdef);
command2 = r2->command;
} else {
ASSERT_TRUE(r2->from_jobdef);
ASSERT_EQ(r2->command, command1);
command2 = r1->command;
}
}
EXPECT_EQ(command2, "echo 2");

using namespace testing;
auto commands1 = GetCommands(jobdef1->RunScripts);
ASSERT_THAT(commands1, ElementsAre(command1));
auto commands2 = GetCommands(jobdef2->RunScripts);
ASSERT_THAT(commands2, ElementsAre(command1, command2));

std::string command3 = "echo 3";

{
auto job = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOB, "CopyJob"));
ASSERT_NE(job, nullptr);
auto commands = GetCommands(job->RunScripts);
EXPECT_EQ(commands.size(), 0);
}
{
auto job = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOB, "JobWithRunscript"));
ASSERT_NE(job, nullptr);
auto commands = GetCommands(job->RunScripts);
EXPECT_THAT(commands, ElementsAre(command3));
}
{
auto job = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOB, "JobWithLevel1"));
ASSERT_NE(job, nullptr);
auto commands = GetCommands(job->RunScripts);
EXPECT_EQ(commands, commands1);
}
{
auto job = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOB, "JobWithLevel2"));
ASSERT_NE(job, nullptr);
auto commands = GetCommands(job->RunScripts);
EXPECT_EQ(commands, commands2);
}
{
auto job = dynamic_cast<JobResource*>(
my_config->GetResWithName(R_JOB, "JobWithRunscriptAndLevel2"));
ASSERT_NE(job, nullptr);
auto commands = GetCommands(job->RunScripts);
EXPECT_THAT(commands, ElementsAre(command1, command2, command3));
}
}
} // namespace directordaemon

0 comments on commit 02770ba

Please sign in to comment.