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

test: add unit test for GenerateScaledObjectName #3892

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

converge
Copy link
Member

@converge converge commented May 11, 2024

Fixes #

Proposed Changes

  • add unit test for GenerateScaledObjectName

note: this is a PR to validate how unit tests should be written for knative. Upcoming unit test is for Keda integration.

@knative-prow knative-prow bot requested review from Leo6Leo and pierDipi May 11, 2024 01:35
@knative-prow knative-prow bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. area/control-plane labels May 11, 2024
Copy link

knative-prow bot commented May 11, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: converge
Once this PR has been reviewed and has the lgtm label, please assign lionelvillard for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

Copy link

codecov bot commented May 11, 2024

Codecov Report

Attention: Patch coverage is 4.91803% with 58 lines in your changes are missing coverage. Please review.

Project coverage is 49.37%. Comparing base (02a879b) to head (8543c2c).
Report is 116 commits behind head on main.

Files Patch % Lines
...ntrol-plane/pkg/autoscaler/keda/mocks/keda_mock.go 3.33% 58 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##               main    #3892       +/-   ##
=============================================
- Coverage     61.99%   49.37%   -12.63%     
=============================================
  Files           188      247       +59     
  Lines         12634    14909     +2275     
  Branches        273        0      -273     
=============================================
- Hits           7833     7361      -472     
- Misses         4187     6792     +2605     
- Partials        614      756      +142     
Flag Coverage Δ
java-unittests ?

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.

Copy link

knative-prow bot commented May 11, 2024

@converge: 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
upgrade-tests_eventing-kafka-broker_main 8543c2c link true /test upgrade-tests

Your PR dashboard.

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.

Copy link
Member

@Cali0707 Cali0707 left a comment

Choose a reason for hiding this comment

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

This looks good @converge - thanks!

I'm not very familiar with when we add mocks to the tests in this repo, so I'm going to let @pierDipi take a look at the test mocks before we approve this

/assign @pierDipi

Comment on lines +28 to +37
func TestGenerateScaledObjectName(t *testing.T) {
type args struct {
obj v1.Object
}
tests := []struct {
name string
args args
wantPrefix string
wantSuffix types.UID
}{
Copy link
Member

Choose a reason for hiding this comment

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

Nice use of the table testing pattern! This is how we normally do unit tests in eventing (as I'm sure you noticed)

Copy link
Member

@pierDipi pierDipi left a comment

Choose a reason for hiding this comment

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

Did you consider having a test in https://github.com/knative-extensions/eventing-kafka-broker/blob/main/control-plane/pkg/reconciler/consumergroup/consumergroup_test.go that would assert that the ConsumerGroup reconciler creates all KEDA objects when the feature flag is enabeled?

Such test would cover also the GenerateScaledObjectName since we need to explicitly indicate the object names (and spec), similar to how is done here for Consumers

WantCreates: []runtime.Object{
NewConsumer(1,
ConsumerFinalizer(),
ConsumerSpec(NewConsumerSpec(
ConsumerTopics("t1", "t2"),
ConsumerConfigs(
ConsumerBootstrapServersConfig(ChannelBootstrapServers),
ConsumerGroupIdConfig("my.group.id"),
),
ConsumerVReplicas(1),
ConsumerPlacement(kafkainternals.PodBind{PodName: "p1", PodNamespace: systemNamespace}),
)),
),
NewConsumer(2,
ConsumerFinalizer(),
ConsumerSpec(NewConsumerSpec(
ConsumerTopics("t1", "t2"),
ConsumerConfigs(
ConsumerBootstrapServersConfig(ChannelBootstrapServers),
ConsumerGroupIdConfig("my.group.id"),
),
ConsumerVReplicas(1),
ConsumerPlacement(kafkainternals.PodBind{PodName: "p2", PodNamespace: systemNamespace}),
)),
),
},

The benefit is that we don't need to have mocks, while having the benefit of a "unit test" that is fast and doesn't interact with the real k8s API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/control-plane size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants