Commit 75af5cd3 authored by Julien Muchembled's avatar Julien Muchembled

kvm, jupyter: fix proxy.db queries in instantiation tests

parent 392b9e8a
...@@ -32,7 +32,7 @@ import os ...@@ -32,7 +32,7 @@ import os
import requests import requests
import sqlite3 import sqlite3
from slapos.proxy.db_version import DB_VERSION
from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass
setUpModule, InstanceTestCase = makeModuleSetUpAndTestCaseClass( setUpModule, InstanceTestCase = makeModuleSetUpAndTestCaseClass(
...@@ -169,27 +169,21 @@ class SelectMixin(object): ...@@ -169,27 +169,21 @@ class SelectMixin(object):
) )
return sqlite3.connect(sqlitedb_file) return sqlite3.connect(sqlitedb_file)
def select(self, fields, table, where={}): def select(self, table, fields=('*',), **where):
connection = self.sqlite3_connect() db = self.sqlite3_connect()
try:
def dict_factory(cursor, row): db.row_factory = lambda cursor, row: {
d = {} col[0]: row[idx]
for idx, col in enumerate(cursor.description): for idx, col in enumerate(cursor.description)
d[col[0]] = row[idx] }
return d return db.execute("SELECT %s FROM %s%s%s" % (
connection.row_factory = dict_factory
cursor = connection.cursor()
condition = " AND ".join("%s='%s'" % (k, v) for k, v in where.items())
cursor.execute(
"SELECT %s FROM %s%s"
% (
", ".join(fields), ", ".join(fields),
table, table, DB_VERSION,
" WHERE %s" % condition if where else "", " WHERE " + " AND ".join("%s='%s'" % x for x in where.items())
) if where else "",
) )).fetchall()
return cursor.fetchall() finally:
db.close()
class TestJupyterCustomFrontend(SelectMixin, InstanceTestCase): class TestJupyterCustomFrontend(SelectMixin, InstanceTestCase):
...@@ -226,7 +220,7 @@ class TestJupyterCustomFrontend(SelectMixin, InstanceTestCase): ...@@ -226,7 +220,7 @@ class TestJupyterCustomFrontend(SelectMixin, InstanceTestCase):
except Exception: except Exception:
pass pass
selection = self.select(fields=["*"], table = "slave14", where = {"hosted_by": r._partition_id}) selection = self.select("slave", hosted_by=r._partition_id)
self.assertEqual(len(selection), 1) self.assertEqual(len(selection), 1)
...@@ -268,7 +262,7 @@ class TestJupyterCustomAdditional(SelectMixin, InstanceTestCase): ...@@ -268,7 +262,7 @@ class TestJupyterCustomAdditional(SelectMixin, InstanceTestCase):
except Exception: except Exception:
pass pass
selection = self.select(fields=["*"], table = "slave14", where = {"hosted_by": r._partition_id}) selection = self.select("slave", hosted_by=r._partition_id)
self.assertEqual(len(selection), 1) self.assertEqual(len(selection), 1)
......
...@@ -46,6 +46,7 @@ import time ...@@ -46,6 +46,7 @@ import time
import shutil import shutil
import sys import sys
from slapos.proxy.db_version import DB_VERSION
from slapos.recipe.librecipe import generateHashFromFiles from slapos.recipe.librecipe import generateHashFromFiles
from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass
from slapos.slap.standalone import SlapOSNodeCommandError from slapos.slap.standalone import SlapOSNodeCommandError
...@@ -214,22 +215,19 @@ class MonitorAccessMixin(object): ...@@ -214,22 +215,19 @@ class MonitorAccessMixin(object):
return sqlite3.connect(sqlitedb_file) return sqlite3.connect(sqlitedb_file)
def get_all_instantiated_partition_list(self): def get_all_instantiated_partition_list(self):
connection = self.sqlite3_connect() db = self.sqlite3_connect()
try:
def dict_factory(cursor, row): db.row_factory = lambda cursor, row: {
d = {} col[0]: row[idx]
for idx, col in enumerate(cursor.description): for idx, col in enumerate(cursor.description)
d[col[0]] = row[idx] }
return d return db.execute(
connection.row_factory = dict_factory "SELECT reference, xml, connection_xml, partition_reference,"
cursor = connection.cursor() " software_release, requested_state, software_type"
" FROM partition%s"
cursor.execute( " WHERE slap_state='busy'" % DB_VERSION).fetchall()
"SELECT reference, xml, connection_xml, partition_reference, " finally:
"software_release, requested_state, software_type " db.close()
"FROM partition14 "
"WHERE slap_state='busy'")
return cursor.fetchall()
def test_access_monitor(self): def test_access_monitor(self):
connection_parameter_dict = self.computer_partition\ connection_parameter_dict = self.computer_partition\
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment