From 4bed3349bb2abd490ca56a4945d7b7fc499bef88 Mon Sep 17 00:00:00 2001 From: makkalot Date: Tue, 29 Jul 2008 14:33:10 +0300 Subject: some beautiful tests for group code :) --- test/unittest/test_groups.py | 223 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 188 insertions(+), 35 deletions(-) (limited to 'test/unittest') diff --git a/test/unittest/test_groups.py b/test/unittest/test_groups.py index 95dd795..56e7c2a 100644 --- a/test/unittest/test_groups.py +++ b/test/unittest/test_groups.py @@ -16,59 +16,212 @@ class GroupFileBuilder(object): os.unlink(self.filename) self.cp = ConfigParser.SafeConfigParser() - def create(self, dict): + def create(self, group_dict): # dict is a dict of section names, whose values # are a list of tuples of key=value - # aka foo = {'section1':[(key1, value1), (key2, value2)], - # "section2":[(key3, value3)]} - for section in dict.keys(): + #the dict should be : {'groupname':{'option':(values),'option2':(values)}} + for section,section_value in group_dict.iteritems(): self.cp.add_section(section) - for (option, value) in dict[section]: - self.cp.set(section, option, value) - - fo = open(self.filename, "a+") + for host_option,hosts in section_value.iteritems(): + self.cp.set(section,host_option,",".join(hosts)) + + #save the changes + fo = open(self.filename, "w") self.cp.write(fo) -class GroupsBase(object): - def __init__(self): - self.minions = fc.Minions("*", groups_file=GROUP_TEST) +class TestGroupsBase(object): + def setUp(self): + self.minions = fc.Minions("*", groups_file=GROUP_TEST) + import os + + #we want a fresh copy :) + if os.path.exists(GROUP_TEST): + os.remove(GROUP_TEST) - def test_expand_servers(self): - result = self.minions.get_urls() - print result + self.gfb = GroupFileBuilder() + self.the_groups_dict = { + "home_group":{'host':['first','second','third']}, + } + + self.test_dict = { + "home_group":['first','second','third'] + } + + + #create the file + self.gfb.create(self.the_groups_dict) + + + #def test_expand_servers(self): + # result = self.minions.get_urls() + # print result def test_get_groups(self): + #will reset on every test + self.setUp() result = self.minions.group_class.get_groups() - print result + assert self.test_dict == result -class Groups(GroupsBase): - def get_hosts_by_group_goo(self, group_goo): - group_dict = fc.get_groups() - hosts = fc.get_hosts_by_groupgoo(group_dict, group_goo) - print hosts + def test_add_hosts_to_group(self): + self.setUp() + self.minions.group_class.add_hosts_to_group("home_group","fourth,sixth") + result = self.minions.group_class.get_groups() + self.test_dict["home_group"].append("fourth") + self.test_dict["home_group"].append("sixth") + + #print "The result we got is : ",result + #print "The current into memory is : ",self.test_dict + assert self.test_dict == result + + self.minions.group_class.add_hosts_to_group("home_group","wormy;troll") + self.test_dict["home_group"].append("wormy") + self.test_dict["home_group"].append("troll") + assert self.test_dict == result + -class TestGroups(Groups): - def __init__(self): - self.minions = fc.Minions("@blippy", groups_file=GROUP_TEST) + def test_add_host_to_group(self): + self.setUp() + + self.minions.group_class.add_host_to_group("home_group","bloop") + result = self.minions.group_class.get_groups() + self.test_dict["home_group"].append("bloop") + assert self.test_dict == result + - def setUp(self): - self.gfb = GroupFileBuilder() - self.gfb.create({'blippy':[('host', 'localhost')]}) + def test_save_changes(self): + self.setUp() + self.minions.group_class.add_host_to_group("home_group","bloop") + self.minions.group_class.save_changes() + result = self.minions.group_class.get_groups() + + assert result == self.util_save_change() + + def test_remove_group(self): + self.setUp() + self.minions.group_class.add_group("lab_group") + self.minions.group_class.add_host_to_group("lab_group","bloop") + result = self.minions.group_class.get_groups() + self.test_dict["lab_group"]=[] + self.test_dict["lab_group"].append("bloop") + + assert self.test_dict == result + + self.minions.group_class.remove_group("lab_group") + result = self.minions.group_class.get_groups() + del self.test_dict["lab_group"] + assert self.test_dict == result + + #what if activated the save changes + self.minions.group_class.add_group("lab_group") + self.minions.group_class.add_host_to_group("lab_group","bloop") + self.minions.group_class.save_changes() + self.minions.group_class.remove_group("lab_group",save = True) + result = self.minions.group_class.get_groups() + assert result == self.util_save_change() + - def test_get_host_by_group_goo(self): - results = self.minions.get_urls() - print results + def test_remove_host(self): + self.setUp() + self.minions.group_class.remove_host("home_group","first") + self.test_dict["home_group"].remove("first") + result = self.minions.group_class.get_groups() + assert self.test_dict == result + + #if activated the save changes ON + self.minions.group_class.remove_host("home_group","second",save = True) + result = self.minions.group_class.get_groups() + #print "The result we got is : ",result + #print "The data from file is :i ",self.util_save_change() + assert result == self.util_save_change() + + def test_add_group(self): + self.setUp() + self.minions.group_class.add_group("lab_group") + self.test_dict["lab_group"]=[] + result = self.minions.group_class.get_groups() + + #print "The result after adding the group is : ",result + #print "The current test dict is : ",self.test_dict + #if you have chosen to save the changes ? + assert self.test_dict == result + + self.minions.group_class.add_group("data_group",save = True) + self.test_dict["data_group"]=[] + result = self.minions.group_class.get_groups() + #print "The result we got is : ",result + #print "The data from file is :i ",self.util_save_change() + assert result == self.util_save_change() + + def util_save_change(self): + """ + Will create a tmp object of groups to pull + current changes from conf file + + """ + tmp_minions = fc.Minions("*", groups_file=GROUP_TEST) + result = tmp_minions.group_class.get_groups() + del tmp_minions + #result tobe tested + return result + + def test_get_hosts_by_group_glob(self): + + self.setUp() + spec = "@home_group;some*.com" + tmp_minions = fc.Minions(spec, groups_file=GROUP_TEST) + result = tmp_minions.group_class.get_hosts_by_group_glob(spec) + #print "test_get_hosts_by_group_glob result is : ",result + assert result == self.test_dict["home_group"] + + +class TestMinionGroups(object): + """ + Test the minion methods that wraps the group classes + """ + def setUp(self): + from certmaster.certmaster import CertMaster + cm = CertMaster() + + #firstly create a group of some real ones + self.signed_certs = cm.get_signed_certs() -# FIXME: comment this out till I setup a way to test with a speciic -# test config -akl + #we want a fresh copy :) + if os.path.exists(GROUP_TEST): + os.remove(GROUP_TEST) -# def test_get_hosts_by_groupgoo(self): -# group_dict = fc.get_groups() -# hosts = fc.get_hosts_by_groupgoo(group_dict, "@blippy") -# print hosts + self.gfb = GroupFileBuilder() + self.the_groups_dict = { + "home_group":{'host':self.signed_certs}, + } + + self.test_dict = { + "home_group":self.signed_certs + } + + + #create the file + self.gfb.create(self.the_groups_dict) + + + #print "The signet certs are : ",cm.get_signed_certs() + #self.spec = "@home_group;some*.com" + self.spec = "*" + self.test_minions = fc.Minions(self.spec, groups_file=GROUP_TEST) + + + def test_get_urls(self): + the_urls = self.test_minions.get_urls() + print the_urls + + def test_get_all_hosts(self): + self.setUp() + result = self.test_minions.get_all_hosts() + #print "The result is : ",result + #print "The home group is : ",self.test_dict["home_group"] + assert result == self.test_dict["home_group"] -- cgit From 9bc72b2c3a882e69267f878272df80a7ac35b30e Mon Sep 17 00:00:00 2001 From: makkalot Date: Thu, 31 Jul 2008 18:39:42 +0300 Subject: add more methods for groups api --- test/unittest/test_groups.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/unittest') diff --git a/test/unittest/test_groups.py b/test/unittest/test_groups.py index 56e7c2a..96ad29b 100644 --- a/test/unittest/test_groups.py +++ b/test/unittest/test_groups.py @@ -57,6 +57,7 @@ class TestGroupsBase(object): #def test_expand_servers(self): # result = self.minions.get_urls() # print result + assert result == self.util_save_change() def test_get_groups(self): #will reset on every test @@ -64,6 +65,11 @@ class TestGroupsBase(object): result = self.minions.group_class.get_groups() assert self.test_dict == result + def test_get_group_names(self): + self.setUp() + result = self.minions.group_class.get_group_names() + assert self.test_dict.keys() == result + def test_add_hosts_to_group(self): self.setUp() @@ -90,6 +96,19 @@ class TestGroupsBase(object): self.test_dict["home_group"].append("bloop") assert self.test_dict == result + def test_add_host_list(self): + + self.setUp() + self.minions.group_class.add_host_list("home_group",["bloop","woop","zoo"]) + self.test_dict["home_group"].extend(["bloop","woop","zoo"]) + result = self.minions.group_class.get_groups() + assert self.test_dict == result + + #add one for save + self.minions.group_class.add_host_list("home_group",["hey.com"],save = True) + result = self.minions.group_class.get_groups() + assert result == self.util_save_change() + def test_save_changes(self): self.setUp() @@ -138,7 +157,22 @@ class TestGroupsBase(object): #print "The result we got is : ",result #print "The data from file is :i ",self.util_save_change() assert result == self.util_save_change() + + def test_remove_host_list(self): + self.setUp() + self.minions.group_class.remove_host_list("home_group",["first","second"]) + self.test_dict["home_group"].remove("first") + self.test_dict["home_group"].remove("second") + result = self.minions.group_class.get_groups() + assert self.test_dict == result + #also check the save situation + self.minions.group_class.add_host_to_group("home_group","bloop") + self.minions.group_class.remove_host_list("home_group",["bloop"],save = True) + result = self.minions.group_class.get_groups() + assert result == self.util_save_change() + + def test_add_group(self): self.setUp() self.minions.group_class.add_group("lab_group") -- cgit