generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(controllers): migrate core pkg/controllers tests to Ginkgo #5679
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
Open
hxrshxz
wants to merge
7
commits into
fluid-cloudnative:master
Choose a base branch
from
hxrshxz:lfx/week-1-pkg-controllers-core-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+930
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
11a2a1a
test(ctrl): add manager and operation controller tests
hxrshxz 9135ade
test(ctrl): add core engine test helper
hxrshxz 4b7cf67
test(ctrl): tighten controller helper coverage from bot review
hxrshxz 69d2cd0
test(controllers): isolate GetConfigOrDie test in subprocess
hxrshxz 13b970e
fix: update copyright year to 2026 in test files
hxrshxz 43b15a1
test(ctrl): address sonarqube bot comments
hxrshxz e8b20c0
test(jindo): improve minio setup and data seeding in e2e script
hxrshxz 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,49 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
|
|
||
| datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
| "github.com/fluid-cloudnative/fluid/pkg/dataoperation" | ||
| cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime" | ||
| ) | ||
|
|
||
| type fakeEngineCore struct { | ||
| id string | ||
| } | ||
|
|
||
| func (e *fakeEngineCore) ID() string { return e.id } | ||
|
|
||
| func (e *fakeEngineCore) Shutdown() error { return nil } | ||
|
|
||
| func (e *fakeEngineCore) Setup(ctx cruntime.ReconcileRequestContext) (bool, error) { | ||
| return true, nil | ||
| } | ||
|
|
||
| func (e *fakeEngineCore) CreateVolume() error { return nil } | ||
|
|
||
| func (e *fakeEngineCore) DeleteVolume() error { return nil } | ||
|
|
||
| func (e *fakeEngineCore) Sync(ctx cruntime.ReconcileRequestContext) error { return nil } | ||
|
|
||
| func (e *fakeEngineCore) Validate(ctx cruntime.ReconcileRequestContext) error { return nil } | ||
|
|
||
| func (e *fakeEngineCore) Operate(ctx cruntime.ReconcileRequestContext, opStatus *datav1alpha1.OperationStatus, operation dataoperation.OperationInterface) (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
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,205 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| const testItem = "test-item" | ||
|
|
||
| var _ = Describe("NewFluidControllerRateLimiter", func() { | ||
| It("should create a rate limiter with valid parameters", func() { | ||
| limiter := NewFluidControllerRateLimiter( | ||
| 5*time.Millisecond, | ||
| 1000*time.Second, | ||
| 10, | ||
| 100, | ||
| ) | ||
| Expect(limiter).NotTo(BeNil()) | ||
| }) | ||
|
|
||
| It("should return increasing delays for repeated failures on the same item", func() { | ||
| limiter := NewFluidControllerRateLimiter( | ||
| 5*time.Millisecond, | ||
| 1000*time.Second, | ||
| 10, | ||
| 100, | ||
| ) | ||
| first := limiter.When(testItem) | ||
| second := limiter.When(testItem) | ||
| Expect(second).To(BeNumerically(">=", first)) | ||
| }) | ||
|
|
||
| It("should return different delays for different items", func() { | ||
| limiter := NewFluidControllerRateLimiter( | ||
| 5*time.Millisecond, | ||
| 1000*time.Second, | ||
| 10, | ||
| 100, | ||
| ) | ||
| // Advance item-a several times to increase its delay | ||
| for i := 0; i < 5; i++ { | ||
| limiter.When("item-a") | ||
| } | ||
| delayA := limiter.When("item-a") | ||
| delayB := limiter.When("item-b") | ||
| Expect(delayA).To(BeNumerically(">", delayB)) | ||
| }) | ||
|
|
||
| It("should respect the max delay parameter", func() { | ||
| maxDelay := 100 * time.Millisecond | ||
| limiter := NewFluidControllerRateLimiter( | ||
| 5*time.Millisecond, | ||
| maxDelay, | ||
| 10, | ||
| 100, | ||
| ) | ||
| // Push the item through many failures | ||
| for i := 0; i < 100; i++ { | ||
| limiter.When(testItem) | ||
| } | ||
| delay := limiter.When(testItem) | ||
| Expect(delay).To(BeNumerically("<=", maxDelay)) | ||
| }) | ||
|
|
||
| It("should reset delay after forgetting an item", func() { | ||
| limiter := NewFluidControllerRateLimiter( | ||
| 5*time.Millisecond, | ||
| 1000*time.Second, | ||
| 10, | ||
| 100, | ||
| ) | ||
| for i := 0; i < 10; i++ { | ||
| limiter.When(testItem) | ||
| } | ||
| limiter.Forget(testItem) | ||
| delay := limiter.When(testItem) | ||
| firstDelay := limiter.When("fresh-item") | ||
| // After forget, the item's delay should be close to the initial value | ||
| Expect(delay).To(BeNumerically("<=", firstDelay*2)) | ||
| }) | ||
| }) | ||
|
|
||
| var _ = Describe("manager client and config helpers", func() { | ||
| Describe("NewFluidControllerClient", func() { | ||
| var ( | ||
| originalVal string | ||
| wasSet bool | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| originalVal, wasSet = os.LookupEnv("HELM_DRIVER") | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| if wasSet { | ||
| Expect(os.Setenv("HELM_DRIVER", originalVal)).To(Succeed()) | ||
| } else { | ||
| Expect(os.Unsetenv("HELM_DRIVER")).To(Succeed()) | ||
| } | ||
| }) | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| It("returns error on secret driver path with nil rest config", func() { | ||
| Expect(os.Setenv("HELM_DRIVER", "secret")).To(Succeed()) | ||
|
|
||
| c, err := NewFluidControllerClient(nil, client.Options{}) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(c).To(BeNil()) | ||
| }) | ||
|
|
||
| It("returns error on cache-bypass path with nil rest config", func() { | ||
| Expect(os.Setenv("HELM_DRIVER", "configmap")).To(Succeed()) | ||
|
|
||
| c, err := NewFluidControllerClient(nil, client.Options{Cache: &client.CacheOptions{}}) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(c).To(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("NewCacheClientBypassSecrets", func() { | ||
| It("returns error with nil rest config", func() { | ||
| c, err := NewCacheClientBypassSecrets(nil, client.Options{Cache: &client.CacheOptions{}}) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(c).To(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GetConfigOrDieWithQPSAndBurst", func() { | ||
| var ( | ||
| originalKubeconfig string | ||
| wasSet bool | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| originalKubeconfig, wasSet = os.LookupEnv("KUBECONFIG") | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| if wasSet { | ||
| Expect(os.Setenv("KUBECONFIG", originalKubeconfig)).To(Succeed()) | ||
| } else { | ||
| Expect(os.Unsetenv("KUBECONFIG")).To(Succeed()) | ||
| } | ||
| }) | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| It("sets qps and burst when both are positive", func() { | ||
| if os.Getenv("FLUID_GET_CONFIG_SUBPROCESS") == "1" { | ||
| cfg := GetConfigOrDieWithQPSAndBurst(123, 456) | ||
| if cfg == nil || cfg.QPS != float32(123) || cfg.Burst != 456 { | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| os.Exit(2) | ||
| } | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| tmpDir := GinkgoT().TempDir() | ||
| kubeconfig := filepath.Join(tmpDir, "config") | ||
| content := `apiVersion: v1 | ||
| kind: Config | ||
| clusters: | ||
| - name: local | ||
| cluster: | ||
| server: https://127.0.0.1:65535 | ||
| insecure-skip-tls-verify: true | ||
| users: | ||
| - name: local-user | ||
| user: | ||
| token: fake-token | ||
| contexts: | ||
| - name: local | ||
| context: | ||
| cluster: local | ||
| user: local-user | ||
| current-context: local | ||
| ` | ||
| Expect(os.WriteFile(kubeconfig, []byte(content), 0o600)).To(Succeed()) | ||
| Expect(os.Setenv("KUBECONFIG", kubeconfig)).To(Succeed()) | ||
|
|
||
| cmd := exec.Command(os.Args[0], "-test.run=TestControllers", "-ginkgo.focus=sets qps and burst when both are positive") | ||
| cmd.Env = append(os.Environ(), "FLUID_GET_CONFIG_SUBPROCESS=1", "KUBECONFIG="+kubeconfig) | ||
| out, err := cmd.CombinedOutput() | ||
| Expect(err).NotTo(HaveOccurred(), string(out)) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.