本文整理匯總了Golang中github.com/mozilla-services/heka/pipeline.NewPipelinePack函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPipelinePack函數的具體用法?Golang NewPipelinePack怎麽用?Golang NewPipelinePack使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPipelinePack函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getEmptyKeenOutputPack
func getEmptyKeenOutputPack() *pipeline.PipelinePack {
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message.SetType("KeenOutput")
pack.Decoded = true
pack.Message.SetTimestamp(int64(EPOCH_TS * 1e9))
return pack
}
示例2: getSentryPack
func getSentryPack() (pack *pipeline.PipelinePack) {
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack = pipeline.NewPipelinePack(recycleChan)
pack.Message.SetType("sentry")
fTimeStamp, _ := message.NewField("epoch_timestamp", EPOCH_TS, "utc-seconds")
fDsn, _ := message.NewField("dsn", DSN, "uri")
pack.Message.AddField(fTimeStamp)
pack.Message.AddField(fDsn)
pack.Message.SetPayload(PAYLOAD)
pack.Decoded = true
return
}
示例3: getStatsdPack
func getStatsdPack(typeStr string, payload string) (pack *pipeline.PipelinePack) {
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack = pipeline.NewPipelinePack(recycleChan)
pack.Message.SetType(typeStr)
pack.Message.SetLogger("thenamespace")
fName, _ := message.NewField("name", "myname", "")
fRate, _ := message.NewField("rate", .30, "")
pack.Message.AddField(fName)
pack.Message.AddField(fRate)
pack.Message.SetPayload(payload)
pack.Decoded = true
return pack
}
示例4: RstEncoderSpec
func RstEncoderSpec(c gs.Context) {
c.Specify("A RstEncoder", func() {
encoder := new(RstEncoder)
supply := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(supply)
payload := "This is the payload!"
pack.Message.SetPayload(payload)
loc, err := time.LoadLocation("America/Chicago")
c.Assume(err, gs.IsNil)
timestamp := time.Date(1971, 10, 7, 18, 47, 0, 0, loc)
pack.Message.SetTimestamp(timestamp.UnixNano())
pack.Message.SetType("test.type")
pack.Message.SetHostname("somehost.example.com")
pack.Message.SetPid(12345)
pack.Message.SetUuid(uuid.Parse("72de6a05-1b99-4a88-84c2-90797624c68f"))
pack.Message.SetLogger("loggyloglog")
pack.Message.SetEnvVersion("0.8")
field, err := message.NewField("intfield", 23, "count")
c.Assume(err, gs.IsNil)
field.AddValue(24)
field.AddValue(25)
pack.Message.AddField(field)
field, err = message.NewField("stringfield", "hold", "foreigner")
c.Assume(err, gs.IsNil)
field.AddValue("your")
field.AddValue("head")
field.AddValue("up")
pack.Message.AddField(field)
field, err = message.NewField("bool", true, "")
c.Assume(err, gs.IsNil)
field.AddValue(false)
pack.Message.AddField(field)
field, err = message.NewField("bool", false, "false")
c.Assume(err, gs.IsNil)
pack.Message.AddField(field)
field, err = message.NewField("float", 345.6789, "kelvin")
c.Assume(err, gs.IsNil)
field.AddValue(139847987987987.878732819)
pack.Message.AddField(field)
field, err = message.NewField("bytes", []byte("encode me"), "binary")
c.Assume(err, gs.IsNil)
field.AddValue([]byte("and me"))
field.AddValue([]byte("and me too"))
pack.Message.AddField(field)
expected := `
:Timestamp: 1971-10-07 23:47:00 +0000 UTC
:Type: test.type
:Hostname: somehost.example.com
:Pid: 12345
:Uuid: 72de6a05-1b99-4a88-84c2-90797624c68f
:Logger: loggyloglog
:Payload: This is the payload!
:EnvVersion: 0.8
:Severity: 7
:Fields:
| name:"intfield" type:integer value:[23,24,25] representation:"count"
| name:"stringfield" type:string value:["hold","your","head","up"] representation:"foreigner"
| name:"bool" type:bool value:[true,false]
| name:"bool" type:bool value:false representation:"false"
| name:"float" type:double value:[345.6789,1.3984798798798788e+14] representation:"kelvin"
| name:"bytes" type:bytes value:[ZW5jb2RlIG1l,YW5kIG1l,YW5kIG1lIHRvbw==] representation:"binary"
`
c.Specify("serializes a message correctly", func() {
err = encoder.Init(nil)
c.Assume(err, gs.IsNil)
output, err := encoder.Encode(pack)
c.Expect(err, gs.IsNil)
c.Expect(string(output), gs.Equals, expected)
})
})
}
示例5: UdpOutputSpec
func UdpOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
udpOutput := new(UdpOutput)
config := udpOutput.ConfigStruct().(*UdpOutputConfig)
oth := plugins_ts.NewOutputTestHelper(ctrl)
encoder := new(plugins.PayloadEncoder)
encoder.Init(new(plugins.PayloadEncoderConfig))
inChan := make(chan *pipeline.PipelinePack, 1)
rChan := make(chan *pipeline.PipelinePack, 1)
var wg sync.WaitGroup
var rAddr net.Addr
c.Specify("A UdpOutput", func() {
msg := pipeline_ts.GetTestMessage()
payload := "Write me out to the network."
msg.SetPayload(payload)
pack := pipeline.NewPipelinePack(rChan)
pack.Message = msg
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().UpdateCursor("").AnyTimes()
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(pack).Return(encoder.Encode(pack))
c.Specify("using UDP", func() {
addr := "127.0.0.1:45678"
config.Address = addr
ch := make(chan string, 1)
collectData := func() {
conn, err := net.ListenPacket("udp", addr)
if err != nil {
ch <- err.Error()
return
}
ch <- "ready"
b := make([]byte, 1000)
var n int
n, rAddr, _ = conn.ReadFrom(b)
ch <- string(b[:n])
conn.Close()
}
go collectData()
result := <-ch // Wait for server to be ready.
c.Assume(result, gs.Equals, "ready")
c.Specify("writes out to the network", func() {
err := udpOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = udpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
close(inChan)
wg.Wait()
})
c.Specify("uses the specified local address", func() {
config.LocalAddress = "localhost:12345"
err := udpOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = udpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
c.Expect(rAddr.Network(), gs.Equals, "udp")
c.Expect(rAddr.String(), gs.Equals, "127.0.0.1:12345")
close(inChan)
wg.Wait()
})
})
c.Specify("using Unix datagrams", func() {
if runtime.GOOS == "windows" {
return
}
testUnixAddr := func() string {
//.........這裏部分代碼省略.........
示例6: CloudwatchInputSpec
func CloudwatchInputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
c.Specify("A CloudwatchInput", func() {
input := new(CloudwatchInput)
inputConfig := input.ConfigStruct().(*CloudwatchInputConfig)
inputConfig.MetricName = "Test"
inputConfig.Statistics = []string{"Average"}
inputConfig.PollInterval = "1ms"
inputConfig.Region = "us-east-1"
inputConfig.Namespace = "Testing"
err := input.Init(inputConfig)
c.Assume(err, gs.IsNil)
serv := ts.NewMockAWSService(ctrl)
input.cw.Service = serv
ith := new(InputTestHelper)
recycleChan := make(chan *pipeline.PipelinePack, 500)
// set up mock helper, decoder set, and packSupply channel
ith.MockHelper = ts.NewMockPluginHelper(ctrl)
ith.MockInputRunner = ts.NewMockInputRunner(ctrl)
ith.Decoder = ts.NewMockDecoderRunner(ctrl)
ith.PackSupply = make(chan *pipeline.PipelinePack, 1)
ith.DecodeChan = make(chan *pipeline.PipelinePack)
ith.MockDecoderSet = ts.NewMockDecoderSet(ctrl)
ith.Msg = getTestMessage()
ith.Pack = pipeline.NewPipelinePack(recycleChan)
c.Specify("can receive a set of metrics", func() {
ith.PackSupply <- ith.Pack
resp := new(http.Response)
resp.Body = &RespCloser{strings.NewReader(awsResponse)}
resp.StatusCode = 200
// Setup the mock response
ith.MockInputRunner.EXPECT().InChan().Return(ith.PackSupply)
ith.MockInputRunner.EXPECT().Inject(ith.Pack)
serv.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(resp, nil)
go func() {
input.Run(ith.MockInputRunner, ith.MockHelper)
}()
ith.PackSupply <- ith.Pack
close(input.stopChan)
c.Expect(ith.Pack.Message.GetLogger(), gs.Equals, "Testing")
c.Expect(ith.Pack.Message.GetPayload(), gs.Equals, "Test")
val, _ := ith.Pack.Message.GetFieldValue("Unit")
c.Expect(val.(string), gs.Equals, "Seconds")
val, _ = ith.Pack.Message.GetFieldValue("SampleCount")
c.Expect(val.(float64), gs.Equals, float64(837721.0))
})
})
c.Specify("A CloudwatchOutput", func() {
mockOutputRunner := ts.NewMockOutputRunner(ctrl)
mockHelper := ts.NewMockPluginHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack, 1)
recycleChan := make(chan *pipeline.PipelinePack, 1)
mockOutputRunner.EXPECT().InChan().Return(inChan)
msg := getTestMessage()
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message = msg
pack.Decoded = true
output := new(CloudwatchOutput)
outputConfig := output.ConfigStruct().(*CloudwatchOutputConfig)
outputConfig.Retries = 3
outputConfig.Backlog = 10
outputConfig.Namespace = "Test"
outputConfig.Region = "us-east-1"
err := output.Init(outputConfig)
c.Assume(err, gs.IsNil)
serv := ts.NewMockAWSService(ctrl)
output.cw.Service = serv
c.Specify("can send a batch of metrics", func() {
resp := new(http.Response)
resp.Body = &RespCloser{strings.NewReader(awsSuccessResponse)}
resp.StatusCode = 200
pack.Message.SetPayload(simpleJsonPayload)
serv.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(resp, nil)
mockOutputRunner.EXPECT().LogMessage(gomock.Any())
finished := make(chan bool)
inChan <- pack
go func() {
output.Run(mockOutputRunner, mockHelper)
finished <- true
}()
//.........這裏部分代碼省略.........
示例7: FilterSpec
func FilterSpec(c gs.Context) {
t := new(ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fth := NewFilterTestHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack, 1)
pConfig := pipeline.NewPipelineConfig(nil)
c.Specify("A SandboxFilter", func() {
sbFilter := new(SandboxFilter)
sbFilter.SetPipelineConfig(pConfig)
config := sbFilter.ConfigStruct().(*sandbox.SandboxConfig)
config.MemoryLimit = 64000
config.InstructionLimit = 1000
config.OutputLimit = 1024
msg := getTestMessage()
pack := pipeline.NewPipelinePack(pConfig.InjectRecycleChan())
pack.Message = msg
c.Specify("Uninitialized", func() {
err := sbFilter.ReportMsg(msg)
c.Expect(err, gs.IsNil)
})
c.Specify("Over inject messages from ProcessMessage", func() {
var timer <-chan time.Time
fth.MockFilterRunner.EXPECT().Ticker().Return(timer)
fth.MockFilterRunner.EXPECT().InChan().Return(inChan)
fth.MockFilterRunner.EXPECT().UsesBuffering().Return(true)
fth.MockFilterRunner.EXPECT().Name().Return("processinject").Times(2)
fth.MockFilterRunner.EXPECT().Inject(pack).Return(true).Times(2)
fth.MockHelper.EXPECT().PipelinePack(uint(0)).Return(pack, nil).Times(2)
fth.MockHelper.EXPECT().PipelineConfig().Return(pConfig)
config.ScriptFilename = "../lua/testsupport/processinject.lua"
config.ModuleDirectory = "../lua/modules"
err := sbFilter.Init(config)
c.Assume(err, gs.IsNil)
inChan <- pack
close(inChan)
err = sbFilter.Run(fth.MockFilterRunner, fth.MockHelper)
var termErr pipeline.TerminatedError
if runtime.GOOS == "windows" {
termErr = pipeline.TerminatedError("process_message() ..\\lua\\testsupport\\processinject.lua:8: inject_payload() exceeded InjectMessage count")
} else {
termErr = pipeline.TerminatedError("process_message() ../lua/testsupport/processinject.lua:8: inject_payload() exceeded InjectMessage count")
}
c.Expect(err.Error(), gs.Equals, termErr.Error())
})
c.Specify("Over inject messages from TimerEvent", func() {
var timer <-chan time.Time
timer = time.Tick(time.Duration(1) * time.Millisecond)
fth.MockFilterRunner.EXPECT().Ticker().Return(timer)
fth.MockFilterRunner.EXPECT().InChan().Return(inChan)
fth.MockFilterRunner.EXPECT().UsesBuffering().Return(true)
fth.MockFilterRunner.EXPECT().Name().Return("timerinject").Times(11)
fth.MockFilterRunner.EXPECT().Inject(pack).Return(true).Times(11)
fth.MockHelper.EXPECT().PipelinePack(uint(0)).Return(pack, nil).Times(11)
fth.MockHelper.EXPECT().PipelineConfig().Return(pConfig)
config.ScriptFilename = "../lua/testsupport/timerinject.lua"
config.ModuleDirectory = "../lua/modules"
err := sbFilter.Init(config)
c.Assume(err, gs.IsNil)
go func() {
time.Sleep(time.Duration(250) * time.Millisecond)
close(inChan)
}()
err = sbFilter.Run(fth.MockFilterRunner, fth.MockHelper)
var termErr pipeline.TerminatedError
if runtime.GOOS == "windows" {
termErr = pipeline.TerminatedError("timer_event() ..\\lua\\testsupport\\timerinject.lua:13: inject_payload() exceeded InjectMessage count")
} else {
termErr = pipeline.TerminatedError("timer_event() ../lua/testsupport/timerinject.lua:13: inject_payload() exceeded InjectMessage count")
}
c.Expect(err.Error(), gs.Equals, termErr.Error())
})
c.Specify("Preserves data", func() {
var timer <-chan time.Time
fth.MockFilterRunner.EXPECT().Ticker().Return(timer)
fth.MockFilterRunner.EXPECT().InChan().Return(inChan)
fth.MockFilterRunner.EXPECT().UsesBuffering().Return(true)
fth.MockHelper.EXPECT().PipelineConfig().Return(pConfig)
config.ScriptFilename = "../lua/testsupport/serialize.lua"
config.ModuleDirectory = "../lua/modules"
config.PreserveData = true
sbFilter.SetName("serialize")
err := sbFilter.Init(config)
c.Assume(err, gs.IsNil)
close(inChan)
err = sbFilter.Run(fth.MockFilterRunner, fth.MockHelper)
c.Expect(err, gs.IsNil)
_, err = os.Stat("sandbox_preservation/serialize.data")
c.Expect(err, gs.IsNil)
//.........這裏部分代碼省略.........
示例8: DecoderSpec
func DecoderSpec(c gs.Context) {
t := new(ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// NewPipelineConfig sets up Globals which is needed for the
// pipeline.Prepend*Dir functions to not die during plugin Init().
_ = pipeline.NewPipelineConfig(nil)
c.Specify("A SandboxDecoder", func() {
decoder := new(SandboxDecoder)
conf := decoder.ConfigStruct().(*sandbox.SandboxConfig)
supply := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(supply)
dRunner := pm.NewMockDecoderRunner(ctrl)
conf.ScriptType = "lua"
c.Specify("that uses lpeg and inject_message", func() {
dRunner.EXPECT().Name().Return("serialize")
conf.ScriptFilename = "../lua/testsupport/decoder.lua"
err := decoder.Init(conf)
c.Assume(err, gs.IsNil)
c.Specify("decodes simple messages", func() {
data := "1376389920 debug id=2321 url=example.com item=1"
decoder.SetDecoderRunner(dRunner)
pack.Message.SetPayload(data)
_, err = decoder.Decode(pack)
c.Assume(err, gs.IsNil)
c.Expect(pack.Message.GetTimestamp(),
gs.Equals,
int64(1376389920000000000))
c.Expect(pack.Message.GetSeverity(), gs.Equals, int32(7))
var ok bool
var value interface{}
value, ok = pack.Message.GetFieldValue("id")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "2321")
value, ok = pack.Message.GetFieldValue("url")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "example.com")
value, ok = pack.Message.GetFieldValue("item")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "1")
decoder.Shutdown()
})
c.Specify("decodes an invalid messages", func() {
data := "1376389920 bogus id=2321 url=example.com item=1"
decoder.SetDecoderRunner(dRunner)
pack.Message.SetPayload(data)
packs, err := decoder.Decode(pack)
c.Expect(len(packs), gs.Equals, 0)
c.Expect(err.Error(), gs.Equals, "Failed parsing: "+data)
c.Expect(decoder.processMessageFailures, gs.Equals, int64(1))
decoder.Shutdown()
})
c.Specify("Preserves data", func() {
conf.ScriptFilename = "../lua/testsupport/serialize.lua"
conf.PreserveData = true
err := decoder.Init(conf)
c.Assume(err, gs.IsNil)
decoder.SetDecoderRunner(dRunner)
decoder.Shutdown()
_, err = os.Stat("sandbox_preservation/serialize.data")
c.Expect(err, gs.IsNil)
err = os.Remove("sandbox_preservation/serialize.data")
c.Expect(err, gs.IsNil)
})
})
c.Specify("that only uses write_message", func() {
conf.ScriptFilename = "../lua/testsupport/write_message_decoder.lua"
dRunner.EXPECT().Name().Return("write_message")
err := decoder.Init(conf)
decoder.SetDecoderRunner(dRunner)
c.Assume(err, gs.IsNil)
c.Specify("adds a string field to the message", func() {
data := "string field scribble"
pack.Message.SetPayload(data)
packs, err := decoder.Decode(pack)
c.Expect(err, gs.IsNil)
c.Expect(len(packs), gs.Equals, 1)
c.Expect(packs[0], gs.Equals, pack)
value, ok := pack.Message.GetFieldValue("scribble")
c.Expect(ok, gs.IsTrue)
c.Expect(value.(string), gs.Equals, "foo")
})
c.Specify("adds a numeric field to the message", func() {
data := "num field scribble"
pack.Message.SetPayload(data)
//.........這裏部分代碼省略.........
示例9: DashboardOutputSpec
func DashboardOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := pipeline.NewPipelineConfig(nil)
dashboardOutput := new(DashboardOutput)
dashboardOutput.pConfig = pConfig
oth := plugins_ts.NewOutputTestHelper(ctrl)
oth.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
oth.MockOutputRunner = pipelinemock.NewMockOutputRunner(ctrl)
errChan := make(chan error, 1)
startOutput := func() {
go func() {
errChan <- dashboardOutput.Run(oth.MockOutputRunner, oth.MockHelper)
}()
}
if runtime.GOOS != "windows" {
c.Specify("A DashboardOutput", func() {
tmpdir, err := ioutil.TempDir("", "dashboard_output_test")
c.Assume(err, gs.IsNil)
config := dashboardOutput.ConfigStruct().(*DashboardOutputConfig)
config.WorkingDirectory = tmpdir
c.Specify("Init halts if basedirectory is not writable", func() {
err := os.MkdirAll(tmpdir, 0400)
c.Assume(err, gs.IsNil)
defer os.RemoveAll(tmpdir)
err = dashboardOutput.Init(config)
c.Assume(err, gs.Not(gs.IsNil))
})
c.Specify("that is running", func() {
startedChan := make(chan bool, 1)
defer close(startedChan)
ts := httptest.NewUnstartedServer(nil)
dashboardOutput.starterFunc = func(hli *DashboardOutput) error {
ts.Start()
startedChan <- true
return nil
}
ticker := make(chan time.Time)
inChan := make(chan *pipeline.PipelinePack, 1)
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message = pipeline_ts.GetTestMessage()
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().Ticker().Return(ticker)
err := os.MkdirAll(tmpdir, 0700)
c.Assume(err, gs.IsNil)
defer os.RemoveAll(tmpdir)
dashboardOutput.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Noop
})
c.Specify("sets custom http headers", func() {
config.Headers = http.Header{
"One": []string{"two", "three"},
"Four": []string{"five", "six", "seven"},
}
err = dashboardOutput.Init(config)
c.Assume(err, gs.IsNil)
ts.Config = dashboardOutput.server
startOutput()
inChan <- pack
<-startedChan
resp, err := http.Get(ts.URL)
c.Assume(err, gs.IsNil)
resp.Body.Close()
c.Assume(resp.StatusCode, gs.Equals, 200)
// Verify headers are there
eq := reflect.DeepEqual(resp.Header["One"], config.Headers["One"])
c.Expect(eq, gs.IsTrue)
eq = reflect.DeepEqual(resp.Header["Four"], config.Headers["Four"])
c.Expect(eq, gs.IsTrue)
})
close(inChan)
c.Expect(<-errChan, gs.IsNil)
ts.Close()
})
})
}
}
示例10: NsqOutputSpec
func NsqOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := pipeline.NewPipelineConfig(nil)
var wg sync.WaitGroup
errChan := make(chan error, 1)
defer close(errChan)
c.Specify("A nsq output", func() {
output := new(NsqOutput)
oth := plugins_ts.NewOutputTestHelper(ctrl)
config := output.ConfigStruct().(*NsqOutputConfig)
config.Topic = "test"
startOutput := func() {
wg.Add(1)
go func() {
errChan <- output.Run(oth.MockOutputRunner, oth.MockHelper)
wg.Done()
}()
}
var mockProducer *MockProducer
output.newProducer = func(addr string, config *nsq.Config) (Producer, error) {
mockProducer, _ = NewMockProducer(addr, config)
return mockProducer, nil
}
msg := pipeline_ts.GetTestMessage()
pack := pipeline.NewPipelinePack(pConfig.InputRecycleChan())
pack.Message = msg
c.Specify("requires at least one address", func() {
err := output.Init(config)
c.Expect(err, gs.Not(gs.IsNil))
})
config.Addresses = []string{"test.com:4150"}
c.Specify("requires an encoder", func() {
err := output.Init(config)
c.Assume(err, gs.IsNil)
oth.MockOutputRunner.EXPECT().Encoder().Return(nil)
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.Not(gs.IsNil))
})
c.Specify("that is started", func() {
encoder := new(plugins.PayloadEncoder)
encoder.Init(&plugins.PayloadEncoderConfig{PrefixTs: false})
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(pack).Return(encoder.Encode(pack))
inChan := make(chan *pipeline.PipelinePack, 1)
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
c.Specify("publishes a message with the configured topic", func() {
err := output.Init(config)
c.Assume(err, gs.IsNil)
startOutput()
inChan <- pack
transaction := <-mockProducer.respChan
c.Expect(transaction.Error, gs.IsNil)
close(inChan)
wg.Wait()
c.Expect(<-errChan, gs.IsNil)
})
})
})
}
示例11: UnixOutputSpec
func UnixOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
c.Specify("A UnixOutput", func() {
unixOutput := new(UdpOutput)
config := unixOutput.ConfigStruct().(*UdpOutputConfig)
oth := plugins_ts.NewOutputTestHelper(ctrl)
encoder := new(plugins.PayloadEncoder)
encoder.Init(new(plugins.PayloadEncoderConfig))
inChan := make(chan *pipeline.PipelinePack, 1)
rChan := make(chan *pipeline.PipelinePack, 1)
msg := pipeline_ts.GetTestMessage()
payload := "Write me out to the network."
msg.SetPayload(payload)
pack := pipeline.NewPipelinePack(rChan)
pack.Message = msg
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(pack).Return(encoder.Encode(pack))
c.Specify("using UDP", func() {
addr := "@/heka/unix-out"
config.Address = addr
ch := make(chan string, 1)
var wg sync.WaitGroup
var rAddr net.Addr
collectData := func() {
conn, err := net.ListenPacket("unix", addr)
if err != nil {
ch <- err.Error()
return
}
ch <- "ready"
b := make([]byte, 1000)
var n int
n, rAddr, _ = conn.ReadFrom(b)
ch <- string(b[:n])
conn.Close()
}
go collectData()
result := <-ch // Wait for server to be ready.
c.Assume(result, gs.Equals, "ready")
c.Specify("writes out to the network", func() {
err := unixOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = unixOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
close(inChan)
wg.Wait()
})
c.Specify("uses the specified local address", func() {
config.LocalAddress = "@/heka/unix-out"
err := unixOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = unixOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
c.Expect(rAddr.Network(), gs.Equals, "unix")
c.Expect(rAddr.String(), gs.Equals, "@/heka/unix-out")
close(inChan)
wg.Wait()
})
})
})
}
示例12: OutputSpec
func OutputSpec(c gs.Context) {
t := new(ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
oth := plugins_ts.NewOutputTestHelper(ctrl)
pConfig := pipeline.NewPipelineConfig(nil)
inChan := make(chan *pipeline.PipelinePack, 1)
timer := make(chan time.Time, 1)
c.Specify("A SandboxOutput", func() {
output := new(SandboxOutput)
output.SetPipelineConfig(pConfig)
conf := output.ConfigStruct().(*sandbox.SandboxConfig)
conf.ScriptFilename = "../lua/testsupport/output.lua"
conf.ModuleDirectory = "../lua/modules;../lua/testsupport/modules"
supply := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(supply)
data := "1376389920 debug id=2321 url=example.com item=1"
pack.Message.SetPayload(data)
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().UpdateCursor("").AnyTimes()
oth.MockOutputRunner.EXPECT().Ticker().Return(timer)
c.Specify("writes a payload to file", func() {
err := output.Init(conf)
c.Expect(err, gs.IsNil)
inChan <- pack
close(inChan)
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Assume(err, gs.IsNil)
tmpFile, err := os.Open("output.lua.txt")
defer tmpFile.Close()
c.Assume(err, gs.IsNil)
contents, err := ioutil.ReadAll(tmpFile)
c.Assume(err, gs.IsNil)
c.Expect(string(contents), gs.Equals, data)
c.Expect(output.processMessageCount, gs.Equals, int64(1))
c.Expect(output.processMessageSamples, gs.Equals, int64(1))
c.Expect(output.processMessageFailures, gs.Equals, int64(0))
})
c.Specify("failure processing data", func() {
err := output.Init(conf)
c.Expect(err, gs.IsNil)
pack.Message.SetPayload("FAILURE")
pack.BufferedPack = true
pack.DelivErrChan = make(chan error, 1)
inChan <- pack
close(inChan)
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Assume(err, gs.IsNil)
c.Expect(output.processMessageFailures, gs.Equals, int64(1))
err = <-pack.DelivErrChan
c.Expect(err.Error(), gs.Equals, "failure message")
})
c.Specify("user abort processing data", func() {
err := output.Init(conf)
c.Expect(err, gs.IsNil)
e := errors.New("FATAL: user abort")
pack.Message.SetPayload("USERABORT")
inChan <- pack
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err.Error(), gs.Equals, e.Error())
})
c.Specify("fatal error processing data", func() {
err := output.Init(conf)
c.Expect(err, gs.IsNil)
pack.Message.SetPayload("FATAL")
inChan <- pack
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.Not(gs.IsNil))
})
c.Specify("fatal error in timer_event", func() {
err := output.Init(conf)
c.Expect(err, gs.IsNil)
timer <- time.Now()
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.Not(gs.IsNil))
})
c.Specify("fatal error in shutdown timer_event", func() {
conf.TimerEventOnShutdown = true
err := output.Init(conf)
c.Expect(err, gs.IsNil)
close(inChan)
err = output.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.Not(gs.IsNil))
})
})
}
示例13: getTestPipelinePack
func getTestPipelinePack() *pipeline.PipelinePack {
return pipeline.NewPipelinePack(&config)
}
示例14: NagiosOutputSpec
func NagiosOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
c.Specify("A NagiosOutput", func() {
output := new(NagiosOutput)
config := output.ConfigStruct().(*NagiosOutputConfig)
config.Url = "http://localhost:55580/foo/bar"
mockOutputRunner := pipelinemock.NewMockOutputRunner(ctrl)
mockHelper := pipelinemock.NewMockPluginHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack)
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(recycleChan)
msg := pipeline_ts.GetTestMessage()
pack.Message = msg
var req *http.Request
var outputWg, reqWg sync.WaitGroup
run := func() {
mockOutputRunner.EXPECT().InChan().Return(inChan)
mockOutputRunner.EXPECT().UpdateCursor("").AnyTimes()
output.Run(mockOutputRunner, mockHelper)
outputWg.Done()
}
const payload = "something"
c.Specify("using HTTP transport", func() {
// Spin up an HTTP listener.
listener, err := net.Listen("tcp", "127.0.0.1:55580")
c.Assume(err, gs.IsNil)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close")
r.ParseForm()
req = r
listener.Close()
reqWg.Done()
})
go http.Serve(listener, mux)
c.Specify("sends a valid HTTP POST", func() {
err = output.Init(config)
c.Assume(err, gs.IsNil)
outputWg.Add(1)
go run()
msg.SetPayload("OK:" + payload)
reqWg.Add(1)
inChan <- pack
close(inChan)
outputWg.Wait()
reqWg.Wait()
c.Expect(req.FormValue("plugin_output"), gs.Equals, payload)
c.Expect(req.FormValue("plugin_state"), gs.Equals, "0")
})
c.Specify("correctly maps alternate state", func() {
err = output.Init(config)
c.Assume(err, gs.IsNil)
outputWg.Add(1)
go run()
msg.SetPayload("CRITICAL:" + payload)
reqWg.Add(1)
inChan <- pack
close(inChan)
outputWg.Wait()
reqWg.Wait()
c.Expect(req.FormValue("plugin_output"), gs.Equals, payload)
c.Expect(req.FormValue("plugin_state"), gs.Equals, "2")
})
})
if runtime.GOOS != "windows" {
outPath := filepath.Join(os.TempDir(), "heka-nagios-test-output.txt")
echoFile := fmt.Sprintf(echoFileTmpl, outPath)
c.Specify("using a send_ncsa binary", func() {
binPath := pipeline_ts.WriteStringToTmpFile(echoFile)
os.Chmod(binPath, 0700)
defer func() {
os.Remove(binPath)
os.Remove(outPath)
}()
config.SendNscaBin = binPath
config.SendNscaArgs = []string{"arg1", "arg2"}
err := output.Init(config)
c.Assume(err, gs.IsNil)
outputWg.Add(1)
go run()
//.........這裏部分代碼省略.........
示例15: DecoderSpec
func DecoderSpec(c gs.Context) {
t := new(ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// NewPipelineConfig sets up Globals which is needed for the
// pipeline.Prepend*Dir functions to not die during plugin Init().
_ = pipeline.NewPipelineConfig(nil)
c.Specify("A SandboxDecoder", func() {
decoder := new(SandboxDecoder)
conf := decoder.ConfigStruct().(*sandbox.SandboxConfig)
supply := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(supply)
dRunner := pm.NewMockDecoderRunner(ctrl)
c.Specify("that uses lpeg and inject_message", func() {
dRunner.EXPECT().Name().Return("serialize")
conf.ScriptFilename = "../lua/testsupport/decoder.lua"
err := decoder.Init(conf)
c.Assume(err, gs.IsNil)
c.Specify("decodes simple messages", func() {
data := "1376389920 debug id=2321 url=example.com item=1"
decoder.SetDecoderRunner(dRunner)
pack.Message.SetPayload(data)
_, err = decoder.Decode(pack)
c.Assume(err, gs.IsNil)
c.Expect(pack.Message.GetTimestamp(),
gs.Equals,
int64(1376389920000000000))
c.Expect(pack.Message.GetSeverity(), gs.Equals, int32(7))
var ok bool
var value interface{}
value, ok = pack.Message.GetFieldValue("id")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "2321")
value, ok = pack.Message.GetFieldValue("url")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "example.com")
value, ok = pack.Message.GetFieldValue("item")
c.Expect(ok, gs.Equals, true)
c.Expect(value, gs.Equals, "1")
decoder.Shutdown()
})
c.Specify("decodes an invalid messages", func() {
data := "1376389920 bogus id=2321 url=example.com item=1"
decoder.SetDecoderRunner(dRunner)
pack.Message.SetPayload(data)
packs, err := decoder.Decode(pack)
c.Expect(len(packs), gs.Equals, 0)
c.Expect(err.Error(), gs.Equals, "Failed parsing: "+data)
c.Expect(decoder.processMessageFailures, gs.Equals, int64(1))
decoder.Shutdown()
})
c.Specify("Preserves data", func() {
conf.ScriptFilename = "../lua/testsupport/serialize.lua"
conf.PreserveData = true
err := decoder.Init(conf)
c.Assume(err, gs.IsNil)
decoder.SetDecoderRunner(dRunner)
decoder.Shutdown()
_, err = os.Stat("sandbox_preservation/serialize.data")
c.Expect(err, gs.IsNil)
err = os.Remove("sandbox_preservation/serialize.data")
c.Expect(err, gs.IsNil)
})
})
c.Specify("that only uses write_message", func() {
conf.ScriptFilename = "../lua/testsupport/write_message_decoder.lua"
dRunner.EXPECT().Name().Return("write_message")
err := decoder.Init(conf)
decoder.SetDecoderRunner(dRunner)
c.Assume(err, gs.IsNil)
c.Specify("adds a string field to the message", func() {
data := "string field scribble"
pack.Message.SetPayload(data)
packs, err := decoder.Decode(pack)
c.Expect(err, gs.IsNil)
c.Expect(len(packs), gs.Equals, 1)
c.Expect(packs[0], gs.Equals, pack)
value, ok := pack.Message.GetFieldValue("scribble")
c.Expect(ok, gs.IsTrue)
c.Expect(value.(string), gs.Equals, "foo")
})
c.Specify("adds a numeric field to the message", func() {
data := "num field scribble"
pack.Message.SetPayload(data)
packs, err := decoder.Decode(pack)
//.........這裏部分代碼省略.........