bringing in testkit tools
[feisty_meow.git] / testkit / library / file_management.sh
1
2 # these are the pieces that we'll use to assemble mostly random files.
3 RANDOM_CHUNK_FILES=($TEST_TEMP/random.0 $TEST_TEMP/random.1 $TEST_TEMP/random.2 $TEST_TEMP/random.3 $TEST_TEMP/random.4)
4
5 # largest chunk of random data we'll actually generate at a time, in each chunk file.
6 export MAX_CHUNK_FILE=65536
7
8 # returns the file size for the first argument.
9 function getFileSize()
10 {
11   local file="$1"; shift
12   if isMacOSX; then
13     stat -f%z "$file"
14   else
15     stat --printf="%s" "$file"
16   fi
17 }
18
19 # outputs the number of seconds since the epoch.
20 function getTimeStamp()
21 {
22   date +%s
23 }
24
25 # makes sure the chunk files are all generated.
26 function prepareRandomChunks()
27 {
28   local i
29   for ((i = 0; i < ${#RANDOM_CHUNK_FILES[@]}; i++)); do
30     # make the chunk files if they don't exist.
31     local currfile="${RANDOM_CHUNK_FILES[$i]}"
32     if [ ! -f "$currfile" ]; then
33       local filesize=$MAX_CHUNK_FILE
34       # pick a value to add or subtract from the constant sized chunk, so we won't always be
35       # using files at the same boundaries or with a power of 2 size.
36       local moddy=$(( ($(echo $RANDOM) % 128) - 64 ))
37       ((filesize -= $moddy))
38 #echo creating chunk file $currfile of size $filesize
39       dd if=/dev/urandom of=$currfile bs=1 count=$filesize &>/dev/null
40       assertEquals "creating random chunk file $currfile" 0 $?
41     fi
42   done
43 }
44
45 # creates a somewhat random file for testing.  this will be assembled out of
46 # our chunks of random files, so is not truly random, but we've found that the
47 # random number generator is a HUGE drag on our testing speed.  this is still
48 # pretty random data.  the first argument is the file name and the second is
49 # the desired file size.
50 function createRandomFile()
51 {
52   local file="$1"; shift
53   local size="$1"; shift
54
55   prepareRandomChunks
56
57   local stampBefore=$(getTimeStamp)
58
59   # truncate any existing stuff.
60   echo -n >"$file"
61
62   while [ $(getFileSize "$file") -lt $size ]; do
63     which_chunker=$(expr $(echo $RANDOM) % ${#RANDOM_CHUNK_FILES[@]})
64 #echo choosing chunk file $which_chunker
65     cat "${RANDOM_CHUNK_FILES[$which_chunker]}" >>"$file"
66   done
67
68 #echo file size after random chunkings is $(getFileSize "$file")
69
70   local fsizenow="$(getFileSize "$file")"
71 #echo size now is $fsizenow and desired is $size
72   if [ $fsizenow -gt $size ]; then
73 #echo trying to truncate file
74     truncate -s $size "$file"
75   fi
76 #echo file size after truncate is $(getFileSize "$file") and expected size is $size
77
78   local stampAfter=$(getTimeStamp)
79   local secs=$(($stampAfter - $stampBefore))
80   if [ $secs -le 0 ]; then
81     # even though it claims it took zero time, we know better, but we also don't want to
82     # divide by zero, so it loses its credit for being very fast here.
83     secs=1
84   fi
85   local kbs=$(( $size / $secs / 1024))
86   
87   local fsizenow="$(getFileSize "$file")"
88   assertEquals "Creating random file of $size bytes at ${kbs} kbps in: $file" $size $fsizenow
89 }
90