|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package operator_metrics_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + . "github.com/kedacore/http-add-on/tests/helper" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + testName = "operator-metrics-test" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + testNamespace = fmt.Sprintf("%s-ns", testName) |
| 24 | + clientName = fmt.Sprintf("%s-client", testName) |
| 25 | + kedaOperatorMetricsURL = "https://keda-add-ons-http-operator-metrics.keda:8443/metrics" |
| 26 | + kedaOperatorMetricsHTTPURL = "http://keda-add-ons-http-operator-metrics.keda:8443/metrics" |
| 27 | + operatorPodSelector = "app.kubernetes.io/instance=operator" |
| 28 | +) |
| 29 | + |
| 30 | +type templateData struct { |
| 31 | + TestNamespace string |
| 32 | + ClientName string |
| 33 | +} |
| 34 | + |
| 35 | +const ( |
| 36 | + clientTemplate = ` |
| 37 | +apiVersion: v1 |
| 38 | +kind: Pod |
| 39 | +metadata: |
| 40 | + name: {{.ClientName}} |
| 41 | + namespace: {{.TestNamespace}} |
| 42 | +spec: |
| 43 | + containers: |
| 44 | + - name: {{.ClientName}} |
| 45 | + image: curlimages/curl |
| 46 | + command: |
| 47 | + - sh |
| 48 | + - -c |
| 49 | + - "exec tail -f /dev/null"` |
| 50 | + |
| 51 | + serviceAccountTemplate = ` |
| 52 | +apiVersion: v1 |
| 53 | +kind: ServiceAccount |
| 54 | +metadata: |
| 55 | + name: {{.ClientName}} |
| 56 | + namespace: {{.TestNamespace}}` |
| 57 | + |
| 58 | + clusterRoleBindingTemplate = ` |
| 59 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 60 | +kind: ClusterRoleBinding |
| 61 | +metadata: |
| 62 | + name: {{.ClientName}}-metrics-reader |
| 63 | +roleRef: |
| 64 | + apiGroup: rbac.authorization.k8s.io |
| 65 | + kind: ClusterRole |
| 66 | + name: operator-metrics-reader |
| 67 | +subjects: |
| 68 | +- kind: ServiceAccount |
| 69 | + name: {{.ClientName}} |
| 70 | + namespace: {{.TestNamespace}}` |
| 71 | +) |
| 72 | + |
| 73 | +func TestOperatorMetrics(t *testing.T) { |
| 74 | + // setup |
| 75 | + t.Log("--- setting up ---") |
| 76 | + // Create kubernetes resources |
| 77 | + kc := GetKubernetesClient(t) |
| 78 | + data, templates := getTemplateData() |
| 79 | + CreateKubernetesResources(t, kc, testNamespace, data, templates) |
| 80 | + |
| 81 | + // Wait for client pod to be ready |
| 82 | + assert.True(t, WaitForAllPodRunningInNamespace(t, kc, testNamespace, 6, 10), |
| 83 | + "client pod should be running") |
| 84 | + |
| 85 | + t.Log("--- testing operator metrics endpoint ---") |
| 86 | + |
| 87 | + // Test 1: HTTPS endpoint should be accessible (will fail cert validation but should return metrics) |
| 88 | + t.Log("Test 1: Verify HTTPS endpoint is available") |
| 89 | + testHTTPSEndpoint(t) |
| 90 | + |
| 91 | + // Test 2: HTTP should not work (redirected or refused) |
| 92 | + t.Log("Test 2: Verify HTTP endpoint is not accessible") |
| 93 | + testHTTPEndpointNotAccessible(t) |
| 94 | + |
| 95 | + // Test 3: Verify metrics are returned |
| 96 | + t.Log("Test 3: Verify metrics content") |
| 97 | + testMetricsContent(t) |
| 98 | + |
| 99 | + // cleanup |
| 100 | + DeleteKubernetesResources(t, testNamespace, data, templates) |
| 101 | +} |
| 102 | + |
| 103 | +func testHTTPSEndpoint(t *testing.T) { |
| 104 | + // Use curl with -k to skip certificate validation (self-signed cert) |
| 105 | + cmd := fmt.Sprintf("curl -k --max-time 10 %s", kedaOperatorMetricsURL) |
| 106 | + out, errOut, err := ExecCommandOnSpecificPod(t, clientName, testNamespace, cmd) |
| 107 | + |
| 108 | + // We expect this to succeed with a self-signed certificate |
| 109 | + if err != nil { |
| 110 | + t.Logf("HTTPS endpoint test - Output: %s, Error output: %s, Error: %v", out, errOut, err) |
| 111 | + } |
| 112 | + |
| 113 | + // The endpoint should return something (even if authentication fails, it should respond) |
| 114 | + assert.True(t, err == nil || strings.Contains(errOut, "Forbidden") || strings.Contains(out, "Forbidden"), |
| 115 | + "HTTPS endpoint should respond (either with metrics or authentication error)") |
| 116 | +} |
| 117 | + |
| 118 | +func testHTTPEndpointNotAccessible(t *testing.T) { |
| 119 | + // Try HTTP - should fail or redirect |
| 120 | + cmd := fmt.Sprintf("curl --max-time 10 %s", kedaOperatorMetricsHTTPURL) |
| 121 | + out, errOut, err := ExecCommandOnSpecificPod(t, clientName, testNamespace, cmd) |
| 122 | + |
| 123 | + // HTTP should not work since we're using SecureServing |
| 124 | + assert.True(t, err != nil || strings.Contains(errOut, "Empty reply") || strings.Contains(out, "Empty reply"), |
| 125 | + "HTTP endpoint should not be accessible. Output: %s, Error: %s", out, errOut) |
| 126 | +} |
| 127 | + |
| 128 | +func testMetricsContent(t *testing.T) { |
| 129 | + // Get the operator pod name |
| 130 | + pods, err := KubeClient.CoreV1().Pods("keda").List(context.Background(), metav1.ListOptions{ |
| 131 | + LabelSelector: operatorPodSelector, |
| 132 | + }) |
| 133 | + assert.NoError(t, err, "should be able to list operator pods") |
| 134 | + assert.NotEmpty(t, pods.Items, "should find at least one operator pod") |
| 135 | + |
| 136 | + operatorPodName := pods.Items[0].Name |
| 137 | + |
| 138 | + // Access metrics from within the operator pod itself (bypasses auth) |
| 139 | + cmd := "curl -k https://localhost:8443/metrics" |
| 140 | + out, errOut, err := ExecCommandOnSpecificPod(t, operatorPodName, "keda", cmd) |
| 141 | + |
| 142 | + if err != nil { |
| 143 | + t.Logf("Metrics content test - Output: %s, Error output: %s, Error: %v", out, errOut, err) |
| 144 | + } |
| 145 | + |
| 146 | + // Verify that metrics are returned |
| 147 | + assert.NoError(t, err, "should be able to access metrics from operator pod") |
| 148 | + assert.True(t, strings.Contains(out, "# HELP") || strings.Contains(out, "# TYPE"), |
| 149 | + "metrics should contain Prometheus format. Output: %s", out) |
| 150 | +} |
| 151 | + |
| 152 | +func getTemplateData() (templateData, []Template) { |
| 153 | + return templateData{ |
| 154 | + TestNamespace: testNamespace, |
| 155 | + ClientName: clientName, |
| 156 | + }, []Template{ |
| 157 | + {Name: "clientTemplate", Config: clientTemplate}, |
| 158 | + {Name: "serviceAccountTemplate", Config: serviceAccountTemplate}, |
| 159 | + {Name: "clusterRoleBindingTemplate", Config: clusterRoleBindingTemplate}, |
| 160 | + } |
| 161 | +} |
0 commit comments