Skip to content

Commit 01fb51a

Browse files
committed
Use OpenShift Route in Alertmanager tests
Signed-off-by: machadovilaca <[email protected]>
1 parent 48ce682 commit 01fb51a

File tree

1 file changed

+17
-53
lines changed

1 file changed

+17
-53
lines changed

tests/func-tests/observability_controller_test.go

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"crypto/tls"
66
"fmt"
77
"net/http"
8-
"os/exec"
9-
"time"
108

119
. "github.com/onsi/ginkgo/v2"
1210
. "github.com/onsi/gomega"
11+
routev1 "github.com/openshift/api/route/v1"
1312

1413
v1 "k8s.io/api/core/v1"
14+
"k8s.io/apimachinery/pkg/types"
1515
"k8s.io/client-go/rest"
1616
"sigs.k8s.io/controller-runtime/pkg/client"
1717

@@ -27,7 +27,6 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, testNam
2727
cli client.Client
2828
cliConfig *rest.Config
2929
httpClient http.Client
30-
portForwardCmd *exec.Cmd
3130
alertmanagerURL string
3231
)
3332

@@ -40,21 +39,11 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, testNam
4039
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
4140
}}
4241

43-
if isAlertmanagerAccessible(httpClient, observability.AlertmanagerSvcHost, cliConfig.BearerToken) {
44-
alertmanagerURL = observability.AlertmanagerSvcHost
45-
} else {
46-
var localPort int
47-
var err error
48-
49-
portForwardCmd, localPort, err = setupPortForward()
50-
Expect(err).ToNot(HaveOccurred())
51-
52-
alertmanagerURL = fmt.Sprintf("https://localhost:%d", localPort)
53-
}
54-
})
55-
56-
AfterEach(func(ctx context.Context) {
57-
cleanupPortForward(portForwardCmd)
42+
// Try to get alertmanager URL from OpenShift route first
43+
routeHost, err := getAlertmanagerRouteHost(ctx, cli)
44+
Expect(err).ToNot(HaveOccurred())
45+
Expect(routeHost).ToNot(BeEmpty())
46+
alertmanagerURL = fmt.Sprintf("https://%s", routeHost)
5847
})
5948

6049
Context("PodDisruptionBudgetAtLimit", func() {
@@ -95,44 +84,19 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, testNam
9584
})
9685
})
9786

98-
func isAlertmanagerAccessible(httpClient http.Client, svcHost string, bearerToken string) bool {
99-
req, err := http.NewRequest("GET", svcHost+"/-/healthy", nil)
100-
if err != nil {
101-
return false
102-
}
103-
104-
if bearerToken != "" {
105-
req.Header.Add("Authorization", "Bearer "+bearerToken)
106-
}
107-
108-
resp, err := httpClient.Do(req)
87+
func getAlertmanagerRouteHost(ctx context.Context, cli client.Client) (string, error) {
88+
route := &routev1.Route{}
89+
err := cli.Get(ctx, types.NamespacedName{
90+
Name: "alertmanager-main",
91+
Namespace: "openshift-monitoring",
92+
}, route)
10993
if err != nil {
110-
return false
94+
return "", err
11195
}
112-
defer resp.Body.Close()
113-
114-
return resp.StatusCode == 200
115-
}
116-
117-
func setupPortForward() (*exec.Cmd, int, error) {
118-
serviceName := "alertmanager-main"
119-
localPort := 9094
120-
121-
portForwardCmd := exec.Command("oc", "port-forward", "-n", "openshift-monitoring", fmt.Sprintf("service/%s", serviceName), fmt.Sprintf("%d:9094", localPort))
12296

123-
if err := portForwardCmd.Start(); err != nil {
124-
return nil, 0, fmt.Errorf("failed to start port-forward to service %s: %w", serviceName, err)
97+
if len(route.Status.Ingress) > 0 {
98+
return route.Status.Ingress[0].Host, nil
12599
}
126100

127-
// Wait a bit for port-forward to establish
128-
time.Sleep(3 * time.Second)
129-
130-
return portForwardCmd, localPort, nil
131-
}
132-
133-
func cleanupPortForward(cmd *exec.Cmd) {
134-
if cmd != nil && cmd.Process != nil {
135-
_ = cmd.Process.Kill()
136-
_ = cmd.Wait()
137-
}
101+
return "", fmt.Errorf("route has no ingress status")
138102
}

0 commit comments

Comments
 (0)