-
Notifications
You must be signed in to change notification settings - Fork 24
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
b863035
commit 3cc44f0
Showing
4 changed files
with
161 additions
and
68 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
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,117 @@ | ||
/* | ||
* Copyright (C) 2021 IBM, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package opentelemetry | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
otel "github.com/agoda-com/opentelemetry-logs-go" | ||
"github.com/agoda-com/opentelemetry-logs-go/logs" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/operational" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/encode" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const testOtlpConfig = `--- | ||
log-level: debug | ||
pipeline: | ||
- name: encode1 | ||
parameters: | ||
- name: encode1 | ||
encode: | ||
type: otlp | ||
otlp: | ||
grpcaddress: localhost:4317 | ||
` | ||
|
||
type fakeOltpLoggerProvider struct { | ||
} | ||
type fakeOltpLogger struct { | ||
} | ||
|
||
var receivedData []logs.LogRecord | ||
|
||
func (f *fakeOltpLogger) Emit(msg logs.LogRecord) { | ||
receivedData = append(receivedData, msg) | ||
} | ||
|
||
func (f *fakeOltpLoggerProvider) Logger(name string, options ...logs.LoggerOption) logs.Logger { | ||
return &fakeOltpLogger{} | ||
} | ||
|
||
func initNewEncodeOltp(t *testing.T) encode.Encoder { | ||
receivedData = []logs.LogRecord{} | ||
v, cfg := test.InitConfig(t, testOtlpConfig) | ||
require.NotNil(t, v) | ||
|
||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg.Parameters[0]) | ||
require.NoError(t, err) | ||
|
||
// intercept the loggerProvider function | ||
loggerProvider := fakeOltpLoggerProvider{} | ||
otel.SetLoggerProvider(&loggerProvider) | ||
return newEncode | ||
} | ||
|
||
func Test_EncodeOtlp(t *testing.T) { | ||
newEncode := initNewEncodeOltp(t) | ||
encodeOtlp := newEncode.(*EncodeOtlpLogs) | ||
require.Equal(t, "localhost:4317", encodeOtlp.cfg.GrpcAddress) | ||
require.Nil(t, encodeOtlp.cfg.TLS) | ||
require.Empty(t, encodeOtlp.cfg.HttpAddress) | ||
require.Empty(t, encodeOtlp.cfg.Headers) | ||
|
||
entry1 := test.GetIngestMockEntry(true) | ||
entry2 := test.GetIngestMockEntry(false) | ||
newEncode.Encode(entry1) | ||
newEncode.Encode(entry2) | ||
// verify contents of the output | ||
require.Len(t, receivedData, 2) | ||
expected1, _ := json.Marshal(entry1) | ||
expected2, _ := json.Marshal(entry2) | ||
require.Equal(t, string(expected1), *receivedData[0].Body()) | ||
require.Equal(t, string(expected2), *receivedData[1].Body()) | ||
} | ||
|
||
func Test_TLSConfigEmpty(t *testing.T) { | ||
cfg := config.StageParam{Encode: &config.Encode{Otlp: &api.OtlpEncode{HttpAddress: "1.2.3.4:999", TLS: nil}}} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_TLSConfigNotEmpty(t *testing.T) { | ||
cfg := config.StageParam{Encode: &config.Encode{Otlp: &api.OtlpEncode{HttpAddress: "1.2.3.4:999", TLS: &api.ClientTLS{InsecureSkipVerify: true}}}} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_HeadersNotEmpty(t *testing.T) { | ||
headers := make(map[string]string) | ||
headers["key1"] = "value1" | ||
headers["key2"] = "value2" | ||
cfg := config.StageParam{Encode: &config.Encode{Otlp: &api.OtlpEncode{HttpAddress: "1.2.3.4:999", Headers: headers}}} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} |
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