Add a module

Add an execute_test.py module to run tempest test against a cloud

Change-Id: I4d27f180fa6a954a2ecec60df4a63c5897c5b502
This commit is contained in:
ted chang 2014-03-05 03:04:32 -08:00
parent 081f91c398
commit 2985b6919d
1 changed files with 169 additions and 0 deletions

169
refstack/tools/execute_test.py Executable file
View File

@ -0,0 +1,169 @@
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2014 IBM Corp.
#
# 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 ConfigParser
import urllib2
import requests
import os
import fnmatch
import subprocess
import json
import argparse
class Test:
def __init__(self, args):
'''Prepare a tempest test against a cloud'''
self.api_ip = args.API_IP
self.test_id = args.TEST_ID
self.miniConfDict = None
self.extraConfDict = json.loads(args.JSON_CONF)
self.mergedConfDict = {}
#Static names
self.tempestHome = '/tempest'
self.sampleConfFile = self.tempestHome + '/etc/tempest.conf.sample'
self.tempestConfFile = self.tempestHome + '/tempest.config'
self.resultDir = self.tempestHome + '/.testrepository/'
self.result = self.resultDir + 'result'
self.sampleConfParser = ConfigParser.SafeConfigParser()
self.sampleConfParser.read(self.sampleConfFile)
def genConfig(self):
''' Find common keys in mini config, extra config and
tempest.conf.sample. Merge values for each key found and
then write to tempest.conf.
'''
self.miniConfDict = json.loads(self.getMiniConfig())
self.mergeToSampleConf(self.miniConfDict)
self.mergeToSampleConf(self.extraConfDict)
self.writeConfToFile()
def mergeToSampleConf(self, new):
'''
A method used by the genConfig method to merge miniConfDict
extraConfDict and tempest sample config to the self.sampleConfParser
instance
'''
#first loop iterates through sections
for section in new:
#second loop iterates through keys in a section
for key in new[section]:
#find common keys and replace values
if self.sampleConfParser.has_option(section, key):
self.sampleConfParser.set(section, key, new[section][key])
def writeConfToFile(self):
'''A method to write the merged self.sampleConfParser to a file
'''
self.sampleConfParser.write(open(self.tempestConfFile, 'w'))
def getMiniConfig(self):
'''Return a mini config in JSON string
'''
url = "http://%s/get-miniconf?test_id=%s" % (self.api_ip, self.test_id)
j = urllib2.urlopen(url=url)
return j.readlines()[0]
def getTestCases(self):
'''A method used by runTestCases.
It returns a list of test cases from mock up API. In the future,
refstack will run the full test.
TODO: We may not need this in the future but we need the flexibility
right now.
'''
url = "http://%s/get-testcases?test_id=%s" % (self.api_ip,
self.test_id)
j = urllib2.urlopen(url=url)
return j.readlines()[0]
def runTestCases(self):
'''Executes each test case in the list
'''
testcases = json.loads(self.getTestCases())['testcases']
try:
for case in testcases:
cmd = ('%s/run_tests.sh -C %s -N -- %s' %
(self.tempestHome, self.tempestConfFile, case))
subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
print 'ERROR: ' + e.__str__()
def postTestResult(self):
'''
Filter file names under self.resultDir by numbers in a list
Sort list as integers from lowest to highest.
Concatenate files to sefl.result and then POST it.
'''
url = "http://%s/post-result?test_id=%s" % (self.api_ip, self.test_id)
r_list = [l for l in os.listdir(self.resultDir)
if fnmatch.fnmatch(l, '[0-9]*')]
r_list.sort(key=int)
with open(self.result, 'w') as outfile:
for r in r_list:
with open(self.resultDir + r, 'r') as infile:
outfile.write(infile.read())
files = {'file': open(self.result, 'rb')}
r = requests.post(url, files=files)
def run(self):
print 'Generating tempest.config'
self.genConfig()
print 'Get tempest test cases and Run test cases'
self.runTestCases()
print 'Send back the result'
self.postTestResult()
#TODO: The remaining methods are for image discovery.
def createImage(self):
'''Download and create cirros image.
Return the image reference id
'''
#connection = httplib.HTTPSConnection()
pass
def findSmallestFlavor(self):
'''Find the smallest list flavor by sorting the memory
'''
pass
def deleteImage(self):
'''Delete a image
'''
pass
if __name__ == '__main__':
''' Generate tempest.conf from a tempest.conf.sample and then run test
Example:
execute_test.py 172.42.17.1:8000 1 '{"section":{"key":"value",..}}'
'''
parser = argparse.ArgumentParser(description='Starts a tempest test \
associated with a test_id')
parser.add_argument("API_IP",
help="refstack API server IP to retrieve \
configurations. i.e.: 127.0.0.1:8000")
parser.add_argument("TEST_ID",
help="test ID associated with a test")
'''
TODO: Need to decrypt/encrypt password in the json string (args.JSON_CONF)
'''
parser.add_argument("JSON_CONF",
help="Tempest Configurations in JSON string")
args = parser.parse_args()
test = Test(args)
test.run()