github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/replica/replica_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 replica 18 19 import ( 20 "testing" 21 22 "camlistore.org/pkg/blob" 23 "camlistore.org/pkg/blobserver" 24 "camlistore.org/pkg/blobserver/storagetest" 25 "camlistore.org/pkg/jsonconfig" 26 "camlistore.org/pkg/test" 27 ) 28 29 func newReplica(t *testing.T, config jsonconfig.Obj) *replicaStorage { 30 ld := test.NewLoader() 31 sto, err := newFromConfig(ld, config) 32 if err != nil { 33 t.Fatalf("Invalid config: %v", err) 34 } 35 return sto.(*replicaStorage) 36 } 37 38 func mustReceive(t *testing.T, dst blobserver.Storage, tb *test.Blob) blob.SizedRef { 39 tbRef := tb.BlobRef() 40 sb, err := blobserver.Receive(dst, tbRef, tb.Reader()) 41 if err != nil { 42 t.Fatalf("Receive: %v", err) 43 } 44 if int(sb.Size) != len(tb.Contents) { 45 t.Fatalf("size = %d; want %d", sb.Size, len(tb.Contents)) 46 } 47 if sb.Ref != tbRef { 48 t.Fatal("wrong blob received") 49 } 50 return sb 51 } 52 53 func TestReceiveGood(t *testing.T) { 54 sto := newReplica(t, map[string]interface{}{ 55 "backends": []interface{}{"/good-1/", "/good-2/"}, 56 }) 57 tb := &test.Blob{Contents: "stuff"} 58 sb := mustReceive(t, sto, tb) 59 60 if len(sto.replicas) != 2 { 61 t.Fatalf("replicas = %d; want 2", len(sto.replicas)) 62 } 63 for i, rep := range sto.replicas { 64 got, err := blobserver.StatBlob(rep, sb.Ref) 65 if err != nil { 66 t.Errorf("Replica %s got stat error %v", sto.replicaPrefixes[i], err) 67 } else if got != sb { 68 t.Errorf("Replica %s got %+v; want %+v", sto.replicaPrefixes[i], got, sb) 69 } 70 } 71 } 72 73 func TestReceiveOneGoodOneFail(t *testing.T) { 74 sto := newReplica(t, map[string]interface{}{ 75 "backends": []interface{}{"/good-1/", "/fail-1/"}, 76 "minWritesForSuccess": float64(1), 77 }) 78 tb := &test.Blob{Contents: "stuff"} 79 sb := mustReceive(t, sto, tb) 80 81 if len(sto.replicas) != 2 { 82 t.Fatalf("replicas = %d; want 2", len(sto.replicas)) 83 } 84 for i, rep := range sto.replicas { 85 got, err := blobserver.StatBlob(rep, sb.Ref) 86 pfx := sto.replicaPrefixes[i] 87 if (i == 0) != (err == nil) { 88 t.Errorf("For replica %s, unexpected error: %v", pfx, err) 89 } 90 if err == nil && got != sb { 91 t.Errorf("Replica %s got %+v; want %+v", sto.replicaPrefixes[i], got, sb) 92 } 93 } 94 } 95 96 func TestReplica(t *testing.T) { 97 storagetest.Test(t, func(t *testing.T) (sto blobserver.Storage, cleanup func()) { 98 sto = newReplica(t, map[string]interface{}{ 99 "backends": []interface{}{"/good-1/", "/good-2/"}, 100 }) 101 return sto, func() {} 102 }) 103 }