github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/resource_test.go (about)

     1  package processor
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/message"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  	"github.com/Jeffail/benthos/v3/lib/types"
    11  )
    12  
    13  type fakeProcMgr struct {
    14  	procs map[string]Type
    15  }
    16  
    17  func (f *fakeProcMgr) RegisterEndpoint(path, desc string, h http.HandlerFunc) {
    18  }
    19  func (f *fakeProcMgr) GetCache(name string) (types.Cache, error) {
    20  	return nil, types.ErrCacheNotFound
    21  }
    22  func (f *fakeProcMgr) GetCondition(name string) (types.Condition, error) {
    23  	return nil, types.ErrConditionNotFound
    24  }
    25  func (f *fakeProcMgr) GetProcessor(name string) (types.Processor, error) {
    26  	if p, exists := f.procs[name]; exists {
    27  		return p, nil
    28  	}
    29  	return nil, types.ErrProcessorNotFound
    30  }
    31  func (f *fakeProcMgr) GetRateLimit(name string) (types.RateLimit, error) {
    32  	return nil, types.ErrRateLimitNotFound
    33  }
    34  func (f *fakeProcMgr) GetPlugin(name string) (interface{}, error) {
    35  	return nil, types.ErrPluginNotFound
    36  }
    37  func (f *fakeProcMgr) GetPipe(name string) (<-chan types.Transaction, error) {
    38  	return nil, types.ErrPipeNotFound
    39  }
    40  func (f *fakeProcMgr) SetPipe(name string, prod <-chan types.Transaction)   {}
    41  func (f *fakeProcMgr) UnsetPipe(name string, prod <-chan types.Transaction) {}
    42  
    43  func TestResourceProc(t *testing.T) {
    44  	conf := NewConfig()
    45  	conf.Type = "text"
    46  	conf.Text.Operator = "prepend"
    47  	conf.Text.Value = "foo: "
    48  
    49  	resProc, err := New(conf, nil, log.Noop(), metrics.Noop())
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	mgr := &fakeProcMgr{
    55  		procs: map[string]Type{
    56  			"foo": resProc,
    57  		},
    58  	}
    59  
    60  	nConf := NewConfig()
    61  	nConf.Type = "resource"
    62  	nConf.Resource = "foo"
    63  
    64  	p, err := New(nConf, mgr, log.Noop(), metrics.Noop())
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	msgs, res := p.ProcessMessage(message.New([][]byte{[]byte("bar")}))
    70  	if res != nil {
    71  		t.Fatal(res.Error())
    72  	}
    73  	if len(msgs) != 1 {
    74  		t.Error("Expected only 1 message")
    75  	}
    76  	if exp, act := "foo: bar", string(msgs[0].Get(0).Get()); exp != act {
    77  		t.Errorf("Wrong result: %v != %v", act, exp)
    78  	}
    79  }
    80  
    81  func TestResourceBadName(t *testing.T) {
    82  	mgr := &fakeProcMgr{
    83  		procs: map[string]Type{},
    84  	}
    85  
    86  	conf := NewConfig()
    87  	conf.Type = "resource"
    88  	conf.Resource = "foo"
    89  
    90  	_, err := NewResource(conf, mgr, log.Noop(), metrics.Noop())
    91  	if err == nil {
    92  		t.Error("expected error from bad resource")
    93  	}
    94  }