Skip to content

Commit

Permalink
Merge pull request #1188 from HediBenFraj/dev/HediBenFraj/master/jobi…
Browse files Browse the repository at this point in the history
…d-availabilty-check-exexcuting-run-command

console: aborting job run if jobId doesn't exist in catalog.
  • Loading branch information
joergsteffens committed Jul 7, 2022
2 parents a97809a + 85b986e commit 9b61bc6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -90,6 +90,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
- build: Now use solaris 11.4-11.4.42.0.0.111.0 [PR #1189]
- bconsole: removed commas from jobid attribute in list jobs and llist jobs outputs [PR #1126]
- testing: matrix.yml: run multiple tests sequentially [PR #1193]
- console: aborting job run if jobid doesn't exist in catalog [PR 1188]

### Deprecated
- make_catalog_backup.pl is now a shell wrapper script which will be removed in version 23.
Expand Down
1 change: 1 addition & 0 deletions core/src/cats/cats.h
Expand Up @@ -683,6 +683,7 @@ class BareosDb : public BareosDbQueryEnum {
POOLMEM*& stime,
char* job);
bool FindLastJobid(JobControlRecord* jcr, const char* Name, JobDbRecord* jr);
bool FindJobById(JobControlRecord* jcr, const std::string id);
int FindNextVolume(JobControlRecord* jcr,
int index,
bool InChanger,
Expand Down
20 changes: 20 additions & 0 deletions core/src/cats/sql_find.cc
Expand Up @@ -355,6 +355,26 @@ bool BareosDb::FindLastJobid(JobControlRecord* jcr,
return true;
}

/**
* @brief BareosDb::FindJobById
* @param jcr
* @param id id of job to look for
* @return returns true if job exists in db, false if not
*/
bool BareosDb::FindJobById(JobControlRecord* jcr, std::string id)
{
std::string query = "SELECT JobId FROM Job WHERE JobId=" + id;
Dmsg1(100, "Query: %s\n", query.c_str());
if (!QUERY_DB(jcr, query.c_str())) { return false; }
if (SqlFetchRow() == NULL) {
Mmsg1(errmsg, _("No Job found with id: %d.\n"), id.c_str());
SqlFreeResult();
return false;
} else {
return true;
}
}

/**
* Search a comma separated list of unwanted volume names and see if given
* VolumeName is on it.
Expand Down
20 changes: 18 additions & 2 deletions core/src/dird/ua_run.cc
Expand Up @@ -358,7 +358,7 @@ bool reRunCmd(UaContext* ua, const char* cmd)
* For Backup and Verify Jobs
* run [job=]<job-name> level=<level-name>
*
* For Restore Jobs
* Jobs
* run <job-name>
*
* Returns: 0 on error
Expand Down Expand Up @@ -1478,6 +1478,13 @@ static bool DisplayJobParameters(UaContext* ua,
if (!GetPint(ua, _("Please enter a JobId for restore: "))) {
return false;
}
std::string jobIdInput = std::to_string(ua->int64_val);
if (!ua->db->FindJobById(jcr, jobIdInput)) {
ua->SendMsg("JobId %s not found in catalog. \n",
jobIdInput.c_str());
return false;
}

jcr->impl->RestoreJobId = ua->int64_val;
}
}
Expand Down Expand Up @@ -1792,14 +1799,23 @@ static bool ScanCommandLineArguments(UaContext* ua, RunContext& rc)
rc.job_name = ua->argv[i];
kw_ok = true;
break;
case 1: /* JobId */
case 1: /* JobId */ {
if (rc.jid && !rc.mod) {
ua->SendMsg(_("JobId specified twice.\n"));
return false;
}
rc.jid = ua->argv[i];
std::vector jobIdList = split_string(rc.jid, ',');
for (const auto& jobId : jobIdList) {
if (!ua->db->FindJobById(ua->jcr, jobId)) {
ua->SendMsg("JobId %s not found in catalog. \n", jobId.c_str());
return false;
}
}

kw_ok = true;
break;
}
case 2: /* client */
case 3: /* fd */
if (rc.client_name) {
Expand Down
41 changes: 41 additions & 0 deletions systemtests/tests/bareos/testrunner-run-non-existing-jobid
@@ -0,0 +1,41 @@
#!/bin/bash
set -e
set -o pipefail
set -u

# Run a job with a non existing jobid
TestName="$(basename "$(pwd)")"
export TestName


#shellcheck source=../environment.in
. ./environment

#shellcheck source=../scripts/functions
. "${rscripts}"/functions

start_test

run_log=$tmp/run.out
JobName=RestoreFiles

rm -f $run_log

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out /dev/null
messages
@$out $run_log
setdebug level=100 storage=File
run job=$JobName jobid=999999 yes
wait
messages
quit
END_OF_DATA

run_bconsole

expect_grep "JobId 999999 not found in catalog." \
"$run_log" \
"The run command accepted a jobid that does not exist in the database."

end_test
11 changes: 6 additions & 5 deletions systemtests/tests/python-bareos/test_list_command.py
Expand Up @@ -238,15 +238,16 @@ def test_list_jobs(self):
self.assertTrue(result["jobs"])
for job in result["jobs"]:
self.assertTrue(job["jobstatus"], "T")

# run RestoreFiles with a non existant jobId so it fails
director.call("run job=RestoreFiles jobid=999999 yes")

# running a job a canceling
director.call("run job=backup-bareos-fd yes")
director.call("cancel job=backup-bareos-fd yes")

# list jobs jobstatus=X,Y,z
result = director.call("list jobs jobstatus=T,f")
result = director.call("list jobs jobstatus=T,A")
self.assertTrue(result["jobs"])
for job in result["jobs"]:
self.assertTrue(job["jobstatus"] == "T" or job["jobstatus"] == "f")
self.assertTrue(job["jobstatus"] == "T" or job["jobstatus"] == "A")

result = director.call("list jobs jobstatus=R")
self.assertFalse(result["jobs"])
Expand Down
2 changes: 1 addition & 1 deletion systemtests/tests/virtualfull/testrunner
Expand Up @@ -133,7 +133,7 @@ if ! grep -q "Fatal error: Could not create bootstrap file" "$tmp/log4.out"; the
estat=1
fi

if ! grep -q "Termination:.*Backup Error" "$tmp/log5.out"; then
if ! grep -q "JobId 2 not found in catalog." "$tmp/log5.out"; then
echo "Consolidating missing jobids did not fail as expected" >&2
cat "$tmp/log5.out"
estat=1
Expand Down

0 comments on commit 9b61bc6

Please sign in to comment.