2012-03-29 00:15:37 +00:00
|
|
|
/*
|
2013-12-18 18:01:13 +00:00
|
|
|
Copyright 2012 The Camlistore Authors.
|
2012-03-29 00:15:37 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mysql_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2012-03-29 23:12:12 +00:00
|
|
|
"fmt"
|
2013-12-18 18:01:13 +00:00
|
|
|
"log"
|
2012-03-29 00:15:37 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"camlistore.org/pkg/index"
|
|
|
|
"camlistore.org/pkg/index/indextest"
|
2013-08-28 19:00:17 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2013-12-18 18:01:13 +00:00
|
|
|
"camlistore.org/pkg/sorted"
|
|
|
|
"camlistore.org/pkg/sorted/kvtest"
|
|
|
|
"camlistore.org/pkg/sorted/mysql"
|
2013-07-08 04:12:18 +00:00
|
|
|
"camlistore.org/pkg/test"
|
2012-03-29 00:15:37 +00:00
|
|
|
|
|
|
|
_ "camlistore.org/third_party/github.com/ziutek/mymysql/godrv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
dbAvailable bool
|
2012-04-09 16:53:49 +00:00
|
|
|
rootdb *sql.DB
|
2012-03-29 00:15:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func checkDB() {
|
|
|
|
var err error
|
2013-12-18 18:01:13 +00:00
|
|
|
rootdb, err = sql.Open("mymysql", "mysql/root/root")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not open rootdb: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var n int
|
|
|
|
err = rootdb.QueryRow("SELECT COUNT(*) FROM user").Scan(&n)
|
|
|
|
if err == nil {
|
|
|
|
dbAvailable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipOrFailIfNoMySQL(t *testing.T) {
|
|
|
|
once.Do(checkDB)
|
|
|
|
if !dbAvailable {
|
|
|
|
// TODO(bradfitz): accept somehow other passwords than
|
|
|
|
// 'root', and/or try localhost unix socket
|
|
|
|
// connections rather than using TCP localhost?
|
|
|
|
err := errors.New("Not running; start a MySQL daemon on the standard port (3306) with root password 'root'")
|
|
|
|
test.DependencyErrorOrSkip(t)
|
|
|
|
t.Fatalf("MySQL not available locally for testing: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func do(db *sql.DB, sql string) {
|
|
|
|
_, err := db.Exec(sql)
|
|
|
|
if err == nil {
|
|
|
|
return
|
2012-03-29 00:15:37 +00:00
|
|
|
}
|
2013-12-18 18:01:13 +00:00
|
|
|
log.Fatalf("Error %v running SQL: %s", err, sql)
|
2012-03-29 00:15:37 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 18:01:13 +00:00
|
|
|
func newSorted(t *testing.T) (kv sorted.KeyValue, clean func()) {
|
|
|
|
skipOrFailIfNoMySQL(t)
|
2013-08-28 19:00:17 +00:00
|
|
|
dbname := "camlitest_" + osutil.Username()
|
2012-04-09 16:53:49 +00:00
|
|
|
do(rootdb, "DROP DATABASE IF EXISTS "+dbname)
|
|
|
|
do(rootdb, "CREATE DATABASE "+dbname)
|
2012-03-29 23:12:12 +00:00
|
|
|
|
2012-04-09 16:53:49 +00:00
|
|
|
db, err := sql.Open("mymysql", dbname+"/root/root")
|
2012-03-29 23:12:12 +00:00
|
|
|
if err != nil {
|
2013-12-18 18:01:13 +00:00
|
|
|
t.Fatalf("opening test database: " + err.Error())
|
2012-03-29 23:12:12 +00:00
|
|
|
}
|
|
|
|
for _, tableSql := range mysql.SQLCreateTables() {
|
|
|
|
do(db, tableSql)
|
|
|
|
}
|
|
|
|
do(db, fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, mysql.SchemaVersion()))
|
2013-12-18 18:01:13 +00:00
|
|
|
|
|
|
|
kv, err = mysql.NewKeyValue(mysql.Config{
|
|
|
|
Database: dbname,
|
|
|
|
User: "root",
|
|
|
|
Password: "root",
|
|
|
|
})
|
2012-03-29 23:12:12 +00:00
|
|
|
if err != nil {
|
2013-12-18 18:01:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return kv, func() {
|
|
|
|
kv.Close()
|
2012-03-29 23:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:01:13 +00:00
|
|
|
func TestSortedKV(t *testing.T) {
|
|
|
|
kv, clean := newSorted(t)
|
|
|
|
defer clean()
|
|
|
|
kvtest.TestSorted(t, kv)
|
2012-03-29 00:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mysqlTester struct{}
|
|
|
|
|
|
|
|
func (mysqlTester) test(t *testing.T, tfn func(*testing.T, func() *index.Index)) {
|
2013-12-18 18:01:13 +00:00
|
|
|
var mu sync.Mutex // guards cleanups
|
|
|
|
var cleanups []func()
|
|
|
|
defer func() {
|
|
|
|
mu.Lock() // never unlocked
|
|
|
|
for _, fn := range cleanups {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
makeIndex := func() *index.Index {
|
|
|
|
s, cleanup := newSorted(t)
|
|
|
|
mu.Lock()
|
|
|
|
cleanups = append(cleanups, cleanup)
|
|
|
|
mu.Unlock()
|
|
|
|
return index.New(s)
|
2012-03-29 00:15:37 +00:00
|
|
|
}
|
|
|
|
tfn(t, makeIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndex_MySQL(t *testing.T) {
|
|
|
|
mysqlTester{}.test(t, indextest.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathsOfSignerTarget_MySQL(t *testing.T) {
|
|
|
|
mysqlTester{}.test(t, indextest.PathsOfSignerTarget)
|
|
|
|
}
|
|
|
|
|
2012-04-13 23:08:40 +00:00
|
|
|
func TestFiles_MySQL(t *testing.T) {
|
2012-03-29 00:15:37 +00:00
|
|
|
mysqlTester{}.test(t, indextest.Files)
|
|
|
|
}
|
2012-11-05 09:29:42 +00:00
|
|
|
|
|
|
|
func TestEdgesTo_MySQL(t *testing.T) {
|
|
|
|
mysqlTester{}.test(t, indextest.EdgesTo)
|
|
|
|
}
|
2013-11-04 22:15:24 +00:00
|
|
|
|
2013-11-27 22:20:28 +00:00
|
|
|
func TestDelete_MySQL(t *testing.T) {
|
|
|
|
mysqlTester{}.test(t, indextest.Delete)
|
2013-11-04 22:15:24 +00:00
|
|
|
}
|