github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/namespace/ns_test.go (about)

     1  /*
     2  Copyright 2014 The Camlistore Authors
     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 namespace
    18  
    19  import (
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  
    24  	"camlistore.org/pkg/blob"
    25  	"camlistore.org/pkg/blobserver"
    26  	"camlistore.org/pkg/blobserver/storagetest"
    27  	"camlistore.org/pkg/context"
    28  	"camlistore.org/pkg/test"
    29  )
    30  
    31  func newNamespace(t *testing.T, ld *test.Loader) *nsto {
    32  	sto, err := newFromConfig(ld, map[string]interface{}{
    33  		"storage": "/good-storage/",
    34  		"inventory": map[string]interface{}{
    35  			"type": "memory",
    36  		},
    37  	})
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	return sto.(*nsto)
    42  }
    43  
    44  func TestStorageTest(t *testing.T) {
    45  	storagetest.Test(t, func(t *testing.T) (sto blobserver.Storage, cleanup func()) {
    46  		ld := test.NewLoader()
    47  		return newNamespace(t, ld), func() {}
    48  	})
    49  }
    50  
    51  func TestIsolation(t *testing.T) {
    52  	ld := test.NewLoader()
    53  	master, _ := ld.GetStorage("/good-storage/")
    54  
    55  	ns1 := newNamespace(t, ld)
    56  	ns2 := newNamespace(t, ld)
    57  	stoMap := map[string]blobserver.Storage{
    58  		"ns1":    ns1,
    59  		"ns2":    ns2,
    60  		"master": master,
    61  	}
    62  
    63  	want := func(src string, want ...blob.Ref) {
    64  		if _, ok := stoMap[src]; !ok {
    65  			t.Fatalf("undefined storage %q", src)
    66  		}
    67  		sort.Sort(blob.ByRef(want))
    68  		var got []blob.Ref
    69  		if err := blobserver.EnumerateAll(context.TODO(), stoMap[src], func(sb blob.SizedRef) error {
    70  			got = append(got, sb.Ref)
    71  			return nil
    72  		}); err != nil {
    73  			t.Fatal(err)
    74  		}
    75  		if !reflect.DeepEqual(got, want) {
    76  			t.Errorf("server %q = %q; want %q", src, got, want)
    77  		}
    78  	}
    79  
    80  	b1 := &test.Blob{Contents: "Blob 1"}
    81  	b1r := b1.BlobRef()
    82  	b2 := &test.Blob{Contents: "Blob 2"}
    83  	b2r := b2.BlobRef()
    84  	b3 := &test.Blob{Contents: "Shared Blob"}
    85  	b3r := b3.BlobRef()
    86  
    87  	b1.MustUpload(t, ns1)
    88  	want("ns1", b1r)
    89  	want("ns2")
    90  	want("master", b1r)
    91  
    92  	b2.MustUpload(t, ns2)
    93  	want("ns1", b1r)
    94  	want("ns2", b2r)
    95  	want("master", b1r, b2r)
    96  
    97  	b3.MustUpload(t, ns2)
    98  	want("ns1", b1r)
    99  	want("ns2", b2r, b3r)
   100  	want("master", b1r, b2r, b3r)
   101  
   102  	b3.MustUpload(t, ns1)
   103  	want("ns1", b1r, b3r)
   104  	want("ns2", b2r, b3r)
   105  	want("master", b1r, b2r, b3r)
   106  
   107  	if _, _, err := ns2.Fetch(b1r); err == nil {
   108  		t.Errorf("b1 shouldn't be accessible via ns2")
   109  	}
   110  }