-
Notifications
You must be signed in to change notification settings - Fork 172
test(metrics): add functional tests verifying component_sent_bytes_total carries component_id labels #3379
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
Merged
openshift-merge-bot
merged 1 commit into
openshift:master
from
Clee2691:LOG-9671-master
Jul 30, 2026
+221
−17
Merged
test(metrics): add functional tests verifying component_sent_bytes_total carries component_id labels #3379
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package functional | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
|
|
||
| "github.com/openshift/cluster-logging-operator/internal/constants" | ||
| "github.com/openshift/cluster-logging-operator/internal/runtime" | ||
| ) | ||
|
|
||
| // SetupMetricsRBAC creates the cluster-scoped RBAC resources needed to scrape | ||
| // the collector's /metrics endpoint from within a test pod. Names are prefixed | ||
| // with the framework's namespace so parallel test packages cannot collide. | ||
| // The returned function deletes all created resources and should be registered | ||
| // with DeferCleanup or called in AfterEach. | ||
| func (f *CollectorFunctionalFramework) SetupMetricsRBAC() (metricsReaderRole *rbacv1.ClusterRole, metricsReaderBinding *rbacv1.ClusterRoleBinding, tokenReviewBinding *rbacv1.ClusterRoleBinding, err error) { | ||
| roleName := fmt.Sprintf("%s-%s-metrics-reader", f.Test.NS.Name, f.Name) | ||
|
|
||
| metricsReaderRole = runtime.NewClusterRole( | ||
| roleName, | ||
| runtime.NewNonResourceURLPolicyRule([]string{"/metrics"}, []string{"get"}), | ||
| ) | ||
| if err = f.Test.Create(metricsReaderRole); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| metricsReaderBinding = runtime.NewClusterRoleBinding( | ||
| roleName, | ||
| runtime.NewClusterRoleRef(roleName), | ||
| runtime.NewServiceAccountSubject("default", f.Namespace), | ||
| ) | ||
| if err = f.Test.Create(metricsReaderBinding); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| tokenReviewName := fmt.Sprintf("%s-%s-token-reviewer", f.Test.NS.Name, f.Name) | ||
| tokenReviewBinding = runtime.NewClusterRoleBinding( | ||
| tokenReviewName, | ||
| runtime.NewClusterRoleRef("system:auth-delegator"), | ||
| runtime.NewServiceAccountSubject("default", f.Namespace), | ||
| ) | ||
| if err = f.Test.Create(tokenReviewBinding); err != nil { | ||
| return nil, nil, nil, err | ||
|
Clee2691 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return metricsReaderRole, metricsReaderBinding, tokenReviewBinding, nil | ||
| } | ||
|
|
||
| // CollectMetricLines polls the collector's Prometheus endpoint until a line | ||
| // matching both metricName and waitFor is found, then returns all lines | ||
| // matching metricName. | ||
| func (f *CollectorFunctionalFramework) CollectMetricLines(metricName, waitFor string, timeout time.Duration) ([]string, error) { | ||
| var matched []string | ||
| var lastErr error | ||
| err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(ctx context.Context) (bool, error) { | ||
| raw, err := f.RunCommand(constants.CollectorName, "bash", "-c", | ||
| fmt.Sprintf("curl -ks --max-time 10 -H \"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)\" https://%s.%s:24231/metrics", f.Name, f.Namespace)) | ||
| if err != nil { | ||
| lastErr = err | ||
| return false, nil | ||
| } | ||
|
Clee2691 marked this conversation as resolved.
|
||
| matched = nil | ||
|
qodo-for-rh-openshift[bot] marked this conversation as resolved.
|
||
| for _, line := range strings.Split(raw, "\n") { | ||
| if strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| if strings.Contains(line, metricName) { | ||
| matched = append(matched, line) | ||
| } | ||
| } | ||
|
Clee2691 marked this conversation as resolved.
|
||
| for _, line := range matched { | ||
| if strings.Contains(line, waitFor) { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, nil | ||
| }) | ||
| if err != nil && lastErr != nil { | ||
| return matched, fmt.Errorf("%w (last scrape error: %v)", err, lastErr) | ||
| } | ||
| return matched, err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall if we have a cleanup function for functional tests but do we need to be concerned about cleaning up these clusterrolbinding in a test cluster because of potentially "false positives" due to essentially "name" collision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a concern because the name of the binding and other rbac resources is derived like this:
roleName := fmt.Sprintf("%s-%s-metrics-reader", f.Test.NS.Name, f.Name)The test's namespace name is unique for every tests so there shouldn't be any collision especially since the NS name is generated from
UniqueNameForTest()