def test_device_table_view(self): device_type = DeviceType(name="generic") device_type.save() # pylint: disable=no-member device = Device(device_type=device_type, hostname="generic1") device.save() # pylint: disable=no-member view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 1)
def test_bbb_valid_template(self): name = "beaglebone-black" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="bbb-01", health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone( [d for d in Device.objects.filter(device_type=dt)]) self.assertTrue(settings.DEVICES_PATH.startswith(self.basedir)) found = False for device_file in os.listdir(settings.DEVICE_TYPES_PATH): if device_file == "%s.jinja2" % name: found = True break if not found: self.fail("Configuration error - %s.jinja2 should exist in %s" % (name, settings.DEVICE_TYPES_PATH)) self.assertIsNotNone([device in Device.objects.filter(device_type=dt)]) self.assertIsNotNone(device.load_configuration()) self.assertTrue(bool(load_devicetype_template( device.device_type.name))) self.assertFalse(invalid_template(device.device_type))
def make_device(self, device_type=None, hostname=None, tags=None, is_public=True, **kw): if device_type is None: device_type = self.ensure_device_type() if hostname is None: hostname = self.getUniqueString() if not isinstance(tags, list): tags = [] # a hidden device type will override is_public device = Device(device_type=device_type, is_public=is_public, state=Device.STATE_IDLE, hostname=hostname, **kw) device.tags.set(tags) logging.debug( "making a device of type %s %s %s with tags '%s'", device_type, device.is_public, device.hostname, ", ".join([x.name for x in device.tags.all()]), ) device.save() return device
def test_juno_vexpress_valid_template(self): name = "juno-r2" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="juno-r2-01", health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone( [d for d in Device.objects.filter(device_type=dt)]) for device_file in os.listdir(settings.DEVICE_TYPES_PATH): if device_file == "juno-r2.jinja2": self.fail( "Configuration error - %s.jinja2 should NOT exist in %s" % (name, settings.DEVICE_TYPES_PATH)) self.assertTrue(settings.DEVICES_PATH.startswith(self.basedir)) self.assertEqual("juno-r2-01", device.hostname) self.assertIsNotNone(device.load_configuration()) self.assertEqual( [device], [device for device in Device.objects.filter(device_type=dt)]) self.assertEqual("juno", device.get_extends()) self.assertFalse( bool(load_devicetype_template(device.device_type.name))) self.assertFalse(invalid_template(device.device_type))
def test_device_table_hidden(self): hidden = DeviceType(name="hidden", owners_only=True, health_check_job='') hidden.save() device = Device(device_type=hidden, hostname='hidden1', status=Device.OFFLINE) device.save() view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 0)
def make_device(self, device_type=None, hostname=None, tags=None, is_public=True, **kw): if device_type is None: device_type = self.make_device_type() if hostname is None: hostname = self.getUniqueString() if tags and type(tags) != list: tags = [] device = Device(device_type=device_type, is_public=is_public, hostname=hostname, **kw) if tags: device.tags = tags logging.debug("making a device of type %s %s %s with tags '%s'" % ( device_type, device.is_public, device.hostname, ", ".join([x.name for x in device.tags.all()]), )) device.save() return device
def test_device_table_hidden(self): hidden = DeviceType(name="hidden", owners_only=True) hidden.save() # pylint: disable=no-member device = Device(device_type=hidden, hostname='hidden1', status=Device.OFFLINE) device.save() # pylint: disable=no-member view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 0)
def make_device(self, device_type=None, hostname=None, tags=None, is_public=True, **kw): if device_type is None: device_type = self.ensure_device_type() if hostname is None: hostname = self.getUniqueString() if tags and type(tags) != list: tags = [] # a hidden device type will override is_public device = Device(device_type=device_type, is_public=is_public, hostname=hostname, is_pipeline=True, **kw) self.make_fake_qemu_device(hostname) if tags: device.tags = tags logging.debug("making a device of type %s %s %s with tags '%s'" % (device_type, device.is_public, device.hostname, ", ".join([x.name for x in device.tags.all()]))) device.save() return device
def test_device_table_hidden(self): hidden = DeviceType(name="hidden", owners_only=True) hidden.save() # pylint: disable=no-member device = Device(device_type=hidden, hostname='hidden1') device.save() # pylint: disable=no-member view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 0)
def test_active_device_types(self): name = "beaglebone-black" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname='bbb-01', health=Device.HEALTH_GOOD) device.save() device = Device(device_type=dt, hostname='bbb-02', health=Device.HEALTH_RETIRED) device.save() name = "juno-r2" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname='juno-r2-01', health=Device.HEALTH_RETIRED) device.save() name = "juno" dt = DeviceType(name=name) dt.display = False dt.save() dt.refresh_from_db() dt.refresh_from_db() device = Device(device_type=dt, hostname='juno-01', health=Device.HEALTH_UNKNOWN) device.save() name = "qemu" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname='qemu-01', health=Device.HEALTH_GOOD) device.save() self.assertEqual( {'bbb-01', 'bbb-02', 'juno-r2-01', 'qemu-01', 'juno-01'}, set(Device.objects.all().values_list('hostname', flat=True))) self.assertEqual({'beaglebone-black', 'juno', 'juno-r2', 'qemu'}, set(DeviceType.objects.values_list('name', flat=True))) # exclude juno-r2 because all devices of that device-type are retired. # exclude juno because the device_type is set to not be displayed. # include beaglebone-black because not all devices of that type are retired. # include qemu because none of the devices of that type are retired. self.assertEqual({'beaglebone-black', 'qemu'}, set(active_device_types().values_list('name', flat=True)))
def test_device_permissions_test(self): dt = DeviceType(name="type1") dt.save() device = Device(device_type=dt, hostname="device1") device.save() group = self.factory.make_group() user1 = self.factory.make_user() user1.groups.add(group) group2 = self.factory.make_group() user2 = self.factory.make_user() user2.groups.add(group2) GroupDevicePermission.objects.assign_perm("submit_to_device", group, device) self.assertEqual(device.can_submit(user2), False) self.assertEqual(device.can_submit(user1), True) GroupDevicePermission.objects.remove_perm("submit_to_device", group, device) delattr(user1, "_cached_has_perm") delattr(user2, "_cached_has_perm") self.assertEqual(device.can_view(user2), True) self.assertEqual(device.can_view(user1), True) GroupDeviceTypePermission.objects.assign_perm("view_devicetype", group, dt) delattr(user1, "_cached_has_perm") delattr(user2, "_cached_has_perm") self.assertEqual(device.can_view(user2), False) self.assertEqual(device.can_view(user1), True) GroupDeviceTypePermission.objects.remove_perm("view_devicetype", group, dt) GroupDevicePermission.objects.assign_perm("view_device", group, device) delattr(user1, "_cached_has_perm") delattr(user2, "_cached_has_perm") self.assertEqual(device.can_view(user2), False) self.assertEqual(device.can_view(user1), True) GroupDevicePermission.objects.assign_perm("view_device", group2, device) delattr(user1, "_cached_has_perm") delattr(user2, "_cached_has_perm") self.assertEqual(device.can_view(user2), True) self.assertEqual(device.can_view(user1), True) device.health = Device.HEALTH_RETIRED device.save() self.assertEqual(device.can_submit(user2), False) self.assertEqual(device.can_submit(user1), False) # Test that global permission works as intended. user3 = self.factory.make_user() user3.user_permissions.add( Permission.objects.get(codename="change_device")) self.assertEqual(device.can_change(user3), True)
def test_device_table_view(self): device_type = DeviceType(name="generic", owners_only=False) device_type.save() # pylint: disable=no-member device = Device(device_type=device_type, hostname='generic1', status=Device.OFFLINE) user = self.make_user() device.user = user device.save() # pylint: disable=no-member view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 1)
def test_device_table_view(self): device_type = DeviceType(name="generic", owners_only=False, health_check_job='') device_type.save() device = Device(device_type=device_type, hostname='generic1', status=Device.OFFLINE) user = self.make_user() device.user = user device.save() view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 1)
def test_put_into_looping_mode(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo01', status=Device.OFFLINE) device.save() device.put_into_looping_mode(None, None) self.assertEqual(device.status, Device.IDLE, "should be IDLE") self.assertEqual(device.health_status, Device.HEALTH_LOOPING, "should be LOOPING")
def test_device_table_view(self): device_type = DeviceType(name="generic", owners_only=False) device_type.save() # pylint: disable=no-member device = Device(device_type=device_type, hostname='generic1') user = self.make_user() device.user = user device.save() # pylint: disable=no-member view = TestDeviceView(None) self.assertEqual(len(view.get_queryset()), 1)
def test_access_while_private(self): hidden = DeviceType(name="hidden", owners_only=True) device = Device(device_type=hidden, hostname='hidden1', is_public=False) user = self.factory.make_user() device.user = user device.save() self.assertEqual(device.is_public, False) self.assertEqual(device.user, user) user2 = self.factory.make_user() self.assertEqual(device.can_submit(user2), False) self.assertEqual(device.can_submit(user), True)
def test_access_while_hidden(self): hidden = DeviceType(name="hidden", owners_only=True, health_check_job='') device = Device(device_type=hidden, hostname='hidden1', status=Device.OFFLINE) user = self.factory.make_user() device.user = user device.save() self.assertEqual(device.is_public, False) self.assertEqual(device.user, user) user2 = self.factory.make_user() self.assertEqual(device.can_submit(user2), False) self.assertEqual(device.can_submit(user), True)
def test_access_retired_hidden(self): hidden = DeviceType(name="hidden", owners_only=True, health_check_job='') device = Device(device_type=hidden, hostname='hidden2', status=Device.RETIRED) user = self.factory.make_user() device.user = user device.save() self.assertEqual(device.is_public, False) self.assertEqual(device.user, user) user2 = self.factory.make_user() self.assertEqual(device.can_submit(user2), False) # user cannot submit as the device is retired self.assertEqual(device.can_submit(user), False)
def test_access_while_private(self): hidden = DeviceType(name="hidden", owners_only=True) device = Device(device_type=hidden, hostname="hidden1", is_public=False) user = self.factory.make_user() device.user = user device.save() self.assertEqual(device.is_public, False) self.assertEqual(device.user, user) user2 = self.factory.make_user() self.assertEqual(device.can_submit(user2), False) self.assertEqual(device.can_submit(user), True)
def test_unknown_invalid_template(self): name = "nowhere-never-skip" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="test-01", health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) self.assertIsNone(device.load_configuration()) self.assertIsNotNone([device in Device.objects.filter(device_type=dt)]) self.assertFalse(bool(load_devicetype_template(device.device_type.name))) self.assertTrue(invalid_template(device.device_type))
def test_maintenance_mode(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo01', status=Device.IDLE) device.save() device.put_into_maintenance_mode(None, None) self.assertEqual(device.status, Device.OFFLINE, "should be offline") device.status = Device.RUNNING device.put_into_maintenance_mode(None, None) self.assertEqual(device.status, Device.OFFLINING, "should be offlining")
def test_bbb_valid_template(self): name = "beaglebone-black" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="bbb-01", health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) self.assertTrue(File("device-type").exists(f"{name}.jinja2")) self.assertIsNotNone([device in Device.objects.filter(device_type=dt)]) self.assertIsNotNone(device.load_configuration()) self.assertTrue(bool(load_devicetype_template(device.device_type.name))) self.assertFalse(invalid_template(device.device_type))
def make_device(self, device_type=None, hostname=None, tags=None, is_public=True, **kw): if device_type is None: device_type = self.ensure_device_type() if hostname is None: hostname = self.getUniqueString() if type(tags) != list: tags = [] # a hidden device type will override is_public device = Device(device_type=device_type, is_public=is_public, hostname=hostname, **kw) device.tags = tags logging.debug("making a device of type %s %s %s with tags '%s'" % (device_type, device.is_public, device.hostname, ", ".join([x.name for x in device.tags.all()]))) device.save() return device
def test_maintenance_mode(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo01', status=Device.IDLE) device.save() device.put_into_maintenance_mode(None, None) self.assertEqual(device.status, Device.OFFLINE, "should be offline") device.status = Device.RUNNING device.put_into_maintenance_mode(None, None) self.assertEqual(device.status, Device.OFFLINING, "should be offlining")
def test_online_mode(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo02', status=Device.OFFLINE) device.save() device.put_into_online_mode(None, None) self.assertEqual(device.status, Device.IDLE, "should be idle") device.status = Device.OFFLINING device.put_into_online_mode(None, None) self.assertIsNone(device.current_job) self.assertEqual(device.status, Device.IDLE, "should be idle") device.status = Device.RETIRED device.put_into_online_mode(None, None) self.assertEqual(device.status, Device.RETIRED, "should be retired")
def make_device(self, device_type=None, hostname=None, tags=None, is_public=True, **kw): if device_type is None: device_type = self.make_device_type() if hostname is None: hostname = self.getUniqueString() if tags and type(tags) != list: tags = [] device = Device(device_type=device_type, is_public=is_public, hostname=hostname, is_pipeline=True, **kw) self.make_fake_qemu_device(hostname) if tags: device.tags = tags logging.debug("making a device of type %s %s %s with tags '%s'" % (device_type, device.is_public, device.hostname, ", ".join([x.name for x in device.tags.all()]))) device.save() return device
def test_unknown_invalid_template(self): name = "nowhere-never-skip" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname='test-01', health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) self.assertTrue(Device.CONFIG_PATH.startswith(self.basedir)) found = False for device_file in os.listdir(self.types_dir): if device_file == '%s.jinja2' % name: self.fail("Configuration error - %s.jinja2 should NOT exist in %s" % (name, self.types_dir)) self.assertIsNone(device.load_configuration()) self.assertIsNotNone([device in Device.objects.filter(device_type=dt)]) self.assertFalse(bool(load_devicetype_template(device.device_type.name))) self.assertTrue(invalid_template(device.device_type))
def test_retired_invalid_template(self): name = "beaglebone-black" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="bbb-01", health=Device.HEALTH_RETIRED) device.save() device.refresh_from_db() self.assertEqual( [], list( Device.objects.filter( Q(device_type=dt), ~Q(health=Device.HEALTH_RETIRED) ) ), ) self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) self.assertFalse(invalid_template(device.device_type))
def test_create_logentry(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo01', status=Device.OFFLINE) device.save() # only unit tests should call these functions with None, None # if that is made a requirement of the device status functions, fix this test. device.put_into_looping_mode(None, None) self.assertEqual(device.status, Device.IDLE, "should be IDLE") self.assertEqual(device.health_status, Device.HEALTH_LOOPING, "should be LOOPING") device_ct = ContentType.objects.get_for_model(Device) self.assertEqual(0, len(LogEntry.objects.filter(content_type=device_ct, action_flag=2).order_by('-action_time'))) user = self.factory.make_user() device.put_into_maintenance_mode(user, 'test_create_logentry') self.assertEqual(device.status, Device.OFFLINE, "should be OFFLINE") self.assertEqual(device.health_status, Device.HEALTH_UNKNOWN, "should be UNKNOWN") # the device state transition also creates a log entry self.assertEqual(2, len(LogEntry.objects.filter(content_type=device_ct, action_flag=2).order_by('-action_time')))
def test_juno_vexpress_valid_template(self): name = "juno-r2" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname='juno-r2-01', health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) for device_file in os.listdir(self.types_dir): if device_file == 'juno-r2.jinja2': self.fail("Configuration error - %s.jinja2 should NOT exist in %s" % (name, self.types_dir)) self.assertTrue(Device.CONFIG_PATH.startswith(self.basedir)) self.assertEqual('juno-r2-01', device.hostname) self.assertIsNotNone(device.load_configuration()) self.assertEqual([device], [device for device in Device.objects.filter(device_type=dt)]) self.assertEqual('juno', device.get_extends()) self.assertFalse(bool(load_devicetype_template(device.device_type.name))) self.assertFalse(invalid_template(device.device_type))
def test_create_logentry(self): foo = DeviceType(name='foo') device = Device(device_type=foo, hostname='foo01', status=Device.OFFLINE) device.save() # only unit tests should call these functions with None, None # if that is made a requirement of the device status functions, fix this test. device.put_into_looping_mode(None, None) self.assertEqual(device.status, Device.IDLE, "should be IDLE") self.assertEqual(device.health_status, Device.HEALTH_LOOPING, "should be LOOPING") device_ct = ContentType.objects.get_for_model(Device) self.assertEqual(0, len(LogEntry.objects.filter(content_type=device_ct, action_flag=2).order_by('-action_time'))) user = self.factory.make_user() device.put_into_maintenance_mode(user, 'test_create_logentry') self.assertEqual(device.status, Device.OFFLINE, "should be OFFLINE") self.assertEqual(device.health_status, Device.HEALTH_UNKNOWN, "should be UNKNOWN") # the device state transition also creates a log entry self.assertEqual(2, len(LogEntry.objects.filter(content_type=device_ct, action_flag=2).order_by('-action_time')))
def test_juno_vexpress_valid_template(self): name = "juno-r2" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device( device_type=dt, hostname="juno-r2-01", health=Device.HEALTH_GOOD ) device.save() device.refresh_from_db() self.assertIsNotNone([d for d in Device.objects.filter(device_type=dt)]) self.assertFalse(File("device-type").exists("juno-r2.jinja2")) self.assertEqual("juno-r2-01", device.hostname) self.assertIsNotNone(device.load_configuration()) self.assertEqual( [device], [device for device in Device.objects.filter(device_type=dt)] ) self.assertEqual("juno", device.get_extends()) self.assertFalse(bool(load_devicetype_template(device.device_type.name))) self.assertFalse(invalid_template(device.device_type))
def test_unknown_invalid_template(self): name = "nowhere-never-skip" dt = DeviceType(name=name) dt.save() dt.refresh_from_db() device = Device(device_type=dt, hostname="test-01", health=Device.HEALTH_GOOD) device.save() device.refresh_from_db() self.assertIsNotNone( [d for d in Device.objects.filter(device_type=dt)]) self.assertTrue(Device.CONFIG_PATH.startswith(self.basedir)) found = False for device_file in os.listdir(self.types_dir): if device_file == "%s.jinja2" % name: self.fail( "Configuration error - %s.jinja2 should NOT exist in %s" % (name, self.types_dir)) self.assertIsNone(device.load_configuration()) self.assertIsNotNone([device in Device.objects.filter(device_type=dt)]) self.assertFalse( bool(load_devicetype_template(device.device_type.name))) self.assertTrue(invalid_template(device.device_type))