| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: LGPL-2.1-only |
| 6 | |
| 7 | # Clean everything under directory but keep directory |
| 8 | function clean_path () |
| 9 | { |
| 10 | local path=$1 |
| 11 | # Use -u from bash top prevent empty expansion of variable yielding a |
| 12 | # list of current directory from find. |
| 13 | set -u |
| 14 | find $path -mindepth 1 -maxdepth 1 -exec rm -rf '{}' \; |
| 15 | set +u |
| 16 | } |
| 17 | |
| 18 | # The extglob shell option must be enabled to use the pattern generated by this |
| 19 | # function (shopt -s extglob/ shopt -u extglob). |
| 20 | # The pattern returned by this function is to validate the form: |
| 21 | # YYYYMMDDTHHMMSS[+-]HHMM-YYYYMMDDTHHMMSS[+-]HHMM |
| 22 | # |
| 23 | # Where YYYYMMDD is today or tomorrow. Tomorrow must be supported in case where |
| 24 | # the chunks are generated close to midnight and one ends up the following day. |
| 25 | function get_chunk_pattern () |
| 26 | { |
| 27 | local today=$1 |
| 28 | tommorow=$(date +%Y%m%d -d "${today}+1days") |
| 29 | pattern_hour_min="[0-9][0-9][0-9][0-9]" |
| 30 | pattern_hour_min_sec="${pattern_hour_min}[0-9][0-9]" |
| 31 | |
| 32 | base_pattern="@(${today}|${tommorow})T${pattern_hour_min_sec}[+-]${pattern_hour_min}" |
| 33 | |
| 34 | # Return the pattern |
| 35 | # YYYYMMDDTHHMMSS[+-]HHMM-YYYYMMDDTHHMMSS[+-]HHMM |
| 36 | echo "${base_pattern}-${base_pattern}" |
| 37 | } |
| 38 | |
| 39 | function validate_test_chunks () |
| 40 | { |
| 41 | local_path=$1 |
| 42 | today=$2 |
| 43 | app_path=$3 |
| 44 | domain=$4 |
| 45 | per_pid=$5 |
| 46 | |
| 47 | local path= |
| 48 | local chunk_pattern=$(get_chunk_pattern ${today}) |
| 49 | |
| 50 | # Enable extglob for the use of chunk_pattern |
| 51 | shopt -s extglob |
| 52 | |
| 53 | # Validate that only 2 chunks are present |
| 54 | nb_chunk=$(ls -A $local_path | wc -l) |
| 55 | test $nb_chunk -eq 2 |
| 56 | ok $? "${local_path} contains 2 chunks only" |
| 57 | |
| 58 | # Check if the first and second chunk folders exist and they contain a ${app_path}/metadata file. |
| 59 | for chunk in $(seq 0 1); do |
| 60 | path=$(ls $local_path/${chunk_pattern}-${chunk}/${app_path}/metadata) |
| 61 | ok $? "Chunk ${chunk} exists based on path $path" |
| 62 | done |
| 63 | |
| 64 | # Make sure we don't have anything else in the first 2 chunk directories |
| 65 | # besides the kernel folder. |
| 66 | for chunk in $(seq 0 1); do |
| 67 | nr_stale=$(ls -A $local_path/${chunk_pattern}-${chunk} | grep -v $domain | wc -l) |
| 68 | ok $nr_stale "No stale folders in chunk ${chunk} directory" |
| 69 | done |
| 70 | |
| 71 | # We expect a complete session of 30 events |
| 72 | validate_trace_count $EVENT_NAME $local_path 30 |
| 73 | |
| 74 | # Chunk 1: 10 events |
| 75 | validate_trace_count $EVENT_NAME $local_path/${chunk_pattern}-0 10 |
| 76 | |
| 77 | # Chunk 2: 20 events |
| 78 | validate_trace_count $EVENT_NAME $local_path/${chunk_pattern}-1 20 |
| 79 | |
| 80 | shopt -u extglob |
| 81 | } |
| 82 | |
| 83 | function rotate_timer_test () |
| 84 | { |
| 85 | local_path=$1 |
| 86 | per_pid=$2 |
| 87 | |
| 88 | today=$(date +%Y%m%d) |
| 89 | nr=0 |
| 90 | nr_iter=0 |
| 91 | expected_chunks=3 |
| 92 | |
| 93 | # Wait for the "archives" folder to appear after the first rotation |
| 94 | until [ -d $local_path ]; do |
| 95 | sleep 1 |
| 96 | done |
| 97 | |
| 98 | # Wait for $expected_chunks to be generated, timeout after |
| 99 | # 3 * $expected_chunks * 0.5s. |
| 100 | # On a laptop with an empty session, a local rotation takes about 200ms, |
| 101 | # and a remote rotation takes about 600ms. |
| 102 | # We currently set the timeout to 6 seconds for 3 rotations, if we get |
| 103 | # errors, we can bump this value. |
| 104 | |
| 105 | until [ $nr -ge $expected_chunks ] || [ $nr_iter -ge $(($expected_chunks * 2 )) ]; do |
| 106 | nr=$(ls $local_path | wc -l) |
| 107 | nr_iter=$(($nr_iter+1)) |
| 108 | sleep 1 |
| 109 | done |
| 110 | test $nr -ge $expected_chunks |
| 111 | ok $? "Generated at least $nr chunks in $(($nr_iter))s" |
| 112 | stop_lttng_tracing_ok $SESSION_NAME |
| 113 | destroy_lttng_session_ok $SESSION_NAME |
| 114 | |
| 115 | i=1 |
| 116 | local chunk_pattern=$(get_chunk_pattern ${today}) |
| 117 | |
| 118 | # Enable extglob for the use of chunk_pattern |
| 119 | shopt -s extglob |
| 120 | |
| 121 | # In a per-pid setup, only the first chunk is a valid trace, the other |
| 122 | # chunks should be empty folders |
| 123 | if test $per_pid = 1; then |
| 124 | validate_trace_empty $local_path/${chunk_pattern}-0 |
| 125 | nr=$(find $local_path/${chunk_pattern}-1/ | wc -l) |
| 126 | # contains self and may contain ust/ subdir (local) or not (remote). |
| 127 | test $nr -le 2 |
| 128 | ok $? "Chunk 2 is empty" |
| 129 | nr=$(find $local_path/${chunk_pattern}-2/ | wc -l) |
| 130 | # contains self and may contain ust/ subdir (local) or not (remote). |
| 131 | test $nr -le 2 |
| 132 | ok $? "Chunk 3 is empty" |
| 133 | else |
| 134 | while [ $i -le $expected_chunks ]; do |
| 135 | validate_trace_empty $local_path/${chunk_pattern}-$i |
| 136 | i=$(($i+1)) |
| 137 | done |
| 138 | fi |
| 139 | shopt -u extglob |
| 140 | } |