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 --- func/overlord/groups.py | 50 +++++++++++++++++++++++++++++++++++++++----- test/unittest/test_groups.py | 34 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/func/overlord/groups.py b/func/overlord/groups.py index a29b793..77010ef 100644 --- a/func/overlord/groups.py +++ b/func/overlord/groups.py @@ -77,8 +77,6 @@ class Groups(object): #a control for it will be missed otherwise :) if len(hoststring)!=0: hosts.append(hoststring) - else: - sys.stderr.write("\nYou can only separate the host by , and ;\n") return hosts #now append the god ones @@ -90,7 +88,7 @@ class Groups(object): return hosts - def add_hosts_to_group(self, group, hoststring): + def add_hosts_to_group(self, group, hoststring,save = False): """ Here you can add more than one hosts to a given group """ @@ -104,7 +102,7 @@ class Groups(object): for host in hosts: self.add_host_to_group(group, host) - def add_host_to_group(self, group, host): + def add_host_to_group(self, group, host,save = False): """ Add a single host to group """ @@ -115,12 +113,34 @@ class Groups(object): if not host in self.__groups[group]: self.__groups[group].append(host) + def add_host_list(self,group,host_list,save = False): + """ + Similar as other add methods but accepts a list of hosts + instead of some strings + """ + if type(host_list) != list: + sys.stderr.write("We accept only lists for for add_host_list method") + return + + for host in host_list: + self.add_host_to_group(group,host) + + if save: + self.save_changes() + + def get_groups(self): """ Simple getter """ return self.__groups + def get_group_names(self): + """ + Getting the groups names + """ + return self.__groups.keys() + def get_hosts_by_group_glob(self, group_glob_str): """ What is that one ? @@ -176,7 +196,9 @@ class Groups(object): return True def remove_host(self,group_name,host,save=False): - + """ + Removes a proper host from the conf file + """ if not self.__groups.has_key(group_name) or not host in self.__groups[group_name]: return False @@ -188,6 +210,24 @@ class Groups(object): return True + def remove_host_list(self,group,host_list,save = False): + """ + Remove a whole list from the conf file of hosts + """ + + if type(host_list) != list: + sys.stderr.write("We accept only lists for for add_host_list method") + return False + + for host in host_list: + self.remove_host(group,host,save = False) + + if save: + self.save_changes() + + + + def add_group(self,group_name,save=False): """ Adding a new group 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