|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +// +build integration |
| 6 | +// +build cloudfoundry |
| 7 | + |
| 8 | +package cloudfoundry |
| 9 | + |
| 10 | +import ( |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/elastic/beats/v7/filebeat/channel" |
| 17 | + "github.com/elastic/beats/v7/filebeat/input" |
| 18 | + "github.com/elastic/beats/v7/libbeat/beat" |
| 19 | + "github.com/elastic/beats/v7/libbeat/common" |
| 20 | + cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test" |
| 21 | +) |
| 22 | + |
| 23 | +func TestInput(t *testing.T) { |
| 24 | + config := common.MustNewConfigFrom(cftest.GetConfigFromEnv(t)) |
| 25 | + |
| 26 | + events := make(chan beat.Event) |
| 27 | + connector := channel.ConnectorFunc(func(*common.Config, beat.ClientConfig) (channel.Outleter, error) { |
| 28 | + return newOutleter(events), nil |
| 29 | + }) |
| 30 | + |
| 31 | + inputCtx := input.Context{Done: make(chan struct{})} |
| 32 | + |
| 33 | + input, err := NewInput(config, connector, inputCtx) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + go input.Run() |
| 37 | + defer input.Stop() |
| 38 | + |
| 39 | + select { |
| 40 | + case e := <-events: |
| 41 | + t.Logf("Event received: %+v", e) |
| 42 | + case <-time.After(10 * time.Second): |
| 43 | + t.Fatal("timeout waiting for events") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type outleter struct { |
| 48 | + events chan<- beat.Event |
| 49 | + done chan struct{} |
| 50 | +} |
| 51 | + |
| 52 | +func newOutleter(events chan<- beat.Event) *outleter { |
| 53 | + return &outleter{ |
| 54 | + events: events, |
| 55 | + done: make(chan struct{}), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (o *outleter) Close() error { |
| 60 | + close(o.done) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (o *outleter) Done() <-chan struct{} { |
| 65 | + return o.done |
| 66 | +} |
| 67 | + |
| 68 | +func (o *outleter) OnEvent(e beat.Event) bool { |
| 69 | + select { |
| 70 | + case o.events <- e: |
| 71 | + return true |
| 72 | + default: |
| 73 | + return false |
| 74 | + } |
| 75 | +} |
0 commit comments