Skip to content

Commit

Permalink
[chore][receiver/k8sobjects] add tests for k8sobjectsreceiver (#27569)
Browse files Browse the repository at this point in the history
**Description:** Adds an e2e test for the k8sobjectsreceiver to github
workflow
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

**Link to tracking Issue:**
[18395](#18395)
  • Loading branch information
jinja2 authored Oct 11, 2023
1 parent 9dfa2f7 commit 918a81e
Show file tree
Hide file tree
Showing 17 changed files with 2,421 additions and 135 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ jobs:
run: |
cd receiver/kubeletstatsreceiver
go test -v --tags=e2e
- name: run receiver/k8sobjectsreceiver e2e tests
run: |
cd receiver/k8sobjectsreceiver
go test -v --tags=e2e
160 changes: 160 additions & 0 deletions receiver/k8sobjectsreceiver/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build e2e
// +build e2e

package k8sobjectsreceiver

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
)

const testKubeConfig = "/tmp/kube-config-otelcol-e2e-testing"
const testObjectsDir = "./testdata/e2e/testobjects/"
const expectedDir = "./testdata/e2e/expected/"

type objAction int

const (
create objAction = iota
createAndDelete // create and delete first, then poll for logs
none
)

func TestE2E(t *testing.T) {

kubeConfig, err := clientcmd.BuildConfigFromFlags("", testKubeConfig)
require.NoError(t, err)
dynamicClient, err := dynamic.NewForConfig(kubeConfig)
require.NoError(t, err)

testID := uuid.NewString()[:8]

f := otlpreceiver.NewFactory()
cfg := f.CreateDefaultConfig().(*otlpreceiver.Config)
cfg.HTTP = nil
logsConsumer := new(consumertest.LogsSink)
rcvr, err := f.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, logsConsumer)
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, err, "failed creating logs receiver")
defer func() {
assert.NoError(t, rcvr.Shutdown(context.Background()))
}()

// startup collector in k8s cluster
collectorObjs := k8stest.CreateCollectorObjects(t, dynamicClient, testID)

defer func() {
for _, obj := range collectorObjs {
require.NoErrorf(t, k8stest.DeleteObject(dynamicClient, obj), "failed to delete object %s", obj.GetName())
}
}()

tests := []struct {
name string
objectFileNames []string
objectAction objAction
expectedFileName string
timeoutMinutes int // timeout to wait for data
}{
{
name: "pull object",
objectFileNames: []string{},
objectAction: none,
expectedFileName: "pull_namespaces.yaml",
timeoutMinutes: 2,
},
{
name: "watch events",
objectFileNames: []string{"event.yaml"},
objectAction: create,
expectedFileName: "watch_events.yaml",
timeoutMinutes: 1,
},
{
name: "watch events in core/v1 version",
objectFileNames: []string{"event_core.yaml"},
objectAction: createAndDelete,
expectedFileName: "watch_events_core.yaml",
timeoutMinutes: 1,
},
{
name: "watch object",
objectFileNames: []string{"namespace.yaml"},
objectAction: create,
expectedFileName: "watch_namespaces.yaml",
timeoutMinutes: 2,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer func() {
time.Sleep(10 * time.Second)
logsConsumer.Reset() // reset consumer at the end since pull results are available at start
}()

expectedFile := filepath.Join(expectedDir, tc.expectedFileName)
var expected plog.Logs
expected, err = golden.ReadLogs(expectedFile)
require.NoErrorf(t, err, "Error reading logs from %s", expectedFile)

// additional k8s objs
if tc.objectAction != none {
testObjs := make([]*unstructured.Unstructured, 0, len(tc.objectFileNames))
for _, fileName := range tc.objectFileNames {
obj, err := os.ReadFile(filepath.Join(testObjectsDir, fileName))
require.NoErrorf(t, err, "failed to read object file %s", fileName)
newObj, err := k8stest.CreateObject(dynamicClient, obj)
require.NoErrorf(t, err, "failed to create k8s object from file %s", fileName)
testObjs = append(testObjs, newObj)
}
if tc.objectAction == createAndDelete {
for _, obj := range testObjs {
require.NoErrorf(t, k8stest.DeleteObject(dynamicClient, obj), "failed to delete object %s", obj.GetName())
}
} else {
defer func() {
for _, obj := range testObjs {
require.NoErrorf(t, k8stest.DeleteObject(dynamicClient, obj), "failed to delete object %s", obj.GetName())
}
}()
}
}

require.Eventuallyf(t, func() bool {
return len(logsConsumer.AllLogs()) > 0
}, time.Duration(tc.timeoutMinutes)*time.Minute, 1*time.Second,
"Timeout: failed to receive logs in %d minutes", tc.timeoutMinutes)

require.NoErrorf(t, plogtest.CompareLogs(expected, logsConsumer.AllLogs()[0],
plogtest.IgnoreObservedTimestamp(),
plogtest.IgnoreResourceLogsOrder(),
plogtest.IgnoreScopeLogsOrder(),
plogtest.IgnoreLogRecordsOrder()),
"Received logs did not match log records in file %s", expectedFile,
)
})
}
}
47 changes: 46 additions & 1 deletion receiver/k8sobjectsreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,51 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobj
go 1.20

require (
github.com/google/uuid v1.3.1
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.87.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.87.0
go.opentelemetry.io/collector/confmap v0.87.0
go.opentelemetry.io/collector/consumer v0.87.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0016
go.opentelemetry.io/collector/receiver v0.87.0
go.opentelemetry.io/collector/receiver/otlpreceiver v0.87.0
go.opentelemetry.io/collector/semconv v0.87.0
go.uber.org/zap v1.26.0
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -41,26 +56,45 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.2.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.87.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.87.0 // indirect
go.opentelemetry.io/collector/config/configauth v0.87.0 // indirect
go.opentelemetry.io/collector/config/configcompression v0.87.0 // indirect
go.opentelemetry.io/collector/config/configgrpc v0.87.0 // indirect
go.opentelemetry.io/collector/config/confighttp v0.87.0 // indirect
go.opentelemetry.io/collector/config/confignet v0.87.0 // indirect
go.opentelemetry.io/collector/config/configopaque v0.87.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.87.0 // indirect
go.opentelemetry.io/collector/config/configtls v0.87.0 // indirect
go.opentelemetry.io/collector/config/internal v0.87.0 // indirect
go.opentelemetry.io/collector/extension v0.87.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.87.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0016 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.45.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.2 // indirect
Expand All @@ -87,3 +121,14 @@ retract (
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest => ../../internal/k8stest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

// ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules
replace cloud.google.com/go => cloud.google.com/go v0.110.7
Loading

0 comments on commit 918a81e

Please sign in to comment.