Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions gitlab-runner-mock/src/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ use wiremock::{Request, Respond};
use crate::{GitlabRunnerMock, MockJobState};

/*
jobs/id => 200 if ok;
jobs/id => 200 if ok;

403 if cancelled
< job-status: canceled in header
}
403 if job token is invalid or the job was cancelled
*/
Comment thread
refi64 marked this conversation as resolved.

#[derive(Default, Deserialize)]
Expand Down
9 changes: 3 additions & 6 deletions gitlab-runner-mock/src/api/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,16 @@ impl Respond for JobTraceResponder {
};

if let Some(job) = self.mock.get_job(id) {
if token != job.token() {
if token != job.token() || job.state() != MockJobState::Running {
ResponseTemplate::new(StatusCode::FORBIDDEN)
} else if job.state() != MockJobState::Running {
ResponseTemplate::new(StatusCode::FORBIDDEN)
.insert_header("Job-Status", &*job.state().to_string())
} else {
match job.append_log(request.body.clone(), start, end) {
Ok(()) => ResponseTemplate::new(StatusCode::ACCEPTED)
.insert_header(
"X-GitLab-Trace-Update-Interval",
&*self.mock.update_interval().to_string(),
self.mock.update_interval().to_string(),
)
.insert_header("Job-Status", &*job.state().to_string()),
.insert_header("Job-Status", job.state().to_string()),
Err(e) => ResponseTemplate::new(StatusCode::RANGE_NOT_SATISFIABLE)
.set_body_string(format!("{e:?}")),
}
Expand Down
33 changes: 13 additions & 20 deletions gitlab-runner-mock/src/api/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,22 @@ impl Respond for JobUpdateResponder {
.unwrap();

if let Some(job) = self.mock.get_job(id) {
if r.token != job.token() {
if r.token != job.token() || job.state() != MockJobState::Running {
ResponseTemplate::new(StatusCode::FORBIDDEN)
} else {
let r = match (job.state(), r.state) {
(MockJobState::Running, MockJobState::Success) => {
job.update_state(r.state);
ResponseTemplate::new(StatusCode::OK)
}
(MockJobState::Running, MockJobState::Failed) => {
job.update_state(r.state);
ResponseTemplate::new(StatusCode::OK)
}
(MockJobState::Running, MockJobState::Running) => {
job.update_state(r.state);
ResponseTemplate::new(StatusCode::OK)
}
(current_state, _) if current_state != MockJobState::Running => {
ResponseTemplate::new(StatusCode::FORBIDDEN)
}
_ => panic!("Invalid state change"),
};
assert!(
matches!(
r.state,
MockJobState::Success | MockJobState::Failed | MockJobState::Running
),
"Invalid state change from {} -> {}",
job.state(),
r.state,
);

r.append_header("Job-Status", &*job.state().to_string())
job.update_state(r.state);
ResponseTemplate::new(StatusCode::OK)
.append_header("Job-Status", job.state().to_string())
}
} else {
ResponseTemplate::new(StatusCode::NOT_FOUND)
Expand Down
20 changes: 6 additions & 14 deletions gitlab-runner/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ where
}

const GITLAB_TRACE_UPDATE_INTERVAL: &str = "X-GitLab-Trace-Update-Interval";
const JOB_STATUS: &str = "Job-Status";

#[derive(Debug, Default, Clone, Serialize)]
struct FeaturesInfo {
Expand Down Expand Up @@ -250,8 +249,10 @@ impl JobResponse {
pub enum Error {
#[error("Unexpected reply code {0}")]
UnexpectedStatus(StatusCode),
#[error("Job cancelled")]
JobCancelled,
// May occur if the job was cancelled, in which case the job token we're
// using gets invalidated.
#[error("Forbidden")]
Forbidden,
#[error("Request failure {0}")]
Request(#[from] reqwest::Error),
#[error("Failed to write to destination {0}")]
Expand Down Expand Up @@ -336,13 +337,6 @@ impl Client {
}
}

fn check_for_job_cancellation(&self, response: &reqwest::Response) -> Result<(), Error> {
match response.headers().get(JOB_STATUS) {
Some(header) if header == "canceled" => Err(Error::JobCancelled),
_ => Ok(()),
}
}

pub async fn update_job(
&self,
id: u64,
Expand All @@ -359,8 +353,6 @@ impl Client {

let r = self.client.put(url).json(&update).send().await?;

self.check_for_job_cancellation(&r)?;

let trace_update_interval = r
.headers()
.get(GITLAB_TRACE_UPDATE_INTERVAL)
Expand All @@ -369,6 +361,7 @@ impl Client {
StatusCode::OK => Ok(JobUpdateReply {
trace_update_interval,
}),
StatusCode::FORBIDDEN => Err(Error::Forbidden),
_ => Err(Error::UnexpectedStatus(r.status())),
}
}
Expand Down Expand Up @@ -406,8 +399,6 @@ impl Client {
.send()
.await?;

self.check_for_job_cancellation(&r)?;

let trace_update_interval = r
.headers()
.get(GITLAB_TRACE_UPDATE_INTERVAL)
Expand All @@ -417,6 +408,7 @@ impl Client {
StatusCode::ACCEPTED => Ok(TraceReply {
trace_update_interval,
}),
StatusCode::FORBIDDEN => Err(Error::Forbidden),
_ => Err(Error::UnexpectedStatus(r.status())),
}
}
Expand Down
4 changes: 2 additions & 2 deletions gitlab-runner/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl Run {
.await
{
Ok(_reply) => (),
Err(crate::client::Error::JobCancelled) => cancel_token.cancel(),
Err(crate::client::Error::Forbidden) => cancel_token.cancel(),
Err(err) => warn!("Failed to update job status: {:?}", err),
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ impl Run {
self.log_offset += len;
reply.trace_update_interval
}
Err(crate::client::Error::JobCancelled) => {
Err(crate::client::Error::Forbidden) => {
cancel_token.cancel();
None
}
Expand Down