diff --git a/roles/limit-log-files/README.rst b/roles/limit-log-files/README.rst index c937db40e..e9cd6ca9e 100644 --- a/roles/limit-log-files/README.rst +++ b/roles/limit-log-files/README.rst @@ -14,9 +14,10 @@ and reports a warning to Zuul. .. zuul:rolevar:: limit_log_files_file_limit: :type: int - :default: 100 + :default: 0 Maximum number of files allowed in the logs directory. + If set to 0, no limit is enforced on the number of files. .. zuul:rolevar:: limit_log_files_file_rules: :type: string diff --git a/roles/limit-log-files/defaults/main.yaml b/roles/limit-log-files/defaults/main.yaml index ff5b2357b..2f8917184 100644 --- a/roles/limit-log-files/defaults/main.yaml +++ b/roles/limit-log-files/defaults/main.yaml @@ -3,7 +3,7 @@ limit_log_files_log_dir: "{{ zuul.executor.log_root }}" limit_log_files_fail: false -limit_log_files_file_limit: 100 +limit_log_files_file_limit: 0 limit_log_files_file_rules: - ".*job-output.json$ 500k" diff --git a/roles/limit-log-files/tasks/main.yaml b/roles/limit-log-files/tasks/main.yaml index 44e7f4790..36cfe260f 100644 --- a/roles/limit-log-files/tasks/main.yaml +++ b/roles/limit-log-files/tasks/main.yaml @@ -18,12 +18,16 @@ exit 1 fi - # Count files in target directory - TOTAL_FILES=$(find "$TARGET_DIR" -type f | wc -l) + # If FILE_LIMIT is 0 allow any number of files + if (( FILE_LIMIT != 0 )); then + # Count files in target directory + TOTAL_FILES=$(find "$TARGET_DIR" -type f | wc -l) + + if (( TOTAL_FILES > FILE_LIMIT )); then + echo "Total number of files in '$TARGET_DIR' exceed limit: $TOTAL_FILES > $FILE_LIMIT" + exit 2 + fi - if (( TOTAL_FILES > FILE_LIMIT )); then - echo "Total number of files in '$TARGET_DIR' exceed limit: $TOTAL_FILES > $FILE_LIMIT" - exit 2 fi ERROR=0 diff --git a/test-playbooks/general/limit-log-files.yaml b/test-playbooks/general/limit-log-files.yaml index ff430a48e..4d2019310 100644 --- a/test-playbooks/general/limit-log-files.yaml +++ b/test-playbooks/general/limit-log-files.yaml @@ -44,10 +44,10 @@ limit_log_files_file_rules: - ".* 250c" - ".* 150c" - # execute role with fail, should fail + # execute role with big file, should fail - name: Block to rescue failing of role block: - - name: Execute role in fail mode + - name: Execute role with big file include_role: name: limit-log-files vars: @@ -59,6 +59,26 @@ - name: Set fact for assertion set_fact: has_failed: true - - name: Assert that second run failed + - name: Assert that run failed + assert: + that: has_failed is defined + + # execute too many files, should fail + - name: Block to rescue failing of role + block: + - name: Execute role with too many files + include_role: + name: limit-log-files + vars: + limit_log_files_fail: true + limit_log_files_file_limit: 1 + limit_log_files_log_dir: "{{ test_folder }}" + limit_log_files_file_rules: + - ".* 500c" + rescue: + - name: Set fact for assertion + set_fact: + has_failed: true + - name: Assert that run failed assert: that: has_failed is defined