forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added scripts to prepare test environment for running translib based servers and go tests. Will not be packaged in the docker image. Signed-off-by: Sachin Holla <sachin.holla@broadcom.com>
- Loading branch information
1 parent
f9118ed
commit 80df301
Showing
3 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python3 | ||
################################################################################ | ||
# # | ||
# Copyright 2023 Broadcom. The term Broadcom refers to Broadcom Inc. and/or # | ||
# its subsidiaries. # | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); # | ||
# you may not use this file except in compliance with the License. # | ||
# You may obtain a copy of the License at # | ||
# # | ||
# http://www.apache.org/licenses/LICENSE-2.0 # | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software # | ||
# distributed under the License is distributed on an "AS IS" BASIS, # | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | ||
# See the License for the specific language governing permissions and # | ||
# limitations under the License. # | ||
# # | ||
################################################################################ | ||
|
||
import json | ||
import os | ||
import redis | ||
import sys | ||
from argparse import ArgumentParser | ||
|
||
|
||
def main(): | ||
p = ArgumentParser( | ||
description="""Creates a database config json file by copying | ||
contents from a source file. Adds/removes the unix socket path in the INSTANCES sections | ||
by reading the correct value from redis config (redis-cli -p PORT config get unixsocket). | ||
""") | ||
p.add_argument( | ||
"-s", "--source", dest="srcfile", metavar="SRCFILE", | ||
default=os.path.join(os.path.dirname(__file__), "database_config.json"), | ||
help="""Source database config json file. | ||
Defaults to {sonic-mgmt-common}/tools/test/database_config.json""") | ||
p.add_argument( | ||
"-o", "--outfile", | ||
help="Output database config json file. Defaults to SRCFILE itself (overwrites)") | ||
|
||
opt = p.parse_args() | ||
|
||
with open(opt.srcfile) as f: | ||
db_config = json.load(f) | ||
|
||
fix_unix_socket_path(opt, db_config) | ||
|
||
if not opt.outfile: | ||
opt.outfile = opt.srcfile | ||
elif os.path.isdir(opt.outfile): | ||
opt.outfile = os.path.join(opt.outfile, os.path.basename(opt.srcfile)) | ||
elif not os.path.exists(os.path.dirname(opt.outfile)): | ||
os.makedirs(os.path.dirname(opt.outfile)) | ||
|
||
with open(opt.outfile, "w") as f: | ||
s = json.dumps(db_config, indent=4) | ||
f.write(s) | ||
|
||
|
||
def fix_unix_socket_path(opt, db_config): | ||
if not "INSTANCES" in db_config: | ||
return | ||
for name, inst in db_config["INSTANCES"].items(): | ||
if "port" in inst: | ||
fix_instance(name, inst) | ||
|
||
|
||
def fix_instance(inst_name, inst_config): | ||
r = redis.Redis( | ||
host=inst_config.get("hostname", "127.0.0.1"), | ||
port=inst_config.get("port"), | ||
decode_responses=True, | ||
) | ||
try: | ||
redis_config = r.config_get("unixsocket") | ||
except: | ||
print(f"Could not fix instance '{inst_name}': {sys.exc_info()[1]}") | ||
return False | ||
finally: | ||
r.close() | ||
|
||
if "unixsocket" in redis_config: | ||
inst_config["unix_socket_path"] = redis_config["unixsocket"] | ||
elif "unix_socket_path" in inst_config: | ||
inst_config.pop("unix_socket_path") | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env bash | ||
|
||
function usage() { | ||
cat << EOM | ||
usage: $(basename $0) [OPTIONS] | ||
Options: | ||
--dest=DIR Copy yang and othermgmt_cmn files here. Creates a temporary directorty | ||
if not specified. | ||
--dbconfig-in=FILE Copy the database_config.json file from this file. | ||
Unix socket paths will be updated by reading the redis configs. | ||
Uses tools/test/database_config.json if not specified. | ||
--dbconfig=FILE Use this database_config.json instead of creating new one under DEST. | ||
EOM | ||
} | ||
|
||
set -e | ||
|
||
mgmt_cmn=$(git -C $(dirname ${BASH_SOURCE}) rev-parse --show-toplevel) | ||
|
||
while [[ $# != 0 ]]; do | ||
case "$1" in | ||
-h|--help) usage; exit 0;; | ||
--dest=*) DEST=${1#*=} ;; | ||
--dbconfig-in=*) DBCONFIG_IN="${1#*=}" ;; | ||
--dbconfig=*) export DB_CONFIG_PATH="${1#*=}" ;; | ||
*) >&2 echo "error: unknown option \"$1\""; exit 1;; | ||
esac | ||
shift | ||
done | ||
|
||
if [[ -z ${DEST} ]]; then | ||
DEST=$(mktemp -d /tmp/translib.XXXXX) | ||
elif [[ ! -d ${DEST} ]]; then | ||
mkdir -p ${DEST} | ||
fi | ||
|
||
# Create database_config.json if not specified through --dbconfig | ||
if [[ -z ${DB_CONFIG_PATH} ]]; then | ||
export DB_CONFIG_PATH=${DEST}/database_config.json | ||
fi | ||
if [[ ! -e ${DB_CONFIG_PATH} ]] || [[ -n ${DBCONFIG_IN} ]]; then | ||
${mgmt_cmn}/tools/test/dbconfig.py \ | ||
-s ${DBCONFIG_IN:-${mgmt_cmn}/tools/test/database_config.json} \ | ||
-o ${DB_CONFIG_PATH} | ||
fi | ||
|
||
|
||
# Prepare yang files directiry for transformer | ||
if [[ -z ${YANG_MODELS_PATH} ]]; then | ||
export YANG_MODELS_PATH=${DEST}/yangs | ||
fi | ||
mkdir -p $V ${YANG_MODELS_PATH} | ||
pushd ${YANG_MODELS_PATH} > /dev/null | ||
rm -rf * | ||
find ${mgmt_cmn}/models/yang -name "*.yang" -exec ln -sf {} \; | ||
ln -sf ${mgmt_cmn}/models/yang/version.xml | ||
ln -sf ${mgmt_cmn}/config/transformer/models_list | ||
popd > /dev/null | ||
|
||
|
||
# Setup CVL schema directory | ||
if [[ -z ${CVL_SCHEMA_PATH} ]]; then | ||
export CVL_SCHEMA_PATH=${mgmt_cmn}/build/cvl/schema | ||
fi | ||
|
||
# Prepare CVL config file with all traces enabled | ||
if [[ -z ${CVL_CFG_FILE} ]]; then | ||
export CVL_CFG_FILE=${DEST}/cvl_cfg.json | ||
if [[ ! -e ${CVL_CFG_FILE} ]]; then | ||
F=${mgmt_cmn}/cvl/conf/cvl_cfg.json | ||
sed -E 's/((TRACE|LOG).*)\"false\"/\1\"true\"/' $F > ${CVL_CFG_FILE} | ||
fi | ||
fi | ||
|
||
|
||
cat << EOM | ||
DB_CONFIG_PATH=${DB_CONFIG_PATH} | ||
YANG_MODELS_PATH=${YANG_MODELS_PATH} | ||
CVL_SCHEMA_PATH=${CVL_SCHEMA_PATH} | ||
CVL_CFG_FILE=${CVL_CFG_FILE} | ||
EOM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env bash | ||
|
||
function print_usage() { | ||
cat << EOM | ||
usage: $(basename $0) [OPTIONS] [TESTARGS] | ||
OPTIONS: | ||
-pkg PACKAGE Test package name. Should be translib or its child package. | ||
Defaults to translib. | ||
-run PATTERN Testcase pattern. Equivalent of 'go test -run PATTERN ...' | ||
-bench PATTERN Benchmark pattern. Only one of -run or -bench is allowed. | ||
Equivalent of 'go test -bench PATTERN -benchmem -run ^$ ...' | ||
-json Dump test logs in json format. Output can be piped to tools | ||
like tparse or gotestsum. | ||
-vet=off Equivalent to -vet=off option. | ||
TESTARGS: Any other arguments to be passed to TestMain. All values that | ||
do not match above listed options are treated as test args. | ||
Equivalent of 'go test ... -args TESTARGS' | ||
EOM | ||
} | ||
|
||
set -e | ||
|
||
TOPDIR=$(git rev-parse --show-toplevel) | ||
GO=${GO:-go} | ||
|
||
TARGS=( -mod=vendor -cover ) | ||
PARGS=() | ||
PKG=translib | ||
TAG=test | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
-h|-help|--help) print_usage; exit 0;; | ||
-p|-pkg|-package) PKG=$2; shift 2;; | ||
-r|-run) TARGS+=( -run "$2" ); shift 2;; | ||
-b|-bench) TARGS+=( -bench "$2" -benchmem -run "^$" ); shift 2;; | ||
-j|-json) TARGS+=( -json ); ECHO=0; shift;; | ||
-vet=off) TARGS+=( -vet=off ); shift;; | ||
*) PARGS+=( "$1"); shift;; | ||
esac | ||
done | ||
|
||
cd ${TOPDIR} | ||
if [[ ! -d ${PKG} ]] && [[ -d translib/${PKG} ]]; then | ||
PKG=translib/${PKG} | ||
fi | ||
|
||
if [[ -z ${GOPATH} ]]; then | ||
export GOPATH=/tmp/go | ||
fi | ||
|
||
export $(${TOPDIR}/tools/test/env.sh --dest=${TOPDIR}/build/test | xargs) | ||
|
||
[[ ${TARGS[*]} =~ -bench ]] || TARGS+=( -v ) | ||
[[ -z ${TAG} ]] || TARGS+=( -tags ${TAG} ) | ||
[[ "${PARGS[@]}" =~ -(also)?log* ]] || PARGS+=( -logtostderr ) | ||
|
||
[[ ${ECHO} == 0 ]] || set -x | ||
${GO} test ./${PKG} "${TARGS[@]}" -args "${PARGS[@]}" |