def _from_csv(cls,system,data,csv_type,log): """ Import data from CSV file into System Objects """ if 'key' in data and data['key']: try: key = Key.by_name(data['key']) except InvalidRequestError: log.append('%s: Invalid Key %s ' % (system.fqdn, data['key'])) return False else: log.append('%s: Key must not be blank!' % system.fqdn) return False if 'key_value' in data and data['key_value']: if key.numeric: system_key_values = system.key_values_int try: key_value = Key_Value_Int.by_key_value(system, key, data['key_value']) except InvalidRequestError: key_value = Key_Value_Int(key=key, key_value=data['key_value']) else: system_key_values = system.key_values_string try: key_value = Key_Value_String.by_key_value(system, key, data['key_value']) except InvalidRequestError: key_value = Key_Value_String(key=key, key_value=data['key_value']) else: log.append('%s: Key Value must not be blank!' % system.fqdn) return False deleted = False if 'deleted' in data: deleted = smart_bool(data['deleted']) if deleted: if key_value in system_key_values: system.record_activity(user=identity.current.user, service=u'CSV', action=u'Removed', field=u'Key/Value', old=u'%s/%s' % (data['key'],data['key_value']), new=u'') system_key_values.remove(key_value) if not key_value.id: session.expunge(key_value) else: if key_value not in system_key_values: system.record_activity(user=identity.current.user, service=u'CSV', action=u'Added', field=u'Key/Value', old=u'', new=u'%s/%s' % (data['key'],data['key_value'])) system_key_values.append(key_value) session.add(key_value) return True
def test_key_present(self): module_key = Key.by_name(u'MODULE') with_module = data_setup.create_system() with_module.key_values_string.extend([ Key_Value_String(module_key, u'cciss'), Key_Value_String(module_key, u'kvm')]) without_module = data_setup.create_system() self.check_filter(""" <hostRequires> <key_value key="MODULE" op="==" /> </hostRequires> """, present=[with_module], absent=[without_module])
def test_by_multiple_key_values(self): with session.begin(): self.system = data_setup.create_system() self.system.key_values_string.append(Key_Value_String( Key.by_name(u'CPUMODEL'), 'foocodename')) self.system.key_values_string.append(Key_Value_String( Key.by_name(u'HVM'), '1')) self.system.key_values_int.append(Key_Value_Int( Key.by_name(u'DISKSPACE'), '1024')) b = self.browser perform_search(b, [('Key/Value', 'HVM', 'is', '1'), ('Key/Value', 'CPUMODEL', 'is', 'foocodename'), ('Key/Value', 'DISKSPACE', 'greater than', '1000')]) check_system_search_results(b, present=[self.system], absent=[self.another_system])
def test_xml_filter(self): with session.begin(): module_key = Key.by_name(u'MODULE') with_module = data_setup.create_system() with_module.key_values_string.extend([ Key_Value_String(module_key, u'cciss'), Key_Value_String(module_key, u'kvm') ]) without_module = data_setup.create_system() out = run_client([ 'bkr', 'list-systems', '--xml-filter', '<key_value key="MODULE" />' ]) returned_systems = out.splitlines() self.assert_(with_module.fqdn in returned_systems, returned_systems) self.assert_(without_module.fqdn not in returned_systems, returned_systems)
def test_existing_keys_are_untouched(self): with session.begin(): system = data_setup.create_system() system.key_values_string.extend([ Key_Value_String(Key.by_name(u'PCIID'), '1022:2000'), # this one gets deleted Key_Value_String(Key.by_name(u'HVM'), '0'), # this one gets updated Key_Value_String(Key.by_name(u'VENDOR'), 'Bob'), # this one should not be touched ]) self.server.legacypush(system.fqdn, {'PCIID': [], 'HVM': True}) with session.begin(): session.refresh(system) self.assertEquals(len(system.key_values_string), 2, system.key_values_string) self.assertEquals(system.key_values_string[0].key.key_name, u'VENDOR') self.assertEquals(system.key_values_string[0].key_value, u'Bob') self.assertEquals(system.key_values_string[1].key.key_name, u'HVM') self.assertEquals(system.key_values_string[1].key_value, u'1')
def test_key_notequal(self): module_key = Key.by_name(u'MODULE') with_cciss = data_setup.create_system() with_cciss.key_values_string.extend([ Key_Value_String(module_key, u'cciss'), Key_Value_String(module_key, u'kvm')]) without_cciss = data_setup.create_system() without_cciss.key_values_string.extend([ Key_Value_String(module_key, u'ida'), Key_Value_String(module_key, u'kvm')]) self.check_filter(""" <hostRequires> <and> <key_value key="MODULE" op="!=" value="cciss"/> </and> </hostRequires> """, present=[without_cciss], absent=[with_cciss])
def test_by_key_value_is_not(self): with session.begin(): self.another_system.key_values_string.append(Key_Value_String( Key.by_name(u'CPUMODEL'), 'foocodename')) b = self.browser perform_search(b, [('Key/Value', 'CPUMODEL', 'is not', 'foocodename')], search_url=u'mine') check_system_search_results(b, present=[self.system], absent=[self.another_system])
def test_xml_filter(self): with session.begin(): module_key = Key.by_name(u'MODULE') with_module = data_setup.create_system() with_module.key_values_string.extend([ Key_Value_String(module_key, u'cciss'), Key_Value_String(module_key, u'kvm') ]) without_module = data_setup.create_system() feed_url = urljoin( get_server_base(), '?' + urlencode({ 'tg_format': 'atom', 'list_tgp_order': '-date_modified', 'xmlsearch': '<key_value key="MODULE" />' })) feed = lxml.etree.parse(urlopen(feed_url)).getroot() self.assert_(self.feed_contains_system(feed, with_module.fqdn)) self.assert_(not self.feed_contains_system(feed, without_module.fqdn))
def test_system_activity_shows_changes(self): with session.begin(): system = data_setup.create_system() system.key_values_string.extend([ Key_Value_String(Key.by_name(u'PCIID'), '1022:2000'), Key_Value_String(Key.by_name(u'PCIID'), '80ee:beef'), ]) self.server.legacypush(system.fqdn, {'PCIID': ['80ee:cafe', '80ee:beef']}) with session.begin(): session.refresh(system) self.assertEquals(system.activity[0].field_name, u'Key/Value') self.assertEquals(system.activity[0].service, u'XMLRPC') self.assertEquals(system.activity[0].action, u'Added') self.assertEquals(system.activity[0].old_value, None) self.assertEquals(system.activity[0].new_value, u'PCIID/80ee:cafe') self.assertEquals(system.activity[1].field_name, u'Key/Value') self.assertEquals(system.activity[1].service, u'XMLRPC') self.assertEquals(system.activity[1].action, u'Removed') self.assertEquals(system.activity[1].old_value, u'PCIID/1022:2000') self.assertEquals(system.activity[1].new_value, None)
def test_by_key_value_is_on_removed_systems_page(self): with session.begin(): system = data_setup.create_system() removed_system = data_setup.create_system(status=SystemStatus.removed) removed_system.key_values_string.append(Key_Value_String( Key.by_name(u'CPUMODEL'), 'foocodename')) # Key Value search from "Removed Systems" b = self.browser perform_search(b, [('Key/Value', 'CPUMODEL', 'is', 'foocodename')], search_url='removed') check_system_search_results(b, present=[removed_system], absent=[system])
def test_multiple_xml_filters(self): with session.begin(): module_key = Key.by_name(u'MODULE') matching = data_setup.create_system() matching.cpu = Cpu(family=6, model=47, model_name=u'Intel') matching.key_values_string.append( Key_Value_String(module_key, u'cciss')) nonmatching = data_setup.create_system() out = run_client([ 'bkr', 'list-systems', '--xml-filter', '<not><lender value="shark"/></not>', '--xml-filter', '<key_value key="MODULE" value="cciss"/>', '--host-filter', 'INTEL__WESTMERE' ]) returned_systems = out.splitlines() self.assertIn(matching.fqdn, returned_systems) self.assertNotIn(nonmatching.fqdn, returned_systems)
def test_by_key_value_is(self): with session.begin(): self.system.key_values_string.append(Key_Value_String( Key.by_name(u'CPUMODEL'), 'foocodename')) b = self.browser b.get(get_server_base()) b.find_element_by_link_text('Show Search Options').click() wait_for_animation(b, '#searchform') Select(b.find_element_by_name('systemsearch-0.table'))\ .select_by_visible_text('Key/Value') Select(b.find_element_by_name('systemsearch-0.keyvalue'))\ .select_by_visible_text('CPUMODEL') Select(b.find_element_by_name('systemsearch-0.operation'))\ .select_by_visible_text('is') b.find_element_by_name('systemsearch-0.value').send_keys('foocodename') b.find_element_by_id('searchform').submit() check_system_search_results(b, present=[self.system], absent=[self.another_system])
def test_bools_are_coerced_to_ints(self): with session.begin(): system = data_setup.create_system() system.key_values_string.append( Key_Value_String(Key.by_name(u'HVM'), '0')) self.server.legacypush(system.fqdn, {'HVM': False}) with session.begin(): session.refresh(system) self.assertEquals(len(system.activity), 0) # nothing has changed, yet self.server.legacypush(system.fqdn, {'HVM': True}) with session.begin(): session.refresh(system) self.assertEquals(system.activity[0].field_name, u'Key/Value') self.assertEquals(system.activity[0].service, u'XMLRPC') self.assertEquals(system.activity[0].action, u'Added') self.assertEquals(system.activity[0].old_value, None) self.assertEquals(system.activity[0].new_value, u'HVM/1')
def setUpClass(cls): cls.system_one_details = { 'fqdn': u'a1', 'type': u'Machine', 'arch': u'i386', 'status': u'Automated', 'owner': data_setup.create_user(), } cls.system_one = data_setup.create_system(**cls.system_one_details) cls.system_one.loaned = data_setup.create_user() cls.system_one.numa = Numa(nodes=2) cls.system_one.key_values_string.append( Key_Value_String(Key.by_name(u'CPUMODEL'), 'foocodename')) cls.system_one.key_values_string.append( Key_Value_String(Key.by_name(u'HVM'), '1')) cls.system_one.cpu = Cpu(flags=['flag1', 'flag2']) cls.system_one.key_values_int.append( Key_Value_Int(Key.by_name(u'DISKSPACE'), '1024')) cls.system_one.key_values_int.append( Key_Value_Int(Key.by_name(u'MEMORY'), '4096')) cls.system_two_details = { 'fqdn': u'a2', 'type': u'Prototype', 'arch': u'x86_64', 'status': u'Manual', 'owner': data_setup.create_user(), } cls.system_two = data_setup.create_system(**cls.system_two_details) cls.system_two.key_values_int.append( Key_Value_Int(Key.by_name(u'DISKSPACE'), '900')) cls.system_two.key_values_string.append( Key_Value_String(Key.by_name(u'HVM'), '1')) device_class = DeviceClass.lazy_create(device_class='class_type') device1 = Device.lazy_create(vendor_id='0000', device_id='0000', subsys_vendor_id='2223', subsys_device_id='2224', bus='0000', driver='0000', device_class_id=device_class.id, description='blah') cls.system_two.devices.append(device1) cls.system_three_details = { 'fqdn': u'a3', 'type': u'Laptop', 'arch': u'ia64', 'status': u'Automated', 'owner': data_setup.create_user(), } cls.system_three = data_setup.create_system(**cls.system_three_details) cls.system_three.numa = Numa(nodes=1) device2 = Device.lazy_create(vendor_id='0000', device_id='0000', subsys_vendor_id='1111', subsys_device_id='1112', bus='0000', driver='0000', device_class_id=device_class.id, description='blah') cls.system_three.devices.append(device2) cls.system_four_details = { 'status': u'Removed', } cls.system_four = data_setup.create_system(**cls.system_four_details) cls.system_four.key_values_string.append( Key_Value_String(Key.by_name(u'CPUMODEL'), 'foocodename'))