def test_0040_wrong_subnet(self): subnets = dormitory.Subnet.q.all() netdev = host.NetDevice.q.first() ip = get_free_ip((subnets[0], )) ip_addr = host.Ip(net_device=netdev, address=ip) def assign_subnet(): ip_addr.subnet = subnets[1] self.assertRaisesRegexp(AssertionError, "Given subnet does not contain the ip", assign_subnet) ip_addr = host.Ip(net_device=netdev, subnet=subnets[1]) def assign_ip(): ip_addr.address = ip self.assertRaisesRegexp(AssertionError, "Subnet does not contain the given ip", assign_ip) def new_instance(): host.Ip(net_device=netdev, subnet=subnets[1], address=ip) self.assertRaisesRegexp(AssertionError, "Subnet does not contain the given ip", new_instance)
def is_back(user, processor): """ After a user moved temporarily out, this function sets group memberships and creates a log message :param user: The User who is back. :param processor: The admin recognizing the users return. :return: The user who returned. """ membership = Membership.q.join( (PropertyGroup, Membership.group_id == PropertyGroup.id)).filter( PropertyGroup.name == config["move_out_tmp"]["group"], Membership.user_id == user.id, Membership.active).one() membership.disable() subnets = user.room.dormitory.subnets ip_address = host.get_free_ip(subnets) subnet = host.select_subnet_for_ip(ip_address, subnets) for user_host in user.user_hosts: create_ip(address=ip_address, subnet=subnet, net_device=user_host.user_net_device) create_user_log_entry(message=config["move_out_tmp"]["log_message_back"], timestamp=datetime.now(), author=processor, user=user) return user
def move(user, dormitory, level, room_number, processor): """ Moves the user into another room. :param user: The user to be moved. :param dormitory: The new dormitory. :param level: The level of the new room. :param room_number: The number of the new room. :param processor: The user who is currently logged in. :return: The user object of the moved user. """ old_room = user.room new_room = Room.q.filter_by( number=room_number, level=level, dormitory_id=dormitory.id ).one() assert old_room is not new_room,\ "A User is only allowed to move in a different room!" user.room = new_room create_user_log_entry( author=processor, message=config["move"]["log_message"].format( from_room=old_room, to_room=new_room), timestamp=datetime.now(), user=user ) # assign a new IP to each net_device for user_host in user.user_hosts: net_dev = user_host.user_net_device if old_room.dormitory_id != new_room.dormitory_id: assert len(net_dev.ips) == 1, "A user should only have one ip!" ip_addr = net_dev.ips[0] old_ip = ip_addr.address new_ip = host.get_free_ip(dormitory.subnets) new_subnet = host.select_subnet_for_ip(new_ip, dormitory.subnets) ip_addr.change_ip(new_ip, new_subnet) create_user_log_entry(author=processor, message=config["move"]["ip_change_log_message"].format( old_ip=old_ip, new_ip=new_ip), timestamp=datetime.now(), user=user) #TODO set new PatchPort for each NetDevice in each Host that moves to the new room #moves the host in the new room and assign the belonging net_device to the new patch_port for user_host in user.user_hosts: user_host.room = new_room return user
def test_0010_correct_subnet_and_ip(self): subnet = dormitory.Subnet.q.first() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(net_device=netdev) ip_addr.address = ip ip_addr.subnet = subnet session.session.add(ip_addr) session.session.commit() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(address=ip, subnet=subnet, net_device=netdev) session.session.add(ip_addr) session.session.commit() host.Ip.q.filter(host.Ip.net_device == netdev).delete() session.session.commit()
def test_0020_missing_subnet(self): subnet = dormitory.Subnet.q.first() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(net_device=netdev) ip_addr.address = ip def commit(): session.session.add(ip_addr) session.session.commit() self.assertRaisesRegexp(Exception, REGEX_NOT_NULL_CONSTRAINT, commit)
def test_0020_change_ip(self): subnet = dormitory.Subnet.q.first() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(net_device=netdev, address=ip, subnet=subnet) session.session.add(ip_addr) session.session.commit() ip_addr = host.Ip.q.first() self.assertEqual(ip_addr.address, ip) ip = get_free_ip((subnet, )) ip_addr.change_ip(ip, subnet) session.session.commit() ip_addr = host.Ip.q.first() self.assertEqual(ip_addr.address, ip) host.Ip.q.delete() session.session.commit()
def test_0020_change_ip(self): subnet = dormitory.Subnet.q.first() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(net_device=netdev, address=ip, subnet=subnet) session.session.add(ip_addr) session.session.commit() ip_addr = host.Ip.q.first() self.assertEqual(ip_addr.address, ip) ip = get_free_ip((subnet,)) ip_addr.change_ip(ip, subnet) session.session.commit() ip_addr = host.Ip.q.first() self.assertEqual(ip_addr.address, ip) host.Ip.q.delete() session.session.commit()
def move(user, dormitory, level, room_number, processor): """ Moves the user into another room. :param user: The user to be moved. :param dormitory: The new dormitory. :param level: The level of the new room. :param room_number: The number of the new room. :param processor: The user who is currently logged in. :return: The user object of the moved user. """ old_room = user.room new_room = Room.q.filter_by(number=room_number, level=level, dormitory_id=dormitory.id).one() assert old_room is not new_room,\ "A User is only allowed to move in a different room!" user.room = new_room create_user_log_entry(author=processor, message=config["move"]["log_message"].format( from_room=old_room, to_room=new_room), timestamp=datetime.now(), user=user) # assign a new IP to each net_device for user_host in user.user_hosts: net_dev = user_host.user_net_device if old_room.dormitory_id != new_room.dormitory_id: assert len(net_dev.ips) == 1, "A user should only have one ip!" ip_addr = net_dev.ips[0] old_ip = ip_addr.address new_ip = host.get_free_ip(dormitory.subnets) new_subnet = host.select_subnet_for_ip(new_ip, dormitory.subnets) ip_addr.change_ip(new_ip, new_subnet) create_user_log_entry( author=processor, message=config["move"]["ip_change_log_message"].format( old_ip=old_ip, new_ip=new_ip), timestamp=datetime.now(), user=user) #TODO set new PatchPort for each NetDevice in each Host that moves to the new room #moves the host in the new room and assign the belonging net_device to the new patch_port for user_host in user.user_hosts: user_host.room = new_room return user
def test_0040_delete_subnet(self): subnet = dormitory.Subnet.q.first() netdev = host.NetDevice.q.first() ip = get_free_ip((subnet, )) ip_addr = host.Ip(net_device=netdev, address=ip, subnet=subnet) session.session.add(ip_addr) session.session.commit() ip_addr.subnet = None self.assertIsNone(ip_addr.subnet) self.assertRaisesRegexp(Exception, REGEX_NOT_NULL_CONSTRAINT, session.session.commit)
def test_0030_get_free_ip_next_to_full(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() host = Host.q.filter(Host.id == UserHostData.dummy_host1.id).one() nd = UserNetDevice(mac="00:00:00:00:00:00", host=host) for num in range(0, 490): if num >= 488: self.assertRaises(SubnetFullException, get_free_ip, subnets) continue ip = get_free_ip(subnets) net = select_subnet_for_ip(ip, subnets) if num < 244: self.assertEqual(ip, self.ip_s1(num)) else: self.assertEqual(ip, self.ip_s2(num % 244)) ip_addr = Ip(address=ip, subnet=net, net_device=nd) session.session.add(ip_addr) session.session.commit() Ip.q.filter(Ip.net_device == nd).delete() session.session.delete(nd) session.session.commit()
def test_0030_get_free_ip_next_to_full(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() host = Host.q.filter(Host.id == UserHostData.dummy_host1.id).one() nd = UserNetDevice(mac="00:00:00:00:00:00", host = host) for num in range(0, 490): if num >= 488: self.assertRaises(SubnetFullException, get_free_ip, subnets) continue ip = get_free_ip(subnets) net = select_subnet_for_ip(ip, subnets) if num < 244: self.assertEqual(ip, self.ip_s1(num)) else: self.assertEqual(ip, self.ip_s2(num % 244)) ip_addr = Ip(address=ip, subnet=net, net_device=nd) session.session.add(ip_addr) session.session.commit() Ip.q.filter(Ip.net_device == nd).delete() session.session.delete(nd) session.session.commit()
def is_back(user, processor): """ After a user moved temporarily out, this function sets group memberships and creates a log message :param user: The User who is back. :param processor: The admin recognizing the users return. :return: The user who returned. """ membership = Membership.q.join( (PropertyGroup, Membership.group_id == PropertyGroup.id) ).filter( PropertyGroup.name == config["move_out_tmp"]["group"], Membership.user_id == user.id, Membership.active ).one() membership.disable() subnets = user.room.dormitory.subnets ip_address = host.get_free_ip(subnets) subnet = host.select_subnet_for_ip(ip_address, subnets) for user_host in user.user_hosts: create_ip( address=ip_address, subnet=subnet, net_device=user_host.user_net_device ) create_user_log_entry( message=config["move_out_tmp"]["log_message_back"], timestamp=datetime.now(), author=processor, user=user ) return user
def test_0010_get_free_ip_simple(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() ip = get_free_ip(subnets) self.assertEqual(ip, self.ip_s1(0))
def move_in(name, login, email, dormitory, level, room_number, mac, processor, host_name=None): """ This function creates a new user, assign him to a room and creates some initial groups and transactions. :param name: The full name of the user. (Max Mustermann) :param login: The unix login for the user. :param email: E-Mail address of the user. :param dormitory: The dormitory the user moves in. :param level: The level the user moves in. :param room_number: The room number the user moves in. :param mac: The mac address of the users pc. :param host_name: An optional Hostname for the users pc. :return: The new user object. """ room = Room.q.filter_by(number=room_number, level=level, dormitory=dormitory).one() # create a new user new_user = User(login=login, name=name, email=email, room=room, registration_date=datetime.now()) plain_password = user.generate_password(12) #TODO: print plain password on paper instead print u"new password: "******"move_in"] for membership in conf["group_memberships"]: group = Group.q.filter(Group.name == membership["name"]).one() start_date = datetime.now() if membership.get("offset"): start_date += timedelta(membership["offset"]) new_membership = create_membership(start_date=start_date, end_date=None, group=group, user=new_user) if membership.get("duration"): assert membership["duration"] > 0 new_membership.end_date = datetime.now() + timedelta( membership["duration"]) setup_user_finance_account(new_user, processor) move_in_user_log_entry = create_user_log_entry(author=processor, message=conf["log_message"], timestamp=datetime.now(), user=new_user) return new_user
def move_in(name, login, email, dormitory, level, room_number, mac, processor, host_name=None): """ This function creates a new user, assign him to a room and creates some initial groups and transactions. :param name: The full name of the user. (Max Mustermann) :param login: The unix login for the user. :param email: E-Mail address of the user. :param dormitory: The dormitory the user moves in. :param level: The level the user moves in. :param room_number: The room number the user moves in. :param mac: The mac address of the users pc. :param host_name: An optional Hostname for the users pc. :return: The new user object. """ room = Room.q.filter_by(number=room_number, level=level, dormitory=dormitory).one() # create a new user new_user = User( login=login, name=name, email=email, room=room, registration_date=datetime.now() ) plain_password = user.generate_password(12) #TODO: print plain password on paper instead print u"new password: "******"move_in"] for membership in conf["group_memberships"]: group = Group.q.filter(Group.name == membership["name"]).one() start_date = datetime.now() if membership.get("offset"): start_date += timedelta(membership["offset"]) new_membership = create_membership( start_date=start_date, end_date=None, group=group, user=new_user ) if membership.get("duration"): assert membership["duration"] > 0 new_membership.end_date = datetime.now() + timedelta(membership["duration"]) setup_user_finance_account(new_user, processor) move_in_user_log_entry = create_user_log_entry( author=processor, message=conf["log_message"], timestamp=datetime.now(), user=new_user ) return new_user