Refactored ranger-agent-test pod

This patch refactor ranger-agent-test pod to use python
module. It also fixes incompatible issue with the old
script that failed pod test with updated ranger-agent pod.

Change-Id: Ied86a3d137d6cff206eecbff5c33b7c0d50f90fb
This commit is contained in:
Chi Lo 2019-11-14 14:32:53 -08:00
parent fef8b29c70
commit cb8c4a9f07
4 changed files with 166 additions and 77 deletions

View File

@ -0,0 +1,157 @@
#!/usr/bin/env python
import base64
import os
import json
import requests
import sys
import uuid
import time
def get_token():
headers = {'Content-Type': 'application/json'}
keystone_ep = os.environ['OS_AUTH_URL']
url = keystone_ep + '/auth/tokens'
data = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": os.environ['OS_USERNAME'],
"domain": {
"name": os.environ['OS_USER_DOMAIN_NAME']
},
"password": os.environ['OS_PASSWORD']
}
}
},
"scope": {
"project": {
"domain": {
"name": os.environ['OS_PROJECT_DOMAIN_NAME']
},
"name": os.environ['OS_PROJECT_NAME']
}
}
}
}
try:
resp = requests.post(url, data=json.dumps(data), headers=headers)
if resp.status_code != 201:
sys.stderr.write("Failed to get token for region\n")
sys.exit(1)
token = resp.headers['x-subject-token']
return token
except Exception as exp:
sys.stderr.write(
"Exp: Failed to get token for region: %s\n" % str(exp))
sys.exit(1)
def notify_ranger_agent_api(uuid):
""" Send notification message to Ranger-agent-api. """
region = os.environ['OS_REGION_NAME']
url = os.environ['RANGER_SERVICE_URL']
# get token for region and send to ranger-agent-api
headers = {}
headers['X-Auth-Token'] = get_token()
# Prepare the request body
data_to_send = {
'ord-notifier': {
'request-id': uuid,
'resource-id': uuid,
'resource-type': 'flavor',
'resource-template-version': '1',
'resource-template-name': 'sanity-test.yaml',
'resource-template-type': 'hot',
'operation': 'create',
'region': region
}
}
invalid_template_data = 'template: heat_template_version: 2020-01-01'
files = {
'json': (None, json.dumps(data_to_send), 'application/json'),
'file': ('heat_template',
base64.b64encode(invalid_template_data.encode()),
'application/yaml')}
exit_code = 1
# Retry up to 5 times
for i in range(5):
time.sleep(15)
try:
resp = requests.post('%s/v1/ord/ord_notifier' % (url),
files=files,
headers=headers)
if resp.status_code != 200:
message = 'failure respond code [%d] received.' % (
resp.status_code)
sys.stderr.write("ORD notification failed: %s\n" % message)
continue
else:
ord_status = resp.json()['ord-notifier-response']['status']
if ord_status == 'Submitted':
sys.stderr.write("ORD notification completed.\n")
exit_code = 0
break
else:
sys.stderr.write("Unexpected ord status: %s\n" %
ord_status)
continue
except Exception as exp:
sys.stderr.write(
"Exp: Failed to post resource: %s\n" % str(exp))
continue
if exit_code:
sys.exit(exit_code)
def validate_resource_status(uuid):
url = os.environ['RANGER_SERVICE_URL']
expected_code = os.environ['END_STATUS_KEY']
exit_code = 1
# Retry up to 5 times
for i in range(5):
time.sleep(15)
try:
resp = requests.get('%s/v1/ord/ord_notifier?Id=%s' % (url, uuid))
if resp.status_code != 200:
sys.stderr.write("Unexpected status code received: %s\n" %
resp.status_code)
continue
else:
ord_error = resp.json()['rds-listener']['error-code']
if ord_error == expected_code:
sys.stderr.write("Expected error code received: %s\n" %
ord_error)
exit_code = 0
break
else:
sys.stderr.write("Unexpected error code received: %s\n" %
ord_error)
continue
except Exception as exp:
sys.stderr.write(
"Exp: Failed to get resource status: %s\n" % str(exp))
continue
sys.exit(exit_code)
if __name__ == "__main__":
test_uuid = uuid.uuid1().hex
notify_ranger_agent_api(test_uuid)
validate_resource_status(test_uuid)

View File

@ -1,70 +0,0 @@
#!/bin/bash
{{/*
Copyright 2017 The Openstack-Helm Authors.
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.
*/}}
set -ex
# Come up with a ranger agent payload
region="${REGION_NAME}"
url="${RANGER_SERVICE_URL}"
UUID=$(python -c 'import uuid; print(uuid.uuid1())')
PAYLOAD="{\"ord-notifier\":{
\"request-id\":\"$UUID\",
\"resource-id\":\"$UUID\",
\"resource-type\":\"flavor\",
\"resource-template-version\":\"1\",
\"resource-template-name\":\"sanity-test.yaml\",
\"resource-template-type\":\"hot\",
\"operation\":\"create\",
\"region\":\"$region\"
}
}"
function assertContains()
{
n=0
expected=$1
until [ $n -ge 5 ]
do
if [ "$expected" == "Submitted" ]; then
msg="$(curl -i -X POST -d "${PAYLOAD}" $url --header "Content-type:application/json")"
else
msg="$(curl -s "$url?Id=$UUID")"
fi
if echo "$msg" | grep -q "$expected"; then
echo "***TEST IS PASSED: EXPECTED=$expected is in Response"
break
else
if [ "$n" == "5" ]; then
echo "***FAILED: EXPECTED=$expected in Response"
exit 1
fi
n=$[$n+1]
sleep 15
fi
done
}
assertContains "Submitted"
# Ranger agent support pull or push both model.
# once request submitted openstack take some time to synchronize.
# we are pulling status for testing purpose by sleeping thread for 15 sec
assertContains "${END_STATUS_KEY}"

View File

@ -40,8 +40,8 @@ data:
{{ tuple "bin/_ranger-agent-engine.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
health-probe.py: |
{{ tuple "bin/_health-probe.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
ranger-agent-test.sh: |+
{{ tuple "bin/_ranger-agent-test.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
ranger-agent-test.py: |+
{{ tuple "bin/_ranger-agent-test.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
rabbit-init.sh: |
{{- include "helm-toolkit.scripts.rabbit_init" . | indent 4 }}
{{- end }}

View File

@ -44,16 +44,18 @@ spec:
env:
- name: RANGER_SERVICE_URL
value: {{ tuple "ranger_agent" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" }}
- name: REGION_NAME
value: {{ .Values.endpoints.identity.auth.ranger_agent.region_name }}
- name: END_STATUS_KEY
value: {{ .Values.conf.test.expected_end_status_key }}
{{- with $env := dict "ksUserSecret" .Values.secrets.identity.ranger_agent }}
{{- include "helm-toolkit.snippets.keystone_openrc_env_vars" $env | indent 8 }}
{{- end }}
command:
- /tmp/ranger-agent-test.sh
- python
- /tmp/ranger-agent-test.py
volumeMounts:
- name: ranger-agent-bin
mountPath: /tmp/ranger-agent-test.sh
subPath: ranger-agent-test.sh
mountPath: /tmp/ranger-agent-test.py
subPath: ranger-agent-test.py
readOnly: true
{{ if $mounts_tests.volumeMounts }}{{ toYaml $mounts_tests.volumeMounts | indent 8 }}{{ end }}
volumes: