-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5121aa6
commit da26d09
Showing
6 changed files
with
199 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
x-pack/dockerlogbeat/pipelinemanager/clientLogReader_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pipelinemanager | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/docker/docker/daemon/logger" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/x-pack/dockerlogbeat/pipelinemock" | ||
"github.com/elastic/beats/x-pack/dockerlogbeat/pipereader" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
mockConnector := &pipelinemock.MockPipelineConnector{} | ||
client := createNewClient(t, mockConnector) | ||
// ConsumePipelineAndSent is what does the actual reading and sending. | ||
// After we spawn this goroutine, we wait until we get something back | ||
go client.ConsumePipelineAndSend() | ||
event := testReturnAndClose(t, mockConnector, client) | ||
assert.Equal(t, event.Fields["message"], "This is a log line") | ||
|
||
} | ||
|
||
func createNewClient(t *testing.T, mockConnector *pipelinemock.MockPipelineConnector) *ClientLogger { | ||
// an example container metadata struct | ||
cfgObject := logger.Info{ | ||
Config: map[string]string{"output.elasticsearch": "localhost:9200"}, | ||
ContainerLabels: map[string]string{"test.label": "test"}, | ||
ContainerID: "3acc92989a97c415905eba090277b8a8834d087e58a95bed55450338ce0758dd", | ||
ContainerName: "testContainer", | ||
ContainerImageName: "TestImage", | ||
} | ||
|
||
// create a new pipeline reader for use with the libbeat client | ||
reader, err := pipereader.NewReaderFromReadCloser(pipelinemock.CreateTestInput(t)) | ||
assert.NoError(t, err) | ||
|
||
client, err := newClientFromPipeline(mockConnector, reader, "aaa", cfgObject) | ||
assert.NoError(t, err) | ||
|
||
return client | ||
} | ||
|
||
func testReturnAndClose(t *testing.T, conn *pipelinemock.MockPipelineConnector, client *ClientLogger) beat.Event { | ||
defer client.Close() | ||
for { | ||
// wait until we get our example event back | ||
if events := conn.GetAllEvents(); len(events) > 0 { | ||
assert.NotEmpty(t, events) | ||
return events[0] | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package pipelinemock | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
) | ||
|
||
// MockBeatClient mocks the Client interface | ||
type MockBeatClient struct { | ||
publishes []beat.Event | ||
closed bool | ||
mtx sync.Mutex | ||
} | ||
|
||
// GetEvents returns the published events | ||
func (c *MockBeatClient) GetEvents() []beat.Event { | ||
c.mtx.Lock() | ||
defer c.mtx.Unlock() | ||
|
||
return c.publishes | ||
} | ||
|
||
// Publish mocks the Client Publish method | ||
func (c *MockBeatClient) Publish(e beat.Event) { | ||
c.PublishAll([]beat.Event{e}) | ||
} | ||
|
||
// PublishAll mocks the Client PublishAll method | ||
func (c *MockBeatClient) PublishAll(events []beat.Event) { | ||
c.mtx.Lock() | ||
defer c.mtx.Unlock() | ||
|
||
for _, e := range events { | ||
c.publishes = append(c.publishes, e) | ||
} | ||
} | ||
|
||
// Close mocks the Client Close method | ||
func (c *MockBeatClient) Close() error { | ||
c.mtx.Lock() | ||
defer c.mtx.Unlock() | ||
|
||
if c.closed { | ||
return fmt.Errorf("mock client already closed") | ||
} | ||
|
||
c.closed = true | ||
return nil | ||
} | ||
|
||
// MockPipelineConnector mocks the PipelineConnector interface | ||
type MockPipelineConnector struct { | ||
clients []*MockBeatClient | ||
mtx sync.Mutex | ||
} | ||
|
||
// GetAllEvents returns all events associated with a pipeline | ||
func (pc *MockPipelineConnector) GetAllEvents() []beat.Event { | ||
|
||
evList := []beat.Event{} | ||
for _, clientEvents := range pc.clients { | ||
evList = append(evList, clientEvents.GetEvents()...) | ||
} | ||
|
||
return evList | ||
} | ||
|
||
// Connect mocks the PipelineConnector Connect method | ||
func (pc *MockPipelineConnector) Connect() (beat.Client, error) { | ||
return pc.ConnectWith(beat.ClientConfig{}) | ||
} | ||
|
||
// ConnectWith mocks the PipelineConnector ConnectWith method | ||
func (pc *MockPipelineConnector) ConnectWith(beat.ClientConfig) (beat.Client, error) { | ||
pc.mtx.Lock() | ||
defer pc.mtx.Unlock() | ||
|
||
c := &MockBeatClient{} | ||
|
||
pc.clients = append(pc.clients, c) | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package pipelinemock | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types/plugins/logdriver" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// CreateTestInput creates a mocked ReadCloser for the pipelineReader | ||
func CreateTestInput(t *testing.T) io.ReadCloser { | ||
//setup | ||
exampleStruct := &logdriver.LogEntry{ | ||
Source: "Test", | ||
TimeNano: 0, | ||
Line: []byte("This is a log line"), | ||
Partial: false, | ||
PartialLogMetadata: &logdriver.PartialLogEntryMetadata{ | ||
Last: false, | ||
Id: "", | ||
Ordinal: 0, | ||
}, | ||
} | ||
|
||
rawBytes, err := proto.Marshal(exampleStruct) | ||
assert.NoError(t, err) | ||
|
||
sizeBytes := make([]byte, 4) | ||
binary.BigEndian.PutUint32(sizeBytes, uint32(len(rawBytes))) | ||
rawBytes = append(sizeBytes, rawBytes...) | ||
rc := ioutil.NopCloser(bytes.NewReader(rawBytes)) | ||
return rc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters