[lxc-devel] [lxd/master] Drop dependencies on Daemon in db.go
freeekanayaka on Github
lxc-bot at linuxcontainers.org
Wed Aug 16 11:39:55 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 481 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170816/ded019b5/attachment.bin>
-------------- next part --------------
From 4f4cd90f07f1cadd98484811494b0ae323c60cb5 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Wed, 16 Aug 2017 11:36:03 +0000
Subject: [PATCH] Drop dependencies on Daemon in db.go
This branch drops any mention of the Daemon structure in db.go, and is
a step towards making the db functionality self-contained (in order to
put it in a separate db/ package).
Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
lxd/.dir-locals.el | 3 +++
lxd/daemon.go | 9 +--------
lxd/db.go | 12 ++++++++++++
lxd/db_test.go | 46 +++++++++++++++++-----------------------------
4 files changed, 33 insertions(+), 37 deletions(-)
create mode 100644 lxd/.dir-locals.el
diff --git a/lxd/.dir-locals.el b/lxd/.dir-locals.el
new file mode 100644
index 000000000..9bebcc48c
--- /dev/null
+++ b/lxd/.dir-locals.el
@@ -0,0 +1,3 @@
+;;; Directory Local Variables
+;;; For more information see (info "(emacs) Directory Variables")
+((go-mode . ((go-test-args . "-tags libsqlite3"))))
diff --git a/lxd/daemon.go b/lxd/daemon.go
index 0e5c3e306..c3e1f547f 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -1405,17 +1405,10 @@ func (s *lxdHttpServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Create a database connection and perform any updates needed.
func initializeDbObject(d *Daemon, path string) error {
- var openPath string
var err error
- timeout := 5 // TODO - make this command-line configurable?
-
- // These are used to tune the transaction BEGIN behavior instead of using the
- // similar "locking_mode" pragma (locking for the whole database connection).
- openPath = fmt.Sprintf("%s?_busy_timeout=%d&_txlock=exclusive", path, timeout*1000)
-
// Open the database. If the file doesn't exist it is created.
- d.db, err = sql.Open("sqlite3_with_fk", openPath)
+ d.db, err = openDb(path)
if err != nil {
return err
}
diff --git a/lxd/db.go b/lxd/db.go
index 974e48349..a0978bd1b 100644
--- a/lxd/db.go
+++ b/lxd/db.go
@@ -224,6 +224,18 @@ func init() {
sql.Register("sqlite3_with_fk", &sqlite3.SQLiteDriver{ConnectHook: enableForeignKeys})
}
+// Open the database with the correct parameters for LXD.
+func openDb(path string) (*sql.DB, error) {
+ timeout := 5 // TODO - make this command-line configurable?
+
+ // These are used to tune the transaction BEGIN behavior instead of using the
+ // similar "locking_mode" pragma (locking for the whole database connection).
+ openPath := fmt.Sprintf("%s?_busy_timeout=%d&_txlock=exclusive", path, timeout*1000)
+
+ // Open the database. If the file doesn't exist it is created.
+ return sql.Open("sqlite3_with_fk", openPath)
+}
+
// Create the initial (current) schema for a given SQLite DB connection.
func createDb(db *sql.DB) (err error) {
latestVersion := dbGetSchema(db)
diff --git a/lxd/db_test.go b/lxd/db_test.go
index 304408883..fd55c32b3 100644
--- a/lxd/db_test.go
+++ b/lxd/db_test.go
@@ -37,6 +37,8 @@ type dbTestSuite struct {
func (s *dbTestSuite) SetupTest() {
s.db = s.CreateTestDb()
+ _, err := s.db.Exec(DB_FIXTURES)
+ s.Nil(err)
}
func (s *dbTestSuite) TearDownTest() {
@@ -44,7 +46,7 @@ func (s *dbTestSuite) TearDownTest() {
}
// Initialize a test in-memory DB.
-func (s *dbTestSuite) CreateTestDb() (db *sql.DB) {
+func (s *dbTestSuite) CreateTestDb() *sql.DB {
// Setup logging if main() hasn't been called/when testing
if logger.Log == nil {
var err error
@@ -52,15 +54,12 @@ func (s *dbTestSuite) CreateTestDb() (db *sql.DB) {
s.Nil(err)
}
- var err error
- d := &Daemon{MockMode: true}
- err = initializeDbObject(d, ":memory:")
+ db, err := openDb(":memory:")
s.Nil(err)
- db = d.db
+ s.Nil(createDb(db))
+ s.Nil(dbUpdatesApplyAll(db, false, nil))
+ return db
- _, err = db.Exec(DB_FIXTURES)
- s.Nil(err)
- return // db is a named output param
}
func TestDBTestSuite(t *testing.T) {
@@ -162,18 +161,12 @@ func (s *dbTestSuite) Test_deleting_an_image_cascades_on_related_tables() {
}
func (s *dbTestSuite) Test_initializing_db_is_idempotent() {
- var db *sql.DB
- var err error
-
// This calls "createDb" once already.
- d := &Daemon{MockMode: true}
- err = initializeDbObject(d, ":memory:")
- db = d.db
+ db := s.CreateTestDb()
defer db.Close()
// Let's call it a second time.
- err = createDb(db)
- s.Nil(err)
+ s.Nil(createDb(db))
}
func (s *dbTestSuite) Test_get_schema_returns_0_on_uninitialized_db() {
@@ -193,9 +186,8 @@ func (s *dbTestSuite) Test_running_dbUpdateFromV6_adds_on_delete_cascade() {
var err error
var count int
- d := &Daemon{MockMode: true}
- err = initializeDbObject(d, ":memory:")
- defer d.db.Close()
+ db := s.CreateTestDb()
+ defer db.Close()
statements := `
CREATE TABLE IF NOT EXISTS containers (
@@ -219,28 +211,28 @@ CREATE TABLE IF NOT EXISTS containers_config (
INSERT INTO containers (name, architecture, type) VALUES ('thename', 1, 1);
INSERT INTO containers_config (container_id, key, value) VALUES (1, 'thekey', 'thevalue');`
- _, err = d.db.Exec(statements)
+ _, err = db.Exec(statements)
s.Nil(err)
// Run the upgrade from V6 code
- err = dbUpdateFromV6(5, 6, d.db)
+ err = dbUpdateFromV6(5, 6, db)
s.Nil(err)
// Make sure the inserted data is still there.
statements = `SELECT count(*) FROM containers_config;`
- err = d.db.QueryRow(statements).Scan(&count)
+ err = db.QueryRow(statements).Scan(&count)
s.Nil(err)
s.Equal(count, 1, "There should be exactly one entry in containers_config!")
// Drop the container.
statements = `DELETE FROM containers WHERE name = 'thename';`
- _, err = d.db.Exec(statements)
+ _, err = db.Exec(statements)
s.Nil(err)
// Make sure there are 0 container_profiles entries left.
statements = `SELECT count(*) FROM containers_profiles;`
- err = d.db.QueryRow(statements).Scan(&count)
+ err = db.QueryRow(statements).Scan(&count)
s.Nil(err)
s.Equal(count, 0, "Deleting a container didn't delete the profile association!")
}
@@ -321,11 +313,7 @@ INSERT INTO containers_config (container_id, key, value) VALUES (1, 'thekey', 't
// The "foreign key" on containers_config now points to nothing.
// Let's run the schema upgrades.
- d := &Daemon{MockMode: true}
- d.db = db
- daemonConfigInit(db)
-
- err = dbUpdatesApplyAll(d.db, false, nil)
+ err = dbUpdatesApplyAll(db, false, nil)
s.Nil(err)
result := dbGetSchema(db)
More information about the lxc-devel
mailing list