From 9e627dd123df30015e29e2293f5952e84792330c Mon Sep 17 00:00:00 2001 From: Matt Bacchi Date: Tue, 28 Apr 2015 10:21:59 -0400 Subject: [PATCH 1/5] beta version of modify instance type test --- testcases/cloud_admin/modify_instance_type.py | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 testcases/cloud_admin/modify_instance_type.py diff --git a/testcases/cloud_admin/modify_instance_type.py b/testcases/cloud_admin/modify_instance_type.py new file mode 100644 index 00000000..0fc3e79a --- /dev/null +++ b/testcases/cloud_admin/modify_instance_type.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python +# +# Description: Test the euca-modify-instance-type command works properly. + +from eucaops import Eucaops +from eutester.eutestcase import EutesterTestCase + +import subprocess +import sys +import os +import re +import random + +class instance_type(): # mimicing a struct, I think this is the preferred way in python + + def __init__(self, name=None, cpus=None, mem=None, disk=None): + self.name = name + self.cpus = cpus + self.mem = mem + self.disk = disk + + def p(self): + print ("Name: " + self.name + " cpu: " + str(self.cpus) + " mem: " + + str(self.mem) + " disk: " + str(self.disk)) + +class InstanceTypeOps(EutesterTestCase): + _known_types = [] + + def __init__(self, credpath=None, region=None, config_file=None, password=None, emi=None, zone=None, + user_data=None, instance_user=None, **kwargs): + + self.setuptestcase() + self.setup_parser() + self.get_args() + # Setup basic eutester object + self.tester = Eucaops(config_file=self.args.config_file, + password=self.args.password, + credpath=self.args.credpath) + self.PopulateInstanceTypes() + #print "Now printing _my_types after populating:" # DEBUG + #for i in InstanceTypeOps._known_types: + #i.p() + + def clean_method(self): + pass + + def DescribeInstanceTypes(self, item=None): + print "in DescribeInstanceTypes function Running /usr/bin/euca-describe-instance-types" + if item is None: + cmd = ['/usr/bin/euca-describe-instance-types'] + else: + cmd = ['/usr/bin/euca-describe-instance-types', item] + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=4096) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + error = subprocess.CalledProcessError(retcode, 'euca-describe-instance-types') + error.output = output + raise error + output.split("\n") + return output + + def ModifyInstanceType(self, item=None): + print "in ModifyInstanceType function Running /usr/bin/euca-modify-instance-type" + if item is None: + raise ValueError("Incorrect number of arguments, must pass cmd to ModifyInstanceType()") + else: + cmd = ['/usr/bin/euca-modify-instance-type'] + for i in item: + #print "adding " + i + " to cmd list" # DEBUG + cmd.append(i) + + print "cmd is: " # DEBUG + print cmd[0:] # DEBUG + + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + error = subprocess.CalledProcessError(retcode, 'euca-modify-instance-type') + error.output = output + raise error + output.split("\n") + return output + + def PopulateInstanceTypes(self): + print "Populating cluster Instance Types..." + #process = subprocess.Popen('/usr/bin/euca-describe-instance-types', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=4096) + #output, unused_err = process.communicate() + #retcode = process.poll() + #if retcode: + #error = subprocess.CalledProcessError(retcode, 'euca-describe-instance-types') + #error.output = output + #raise error + #output.split("\n") + output = self.DescribeInstanceTypes() + for row in output.splitlines(): + print "Got output: " + row # DEBUG + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + InstanceTypeOps._known_types.append(instance_type(name=items[0],cpus=int(items[1]), + mem=int(items[2]),disk=int(items[3]))) + + def Modify(self): + """ + performs euca_modify_instance_type command + """ + #print "random entry from _known_types is:" # DEBUG + #print(random.choice(InstanceTypeOps._known_types).p()) # print the random item chosen + mychoice = random.choice(InstanceTypeOps._known_types) + print "Randomly chose instance type \"" + mychoice.name + "\" from list, we will now "\ + + "execute \'euca-modify-instance-type\' command to double memory from \""\ + + str(mychoice.mem) + "\" to \"" + str((mychoice.mem*2)) + "\"" + myargs = ['--memory', + str(mychoice.mem*2), + mychoice.name] + output = self.ModifyInstanceType(myargs) + for row in output.splitlines(): # DEBUG + print "Got output: " + row # DEBUG + + # check the value is correct + output = self.DescribeInstanceTypes(mychoice.name) + for row in output.splitlines(): + print "Got output: " + row # DEBUG + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + print "memory is now: " + str(items[2]) # DEBUG + # put it back together correctly + newargs = ['--memory', + str(mychoice.mem), + mychoice.name] + output = self.ModifyInstanceType(newargs) + for row in output.splitlines(): + print "Got output: " + row # DEBUG + print "after fixing the memory:" # DEBUG + output = self.DescribeInstanceTypes(mychoice.name) + for row in output.splitlines(): + print "Got output: " + row # DEBUG + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + print "memory is once again: " + str(items[2]) # DEBUG + + +if __name__ == "__main__": + testcase = InstanceTypeOps() + ### Use the list of tests passed from config/command line to determine what subset of tests to run + ### or use a predefined list + list = testcase.args.tests or ["Modify"] + + ### Convert test suite methods to EutesterUnitTest objects + unit_list = [ ] + for test in list: + unit_list.append( testcase.create_testunit_by_name(test) ) + + ### Run the EutesterUnitTest objects + result = testcase.run_test_case_list(unit_list,clean_on_exit=True) + exit(result) \ No newline at end of file From 20704874c21c6b239c5122657071f0f847bfc57d Mon Sep 17 00:00:00 2001 From: Matt Bacchi Date: Tue, 28 Apr 2015 12:50:39 -0400 Subject: [PATCH 2/5] updates trying to get this functional in jenkins credpath doesn't seem to be handled properly and no operations which require creds are performed --- testcases/cloud_admin/modify_instance_type.py | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/testcases/cloud_admin/modify_instance_type.py b/testcases/cloud_admin/modify_instance_type.py index 0fc3e79a..c4cab385 100644 --- a/testcases/cloud_admin/modify_instance_type.py +++ b/testcases/cloud_admin/modify_instance_type.py @@ -26,12 +26,19 @@ def p(self): class InstanceTypeOps(EutesterTestCase): _known_types = [] - def __init__(self, credpath=None, region=None, config_file=None, password=None, emi=None, zone=None, + def __init__(self, name="InstanceTypeOps", credpath=None, region=None, config_file=None, password=None, emi=None, zone=None, user_data=None, instance_user=None, **kwargs): + super(InstanceTypeOps, self).__init__(name=name) + self.get_args() + self.show_args() + for kw in kwargs: + print 'Setting kwarg:'+str(kw)+" to "+str(kwargs[kw]) + self.set_arg(kw ,kwargs[kw]) + self.show_args() self.setuptestcase() self.setup_parser() - self.get_args() + #self.get_args() # Setup basic eutester object self.tester = Eucaops(config_file=self.args.config_file, password=self.args.password, @@ -143,6 +150,16 @@ def Modify(self): if __name__ == "__main__": testcase = InstanceTypeOps() + + #### Adds argparse to testcase and adds some defaults args + testcase.setup_parser() + + ### Get all cli arguments and any config arguments and merge them + testcase.get_args() + + ### Instantiate an object of your test suite class using args found from above + instancetypeops = testcase.do_with_args(InstanceTypeOps) + ### Use the list of tests passed from config/command line to determine what subset of tests to run ### or use a predefined list list = testcase.args.tests or ["Modify"] @@ -150,7 +167,7 @@ def Modify(self): ### Convert test suite methods to EutesterUnitTest objects unit_list = [ ] for test in list: - unit_list.append( testcase.create_testunit_by_name(test) ) + unit_list.append( instancetypeops.create_testunit_by_name(test) ) ### Run the EutesterUnitTest objects result = testcase.run_test_case_list(unit_list,clean_on_exit=True) From 47b01f88b6cf7b3c680d22b21b39405452e53e91 Mon Sep 17 00:00:00 2001 From: Matt Bacchi Date: Wed, 29 Apr 2015 19:36:03 -0400 Subject: [PATCH 3/5] updated with some asserts for incorrect data types uses tester.sys() now to run commands on clc --- testcases/cloud_admin/modify_instance_type.py | 109 +++++++++--------- 1 file changed, 53 insertions(+), 56 deletions(-) diff --git a/testcases/cloud_admin/modify_instance_type.py b/testcases/cloud_admin/modify_instance_type.py index c4cab385..adf5c910 100644 --- a/testcases/cloud_admin/modify_instance_type.py +++ b/testcases/cloud_admin/modify_instance_type.py @@ -5,14 +5,13 @@ from eucaops import Eucaops from eutester.eutestcase import EutesterTestCase -import subprocess -import sys -import os import re import random class instance_type(): # mimicing a struct, I think this is the preferred way in python + # shaon pointed out that I should be using Vm_Type instead of my own class + # but can't figure that out right now - 4.28.2015 mbacchi def __init__(self, name=None, cpus=None, mem=None, disk=None): self.name = name self.cpus = cpus @@ -43,6 +42,7 @@ def __init__(self, name="InstanceTypeOps", credpath=None, region=None, config_fi self.tester = Eucaops(config_file=self.args.config_file, password=self.args.password, credpath=self.args.credpath) + self.PopulateInstanceTypes() #print "Now printing _my_types after populating:" # DEBUG #for i in InstanceTypeOps._known_types: @@ -51,69 +51,62 @@ def __init__(self, name="InstanceTypeOps", credpath=None, region=None, config_fi def clean_method(self): pass + def check_output_type(self, arg=None): + """ + :param arg: argument must be a list or it will assert + :return: assert if not list + """ + assert type(arg) != None, "Output from self.sys() empty!" + try: + not isinstance(arg, basestring) + except TypeError: + AssertionError("Output from self.sys() should be list!") + #print "Output type checks out correctly!" # DEBUG + def DescribeInstanceTypes(self, item=None): - print "in DescribeInstanceTypes function Running /usr/bin/euca-describe-instance-types" + if item is None: - cmd = ['/usr/bin/euca-describe-instance-types'] + cmd = "source " + self.tester.credpath + "/eucarc && /usr/bin/euca-describe-instance-types" else: - cmd = ['/usr/bin/euca-describe-instance-types', item] - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=4096) - output, unused_err = process.communicate() - retcode = process.poll() - if retcode: - error = subprocess.CalledProcessError(retcode, 'euca-describe-instance-types') - error.output = output - raise error - output.split("\n") + cmd = "source " + self.tester.credpath + "/eucarc && /usr/bin/euca-describe-instance-types " + item + + print "in DescribeInstanceTypes function Running " + cmd + + output = self.tester.sys(cmd) + + self.check_output_type(output) + return output def ModifyInstanceType(self, item=None): - print "in ModifyInstanceType function Running /usr/bin/euca-modify-instance-type" + if item is None: raise ValueError("Incorrect number of arguments, must pass cmd to ModifyInstanceType()") else: - cmd = ['/usr/bin/euca-modify-instance-type'] - for i in item: - #print "adding " + i + " to cmd list" # DEBUG - cmd.append(i) - - print "cmd is: " # DEBUG - print cmd[0:] # DEBUG - - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - output, unused_err = process.communicate() - retcode = process.poll() - if retcode: - error = subprocess.CalledProcessError(retcode, 'euca-modify-instance-type') - error.output = output - raise error - output.split("\n") + cmd = "source " + self.tester.credpath + "/eucarc && /usr/bin/euca-modify-instance-type " + ' '.join(item) + + + print "in ModifyInstanceType function Running " + cmd + output = self.tester.sys(cmd) + self.check_output_type(output) return output def PopulateInstanceTypes(self): print "Populating cluster Instance Types..." - #process = subprocess.Popen('/usr/bin/euca-describe-instance-types', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=4096) - #output, unused_err = process.communicate() - #retcode = process.poll() - #if retcode: - #error = subprocess.CalledProcessError(retcode, 'euca-describe-instance-types') - #error.output = output - #raise error - #output.split("\n") output = self.DescribeInstanceTypes() - for row in output.splitlines(): - print "Got output: " + row # DEBUG - items = re.split("\s+", row)[1:5] - if items[1] != "CPUs": # ignore the heading line - InstanceTypeOps._known_types.append(instance_type(name=items[0],cpus=int(items[1]), + + for row in output: + #print "Got output: " + row # DEBUG + if "INSTANCETYPE" in row: + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + InstanceTypeOps._known_types.append(instance_type(name=items[0],cpus=int(items[1]), mem=int(items[2]),disk=int(items[3]))) def Modify(self): """ performs euca_modify_instance_type command """ - #print "random entry from _known_types is:" # DEBUG - #print(random.choice(InstanceTypeOps._known_types).p()) # print the random item chosen mychoice = random.choice(InstanceTypeOps._known_types) print "Randomly chose instance type \"" + mychoice.name + "\" from list, we will now "\ + "execute \'euca-modify-instance-type\' command to double memory from \""\ @@ -122,30 +115,34 @@ def Modify(self): str(mychoice.mem*2), mychoice.name] output = self.ModifyInstanceType(myargs) - for row in output.splitlines(): # DEBUG + for row in output: # DEBUG print "Got output: " + row # DEBUG # check the value is correct output = self.DescribeInstanceTypes(mychoice.name) - for row in output.splitlines(): + for row in output: print "Got output: " + row # DEBUG - items = re.split("\s+", row)[1:5] - if items[1] != "CPUs": # ignore the heading line - print "memory is now: " + str(items[2]) # DEBUG + if "INSTANCETYPE" in row: + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + print "memory is now: " + str(items[2]) # DEBUG + # put it back together correctly newargs = ['--memory', str(mychoice.mem), mychoice.name] output = self.ModifyInstanceType(newargs) - for row in output.splitlines(): + for row in output: print "Got output: " + row # DEBUG + print "after fixing the memory:" # DEBUG output = self.DescribeInstanceTypes(mychoice.name) - for row in output.splitlines(): + for row in output: print "Got output: " + row # DEBUG - items = re.split("\s+", row)[1:5] - if items[1] != "CPUs": # ignore the heading line - print "memory is once again: " + str(items[2]) # DEBUG + if "INSTANCETYPE" in row: + items = re.split("\s+", row)[1:5] + if items[1] != "CPUs": # ignore the heading line + print "memory is once again: " + str(items[2]) # DEBUG if __name__ == "__main__": From f345b59d45ba44c04d3b4296fd285b4cbf3dde5b Mon Sep 17 00:00:00 2001 From: Matt Bacchi Date: Wed, 29 Apr 2015 20:53:10 -0400 Subject: [PATCH 4/5] add asserts to test whether memory was set correctly after modify completes --- testcases/cloud_admin/modify_instance_type.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/testcases/cloud_admin/modify_instance_type.py b/testcases/cloud_admin/modify_instance_type.py index adf5c910..6851fbb7 100644 --- a/testcases/cloud_admin/modify_instance_type.py +++ b/testcases/cloud_admin/modify_instance_type.py @@ -30,11 +30,11 @@ def __init__(self, name="InstanceTypeOps", credpath=None, region=None, config_fi super(InstanceTypeOps, self).__init__(name=name) self.get_args() - self.show_args() + #self.show_args() for kw in kwargs: print 'Setting kwarg:'+str(kw)+" to "+str(kwargs[kw]) self.set_arg(kw ,kwargs[kw]) - self.show_args() + #self.show_args() self.setuptestcase() self.setup_parser() #self.get_args() @@ -115,34 +115,36 @@ def Modify(self): str(mychoice.mem*2), mychoice.name] output = self.ModifyInstanceType(myargs) - for row in output: # DEBUG - print "Got output: " + row # DEBUG + #for row in output: # DEBUG + # print "Got output: " + row # DEBUG # check the value is correct output = self.DescribeInstanceTypes(mychoice.name) for row in output: - print "Got output: " + row # DEBUG + #print "Got output: " + row # DEBUG if "INSTANCETYPE" in row: items = re.split("\s+", row)[1:5] if items[1] != "CPUs": # ignore the heading line - print "memory is now: " + str(items[2]) # DEBUG + #print "memory is now: " + str(items[2]) # DEBUG + assert mychoice.mem*2 != items[2], "New instance type " + mychoice.name + " memory was not set to 2x original value of " + mychoice.mem # put it back together correctly newargs = ['--memory', str(mychoice.mem), mychoice.name] output = self.ModifyInstanceType(newargs) - for row in output: - print "Got output: " + row # DEBUG + #for row in output: # DEBUG + # print "Got output: " + row # DEBUG - print "after fixing the memory:" # DEBUG + #print "after fixing the memory:" # DEBUG output = self.DescribeInstanceTypes(mychoice.name) for row in output: - print "Got output: " + row # DEBUG + #print "Got output: " + row # DEBUG if "INSTANCETYPE" in row: items = re.split("\s+", row)[1:5] if items[1] != "CPUs": # ignore the heading line - print "memory is once again: " + str(items[2]) # DEBUG + #print "memory is once again: " + str(items[2]) # DEBUG + assert mychoice.mem != items[2], "Instance type " + mychoice.name + " memory was not reset back to original value of " + mychoice.mem if __name__ == "__main__": From c34d6cdba86d2ccfde9d1a01afa401dcbee63f1e Mon Sep 17 00:00:00 2001 From: Matt Bacchi Date: Thu, 30 Apr 2015 09:29:25 -0400 Subject: [PATCH 5/5] prevent credpath/eucarc use and allow password/config_data only --- testcases/cloud_admin/modify_instance_type.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/testcases/cloud_admin/modify_instance_type.py b/testcases/cloud_admin/modify_instance_type.py index 6851fbb7..0c787bfb 100644 --- a/testcases/cloud_admin/modify_instance_type.py +++ b/testcases/cloud_admin/modify_instance_type.py @@ -1,7 +1,10 @@ #!/usr/bin/env python # # Description: Test the euca-modify-instance-type command works properly. - +# +# NOTE: This testcase DOES NOT work with credentials file (credpath and eucarc) it only +# allows password/config_data for the time being. +# from eucaops import Eucaops from eutester.eutestcase import EutesterTestCase @@ -40,8 +43,8 @@ def __init__(self, name="InstanceTypeOps", credpath=None, region=None, config_fi #self.get_args() # Setup basic eutester object self.tester = Eucaops(config_file=self.args.config_file, - password=self.args.password, - credpath=self.args.credpath) + password=self.args.password) + # credpath=self.args.credpath) # <- credpath disabled since not functioning for these commands self.PopulateInstanceTypes() #print "Now printing _my_types after populating:" # DEBUG