github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/index/mongo/mongoindex_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mongo_test
    18  
    19  import (
    20  	"errors"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"camlistore.org/pkg/index"
    26  	"camlistore.org/pkg/index/indextest"
    27  	"camlistore.org/pkg/sorted"
    28  	"camlistore.org/pkg/sorted/kvtest"
    29  	"camlistore.org/pkg/sorted/mongo"
    30  	"camlistore.org/pkg/test"
    31  )
    32  
    33  var (
    34  	once              sync.Once
    35  	mongoNotAvailable bool
    36  )
    37  
    38  func checkMongoUp() {
    39  	mongoNotAvailable = !mongo.Ping("localhost", 500*time.Millisecond)
    40  }
    41  
    42  func skipOrFailIfNoMongo(t *testing.T) {
    43  	once.Do(checkMongoUp)
    44  	if mongoNotAvailable {
    45  		err := errors.New("Not running; start a mongoDB daemon on the standard port (27017). The \"keys\" collection in the \"camlitest\" database will be used.")
    46  		test.DependencyErrorOrSkip(t)
    47  		t.Fatalf("Mongo not available locally for testing: %v", err)
    48  	}
    49  }
    50  
    51  func newSorted(t *testing.T) (kv sorted.KeyValue, cleanup func()) {
    52  	skipOrFailIfNoMongo(t)
    53  
    54  	// connect without credentials and wipe the database
    55  	cfg := mongo.Config{
    56  		Server:   "localhost",
    57  		Database: "camlitest",
    58  	}
    59  	var err error
    60  	kv, err = mongo.NewKeyValue(cfg)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	wiper, ok := kv.(sorted.Wiper)
    65  	if !ok {
    66  		panic("mongo KeyValue not a Wiper")
    67  	}
    68  	err = wiper.Wipe()
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	return kv, func() {
    73  		kv.Close()
    74  	}
    75  }
    76  
    77  func TestSortedKV(t *testing.T) {
    78  	kv, cleanup := newSorted(t)
    79  	defer cleanup()
    80  	kvtest.TestSorted(t, kv)
    81  }
    82  
    83  type mongoTester struct{}
    84  
    85  func (mongoTester) test(t *testing.T, tfn func(*testing.T, func() *index.Index)) {
    86  	defer test.TLog(t)()
    87  	var mu sync.Mutex // guards cleanups
    88  	var cleanups []func()
    89  	defer func() {
    90  		mu.Lock() // never unlocked
    91  		for _, fn := range cleanups {
    92  			fn()
    93  		}
    94  	}()
    95  	initIndex := func() *index.Index {
    96  		kv, cleanup := newSorted(t)
    97  		mu.Lock()
    98  		cleanups = append(cleanups, cleanup)
    99  		mu.Unlock()
   100  		return index.New(kv)
   101  	}
   102  	tfn(t, initIndex)
   103  }
   104  
   105  func TestIndex_Mongo(t *testing.T) {
   106  	mongoTester{}.test(t, indextest.Index)
   107  }
   108  
   109  func TestPathsOfSignerTarget_Mongo(t *testing.T) {
   110  	mongoTester{}.test(t, indextest.PathsOfSignerTarget)
   111  }
   112  
   113  func TestFiles_Mongo(t *testing.T) {
   114  	mongoTester{}.test(t, indextest.Files)
   115  }
   116  
   117  func TestEdgesTo_Mongo(t *testing.T) {
   118  	mongoTester{}.test(t, indextest.EdgesTo)
   119  }
   120  
   121  func TestDelete_Mongo(t *testing.T) {
   122  	mongoTester{}.test(t, indextest.Delete)
   123  }