generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
test: migrate container tests from testify to ginkgo #5429
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
Closed
yuvraj-kolkar17
wants to merge
3
commits into
fluid-cloudnative:master
from
yuvraj-kolkar17:migrate-tests-to-ginkgo
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -17,90 +17,130 @@ limitations under the License. | |
| package fuse | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| "errors" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/fluid-cloudnative/fluid/pkg/common" | ||
| "github.com/fluid-cloudnative/fluid/pkg/utils/applications/pod" | ||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| func Test_findInjectedSidecars(t *testing.T) { | ||
|
|
||
| pod1 := &corev1.Pod{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "test", | ||
| }, | ||
| { | ||
| Name: "test2", | ||
| var _ = Describe("findInjectedSidecars", func() { | ||
| Context("when pod has no injected sidecars", func() { | ||
| It("should return empty slice", func() { | ||
| pod1 := &corev1.Pod{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "test", | ||
| }, | ||
| { | ||
| Name: "test2", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| podObjs1, err := pod.NewApplication(pod1).GetPodSpecs() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
| podObjs, err := pod.NewApplication(pod1).GetPodSpecs() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| pod2 := &corev1.Pod{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "fluid-fuse-0", | ||
| }, | ||
| { | ||
| Name: "test", | ||
| injectedSidecars, err := findInjectedSidecars(podObjs[0]) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(injectedSidecars).To(BeEmpty()) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when pod has one injected sidecar", func() { | ||
| It("should return the injected sidecar", func() { | ||
| pod2 := &corev1.Pod{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "fluid-fuse-0", | ||
| }, | ||
| { | ||
| Name: "test", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| podObjs2, err := pod.NewApplication(pod2).GetPodSpecs() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
| podObjs, err := pod.NewApplication(pod2).GetPodSpecs() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| type args struct { | ||
| pod common.FluidObject | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| wantInjectedSidecars []corev1.Container | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "no_injected_sidecars", | ||
| args: args{ | ||
| pod: podObjs1[0], | ||
| }, | ||
| wantInjectedSidecars: []corev1.Container{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "one_injected_sidecar", | ||
| args: args{ | ||
| pod: podObjs2[0], | ||
| }, | ||
| wantInjectedSidecars: []corev1.Container{ | ||
| { | ||
| Name: "fluid-fuse-0", | ||
| injectedSidecars, err := findInjectedSidecars(podObjs[0]) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(injectedSidecars).To(HaveLen(1)) | ||
| Expect(injectedSidecars[0].Name).To(Equal("fluid-fuse-0")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when pod has multiple injected sidecars", func() { | ||
| It("should return all injected sidecars", func() { | ||
| pod3 := &corev1.Pod{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "fluid-fuse-0", | ||
| }, | ||
| { | ||
| Name: "test", | ||
| }, | ||
| { | ||
| Name: "fluid-fuse-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gotInjectedSidecars, err := findInjectedSidecars(tt.args.pod) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("findInjectedSidecars() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(gotInjectedSidecars, tt.wantInjectedSidecars) { | ||
| t.Errorf("findInjectedSidecars() = %v, want %v", gotInjectedSidecars, tt.wantInjectedSidecars) | ||
| podObjs, err := pod.NewApplication(pod3).GetPodSpecs() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| injectedSidecars, err := findInjectedSidecars(podObjs[0]) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(injectedSidecars).To(HaveLen(2)) | ||
| Expect(injectedSidecars[0].Name).To(Equal("fluid-fuse-0")) | ||
| Expect(injectedSidecars[1].Name).To(Equal("fluid-fuse-1")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when GetContainers returns an error", func() { | ||
| It("should return the error immediately", func() { | ||
| // Create a mock FluidObject that returns an error | ||
| mockPod := &mockFluidObject{ | ||
| shouldError: true, | ||
| } | ||
|
|
||
| injectedSidecars, err := findInjectedSidecars(mockPod) | ||
|
|
||
| Expect(err).To(HaveOccurred()) | ||
| Expect(injectedSidecars).To(BeEmpty()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| // mockFluidObject is a mock implementation of common.FluidObject for testing error handling | ||
| type mockFluidObject struct { | ||
| shouldError bool | ||
| containers []corev1.Container | ||
| } | ||
|
|
||
| func (m *mockFluidObject) GetContainers() ([]corev1.Container, error) { | ||
| if m.shouldError { | ||
| return nil, ErrMockGetContainers | ||
| } | ||
| return m.containers, nil | ||
| } | ||
|
|
||
| // Implement other required methods from common.FluidObject interface | ||
| // (these are placeholders and should be adjusted based on the actual interface) | ||
| func (m *mockFluidObject) GetName() string { | ||
| return "mock-pod" | ||
| } | ||
|
|
||
| func (m *mockFluidObject) GetNamespace() string { | ||
| return "default" | ||
| } | ||
|
|
||
| var ErrMockGetContainers = errors.New("mock error from GetContainers") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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.
The
mockFluidObjectdoes not fully implement thecommon.FluidObjectinterface, which will cause a compilation error. To fix this, all methods of the interface must be implemented.Additionally, the
GetNameandGetNamespacemethods are not part of thecommon.FluidObjectinterface and should be removed to avoid confusion.Note that to implement the interface correctly, you will also need to add the following imports: