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

add test for k8s events option #1670

Merged
merged 2 commits into from
Feb 21, 2025
Merged
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
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
Loading