Created
May 16, 2017 23:29
-
-
Save sabre1041/685c8d0c968cccc16e38245f05ac5ca6 to your computer and use it in GitHub Desktop.
Validation script for SVT conformance
This file contains hidden or 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
| #!/usr/bin/env python | |
| import json, subprocess, time, copy, sys, os, yaml, tempfile, shutil, math, urllib2 | |
| from datetime import datetime | |
| from clusterloaderstorage import * | |
| from multiprocessing import Process | |
| from flask import Flask, request | |
| globalvars = {} | |
| globalvars["kubeconfig"] = os.path.expanduser("~") + "/.kube/config" | |
| #globalvars["debugoption"] = False | |
| label_selector="purpose=test" | |
| total_iterations=240 | |
| sleep_timeout=5 | |
| def oc_command(args, globalvars): | |
| tmpfile=tempfile.NamedTemporaryFile() | |
| # see https://github.com/openshift/origin/issues/7063 for details why this is done. | |
| shutil.copyfile(globalvars["kubeconfig"], tmpfile.name) | |
| ret = subprocess.check_output("KUBECONFIG="+tmpfile.name+" "+args, shell=True) | |
| # if globalvars["debugoption"]: | |
| # print args | |
| # if args.find("oc process") == -1: | |
| # print ret | |
| tmpfile.close() | |
| return ret | |
| def validate_project_builds(project, globalvars): | |
| build_configs = [] | |
| build_config_raw = oc_command("oc get bc --no-headers -n={} | awk '{{ print $1 }}'".format(project), globalvars) | |
| build_configs.extend([y for y in (x.strip() for x in build_config_raw.splitlines()) if y]) | |
| if not build_configs: | |
| return 0 | |
| for buildconfig in build_configs: | |
| print "Validating BuildConfig {} in Project {}...".format(buildconfig, project) | |
| count = 0 | |
| while count <= total_iterations: | |
| build = oc_command("oc get builds --no-headers -n={} -l=buildconfig={} --template='{{{{ range .items }}}} {{{{ .metadata.name }}}},{{{{ .status.phase }}}} {{{{ end }}}}'".format(project, buildconfig), globalvars) | |
| if build: | |
| build_results = build.strip().split(",") | |
| if build_results[1] == "Complete": | |
| break | |
| elif build_results[1] == "Failed" or build_results[1] == "Error": | |
| print "Error: BuildConfig {} has status {}".format(build_results[0],build_results[1]) | |
| return 1 | |
| if count == (total_iterations - 1): | |
| print "Failed to validate build exists" | |
| return 1 | |
| count += 1 | |
| time.sleep(sleep_timeout) | |
| return 0 | |
| def validate_project_deployments(project, globalvars): | |
| deployment_configs = [] | |
| deployment_config_raw = oc_command("oc get dc --no-headers -n={} | awk '{{ print $1 }}'".format(project), globalvars) | |
| deployment_configs.extend([y for y in (x.strip() for x in deployment_config_raw.splitlines()) if y]) | |
| if not deployment_configs: | |
| return 0 | |
| for deploymentconfig in deployment_configs: | |
| print "Validating DeploymentConfig {} in Project {}...".format(deploymentconfig, project) | |
| count = 0 | |
| while count <= total_iterations: | |
| latest_deployment = oc_command("oc get dc {} -n {} --template='{{{{ .status.latestVersion }}}}'".format(deploymentconfig,project), globalvars) | |
| if latest_deployment > 0: | |
| rc_name="{}-{}".format(deploymentconfig,latest_deployment) | |
| deployment_status = oc_command("oc get rc {} -n {} --template='{{{{ index .metadata.annotations \"openshift.io/deployment.phase\" }}}}'".format(rc_name,project), globalvars) | |
| if deployment_status == "Complete": | |
| break | |
| elif deployment_status == "Error" or deployment_status == "Failed": | |
| print "Error: DeploymentConfig {} has status {}".format(deploymentconfig,deployment_status) | |
| return 1 | |
| if count == (total_iterations - 1): | |
| print "Failed to validate Deployment for DeploymentConfig {} in Project {}".format(deploymentconfig, project) | |
| return 1 | |
| count += 1 | |
| time.sleep(sleep_timeout) | |
| return 0 | |
| def validate_project_routes(project, globalvars): | |
| routes = [] | |
| routes_raw = oc_command("oc get routes --no-headers -n={} | awk '{{ print $1 }}'".format(project), globalvars) | |
| routes.extend([y for y in (x.strip() for x in routes_raw.splitlines()) if y]) | |
| for route in routes: | |
| print "Validating Route {} in Project {}...".format(route, project) | |
| route_url = oc_command("oc get route {} -n {} --template='{{{{ if .spec.tls }}}}https{{{{ else }}}}http{{{{ end }}}}://{{{{ .spec.host }}}} '".format(route,project), globalvars) | |
| if not route_url.startswith("https"): | |
| try: | |
| connection = urllib2.urlopen(route_url) | |
| httpcode = connection.getcode() | |
| connection.close() | |
| if httpcode != 200: | |
| print "Failed to Validate {} for route {} in project {}. HTTP Code {}".format(route_url,route,project,httpcode) | |
| except urllib2.HTTPError, e: | |
| print "Error accessing {} for route {} in project {}. HTTP Code {}".format(route_url,route,project,e.getCode()) | |
| return 1 | |
| else: | |
| print "Skipping Validation of secure route {} in project {}".format(route,project) | |
| return 0 | |
| project_list = [] | |
| projects_raw = oc_command("oc get projects --no-headers -l={} | awk '{{ print $1 }}'".format(label_selector), globalvars) | |
| project_list.extend([y for y in (x.strip() for x in projects_raw.splitlines()) if y]) | |
| print "\nValidating Builds...\n" | |
| for project in project_list: | |
| result = validate_project_builds(project, globalvars) | |
| if result != 0: | |
| sys.exit(1) | |
| print "\nValidating Deployments...\n" | |
| for project in project_list: | |
| result = validate_project_deployments(project, globalvars) | |
| if result != 0: | |
| sys.exit(1) | |
| print "\nValidating Routes...\n" | |
| for project in project_list: | |
| result = validate_project_routes(project, globalvars) | |
| if result != 0: | |
| sys.exit(1) | |
| print "\nSuccessfully Validated Cluster Components!\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment