Skip to content

OTA-1817: Adding unit tests for the extend recommended alerts#2262

Open
hongkailiu wants to merge 2 commits intoopenshift:mainfrom
hongkailiu:pr2213
Open

OTA-1817: Adding unit tests for the extend recommended alerts#2262
hongkailiu wants to merge 2 commits intoopenshift:mainfrom
hongkailiu:pr2213

Conversation

@hongkailiu
Copy link
Copy Markdown
Member

@hongkailiu hongkailiu commented Apr 28, 2026

Re-open #2213

Summary by CodeRabbit

  • Tests
    • Added new example fixtures and test data for the upgrade recommendation feature, demonstrating alert condition scenarios and cluster state configurations.

JianLi-RH and others added 2 commits March 11, 2026 17:38
pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert:
Add Additional Alerts to the OCP Update Precheck

Component teams want to raise special alerts to the `oc adm upgrade recommend ...`
command for consideration by cluster-admins considering a future cluster update.
And we also give `label-maintainers` the ability to add or remove alerts from
the recommend-precheck by a new label `openShiftUpdatePrecheck`[1]

We can follow below steps to generate these test files:

Launch a cluster by cluster-bot:
  $ launch 4.22 aws

Cluster Bot gives the default (X.509 certificate based) kubeconfig
returned by the installer, but Thanos alert-retrieval requires a
token.  So I created one using the cluster-version operator's
namespace:
  $ TOKEN="$(oc -n openshift-cluster-version create token default)"

And Cluster Bot clusters use self-signed certificate, so I collected
all possible Kube API server and ingress TLS certificates in a file:
  $ oc -n openshift-kube-apiserver get -o json secrets | jq -r '.items[] | select(.type == "kubernetes.io/tls").data["tls.crt"] | @base64d' >ca.crt
  $ oc -n openshift-ingress get -o json secrets | jq -r '.items[] | select(.type == "kubernetes.io/tls").data["tls.crt"] | @base64d' >>ca.crt

Prepare test data for warning alerts:

$ cat <<EOF | oc -n openshift-cluster-version create -f -
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: warning-testing
  namespace: openshift-cluster-version
  annotations:
    kubernetes.io/description: Alerting rules for testing.
spec:
  groups:
  - name: VirtWarningPrecheck
    rules:
    - alert: VirtHandlerDaemonSetRolloutFailing
      annotations:
        summary: Test summary.
        description: Test description.
      expr: |
        up{job="cluster-version-operator"} == 1
      labels:
        severity: warning
        namespace: openshift-cluster-version
        reason: UpdatingPrometheusFailed
        openShiftUpdatePrecheck: "true"
  - name: VMCannotBeEvictedWarningNormal
    rules:
    - alert: VMCannotBeEvicted
      annotations:
        summary: Test summary.
        description: Test description.
      expr: |
        up{job="cluster-version-operator"} == 1
      labels:
        severity: warning
        namespace: openshift-cluster-version
        reason: UpdatingPrometheusFailed
        openShiftUpdatePrecheck: "false"
  - name: CustomWarningPrecheck
    rules:
    - alert: TestAlert
      annotations:
        summary: Test summary.
        description: Test description.
      expr: |
        up{job="cluster-version-operator"} == 1
      labels:
        severity: warning
        namespace: openshift-cluster-version
        reason: UpdatingPrometheusFailed
        openShiftUpdatePrecheck: "true"
EOF

retrieved ClusterVersion YAML with:
  $ oc get -o yaml clusterversion version >pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert-cv.yaml

retrieve the cluster's alerts:
  $ OC_ENABLE_CMD_INSPECT_ALERTS=true oc adm inspect-alerts --token "${TOKEN}" --certificate-authority ca.crt | jq . > pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert-alerts.json

then I generated the output fixtures with:
  $ UPDATE=yes go test -v ./pkg/cli/admin/upgrade/recommend/...

delete above alerts:
  $ oc delete PrometheusRule/warning-testing -n openshift-cluster-version

Wait some minutes, then repeat above step to generate new test data for critical alearts:
Create critical alerts:

cat <<EOF | oc -n openshift-cluster-version create -f -
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: critical-testing
  namespace: openshift-cluster-version
  annotations:
    kubernetes.io/description: Alerting rules for testing.
spec:
  groups:
  - name: VirtCriticalPrecheck
    rules:
    - alert: VirtHandlerDaemonSetRolloutFailing
      annotations:
        summary: Test summary.
        description: Test description.
      expr: |
        up{job="cluster-version-operator"} == 1
      labels:
        severity: critical
        namespace: openshift-cluster-version
        reason: UpdatingPrometheusFailed
        openShiftUpdatePrecheck: "true"
  - name: VMCannotBeEvictedCriticalPrecheck
    rules:
    - alert: VMCannotBeEvicted
      for: 1h
      annotations:
        summary: Test summary.
        description: Test description.
      expr: |
        up{job="cluster-version-operator"} == 1
      labels:
        severity: critical
        namespace: openshift-cluster-version
        reason: UpdatingPrometheusFailed
        openShiftUpdatePrecheck: "true"
EOF

Generate test data:
  $ oc get -o yaml clusterversion version >pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert-cv.yaml
  $ OC_ENABLE_CMD_INSPECT_ALERTS=true oc adm inspect-alerts --token "${TOKEN}" --certificate-authority ca.crt | jq . >pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert-alerts.json
  $ UPDATE=yes go test -v ./pkg/cli/admin/upgrade/recommend/...

Then delate critical alerts:
  $ oc delete PrometheusRule/critical-testing -n openshift-cluster-version

[1]: https://issues.redhat.com/browse/OTA-1864
Instead of skipping the cases, each new case is mapped to a random
version.

- The code to skip the cases are no longer needed.
- It adds the coverage for the case "error: no updates available".
@openshift-ci-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label Apr 28, 2026
@openshift-ci-robot
Copy link
Copy Markdown

openshift-ci-robot commented Apr 28, 2026

@hongkailiu: This pull request references OTA-1817 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "5.0.0" version, but no target version was set.

Details

In response to this:

Re-open #2213

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

Walkthrough

Adds example fixtures and test data for alert handling scenarios in the upgrade recommendation CLI tool, including JSON alert responses, Kubernetes manifests, and corresponding CLI output examples for both recommended and critical alert situations.

Changes

Cohort / File(s) Summary
Recommended Alert Examples
pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert-alerts.json, 4.22.0-extend-recommended-alert-cv.yaml, 4.22.0-extend-recommended-alert.output, 4.22.0-extend-recommended-alert.show-outdated-releases-output, 4.22.0-extend-recommended-alert.version-1.2.3-not-important-output
Introduces five fixture files demonstrating a recommended alert scenario: JSON alert response with firing alerts, Kubernetes ClusterVersion manifest with nightly build state, and three example CLI output formats showing alert conditions and upgrade messaging.
Critical Alert Examples
pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert-alerts.json, 4.22.0-extend-recommended-critical-alert-cv.yaml, 4.22.0-extend-recommended-critical-alert.output, 4.22.0-extend-recommended-critical-alert.show-outdated-releases-output, 4.22.0-extend-recommended-critical-alert.version-1.2.3-not-important-output
Introduces five fixture files for a critical alert scenario: JSON response with multiple critical alerts, corresponding ClusterVersion manifest showing non-upgradeability conditions, and three CLI output format examples.
Test Updates
pkg/cli/admin/upgrade/recommend/examples_test.go
Adds two new fixture-to-version mappings for the alert and critical alert ClusterVersion YAML examples, assigning both to version 1.2.3-not-important.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 11 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (11 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly and accurately describes the main objective—adding unit tests for extended recommended alerts functionality.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed The custom check validates Ginkgo test names for stability, but this PR uses standard Go testing package with deterministic test names containing only static file paths and variant names.
Test Structure And Quality ✅ Passed PR adds two parametric test fixture entries following established patterns with mock data, meaningful assertion messages, and appropriate context handling for synchronous operations.
Microshift Test Compatibility ✅ Passed No new Ginkgo e2e tests added; only standard Go unit test fixture entries modified.
Single Node Openshift (Sno) Test Compatibility ✅ Passed PR adds fixture files and updates standard Go unit test with mocked data, not Ginkgo e2e tests.
Topology-Aware Scheduling Compatibility ✅ Passed PR adds only test fixtures and example data files for CLI feature with no deployable resources or scheduling constraints.
Ote Binary Stdout Contract ✅ Passed PR modifies only test code and fixture files without affecting process-level code or uncontrolled stdout writes.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed This PR does not add any new Ginkgo e2e tests. The changes consist entirely of static example fixture files and standard Go unit test data mappings with no IPv4 assumptions or external connectivity requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci openshift-ci Bot requested review from atiratree and ingvagabund April 28, 2026 23:54
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Apr 28, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hongkailiu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 28, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
pkg/cli/admin/upgrade/recommend/examples_test.go (1)

83-85: Fail fast when a specific version mapping is missing.
At Line 83-Line 85, an unmapped cv leaves variant.outputSuffix empty, which can produce confusing fixture-path behavior. Add an explicit guard so missing mappings fail with a clear message.

Proposed refactor
 			var version string
 			if version = variant.versions[cv]; version != "" {
 				variant.outputSuffix = fmt.Sprintf(variant.outputSuffixPattern, version)
+			} else if variant.outputSuffixPattern != "" {
+				t.Fatalf("missing specific-version fixture mapping for %s", cv)
 			}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cli/admin/upgrade/recommend/examples_test.go` around lines 83 - 85, The
code currently lets an unmapped cv produce an empty variant.outputSuffix; change
it to fail fast by checking the lookup result from variant.versions[cv] and
erroring when empty. Specifically, after retrieving version :=
variant.versions[cv] (or when assigning into version), if version == "" call
t.Fatalf (or panic if outside a test helper) with a clear message identifying
the missing mapping (include cv and variant/outputSuffixPattern) so missing
specific-version mappings are caught immediately instead of creating empty
outputSuffix values.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/cli/admin/upgrade/recommend/examples_test.go`:
- Around line 83-85: The code currently lets an unmapped cv produce an empty
variant.outputSuffix; change it to fail fast by checking the lookup result from
variant.versions[cv] and erroring when empty. Specifically, after retrieving
version := variant.versions[cv] (or when assigning into version), if version ==
"" call t.Fatalf (or panic if outside a test helper) with a clear message
identifying the missing mapping (include cv and variant/outputSuffixPattern) so
missing specific-version mappings are caught immediately instead of creating
empty outputSuffix values.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 7e68ed96-c0a7-4706-bb16-bc85abe741a1

📥 Commits

Reviewing files that changed from the base of the PR and between 6b8e30c and 59975c6.

📒 Files selected for processing (11)
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert-alerts.json
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert-cv.yaml
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert.output
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert.show-outdated-releases-output
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-alert.version-1.2.3-not-important-output
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert-alerts.json
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert-cv.yaml
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert.output
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert.show-outdated-releases-output
  • pkg/cli/admin/upgrade/recommend/examples/4.22.0-extend-recommended-critical-alert.version-1.2.3-not-important-output
  • pkg/cli/admin/upgrade/recommend/examples_test.go

@hongkailiu
Copy link
Copy Markdown
Member Author

/retest-required

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Apr 29, 2026

@hongkailiu: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-aws-ovn-upgrade 59975c6 link true /test e2e-aws-ovn-upgrade

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants