Skip to content

Commit

Permalink
Merge pull request bareos#2097
Browse files Browse the repository at this point in the history
Inherit RunScript elements between JobDef resources
  • Loading branch information
BareosBot authored Feb 3, 2025
2 parents 6f6cc93 + d758d2f commit 6ed3b37
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- cmake: add cmake check whether tirpc is installed [PR #2109]
- bconsole: require only one password in the configuration [PR #2116]
- openssl: unify ssl error logging [PR #2078]
- Inherit RunScript elements between JobDef resources [PR #2097]

[PR #2039]: https://github.com/bareos/bareos/pull/2039
[PR #2040]: https://github.com/bareos/bareos/pull/2040
Expand All @@ -40,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[PR #2078]: https://github.com/bareos/bareos/pull/2078
[PR #2079]: https://github.com/bareos/bareos/pull/2079
[PR #2086]: https://github.com/bareos/bareos/pull/2086
[PR #2097]: https://github.com/bareos/bareos/pull/2097
[PR #2102]: https://github.com/bareos/bareos/pull/2102
[PR #2105]: https://github.com/bareos/bareos/pull/2105
[PR #2109]: https://github.com/bareos/bareos/pull/2109
Expand Down
4 changes: 2 additions & 2 deletions core/src/dird/dird_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
Copyright (C) 2013-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 Down Expand Up @@ -2398,7 +2398,7 @@ bool PropagateJobdefs(int res_type, JobResource* res)
// Don't allow the JobDefs pointing to itself.
if (res->jobdefs == res) { return false; }

if (res_type == R_JOB) {
if (res_type == R_JOB || res_type == R_JOBDEFS) {
jobdefs = res->jobdefs;

// Handle RunScripts alists specifically
Expand Down
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 6ed3b37

Please sign in to comment.