Skip to content

Commit

Permalink
add test for k8s events option (#1670)
Browse files Browse the repository at this point in the history
* add test for k8s events option

* fix typo
  • Loading branch information
jinja2 authored Feb 21, 2025
1 parent 0160e16 commit cbe843a
Show file tree
Hide file tree
Showing 9 changed files with 848 additions and 55 deletions.
1 change: 1 addition & 0 deletions .github/workflows/functional_test_v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- functional
- histogram
- configuration_switching
- k8sevents
- istio
runs-on: ubuntu-latest
steps:
Expand Down
63 changes: 63 additions & 0 deletions functional_tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/receiver/receivertest"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -106,6 +110,11 @@ func writeNewExpectedMetricsResult(t *testing.T, file string, metric *pmetric.Me
require.NoError(t, golden.WriteMetrics(t, filepath.Join("results", filepath.Base(file)), *metric))
}

func writeNewExpectedLogsResult(t *testing.T, file string, log *plog.Logs) {
require.NoError(t, os.MkdirAll("results", 0755))
require.NoError(t, golden.WriteLogs(t, filepath.Join("results", filepath.Base(file)), *log))
}

func setupSignalfxReceiver(t *testing.T, port int) *consumertest.MetricsSink {
mc := new(consumertest.MetricsSink)
f := signalfxreceiver.NewFactory()
Expand All @@ -123,3 +132,57 @@ func setupSignalfxReceiver(t *testing.T, port int) *consumertest.MetricsSink {

return mc
}

func checkPodsReady(t *testing.T, clientset *kubernetes.Clientset, namespace, labelSelector string, timeout time.Duration) {
require.Eventually(t, func() bool {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
require.NoError(t, err)
if len(pods.Items) == 0 {
return false
}
for _, pod := range pods.Items {
if pod.Status.Phase != v1.PodRunning {
return false
}
ready := false
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
ready = true
break
}
}
if !ready {
return false
}
}
return true
}, timeout, 5*time.Second, "Pods in namespace %s with label %s are not ready", namespace, labelSelector)
}

func createNamespace(t *testing.T, clientset *kubernetes.Clientset, name string) {
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
_, err := clientset.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
require.NoError(t, err, "failed to create namespace %s", name)

require.Eventually(t, func() bool {
_, err := clientset.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{})
return err == nil
}, 1*time.Minute, 5*time.Second, "namespace %s is not available", name)
}

func labelNamespace(t *testing.T, clientset *kubernetes.Clientset, name, key, value string) {
ns, err := clientset.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{})
require.NoError(t, err)
if ns.Labels == nil {
ns.Labels = make(map[string]string)
}
ns.Labels[key] = value
_, err = clientset.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{})
require.NoError(t, err)
}
57 changes: 2 additions & 55 deletions functional_tests/istio_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright Splunk Inc.
// SPDX-License-Identifier: Apache-2.0
//go:build istio

package functional_tests
Expand Down Expand Up @@ -29,7 +31,6 @@ import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -272,34 +273,6 @@ func downloadIstio(t *testing.T, version string) string {
return istioctlPath
}

func checkPodsReady(t *testing.T, clientset *kubernetes.Clientset, namespace, labelSelector string, timeout time.Duration) {
require.Eventually(t, func() bool {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
require.NoError(t, err)
if len(pods.Items) == 0 {
return false
}
for _, pod := range pods.Items {
if pod.Status.Phase != v1.PodRunning {
return false
}
ready := false
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
ready = true
break
}
}
if !ready {
return false
}
}
return true
}, timeout, 5*time.Second, "Pods in namespace %s with label %s are not ready", namespace, labelSelector)
}

func runCommand(t *testing.T, command string) {
cmd := exec.Command("sh", "-c", command)
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -348,32 +321,6 @@ func createObjectFromURL(t *testing.T, config string, url string) {
}
}

func createNamespace(t *testing.T, clientset *kubernetes.Clientset, name string) {
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
_, err := clientset.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
require.NoError(t, err, "failed to create namespace %s", name)

require.Eventually(t, func() bool {
_, err := clientset.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{})
return err == nil
}, 1*time.Minute, 5*time.Second, "namespace %s is not available", name)
}

func labelNamespace(t *testing.T, clientset *kubernetes.Clientset, name, key, value string) {
ns, err := clientset.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{})
require.NoError(t, err)
if ns.Labels == nil {
ns.Labels = make(map[string]string)
}
ns.Labels[key] = value
_, err = clientset.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{})
require.NoError(t, err)
}

func sendHTTPRequest(t *testing.T, client *http.Client, url, host, header, path string) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit cbe843a

Please sign in to comment.