def test_reorder_groups(self): a, b = Group('a'), Group('b') h1 = Host('h1') h1.add_group(a) h1.add_group(b) h1.reorder_groups(1, 0) assert h1.groups[0].name == 'b' assert h1.groups[1].name == 'a'
def test_remove_self_as_parent(self): """ Removing yourself as parent should not throw any exception. """ groupa, groupb = Group('groupa'), Group('groupb') groupa.add_parent(groupb) groupb.del_parent(groupb)
def test_has_group(self): g1, g2, g3 = Group('g1'), Group('g2'), Group('g3') g2.add_child(g1) g2.add_parent(g3) assert g1.has_group("g2") assert g2.has_group("g1") assert g2.has_group("g3") assert g3.has_group("g2") assert not g3.has_group('u2')
def test_remove_group_from_hosts(self): groupa = Group('groupa') hosta, hostb = Host('a'), Host('b') hosta.add_group(groupa) hostb.add_group(groupa) assert len(groupa.hosts) == 2 groupa.delete() assert len(hosta.groups) == 0 assert len(hostb.groups) == 0
def test_delete(self): hosta = Host('hosta') groupa = Group('groupa') groupb = Group('groupb') hosta.add_group(groupa) hosta.add_group(groupb) hosta.delete() assert len(groupa.hosts) == 0 assert len(groupb.hosts) == 0
def test_remove_notpresent_child(self): child1, a = Group(name='child1'), Group(name='a') a.add_child(child1) assert a.children[0].name == 'child1' a.del_child(child1) assert len(a.children) == 0 try: a.del_child(child1) except Exception: raise pytest.fail("Should not failed")
def test_delete_reparent_groups(self): child1, child2 = Group(name='child1'), Group(name='child2') mid = Group(name='mid') parent1, parent2 = Group(name='par'), Group(name='par2') for child in [child1, child2]: child.add_parent(mid) for par in [parent1, parent2]: mid.add_parent(par) mid.delete(reparent_groups=True) for child in [child1, child2]: assert parent1 in child.parents assert parent2 in child.parents assert mid not in child.parents
def test_reorder_parents(self): parent1, parent2 = Group(name='par1'), Group(name='par2') a = Group('a') a.add_parent(parent1) a.add_parent(parent2) a.reorder_parents(1, 0) assert a.parents[0].name == 'par2' assert a.parents[1].name == 'par1'
def test_reorder_children(self): child1, child2 = Group(name='child1'), Group(name='child2') a = Group('a') a.add_child(child1) a.add_child(child2) a.reorder_children(1, 0) assert a.children[0].name == 'child2' assert a.children[1].name == 'child1'
def test_delete_reparent_hosts(self): g1, g2 = Group(name='todelete'), Group(name='parent') host1 = Host(name='host') host1.add_group(g1) g1.add_parent(g2) g1.delete(reparent_hosts=True) assert g2 in host1.groups assert g1 not in host1.groups
def test_replace_parent(self): """ Groupc replaces groupb """ _, _, groupa, groupb = create_objects() groupc = Group('groupc') groupa.add_parent(groupb) groupa.replace_parent(groupb, groupc) assert groupc in groupa.parents assert groupb not in groupa.parents assert groupa in groupc.children assert groupa not in groupb.children
def test_remove_no_host_anymore(self): groupa = Group('groupa') hosta = Host('a') groupa.add_host(hosta) assert groupa.hosts[0].name == 'a' groupa.del_host(hosta) groupa.del_host(hosta)
def test_remove_host(self): groupa = Group('groupa') hosta = Host('a') groupa.add_host(hosta) assert groupa.hosts[0].name == 'a' groupa.del_host(hosta) assert len(groupa.hosts) == 0
def test_add_invalidchild(self): a = Group(name='a') with pytest.raises(TypeError): a.add_child("bazar")
def test_remove_invalid_host(self): groupa = Group('groupa') with pytest.raises(TypeError): groupa.del_host('q')
def test_add_invalid_host(self): groupa = Group('groupa') with pytest.raises(TypeError): groupa.add_host('a')
def test_add_host(self): groupa = Group('groupa') hosta = Host('a') groupa.add_host(hosta) assert groupa.hosts[0].name == 'a'
def test_repr(self): a = Group('a') assert str(a) == "Group(name='a')"
def test_create_with_no_groupname(self): with pytest.raises(Exception): Group()
def test_add_self_as_parent(self): """ You should not create direct loops. That's not good """ groupa = Group('groupa') with pytest.raises(Exception): groupa.add_parent(groupa)
def test_replace_child(self): child1, child2 = Group(name='child1'), Group(name='child2') mid = Group(name='mid') mid.add_child(child1) mid.replace_child(child1, child2) assert mid.children[0].name == 'child2'
def test_remove_invalid_child(self): child1, a = Group(name='child1'), Group(name='a') a.add_child(child1) assert a.children[0].name == 'child1' with pytest.raises(TypeError): a.del_child("qw")
def test_add_invild_parent(self): groupa = Group('groupa') with pytest.raises(TypeError): groupa.add_parent("babar")
def test_replace_parent_to_self(self): """ You tricky basterd. Should fail """ groupa, groupb = Group('groupa'), Group('groupb') groupa.add_parent(groupb) with pytest.raises(Exception): groupa.replace_parent(groupb, groupa)
def test_delete_reparent_vars(self): child1, child2 = Group(name='child1'), Group(name='child2') mid = Group(name='mid') mid.vars['groupvarname'] = 'value' parent1, parent2 = Group(name='par'), Group(name='par2') for child in [child1, child2]: child.add_parent(mid) for par in [parent1, parent2]: mid.add_parent(par) mid.delete(reparent_vars=True) for parent in [parent1, parent2]: assert parent.vars['groupvarname'] == 'value'
def test_update_groupvar(self): gr1 = Group(name='g1') gr1.vars['a'] = 'va' gr1.set_var('b', 'vb') assert gr1.vars == {'a': 'va', 'b': 'vb'}
def test_has_host(self): g1, h1 = Group('g1'), Host('h1') g1.add_host(h1) assert g1.has_host(u'h1') assert g1.has_host('h1') assert not g1.has_host('u2')
def test_add_self_as_child(self): a = Group(name='a') with pytest.raises(Exception): a.add_child(a)
def create_objects(): groupa, groupb = Group(name='groupa'), Group(name='groupb') hosta, hostb = Host('hosta'), Host('hostb') return (hosta, hostb, groupa, groupb)
def test_remove_child(self): child1, a = Group(name='child1'), Group(name='a') a.add_child(child1) assert a.children[0].name == 'child1' a.del_child(child1)