Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 111 additions & 71 deletions pkg/application/inject/fuse/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Comment on lines +123 to +144
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The mockFluidObject does not fully implement the common.FluidObject interface, which will cause a compilation error. To fix this, all methods of the interface must be implemented.

Additionally, the GetName and GetNamespace methods are not part of the common.FluidObject interface and should be removed to avoid confusion.

Note that to implement the interface correctly, you will also need to add the following imports:

import (
    // ... other imports
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/runtime"
)
// 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
}

// The following methods are added to fully implement the common.FluidObject interface.
func (m *mockFluidObject) GetRoot() runtime.Object                           { return nil }
func (m *mockFluidObject) GetVolumes() ([]corev1.Volume, error)              { return nil, nil }
func (m *mockFluidObject) SetVolumes(volumes []corev1.Volume) error          { return nil }
func (m *mockFluidObject) GetInitContainers() ([]corev1.Container, error)    { return nil, nil }
func (m *mockFluidObject) SetContainers(containers []corev1.Container) error { return nil }
func (m *mockFluidObject) SetInitContainers(containers []corev1.Container) error { return nil }
func (m *mockFluidObject) GetVolumeMounts() ([]corev1.VolumeMount, error)    { return nil, nil }
func (m *mockFluidObject) SetMetaObject(metaObject metav1.ObjectMeta) error  { return nil }
func (m *mockFluidObject) GetMetaObject() (metav1.ObjectMeta, error)         { return metav1.ObjectMeta{}, nil }


var ErrMockGetContainers = errors.New("mock error from GetContainers")
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The errors package is used here but is not imported. This will cause a compilation error. Please add import "errors" to the import block at the top of the file.

Loading