--- a/usr/src/cmd/ai-webserver/AI_database.py Fri Jul 20 16:19:17 2012 -0700
+++ b/usr/src/cmd/ai-webserver/AI_database.py Sun Jul 22 22:34:21 2012 -0600
@@ -641,21 +641,19 @@
return None
-def build_query_str(criteria, criteria_set_in_db, all_criteria_in_db):
+def build_query_str(criteria, criteria_set_in_db, all_criteria_in_db,
+ filter_noncriteria_manifests=True):
''' build a query to find out which manifest is the best
match for the client, based on criteria set in the db.
Args:
criteria: dictionary of client criteria
criteria_set_in_db: list of unstripped criteria currently set in the db
all_criteria_in_db: complete list of criteria.
- - If given, filter manifests which have no criteria set.
- - If None, don't filter manifests which have no criteria set.
+ filter_noncriteria_manifests : if True, filter manifests which have no
+ criteria set.
Returns: query string or 0 if there is an error
'''
- # Filter manifests with no criteria set, if all_criteria_in_db passed in.
- filter_noncriteria_manifests = (all_criteria_in_db is not None)
-
query_str = "SELECT name FROM manifests WHERE "
# Set up search for all manifest matches in the db and then add ORDER
@@ -743,7 +741,7 @@
query_str += ("ORDER BY "
"(COALESCE(MAXmac, MINmac) IS NOT NULL) desc, "
"(COALESCE(MAXipv4, MINipv4) IS NOT NULL) desc, ")
- if "hostname" in criteria:
+ if "hostname" in all_criteria_in_db:
query_str += ("match_hostname('" + sanitizeSQL(criteria['hostname']) +
"', hostname, 1) desc, ")
query_str += ("platform desc, arch desc, cpu desc, "
--- a/usr/src/cmd/ai-webserver/test/test_ai_database.py Fri Jul 20 16:19:17 2012 -0700
+++ b/usr/src/cmd/ai-webserver/test/test_ai_database.py Sun Jul 22 22:34:21 2012 -0600
@@ -298,13 +298,46 @@
class build_query_str(unittest.TestCase):
'''Tests for build_query_str'''
+ @classmethod
+ def setUpClass(cls):
+ '''unit test set up'''
+ dbname = tempfile.NamedTemporaryFile(dir="/tmp", delete=False)
+ cls.dbname = dbname.name
+ cls.db = sqlite3.connect(dbname.name, isolation_level=None)
+
+ # create db
+
+ cls.db.execute("CREATE TABLE manifests (name TEXT, instance INTEGER, "
+ "arch TEXT, hostname TEXT, MINmac INTEGER, "
+ "MAXmac INTEGER, MINipv4 INTEGER, MAXipv4 INTEGER, "
+ "cpu TEXT, platform TEXT, MINnetwork INTEGER, "
+ "MAXnetwork INTEGER, MINmem INTEGER, MAXmem INTEGER, "
+ "zonename TEXT)")
+
+ cls.db.execute("INSERT INTO manifests VALUES('manifest', 0, "
+ "'i86pc', NULL, NULL, NULL, NULL, NULL, NULL,"
+ "NULL, NULL, NULL, NULL, NULL, NULL)")
+
+ cls.db.execute("INSERT INTO manifests VALUES('manifest1', 0, "
+ "NULL, NULL, NULL, NULL, NULL, NULL, NULL,"
+ "NULL, NULL, NULL, NULL, NULL, NULL)")
+
+ cls.aidb = AIdb.DB(cls.dbname, commit=True)
+
+ @classmethod
+ def tearDownClass(cls):
+ '''Class-level variable teardown'''
+ cls.db.close()
+ os.remove(cls.dbname)
+
def test_building_query_str(self):
- ''' test that we get reasonable query str '''
- cri_list = ['MINipv4', 'MAXipv4', 'arch', 'cpu', 'platform',
- 'MINmac', 'MAXmac', 'MINmem', 'MAXmem',
- 'MINnetwork', 'MAXnetwork']
+ ''' test that we get reasonable query str and appropriate manifest '''
+ cri_list = AIdb.getCriteria(self.aidb.getQueue(), onlyUsed=False,
+ strip=False)
+
+ criteria_set_in_db = AIdb.getCriteria(self.aidb.getQueue(),
+ onlyUsed=True)
# Artificially small list to test filter functionality.
- all_cri_list = ['arch']
my_crit_dict = {
'ipv4': '020025224125',
'arch': 'i86pc',
@@ -314,7 +347,8 @@
'mem': '2048',
'mac': 'aabbccddeeff'
}
- query_str = AIdb.build_query_str(my_crit_dict, cri_list, all_cri_list)
+ query_str = AIdb.build_query_str(my_crit_dict, cri_list,
+ criteria_set_in_db)
self.assertTrue(query_str.startswith("SELECT name"))
self.assertTrue("FROM manifests WHERE " in query_str)
self.assertTrue("MAXmem >= 2048 OR MAXmem IS NULL" in query_str)
@@ -333,6 +367,31 @@
self.assertFalse("(cpu IS NULL)" in query_str)
self.assertTrue(query_str.endswith("LIMIT 1"))
+ # execute the query and confirm the manifest name
+ query = AIdb.DBrequest(query_str)
+ self.aidb.getQueue().put(query)
+ query.waitAns()
+ self.assertTrue(query.getResponse()[0]['name'], "manifest")
+
+ def test_filter_noncriteria_manifests(self):
+ ''' test that we don't filter noncriteria manifests'''
+
+ cri_list = AIdb.getCriteria(self.aidb.getQueue(), onlyUsed=False,
+ strip=False)
+
+ criteria_set_in_db = AIdb.getCriteria(self.aidb.getQueue(),
+ onlyUsed=True)
+ my_crit_dict = {'arch': 'sun4v'}
+ query_str = AIdb.build_query_str(my_crit_dict, cri_list,
+ criteria_set_in_db, filter_noncriteria_manifests=False)
+
+ # execute the query and confirm the manifest for which no criteria
+ # is set.
+ query = AIdb.DBrequest(query_str)
+ self.aidb.getQueue().put(query)
+ query.waitAns()
+ self.assertTrue(query.getResponse()[0]['name'], "manifest1")
+
class findManifest(unittest.TestCase):
'''Tests for findManifest'''