1/* 2Copyright 2014 The Perkeep Authors 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package test 18 19import ( 20 "errors" 21 "log" 22 "strings" 23 "sync" 24 25 "perkeep.org/pkg/blobserver" 26) 27 28// NewLoader 29func NewLoader() *Loader { 30 return &Loader{} 31} 32 33type Loader struct { 34 mu sync.Mutex 35 sto map[string]blobserver.Storage 36} 37 38var _ blobserver.Loader = (*Loader)(nil) 39 40func (ld *Loader) FindHandlerByType(handlerType string) (prefix string, handler interface{}, err error) { 41 panic("NOIMPL") 42} 43 44func (ld *Loader) AllHandlers() (map[string]string, map[string]interface{}) { 45 panic("NOIMPL") 46} 47 48func (ld *Loader) MyPrefix() string { 49 return "/lies/" 50} 51 52func (ld *Loader) BaseURL() string { 53 return "http://localhost:1234" 54} 55 56func (ld *Loader) GetHandlerType(prefix string) string { 57 log.Printf("test.Loader: GetHandlerType called but not implemented.") 58 return "" 59} 60 61func (ld *Loader) GetHandler(prefix string) (interface{}, error) { 62 log.Printf("test.Loader: GetHandler called but not implemented.") 63 return nil, errors.New("doesn't exist") 64} 65 66func (ld *Loader) SetStorage(prefix string, s blobserver.Storage) { 67 ld.mu.Lock() 68 defer ld.mu.Unlock() 69 if ld.sto == nil { 70 ld.sto = make(map[string]blobserver.Storage) 71 } 72 ld.sto[prefix] = s 73} 74 75func (ld *Loader) GetStorage(prefix string) (blobserver.Storage, error) { 76 ld.mu.Lock() 77 defer ld.mu.Unlock() 78 if bs, ok := ld.sto[prefix]; ok { 79 return bs, nil 80 } 81 if ld.sto == nil { 82 ld.sto = make(map[string]blobserver.Storage) 83 } 84 sto, err := ld.genStorage(prefix) 85 if err != nil { 86 return nil, err 87 } 88 ld.sto[prefix] = sto 89 return sto, nil 90} 91 92func (ld *Loader) genStorage(prefix string) (blobserver.Storage, error) { 93 if strings.HasPrefix(prefix, "/good") { 94 return &Fetcher{}, nil 95 } 96 if strings.HasPrefix(prefix, "/fail") { 97 return &Fetcher{ReceiveErr: errors.New("test.Loader intentional failure for /fail storage handler")}, nil 98 } 99 panic("test.Loader.GetStorage: unrecognized prefix type") 100} 101