Skip to content
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

add test for pkg/controllers/cluster #5467

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

xovoxy
Copy link
Contributor

@xovoxy xovoxy commented Aug 30, 2024

What type of PR is this?
/kind failing-test

What this PR does / why we need it:
add some test for /pkg/controllers/cluster, improve test coverage to 60 above
Which issue(s) this PR fixes:
Parts of ##5235

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@karmada-bot karmada-bot added the kind/failing-test Categorizes issue or PR as related to a consistently or frequently failing test. label Aug 30, 2024
@karmada-bot karmada-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 30, 2024
@codecov-commenter
Copy link

codecov-commenter commented Aug 30, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 32.33%. Comparing base (136e268) to head (503fb93).
Report is 2 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5467      +/-   ##
==========================================
+ Coverage   31.70%   32.33%   +0.62%     
==========================================
  Files         643      643              
  Lines       44444    44445       +1     
==========================================
+ Hits        14093    14371     +278     
+ Misses      29322    28983     -339     
- Partials     1029     1091      +62     
Flag Coverage Δ
unittests 32.33% <ø> (+0.62%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if tt.cluster != nil {
if err := tc.Client.Create(context.Background(), tt.cluster, &client.CreateOptions{}); err != nil {
t.Fatal(err)
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add deletion of these resources at the end of the test if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, thanks for the review. i cant understand why we need to add the delete operation at the end. my understand is that the obj created by fakeclient should be automatically released after each test case is completed. can you help me understand? thanks~

Copy link
Contributor

Choose a reason for hiding this comment

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

ohh right, i misunderstood it. the remaining pr looks good to me. Thanks for correction.

@anujagrawal699
Copy link
Contributor

/lgtm

@karmada-bot
Copy link
Collaborator

@anujagrawal699: changing LGTM is restricted to collaborators

In response to this:

/lgtm

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.

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks.

}
}

got, err := c.Reconcile(context.Background(), req)
Copy link
Member

Choose a reason for hiding this comment

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

In addition to checking err and result, can you determine whether cluster resources are expected?

Other tests are similar.

@xovoxy
Copy link
Contributor Author

xovoxy commented Sep 4, 2024

hi @XiShanYongYe-Chang, i have made some changes based on your comment. please check it~

@XiShanYongYe-Chang
Copy link
Member

Thanks @xovoxy
/assign

wantErr: false,
},
{
name: "cluster with ready condition",
Copy link
Member

Choose a reason for hiding this comment

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

This case run failed on my local side:

=== RUN   TestController_monitorClusterHealth
=== RUN   TestController_monitorClusterHealth/cluster_with_ready_condition
    cluster_controller_test.go:441: Cluster resource get test-cluster, want false
--- FAIL: TestController_monitorClusterHealth (0.00s)
    --- FAIL: TestController_monitorClusterHealth/cluster_with_ready_condition (0.00s)


FAIL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, i tested this pr locally, and worked fine
Screenshot 2024-09-05 at 09 44 17

and i found an error in the log output and correct it. could you please try again? thanks

Copy link
Member

Choose a reason for hiding this comment

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

ok, let me take a look again.

Copy link
Member

Choose a reason for hiding this comment

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

I update the code to print the detail cluster object:

--- a/pkg/controllers/cluster/cluster_controller_test.go
+++ b/pkg/controllers/cluster/cluster_controller_test.go
@@ -438,7 +438,7 @@ func TestController_monitorClusterHealth(t *testing.T) {

                        cleanUpCluster(cluster)
                        if !reflect.DeepEqual(cluster, tt.wCluster) {
-                               t.Errorf("Cluster resource get %+v, want %+v", cluster, tt.wCluster)
+                               t.Errorf("Cluster resource get %+v, want %+v", *cluster, *tt.wCluster)
                                return
                        }

Then I run the test, it still failed(No matter how many times I run), the command output shows that the conditions of the cluster are different.

Cluster resource get 

{TypeMeta:{Kind: APIVersion:} ObjectMeta:{Name:test-cluster GenerateName: Namespace: SelfLink: UID: ResourceVersion: Generation:0 CreationTimestamp:0001-01-01 00:00:00 +0000 UTC DeletionTimestamp:<nil> DeletionGracePeriodSeconds:<nil> Labels:map[] Annotations:map[] OwnerReferences:[] Finalizers:[karmada.io/cluster-controller] ManagedFields:[]} Spec:{ID: SyncMode:Pull APIEndpoint: SecretRef:<nil> ImpersonatorSecretRef:<nil> InsecureSkipTLSVerification:false ProxyURL: ProxyHeader:map[] Provider: Region: Zone: Zones:[] Taints:[] ResourceModels:[]} Status:{KubernetesVersion: APIEnablements:[] Conditions:[{Type:Ready Status:True ObservedGeneration:0 LastTransitionTime:0001-01-01 00:00:00 +0000 UTC Reason: Message:}] NodeSummary:<nil> ResourceSummary:<nil> RemedyActions:[]}}, want 

{TypeMeta:{Kind: APIVersion:} ObjectMeta:{Name:test-cluster GenerateName: Namespace: SelfLink: UID: ResourceVersion: Generation:0 CreationTimestamp:0001-01-01 00:00:00 +0000 UTC DeletionTimestamp:<nil> DeletionGracePeriodSeconds:<nil> Labels:map[] Annotations:map[] OwnerReferences:[] Finalizers:[karmada.io/cluster-controller] ManagedFields:[]} Spec:{ID: SyncMode:Pull APIEndpoint: SecretRef:<nil> ImpersonatorSecretRef:<nil> InsecureSkipTLSVerification:false ProxyURL: ProxyHeader:map[] Provider: Region: Zone: Zones:[] Taints:[] ResourceModels:[]} Status:{KubernetesVersion: APIEnablements:[] Conditions:[{Type:Ready Status:Unknown ObservedGeneration:0 LastTransitionTime:0001-01-01 00:00:00 +0000 UTC Reason:ClusterStatusUnknown Message:Cluster status controller stopped posting cluster status.}] NodeSummary:<nil> ResourceSummary:<nil> RemedyActions:[]}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright, i will check it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// Step 5: Check whether the probe timestamp has timed out.
if metav1.Now().After(clusterHealth.probeTimestamp.Add(gracePeriod)) {

based on the different in the test results, i suspect this section of logic is causing the issue, but I still dont fully understand why. the previous test cases are still passing on my local.

finally, I set the c.ClusterMonitorGracePeriod = 40 * time.Second and modified the test cases. therefor within the timeout period, controller wont change the condition.

please take a look at this, thanks~

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @xovoxy, let me have a try.

@xovoxy xovoxy force-pushed the cluster-ut branch 2 times, most recently from 9d6eb1b to 6df9222 Compare September 6, 2024 02:22
Namespace: "default",
},
}
if err := tc.syncBindingEviction(key); (err != nil) != tt.wantErr {
Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang Sep 6, 2024

Choose a reason for hiding this comment

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

It's not enough to judge err.

How about moving the addition of taint_manager_test.go to the next PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let me add some detail for this test case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, i have added some details about this, could you check it ? if it still not enough, i will consider your suggestion and moving the taint_manager_test.go to the next pr.

Signed-off-by: xovoxy <xilovele@gmail.com>
@karmada-bot karmada-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 8, 2024
Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks~
/lgtm
/approve

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 9, 2024
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: XiShanYongYe-Chang

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

The pull request process is described here

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

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 9, 2024
@karmada-bot karmada-bot merged commit 54b90a2 into karmada-io:master Sep 9, 2024
12 checks passed
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. kind/failing-test Categorizes issue or PR as related to a consistently or frequently failing test. lgtm Indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants