From 18d13f61ad5733c3c0b13206b303f2aa0cad2b0d Mon Sep 17 00:00:00 2001 From: zirain Date: Sun, 29 Oct 2023 11:16:38 +0800 Subject: [PATCH] update Signed-off-by: zirain --- test/e2e/tests/metric.go | 22 ++++++++++++------- test/e2e/tests/prometheus.go | 42 ++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/test/e2e/tests/metric.go b/test/e2e/tests/metric.go index 0f312dc07b58..2c9a8c296c3a 100644 --- a/test/e2e/tests/metric.go +++ b/test/e2e/tests/metric.go @@ -59,7 +59,7 @@ var MetricTest = suite.ConformanceTest{ if err := ScrapeMetrics(t, suite.Client, types.NamespacedName{ Namespace: "envoy-gateway-system", Name: "same-namespace-gw-metrics", - }, "/stats/prometheus"); err != nil { + }, 19001, "/stats/prometheus"); err != nil { t.Logf("failed to get metric: %v", err) return false, nil } @@ -93,7 +93,7 @@ var MetricTest = suite.ConformanceTest{ if err := ScrapeMetrics(t, suite.Client, types.NamespacedName{ Namespace: "monitoring", Name: "otel-collecot-prometheus", - }, "/metrics"); err != nil { + }, 19001, "/metrics"); err != nil { t.Logf("failed to get metric: %v", err) return false, nil } @@ -105,19 +105,25 @@ var MetricTest = suite.ConformanceTest{ }, } -func ScrapeMetrics(t *testing.T, c client.Client, nn types.NamespacedName, path string) error { +func ScrapeMetrics(t *testing.T, c client.Client, nn types.NamespacedName, port int32, path string) error { svc := corev1.Service{} if err := c.Get(context.Background(), nn, &svc); err != nil { return err } host := "" - for _, ing := range svc.Status.LoadBalancer.Ingress { - if ing.IP != "" { - host = ing.IP - break + switch svc.Spec.Type { + case corev1.ServiceTypeLoadBalancer: + for _, ing := range svc.Status.LoadBalancer.Ingress { + if ing.IP != "" { + host = ing.IP + break + } } + default: + host = fmt.Sprintf("%s.%s.svc", nn.Name, nn.Namespace) } - url := fmt.Sprintf("http://%s:19001%s", host, path) + + url := fmt.Sprintf("http://%s:%d%s", host, port, path) t.Logf("try to request: %s", url) httpClient := http.Client{ diff --git a/test/e2e/tests/prometheus.go b/test/e2e/tests/prometheus.go index d15f5d28154d..afafc1b117de 100644 --- a/test/e2e/tests/prometheus.go +++ b/test/e2e/tests/prometheus.go @@ -10,6 +10,7 @@ package tests import ( "context" + corev1 "k8s.io/api/core/v1" "testing" "time" @@ -23,17 +24,44 @@ func init() { } var PrometheusTest = suite.ConformanceTest{ - ShortName: "Prometheus", - Description: "Make sure Prometheus endpoint is working", + ShortName: "ControlPlane", + Description: "Make sure control plane prometheus endpoint is working", Manifests: []string{"testdata/prometheus.yaml"}, Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { - t.Run("MetricExists", func(t *testing.T) { + t.Run("Prometheus", func(t *testing.T) { + nn := types.NamespacedName{Name: "envoy-gateway-lb", Namespace: "envoy-gateway-system"} if err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, true, func(_ context.Context) (done bool, err error) { - if err := ScrapeMetrics(t, suite.Client, types.NamespacedName{ - Namespace: "envoy-gateway-system", - Name: "envoy-gateway-lb", - }, "/metrics"); err != nil { + svc := corev1.Service{} + if err := suite.Client.Get(context.Background(), nn, &svc); err != nil { + return false, nil + } + + host := "" + switch svc.Spec.Type { + case corev1.ServiceTypeLoadBalancer: + for _, ing := range svc.Status.LoadBalancer.Ingress { + if ing.IP != "" { + host = ing.IP + break + } + } + default: + // do nothing + } + + if host == "" { + return false, nil + } + + return true, nil + }); err != nil { + t.Errorf("failed to get service %s : %v", nn.String(), err) + } + + if err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, true, + func(_ context.Context) (done bool, err error) { + if err := ScrapeMetrics(t, suite.Client, nn, 19001, "/metrics"); err != nil { t.Logf("failed to get metric: %v", err) return false, nil }