1#!/bin/bash 2# Copyright 2019 Huawei Technologies Co., Ltd 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# ============================================================================ 16 17# source the globals and functions for use with cache testing 18export SKIP_ADMIN_COUNTER=false 19declare session_id failed_tests 20. cachetest_lib.sh 21echo 22 23################################################################################ 24# Cache testing: cache_admin argument testing # 25# Summary: Various tests that expect to get failure messages returned # 26################################################################################ 27 28# Double-command test 29cmd="${CACHE_ADMIN} --start --stop" 30CacheAdminCmd "${cmd}" 1 31HandleRcExit $? 0 0 32 33# missing command test 34cmd="${CACHE_ADMIN} --port 50082" 35CacheAdminCmd "${cmd}" 1 36HandleRcExit $? 0 0 37 38# bad arg test 39cmd="${CACHE_ADMIN} -p abc --start" 40CacheAdminCmd "${cmd}" 1 41HandleRcExit $? 0 0 42 43# missing arg test 44cmd="${CACHE_ADMIN} -p --start" 45CacheAdminCmd "${cmd}" 1 46HandleRcExit $? 0 0 47 48# invalid command 49cmd="${CACHE_ADMIN} -p 50082 --start --not_exist_cmd" 50CacheAdminCmd "${cmd}" 1 51HandleRcExit $? 0 0 52 53# spill directory does not exist 54cmd="${CACHE_ADMIN} --start --spilldir /path_that_does_not_exist" 55CacheAdminCmd "${cmd}" 1 56HandleRcExit $? 0 0 57 58# clean up cache server first to test start 59ServerCleanup 60# start cache server 61StartServer 62HandleRcExit $? 1 1 63# start the cache server again, however, this time we expect an error 64cmd="${CACHE_ADMIN} --start" 65CacheAdminCmd "${cmd}" 1 66HandleRcExit $? 0 1 67StopServer 68HandleRcExit $? 1 1 69 70# start cache server twice with different ports 71# this one starts with the default port 50052 72StartServer 73HandleRcExit $? 1 1 74# this one starts with port 50053 75cmd="${CACHE_ADMIN} --start -p 50053" 76CacheAdminCmd "${cmd}" 0 77HandleRcExit $? 1 1 78# stop the cache server with default port 79StopServer 80HandleRcExit $? 1 1 81# stop the cache server with port 50053 82cmd="${CACHE_ADMIN} --stop -p 50053" 83CacheAdminCmd "${cmd}" 0 84HandleRcExit $? 1 1 85 86# stop the cache server without bringing it up 87cmd="${CACHE_ADMIN} --stop" 88CacheAdminCmd "${cmd}" 1 89HandleRcExit $? 0 1 90 91# start the cache server with illegal hostname 92cmd="${CACHE_ADMIN} --start -h 0.0.0.0" 93CacheAdminCmd "${cmd}" 1 94HandleRcExit $? 0 1 95cmd="${CACHE_ADMIN} --start -h illegal" 96CacheAdminCmd "${cmd}" 1 97HandleRcExit $? 0 1 98cmd="${CACHE_ADMIN} --start -h" 99CacheAdminCmd "${cmd}" 1 100HandleRcExit $? 0 1 101cmd="${CACHE_ADMIN} --start -h --hostname" 102CacheAdminCmd "${cmd}" 1 103HandleRcExit $? 0 1 104cmd="${CACHE_ADMIN} --start -h --hostname 127.0.0.1" 105CacheAdminCmd "${cmd}" 1 106HandleRcExit $? 0 1 107 108# start the cache server with illegal port 109cmd="${CACHE_ADMIN} --start -p 0" 110CacheAdminCmd "${cmd}" 1 111HandleRcExit $? 0 1 112cmd="${CACHE_ADMIN} --start -p -1" 113CacheAdminCmd "${cmd}" 1 114HandleRcExit $? 0 1 115cmd="${CACHE_ADMIN} --start -p 65536" 116CacheAdminCmd "${cmd}" 1 117HandleRcExit $? 0 1 118cmd="${CACHE_ADMIN} --start -p illegal" 119CacheAdminCmd "${cmd}" 1 120HandleRcExit $? 0 1 121cmd="${CACHE_ADMIN} --start -p" 122CacheAdminCmd "${cmd}" 1 123HandleRcExit $? 0 1 124 125# find a port that is occupied using netstat 126if [ -x "$(command -v netstat)" ]; then 127 port=$(netstat -ntp | grep -v '::' | awk '{print $4}' | grep -E '^[[:digit:]]+' | awk -F: '{print $2}' | sort -n | tail -n 1) 128 if [ ${port} -gt 1025 ]; then 129 # start cache server with occupied port 130 cmd="${CACHE_ADMIN} --start -p ${port}" 131 CacheAdminCmd "${cmd}" 1 132 HandleRcExit $? 0 1 133 fi 134fi 135 136# generate session before starting the cache server 137cmd="${CACHE_ADMIN} -g" 138CacheAdminCmd "${cmd}" 1 139HandleRcExit $? 0 0 140 141# illegal generate session command 142StartServer 143HandleRcExit $? 1 1 144cmd="${CACHE_ADMIN} -g 1" 145CacheAdminCmd "${cmd}" 1 146HandleRcExit $? 0 0 147 148# illegal destroy session command 149cmd="${CACHE_ADMIN} -d -2" 150CacheAdminCmd "${cmd}" 1 151HandleRcExit $? 0 0 152cmd="${CACHE_ADMIN} -d illegal" 153CacheAdminCmd "${cmd}" 1 154HandleRcExit $? 0 0 155cmd="${CACHE_ADMIN} -d" 156CacheAdminCmd "${cmd}" 1 157HandleRcExit $? 0 0 158# destroy a non-existing session 159cmd="${CACHE_ADMIN} -d 99999" 160CacheAdminCmd "${cmd}" 1 161HandleRcExit $? 0 0 162 163# generate two new sessions to test multi-destroy 164GetSession 165HandleRcExit $? 0 0 166session_id1=$session_id 167GetSession 168HandleRcExit $? 0 0 169session_id2=$session_id 170# test multi-session destroy 171cmd="${CACHE_ADMIN} -d ${session_id1} ${session_id2}" 172CacheAdminCmd "${cmd}" 0 173HandleRcExit $? 0 0 174 175# stop cache server at this point 176StopServer 177HandleRcExit $? 1 1 178 179# illegal number of workers 180cmd="${CACHE_ADMIN} --start -w 0" 181CacheAdminCmd "${cmd}" 1 182HandleRcExit $? 0 0 183cmd="${CACHE_ADMIN} --start -w -1" 184CacheAdminCmd "${cmd}" 1 185HandleRcExit $? 0 0 186cmd="${CACHE_ADMIN} --start -w illegal" 187CacheAdminCmd "${cmd}" 1 188HandleRcExit $? 0 0 189num_cpu=$(grep -c processor /proc/cpuinfo) 190if [ $num_cpu -lt 100 ]; then 191 cmd="${CACHE_ADMIN} --start -w 101" 192else 193 cmd="${CACHE_ADMIN} --start -w $(($num_cpu+1))" 194fi 195CacheAdminCmd "${cmd}" 1 196HandleRcExit $? 0 0 197cmd="${CACHE_ADMIN} --start -w 9999999" 198CacheAdminCmd "${cmd}" 1 199HandleRcExit $? 0 0 200cmd="${CACHE_ADMIN} --start -w" 201CacheAdminCmd "${cmd}" 1 202HandleRcExit $? 0 0 203 204# illegal spill path 205cmd="${CACHE_ADMIN} --start -s" 206CacheAdminCmd "${cmd}" 1 207HandleRcExit $? 0 0 208 209# spill path without writing perm 210if [ "$EUID" -ne 0 ]; then 211 cmd="${CACHE_ADMIN} --start -s /" 212 CacheAdminCmd "${cmd}" 1 213 HandleRcExit $? 0 0 214fi 215 216# illegal log level 217cmd="${CACHE_ADMIN} --start -l 5" 218CacheAdminCmd "${cmd}" 1 219HandleRcExit $? 0 0 220cmd="${CACHE_ADMIN} --start -l -1" 221CacheAdminCmd "${cmd}" 1 222HandleRcExit $? 0 0 223cmd="${CACHE_ADMIN} --start -l" 224CacheAdminCmd "${cmd}" 1 225HandleRcExit $? 0 0 226 227exit ${failed_tests} 228