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