syncable_cli/analyzer/hadolint/rules/
dl3017.rs

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