|
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; |
2 | 10 |
|
3 | 11 | #[test]
|
4 | 12 | fn lookup_job_pattern() {
|
@@ -62,3 +70,65 @@ fn check_pattern(db: &JobDatabase, pattern: &str, expected: &[&str]) {
|
62 | 70 |
|
63 | 71 | assert_eq!(jobs, expected);
|
64 | 72 | }
|
| 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