def _launch(self, ports=[], passwords=[], op=None): LOG.debug('Launching redis processes on ports %s with passwords %s' % (ports, passwords)) is_replication_master = self.is_replication_master primary_ip = self.get_primary_ip() assert primary_ip is not None new_passwords = [] new_ports = [] iptables = IpTables() for port,password in zip(ports, passwords or [None for port in ports]): if op: op.step('Launch Redis %s on port %s' % ('Master' if is_replication_master else 'Slave', port)) try: if op: op.__enter__() if iptables.enabled(): iptables.insert_rule(None, RuleSpec(dport=port, jump='ACCEPT', protocol=P_TCP)) redis_service.create_redis_conf_copy(port) redis_process = redis_service.Redis(is_replication_master, self.persistence_type, port, password) if not redis_process.service.running: LOG.debug('Launch Redis %s on port %s' % ('Master' if is_replication_master else 'Slave', port)) if is_replication_master: current_password = redis_process.init_master(STORAGE_PATH) else: current_password = redis_process.init_slave(STORAGE_PATH, primary_ip, port) new_passwords.append(current_password) new_ports.append(port) LOG.debug('Redis process has been launched on port %s with password %s' % (port, current_password)) else: raise BaseException('Cannot launch redis on port %s: the process is already running' % port) except: if op: op.__exit__(sys.exc_info()) raise finally: if op: op.__exit__(None) return (new_ports, new_passwords)
def setUp(self): self.ip_tables = IpTables() self.ip_tables.flush()
class Test(unittest.TestCase): def setUp(self): self.ip_tables = IpTables() self.ip_tables.flush() def tearDown(self): self.ip_tables.flush() def test_valid_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, source='127.0.0.0/8', dport='90', jump='ACCEPT') print "\ntest_valid_rule\n" print str(rule) self.ip_tables.append_rule(rule) list_system_rules = self.ip_tables.list_rules() for system_rule in list_system_rules: print str(system_rule[0]) self.assertEquals(rule, list_system_rules[0][0]) print "Objects are equal." def test_delete_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, destination='127.0.0.0/8', jump='ACCEPT') print "\ntest_delete_rule\n" print "added:", str(rule) self.ip_tables.append_rule(rule) print "received:", str(self.ip_tables.list_rules()[0][0]) self.ip_tables.delete_rule(rule) print "deleted." self.ip_tables.flush() self.assertEquals(self.ip_tables.list_rules(), []) def test_insert_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, source='127.0.0.0/8', jump='DROP') print "\ntest_insert_rule\n" print "Filling table with rules" self.ip_tables.append_rule(rule) print str(self.ip_tables.list_rules()[0][0]) print "Number of rules in table:", len(self.ip_tables.list_rules()) print "Inserting rule to the top" new_rule = RuleSpec(protocol=iptables.P_TCP, destination='127.0.0.0/8', jump='DROP') self.ip_tables.insert_rule('1', new_rule) print "Number of rules in table changed to:", len( self.ip_tables.list_rules()) print "New rule:\n", str(new_rule) self.assertEquals(len(self.ip_tables.list_rules()), 2) self.assertEquals(new_rule, self.ip_tables.list_rules()[0][0]) self.assertEquals(self.ip_tables.list_rules()[1][0], rule)
class Test(unittest.TestCase): def setUp(self): self.ip_tables = IpTables() self.ip_tables.flush() def tearDown(self): self.ip_tables.flush() def test_valid_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, source="127.0.0.0/8", dport="90", jump="ACCEPT") print "\ntest_valid_rule\n" print str(rule) self.ip_tables.append_rule(rule) list_system_rules = self.ip_tables.list_rules() for system_rule in list_system_rules: print str(system_rule[0]) self.assertEquals(rule, list_system_rules[0][0]) print "Objects are equal." def test_delete_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, destination="127.0.0.0/8", jump="ACCEPT") print "\ntest_delete_rule\n" print "added:", str(rule) self.ip_tables.append_rule(rule) print "received:", str(self.ip_tables.list_rules()[0][0]) self.ip_tables.delete_rule(rule) print "deleted." self.ip_tables.flush() self.assertEquals(self.ip_tables.list_rules(), []) def test_insert_rule(self): rule = RuleSpec(protocol=iptables.P_TCP, source="127.0.0.0/8", jump="DROP") print "\ntest_insert_rule\n" print "Filling table with rules" self.ip_tables.append_rule(rule) print str(self.ip_tables.list_rules()[0][0]) print "Number of rules in table:", len(self.ip_tables.list_rules()) print "Inserting rule to the top" new_rule = RuleSpec(protocol=iptables.P_TCP, destination="127.0.0.0/8", jump="DROP") self.ip_tables.insert_rule("1", new_rule) print "Number of rules in table changed to:", len(self.ip_tables.list_rules()) print "New rule:\n", str(new_rule) self.assertEquals(len(self.ip_tables.list_rules()), 2) self.assertEquals(new_rule, self.ip_tables.list_rules()[0][0]) self.assertEquals(self.ip_tables.list_rules()[1][0], rule)