syncable_cli/analyzer/hadolint/rules/
dl3011.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 "DL3011",
13 Severity::Error,
14 "Valid UNIX ports range from 0 to 65535.",
15 |instr, _shell| {
16 match instr {
17 Instruction::Expose(_) => true,
20 _ => true,
21 }
22 },
23 )
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29 use crate::analyzer::hadolint::config::HadolintConfig;
30 use crate::analyzer::hadolint::lint::{LintResult, lint};
31
32 fn lint_dockerfile(content: &str) -> LintResult {
33 lint(content, &HadolintConfig::default())
34 }
35
36 #[test]
37 fn test_valid_port() {
38 let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 8080");
39 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
40 }
41
42 #[test]
43 fn test_valid_multiple_ports() {
44 let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 80 443 8080");
45 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
46 }
47
48 #[test]
49 fn test_max_valid_port() {
50 let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 65535");
51 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
52 }
53
54 #[test]
55 fn test_min_valid_port() {
56 let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 0");
57 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
58 }
59}