Skip to content

Commit 90f5f36

Browse files
committed
move validation to test
1 parent 21a5e27 commit 90f5f36

File tree

2 files changed

+74
-49
lines changed

2 files changed

+74
-49
lines changed

src/ci/citool/src/jobs.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
mod tests;
33

44
use std::collections::BTreeMap;
5-
use std::path::Path;
5+
use std::vec;
66

77
use anyhow::Context as _;
88
use serde_yaml::Value;
99

10-
use crate::utils::{self, load_env_var};
11-
use crate::{DOCKER_DIRECTORY, GitHubContext};
10+
use crate::GitHubContext;
11+
use crate::utils::load_env_var;
1212

1313
/// Representation of a job loaded from the `src/ci/github-actions/jobs.yml` file.
1414
#[derive(serde::Deserialize, Debug, Clone)]
@@ -47,47 +47,6 @@ impl Job {
4747
fn is_linux(&self) -> bool {
4848
self.os.contains("ubuntu")
4949
}
50-
51-
/// Validate that CodeBuild jobs use Docker images from ghcr.io registry.
52-
/// This is needed because otherwise from CodeBuild we get rate limited by Docker Hub.
53-
fn validate_codebuild_image(&self) -> anyhow::Result<()> {
54-
let is_job_on_codebuild = self.codebuild.unwrap_or(false);
55-
if !is_job_on_codebuild {
56-
// Jobs in GitHub Actions don't get rate limited by Docker Hub.
57-
return Ok(());
58-
}
59-
60-
let image_name = self.image();
61-
// we hardcode host-x86_64 here, because in codebuild we only run jobs for this architecture.
62-
let dockerfile_path =
63-
Path::new(DOCKER_DIRECTORY).join("host-x86_64").join(&image_name).join("Dockerfile");
64-
65-
if !dockerfile_path.exists() {
66-
return Err(anyhow::anyhow!(
67-
"Dockerfile not found for CodeBuild job '{}' at path: {}",
68-
self.name,
69-
dockerfile_path.display()
70-
));
71-
}
72-
73-
let dockerfile_content = utils::read_to_string(&dockerfile_path)?;
74-
75-
// Check if all FROM statement uses ghcr.io registry
76-
let has_ghcr_from = dockerfile_content
77-
.lines()
78-
.filter(|line| line.trim_start().to_lowercase().starts_with("from "))
79-
.all(|line| line.contains("ghcr.io"));
80-
81-
if !has_ghcr_from {
82-
return Err(anyhow::anyhow!(
83-
"CodeBuild job '{}' must use ghcr.io registry in its Dockerfile FROM statement. \
84-
Dockerfile path: {dockerfile_path:?}",
85-
self.name,
86-
));
87-
}
88-
89-
Ok(())
90-
}
9150
}
9251

9352
#[derive(serde::Deserialize, Debug)]
@@ -256,10 +215,6 @@ fn calculate_jobs(
256215
let jobs = substitute_github_vars(jobs.clone())
257216
.context("Failed to substitute GitHub context variables in jobs")?;
258217
let jobs = skip_jobs(jobs, channel);
259-
for j in &jobs {
260-
j.validate_codebuild_image()
261-
.context(format!("Failed to validate CodeBuild job '{}'", j.name))?;
262-
}
263218
let jobs = jobs
264219
.into_iter()
265220
.map(|job| {

src/ci/citool/src/jobs/tests.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use crate::jobs::{JobDatabase, load_job_db};
1+
use std::path::Path;
2+
3+
use crate::{
4+
DOCKER_DIRECTORY, JOBS_YML_PATH,
5+
jobs::{JobDatabase, load_job_db},
6+
utils,
7+
};
8+
9+
use super::Job;
210

311
#[test]
412
fn lookup_job_pattern() {
@@ -62,3 +70,65 @@ fn check_pattern(db: &JobDatabase, pattern: &str, expected: &[&str]) {
6270

6371
assert_eq!(jobs, expected);
6472
}
73+
74+
/// Validate that CodeBuild jobs use Docker images from ghcr.io registry.
75+
/// This is needed because otherwise from CodeBuild we get rate limited by Docker Hub.
76+
fn validate_codebuild_image(job: &Job) -> anyhow::Result<()> {
77+
let is_job_on_codebuild = job.codebuild.unwrap_or(false);
78+
if !is_job_on_codebuild {
79+
// Jobs in GitHub Actions don't get rate limited by Docker Hub.
80+
return Ok(());
81+
}
82+
83+
let image_name = job.image();
84+
// we hardcode host-x86_64 here, because in codebuild we only run jobs for this architecture.
85+
let dockerfile_path =
86+
Path::new(DOCKER_DIRECTORY).join("host-x86_64").join(&image_name).join("Dockerfile");
87+
88+
if !dockerfile_path.exists() {
89+
return Err(anyhow::anyhow!(
90+
"Dockerfile not found for CodeBuild job '{}' at path: {}",
91+
job.name,
92+
dockerfile_path.display()
93+
));
94+
}
95+
96+
let dockerfile_content = utils::read_to_string(&dockerfile_path)?;
97+
98+
// Check if all FROM statement uses ghcr.io registry
99+
let has_ghcr_from = dockerfile_content
100+
.lines()
101+
.filter(|line| line.trim_start().to_lowercase().starts_with("from "))
102+
.all(|line| line.contains("ghcr.io"));
103+
104+
if !has_ghcr_from {
105+
return Err(anyhow::anyhow!(
106+
"CodeBuild job '{}' must use ghcr.io registry in its Dockerfile FROM statement. \
107+
Dockerfile path: {dockerfile_path:?}",
108+
job.name,
109+
));
110+
}
111+
112+
Ok(())
113+
}
114+
115+
#[test]
116+
fn validate_jobs() {
117+
let db = {
118+
let default_jobs_file = Path::new(JOBS_YML_PATH);
119+
let db_str = utils::read_to_string(default_jobs_file).unwrap();
120+
load_job_db(&db_str).expect("Failed to load job database")
121+
};
122+
123+
let all_jobs =
124+
db.pr_jobs.iter().chain(db.try_jobs.iter()).chain(db.auto_jobs.iter()).collect::<Vec<_>>();
125+
126+
let errors: Vec<anyhow::Error> =
127+
all_jobs.into_iter().filter_map(|job| validate_codebuild_image(job).err()).collect();
128+
129+
if !errors.is_empty() {
130+
let error_messages =
131+
errors.into_iter().map(|e| format!("- {e}")).collect::<Vec<_>>().join("\n");
132+
panic!("Job validation failed:\n{error_messages}");
133+
}
134+
}

0 commit comments

Comments
 (0)