Skip to content

Commit

Permalink
Merge pull request #17 from PelionIoT/jq-handling
Browse files Browse the repository at this point in the history
jq, KaaS server address from config & Alpine 3.18
  • Loading branch information
IzumaBOT authored Sep 4, 2023
2 parents a475187 + f90b8cc commit 8c75d2a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 27 deletions.
2 changes: 1 addition & 1 deletion conf/test_pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ spec:
- echo 'pod is running'; sleep 6000000
command:
- /bin/sh
image: alpine:3.9
image: alpine:3.18
imagePullPolicy: IfNotPresent
name: client
resources:
Expand Down
78 changes: 63 additions & 15 deletions scripts/perts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
set -e
SCRIPTNAME="perts.sh"
REPONAME="pelion-edge-ready-test-suite"
SERVER="https://edge-k8s.us-east-1.mbedcloud.com"
KAASSERVER="https://edge-k8s.us-east-1.mbedcloud.com"
KAASSERVER_GIVEN=0
CURDIR=$(pwd)
CONFIG=""
TESTCONFIG="test-config.json"
Expand All @@ -37,17 +38,63 @@ usage() {
echo "Usage: scripts/$SCRIPTNAME -a access-key [-s Edge K8S server URL] [-e]"
echo " -a access key"
echo " -c config file to use (will try to auto-detect if not give)"
echo " -s Edge K8S edge-server URL, default $SERVER"
echo " -s Edge K8S edge-server URL, default $KAASSERVER (if -c config is given, picked up from there)"
echo " -p edge status port, default $EDGEPORT"
echo " -e echo commands (debug), default off"
echo " --help Show this help message and exit"
echo ""
echo "Requires jq to be installed."
echo ""
echo "NOTE! Must be run as sudo, installation steps require sudo rights."
echo "You must run this script from the $REPONAME -folder."
echo "I.e. scripts/$SCRIPTNAME -a <accesskey>"
echo "I.e."
echo " sudo scripts/$SCRIPTNAME -a <accesskey>"
echo " sudo scripts/$SCRIPTNAME -a <accesskey> -c <test-config-file.json>"
exit 1
}

assertInstalled() {
for var in "$@"; do
if ! which "$var" &> /dev/null; then
echo "Please install $var - for example sudo apt-get install $var!"
exit 1
fi
done
}

# cleanup - delete the created config files before exiting.
# - if a parameters is not given, we exit with error code
# as we assume we got trapped.
cleanup() {
local exit_code=0
if [ $# -eq 0 ]; then
exit_code=1
fi
echo "Cleaning up..."

# Check if the config file exists before attempting to delete it
if [ -e "$ROOTKUBECFG" ]; then
rm -f "$ROOTKUBECFG"
echo "Configuration file deleted: $ROOTKUBECFG"
fi
if [ -e "$TESTCONFIG" ]; then
rm -f "$TESTCONFIG"
echo "Test configuration file deleted: $TESTCONFIG"
fi

if [[ -n "$rand_kube" ]];then
mv "$ROOTKUBECFG.$rand_kube" "$ROOTKUBECFG"
fi
if [[ -n "$rand_cfg" ]];then
mv "$TESTCONFIG.$rand_cfg" "$TESTCONFIG"
fi
# Exit the script

exit $exit_code
}

trap 'cleanup' INT TERM ERR

curdir=$(pwd)
lastdir="${curdir##*/}"
if [[ "$lastdir" != "pelion-edge-ready-test-suite" ]]; then
Expand All @@ -60,7 +107,8 @@ while getopts "a:c:s:p:e" opt; do
case $opt in
a ) ACCESSKEY="$OPTARG" ;;
c ) CONFIG="$OPTARG" ;;
s ) SERVER="$OPTARG" ;;
s ) KAASSERVER="$OPTARG"
KAASSERVER_GIVEN=1 ;;
p ) EDGEPORT="$OPTARG"
EDGEPORT_GIVEN=1
;;
Expand Down Expand Up @@ -106,6 +154,8 @@ case $(uname -m) in
;;
esac

assertInstalled jq

# Get nodejs
# wget https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-arm64.tar.gz
# or (depending on your CPU-architecture)
Expand Down Expand Up @@ -166,12 +216,17 @@ if [ -e "$ROOTKUBECFG" ]; then
mv "$ROOTKUBECFG" "$ROOTKUBECFG.$rand_kube"
fi

echo "Creating kubectl config file to $ROOTKUBECFG"
mkdir -p "/home/root/.kube"
# Only try digging up KaaS server from conf file, if it was not given already
cd "$CURDIR"
if [ -n "$CONFIG" ] && [ $KAASSERVER_GIVEN = 0 ]; then
KAASSERVER=$(jq -r '.specifications.kaasServicesAddress' < "$CONFIG" )
fi

echo "Creating kubectl config file to $ROOTKUBECFG against $KAASSERVER"
echo "apiVersion: v1
clusters:
- cluster:
server: $SERVER
server: $KAASSERVER
name: edge-k8s
contexts:
- context:
Expand Down Expand Up @@ -275,13 +330,6 @@ sudo --preserve-env "$NODECMD" index.js -c "$TESTCONFIG"

# Do not leave credentials floating about, delete the kubectl config file.
# Restore original files, if there were any...
rm "$ROOTKUBECFG"
rm "$TESTCONFIG"
if [[ -n "$rand_kube" ]];then
mv "$ROOTKUBECFG.$rand_kube" "$ROOTKUBECFG"
fi
if [[ -n "$rand_cfg" ]];then
mv "$TESTCONFIG.$rand_cfg" "$TESTCONFIG"
fi
cleanup 0

echo "DONE - Check test results under suite_results -folder."
2 changes: 1 addition & 1 deletion test-configs/avnet-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "pelion-edge-ready-test-suite",
"version":"2.6.0",
"version":"2.6.1",
"device_type": "avnet",
"tests_directory": "test-cases",
"accountID": "",
Expand Down
2 changes: 1 addition & 1 deletion test-configs/imx8-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "pelion-edge-ready-test-suite",
"version":"2.6.0",
"version":"2.6.1",
"device_type": "i.MX8",
"tests_directory": "test-cases",
"accountID": "",
Expand Down
2 changes: 1 addition & 1 deletion test-configs/rpi3-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "pelion-edge-ready-test-suite",
"version":"2.6.0",
"version":"2.6.1",
"device_type": "rpi3bplus",
"tests_directory": "test-cases",
"accountID": "",
Expand Down
2 changes: 1 addition & 1 deletion test-configs/rpi4-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "pelion-edge-ready-test-suite",
"version":"2.6.0",
"version":"2.6.1",
"device_type": "rpi4",
"tests_directory": "test-cases",
"accountID": "",
Expand Down
2 changes: 1 addition & 1 deletion test-configs/snap-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "pelion-edge-ready-test-suite",
"version":"2.6.0",
"version":"2.6.1",
"tests_directory": "test-cases",
"accountID": "",
"internal_id": "",
Expand Down
30 changes: 24 additions & 6 deletions utils/kaas_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports.podConfig = (
nodename,
label = { app: 'test' },
containername = 'client',
conatinerimage = 'alpine:3.9'
containerimage = 'alpine:3.18'
) => {
return {
apiVersion: 'v1',
Expand All @@ -41,7 +41,13 @@ module.exports.podConfig = (
containers: [
{
name: containername,
image: conatinerimage,
image: containerimage,
resources: {
limits: {
cpu: '200m',
memory: '100Mi'
}
},
command: ['/bin/sh'],
args: ['-c', "echo 'hello'; sleep 6000000"]
}
Expand All @@ -55,7 +61,7 @@ module.exports.podWithHostNW = (
nodename,
label = { app: 'test' },
containername = 'client',
conatinerimage = 'alpine:3.9'
containerimage = 'alpine:3.18'
) => {
return {
apiVersion: 'v1',
Expand All @@ -72,7 +78,13 @@ module.exports.podWithHostNW = (
containers: [
{
name: containername,
image: conatinerimage,
image: containerimage,
resources: {
limits: {
cpu: '200m',
memory: '100Mi'
}
},
command: ['/bin/sh'],
args: ['-c', "echo 'hello'; sleep 6000000"]
}
Expand All @@ -86,7 +98,7 @@ module.exports.podWithFixHostname = (
nodename,
label = { app: 'test' },
containername = 'client',
conatinerimage = 'alpine:3.9'
containerimage = 'alpine:3.18'
) => {
return {
apiVersion: 'v1',
Expand All @@ -102,7 +114,13 @@ module.exports.podWithFixHostname = (
containers: [
{
name: containername,
image: conatinerimage,
image: containerimage,
resources: {
limits: {
cpu: '200m',
memory: '100Mi'
}
},
command: ['/bin/sh'],
args: ['-c', "echo 'hello'; sleep 6000000"]
}
Expand Down

0 comments on commit 8c75d2a

Please sign in to comment.