syncable_cli/analyzer/hadolint/rules/
dl3032.rs1use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
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 "DL3032",
13 Severity::Warning,
14 "`yum clean all` missing after yum command.",
15 |instr, shell| {
16 match instr {
17 Instruction::Run(_) => {
18 if let Some(shell) = shell {
19 let has_yum_install = shell.any_command(|cmd| {
21 cmd.name == "yum"
22 && cmd.has_any_arg(&["install", "groupinstall", "localinstall"])
23 });
24
25 if !has_yum_install {
26 return true;
27 }
28
29 shell.any_command(|cmd| {
31 (cmd.name == "yum" && cmd.has_any_arg(&["clean"]))
32 || (cmd.name == "rm"
33 && cmd
34 .arguments
35 .iter()
36 .any(|arg| arg.contains("/var/cache/yum")))
37 })
38 } else {
39 true
40 }
41 }
42 _ => true,
43 }
44 },
45 )
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51 use crate::analyzer::hadolint::config::HadolintConfig;
52 use crate::analyzer::hadolint::lint::{LintResult, lint};
53
54 fn lint_dockerfile(content: &str) -> LintResult {
55 lint(content, &HadolintConfig::default())
56 }
57
58 #[test]
59 fn test_yum_install_without_clean() {
60 let result = lint_dockerfile("FROM centos:7\nRUN yum install -y nginx");
61 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3032"));
62 }
63
64 #[test]
65 fn test_yum_install_with_clean() {
66 let result = lint_dockerfile("FROM centos:7\nRUN yum install -y nginx && yum clean all");
67 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3032"));
68 }
69
70 #[test]
71 fn test_yum_install_with_rm_cache() {
72 let result =
73 lint_dockerfile("FROM centos:7\nRUN yum install -y nginx && rm -rf /var/cache/yum");
74 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3032"));
75 }
76
77 #[test]
78 fn test_no_yum_install() {
79 let result = lint_dockerfile("FROM centos:7\nRUN yum update");
80 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3032"));
81 }
82}