syncable_cli/analyzer/hadolint/rules/
dl3017.rs1use crate::analyzer::hadolint::parser::instruction::Instruction;
7use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
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| match instr {
17 Instruction::Run(_) => {
18 if let Some(shell) = shell {
19 !shell.any_command(|cmd| cmd.name == "apk" && cmd.has_any_arg(&["upgrade"]))
20 } else {
21 true
22 }
23 }
24 _ => true,
25 },
26 )
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use crate::analyzer::hadolint::config::HadolintConfig;
33 use crate::analyzer::hadolint::lint::{LintResult, lint};
34
35 fn lint_dockerfile(content: &str) -> LintResult {
36 lint(content, &HadolintConfig::default())
37 }
38
39 #[test]
40 fn test_apk_upgrade() {
41 let result = lint_dockerfile("FROM alpine:3.18\nRUN apk upgrade");
42 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3017"));
43 }
44
45 #[test]
46 fn test_apk_update() {
47 let result = lint_dockerfile("FROM alpine:3.18\nRUN apk update");
48 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3017"));
49 }
50
51 #[test]
52 fn test_apk_add() {
53 let result = lint_dockerfile("FROM alpine:3.18\nRUN apk add --no-cache curl=8.0.0");
54 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3017"));
55 }
56}