Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/k8sattributes] add e2e tests #16263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/k8sattributes-e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sattributesprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add end to end tests for Kubernetes attributes processor.

# One or more tracking issues related to the change
issues: [15651]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
35 changes: 35 additions & 0 deletions .github/workflows/k8s-attributes-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test Kubernetes Attributes

on:
pull_request:
paths:
- 'processor/k8sattributesprocessor/**'
branches:
- main

jobs:
kubernetes-test:
needs: k8s-publish-dev
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Create kind cluster
uses: helm/kind-action@v1.4.0
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Cache Go
id: go-cache
uses: actions/cache@v3
with:
path: |
~/go/bin
~/go/pkg/mod
key: go-cache-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
- name: Test
run: "cd processor/k8sattributesprocessor; export IMAGE=otel/opentelemetry-collector-contrib:0.64.1; go test -v --tags=e2e .; cd ../../"

2 changes: 2 additions & 0 deletions cmd/configschema/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ require (
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.9.7 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down Expand Up @@ -183,7 +183,7 @@ require (
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig => ../../internal/k8sconfig
Expand Down
9 changes: 6 additions & 3 deletions exporter/datadogexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions processor/k8sattributesprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@

Documentation is published to [pkg.go.dev](https://pkg.go.dev/github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor?tab=doc)

## Testing

TODO

[beta]: https://github.com/open-telemetry/opentelemetry-collector#beta
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
152 changes: 152 additions & 0 deletions processor/k8sattributesprocessor/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright 2020 OpenTelemetry Authors
//
// 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.

//go:build e2e
// +build e2e

package k8sattributesprocessor_test

import (
"context"
"fmt"
"os"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/conf"
"sigs.k8s.io/e2e-framework/klient/decoder"
"sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/klient/k8s/resources"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

var (
testenv env.Environment
namespace = "default"
)

func TestMain(m *testing.M) {
testenv = env.New()
path := conf.ResolveKubeConfigFile()
cfg := envconf.NewWithKubeConfig(path).WithNamespace(namespace)

testenv = env.NewWithConfig(cfg)

// Use pre-defined environment funcs to create a kind cluster prior to test run
testenv.Setup()

// Use pre-defined environment funcs to teardown kind cluster after tests
testenv.Finish(
//envfuncs.DeleteNamespace(namespace),
)

// launch package tests
os.Exit(testenv.Run(m))
}

func TestKubernetes(t *testing.T) {
testdata := os.DirFS("testdata/e2e/")
pattern := "*.yaml"
// feature uses pre-generated namespace (see TestMain)
depFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
r, err := resources.New(cfg.Client().RESTConfig())
if err != nil {
t.Fatal(err)
}
if err := decoder.DecodeEachFile(ctx, testdata, pattern,
decoder.CreateIgnoreAlreadyExists(r),
decoder.MutateNamespace(namespace),
MutateOtelCollectorImage(os.Getenv("IMAGE")),
); err != nil {
t.Fatal(err)
}

time.Sleep(2 * time.Second)
return ctx
}).
Assess("deployment creation", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
}
dep := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "example-opentelemetry-collector", Namespace: cfg.Namespace()},
}

err = wait.For(conditions.New(client.Resources()).ResourceMatch(&dep, func(object k8s.Object) bool {
d := object.(*appsv1.Deployment)
return d.Status.ReadyReplicas == *d.Spec.Replicas
}), wait.WithTimeout(time.Minute*2))

demoApp := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "example-emailservice", Namespace: cfg.Namespace()},
}

err = wait.For(conditions.New(client.Resources()).ResourceMatch(&demoApp, func(object k8s.Object) bool {
d := object.(*appsv1.Deployment)
return d.Status.ReadyReplicas == *d.Spec.Replicas
}), wait.WithTimeout(time.Minute*2))

pods := &corev1.PodList{}
err = client.Resources(cfg.Namespace()).List(context.TODO(), pods)
if err != nil || pods.Items == nil {
t.Error("error while getting pods", err)
}
fmt.Println("1")
fmt.Println(pods)

return context.WithValue(ctx, "test-deployment", &dep)
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
r, err := resources.New(cfg.Client().RESTConfig())
if err != nil {
t.Fatal(err)
}

if err := decoder.DecodeEachFile(ctx, testdata, pattern,
decoder.DeleteHandler(r),
decoder.MutateNamespace(namespace),
); err != nil {
t.Fatal(err)
}

return ctx
}).Feature()

testenv.Test(t, depFeature)
}

func MutateOtelCollectorImage(image string) decoder.DecodeOption {
return decoder.MutateOption(func(obj k8s.Object) error {
d, ok := obj.(*appsv1.Deployment)
if !ok {
return nil
}
if d.Name != "example-opentelemetry-collector" {
return nil
}

fmt.Println("running", image)
d.Spec.Template.Spec.Containers[0].Image = image
return nil
})
}
6 changes: 4 additions & 2 deletions processor/k8sattributesprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
k8s.io/api v0.25.4
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
sigs.k8s.io/e2e-framework v0.0.8
)

require (
Expand All @@ -34,7 +35,7 @@ require (
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.4 // indirect
Expand Down Expand Up @@ -75,9 +76,10 @@ require (
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/controller-runtime v0.12.1 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

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