syncable_cli/analyzer/hadolint/rules/
dl3031.rs

1//! DL3031: Do not use yum update
2//!
3//! Using yum update in a Dockerfile is not recommended.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{simple_rule, SimpleRule};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
11    simple_rule(
12        "DL3031",
13        Severity::Warning,
14        "Do not use `yum update`.",
15        |instr, shell| {
16            match instr {
17                Instruction::Run(_) => {
18                    if let Some(shell) = shell {
19                        !shell.any_command(|cmd| {
20                            cmd.name == "yum" && cmd.has_any_arg(&["update", "upgrade"])
21                        })
22                    } else {
23                        true
24                    }
25                }
26                _ => true,
27            }
28        },
29    )
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::analyzer::hadolint::lint::{lint, LintResult};
36    use crate::analyzer::hadolint::config::HadolintConfig;
37
38    fn lint_dockerfile(content: &str) -> LintResult {
39        lint(content, &HadolintConfig::default())
40    }
41
42    #[test]
43    fn test_yum_update() {
44        let result = lint_dockerfile("FROM centos:7\nRUN yum update -y");
45        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3031"));
46    }
47
48    #[test]
49    fn test_yum_install() {
50        let result = lint_dockerfile("FROM centos:7\nRUN yum install -y nginx-1.20.0");
51        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3031"));
52    }
53}