github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/cond/cond_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 cond
    18  
    19  import (
    20  	"testing"
    21  
    22  	"camlistore.org/pkg/blob"
    23  	"camlistore.org/pkg/blobserver"
    24  	"camlistore.org/pkg/blobserver/replica"
    25  	"camlistore.org/pkg/blobserver/storagetest"
    26  	"camlistore.org/pkg/jsonconfig"
    27  	"camlistore.org/pkg/test"
    28  )
    29  
    30  func newCond(t *testing.T, ld *test.Loader, config jsonconfig.Obj) *condStorage {
    31  	sto, err := newFromConfig(ld, config)
    32  	if err != nil {
    33  		t.Fatalf("Invalid config: %v", err)
    34  	}
    35  	return sto.(*condStorage)
    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 TestStorageTest(t *testing.T) {
    54  	storagetest.Test(t, func(t *testing.T) (_ blobserver.Storage, cleanup func()) {
    55  		ld := test.NewLoader()
    56  		s1, _ := ld.GetStorage("/good-schema/")
    57  		s2, _ := ld.GetStorage("/good-other/")
    58  		ld.SetStorage("/replica-all/", replica.NewForTest([]blobserver.Storage{s1, s2}))
    59  		sto := newCond(t, ld, map[string]interface{}{
    60  			"write": map[string]interface{}{
    61  				"if":   "isSchema",
    62  				"then": "/good-schema/",
    63  				"else": "/good-other/",
    64  			},
    65  			"read":   "/replica-all/",
    66  			"remove": "/replica-all/",
    67  		})
    68  		return sto, func() {}
    69  	})
    70  }
    71  
    72  func TestReceiveIsSchema(t *testing.T) {
    73  	ld := test.NewLoader()
    74  	sto := newCond(t, ld, map[string]interface{}{
    75  		"write": map[string]interface{}{
    76  			"if":   "isSchema",
    77  			"then": "/good-schema/",
    78  			"else": "/good-other/",
    79  		},
    80  		"read": "/good-other/",
    81  	})
    82  	otherBlob := &test.Blob{Contents: "stuff"}
    83  	schemaBlob := &test.Blob{Contents: `{"camliVersion": 1, "camliType": "foo"}`}
    84  
    85  	ssb := mustReceive(t, sto, schemaBlob)
    86  	osb := mustReceive(t, sto, otherBlob)
    87  
    88  	ssto, _ := ld.GetStorage("/good-schema/")
    89  	osto, _ := ld.GetStorage("/good-other/")
    90  
    91  	if _, err := blobserver.StatBlob(ssto, ssb.Ref); err != nil {
    92  		t.Errorf("schema blob didn't end up on schema storage")
    93  	}
    94  	if _, err := blobserver.StatBlob(osto, osb.Ref); err != nil {
    95  		t.Errorf("other blob didn't end up on other storage")
    96  	}
    97  }