2012-02-26 12:49:03 +00:00
/ *
Copyright 2011 Google Inc .
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 mongo_test
import (
"errors"
"sync"
"testing"
"time"
"camlistore.org/pkg/index"
"camlistore.org/pkg/index/indextest"
2013-12-12 17:08:28 +00:00
"camlistore.org/pkg/sorted"
"camlistore.org/pkg/sorted/kvtest"
"camlistore.org/pkg/sorted/mongo"
2013-07-08 04:12:18 +00:00
"camlistore.org/pkg/test"
2012-02-26 12:49:03 +00:00
)
var (
once sync . Once
mongoNotAvailable bool
)
func checkMongoUp ( ) {
2013-12-12 17:08:28 +00:00
mongoNotAvailable = ! mongo . Ping ( "localhost" , 500 * time . Millisecond )
2012-02-26 12:49:03 +00:00
}
2013-12-14 14:15:09 +00:00
func skipOrFailIfNoMongo ( t * testing . T ) {
once . Do ( checkMongoUp )
if mongoNotAvailable {
err := errors . New ( "Not running; start a mongoDB daemon on the standard port (27017). The \"keys\" collection in the \"camlitest\" database will be used." )
test . DependencyErrorOrSkip ( t )
t . Fatalf ( "Mongo not available locally for testing: %v" , err )
}
}
2013-12-12 17:08:28 +00:00
func newSorted ( t * testing . T ) ( kv sorted . KeyValue , cleanup func ( ) ) {
2013-12-14 14:15:09 +00:00
skipOrFailIfNoMongo ( t )
2012-10-23 19:45:46 +00:00
// connect without credentials and wipe the database
2013-12-17 00:53:36 +00:00
cfg := mongo . Config {
Server : "localhost" ,
Database : "camlitest" ,
2012-02-26 12:49:03 +00:00
}
2013-12-12 17:08:28 +00:00
var err error
kv , err = mongo . NewKeyValue ( cfg )
2012-02-26 12:49:03 +00:00
if err != nil {
2013-12-12 17:08:28 +00:00
t . Fatal ( err )
2012-02-26 12:49:03 +00:00
}
2013-12-12 17:08:28 +00:00
wiper , ok := kv . ( sorted . Wiper )
if ! ok {
panic ( "mongo KeyValue not a Wiper" )
2012-02-26 12:49:03 +00:00
}
2013-12-12 17:08:28 +00:00
err = wiper . Wipe ( )
2012-10-23 19:45:46 +00:00
if err != nil {
2013-12-12 17:08:28 +00:00
t . Fatal ( err )
2012-10-23 19:45:46 +00:00
}
2013-12-12 17:08:28 +00:00
return kv , func ( ) {
kv . Close ( )
2012-10-23 19:45:46 +00:00
}
2013-12-12 17:08:28 +00:00
}
func TestSortedKV ( t * testing . T ) {
kv , cleanup := newSorted ( t )
defer cleanup ( )
kvtest . TestSorted ( t , kv )
2012-02-26 12:49:03 +00:00
}
type mongoTester struct { }
func ( mongoTester ) test ( t * testing . T , tfn func ( * testing . T , func ( ) * index . Index ) ) {
2013-12-12 17:08:28 +00:00
defer test . TLog ( t ) ( )
2013-12-18 18:01:13 +00:00
var mu sync . Mutex // guards cleanups
2013-12-12 17:08:28 +00:00
var cleanups [ ] func ( )
defer func ( ) {
2013-12-18 18:01:13 +00:00
mu . Lock ( ) // never unlocked
2013-12-12 17:08:28 +00:00
for _ , fn := range cleanups {
fn ( )
}
} ( )
initIndex := func ( ) * index . Index {
kv , cleanup := newSorted ( t )
2013-12-18 18:01:13 +00:00
mu . Lock ( )
2013-12-12 17:08:28 +00:00
cleanups = append ( cleanups , cleanup )
2013-12-18 18:01:13 +00:00
mu . Unlock ( )
2014-03-15 15:36:52 +00:00
return index . MustNew ( t , kv )
2013-12-12 17:08:28 +00:00
}
tfn ( t , initIndex )
2012-02-26 12:49:03 +00:00
}
func TestIndex_Mongo ( t * testing . T ) {
mongoTester { } . test ( t , indextest . Index )
}
func TestPathsOfSignerTarget_Mongo ( t * testing . T ) {
mongoTester { } . test ( t , indextest . PathsOfSignerTarget )
}
func TestFiles_Mongo ( t * testing . T ) {
mongoTester { } . test ( t , indextest . Files )
}
2012-11-05 09:29:42 +00:00
func TestEdgesTo_Mongo ( t * testing . T ) {
mongoTester { } . test ( t , indextest . EdgesTo )
}
2013-11-04 22:15:24 +00:00
2013-11-27 22:20:28 +00:00
func TestDelete_Mongo ( t * testing . T ) {
mongoTester { } . test ( t , indextest . Delete )
2013-11-04 22:15:24 +00:00
}