Skip to content
Open
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
47 changes: 47 additions & 0 deletions api/v1alpha1/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 v1alpha1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("common API helpers", func() {
Describe("MetadataSyncPolicy.AutoSyncEnabled", func() {
It("defaults to true when auto sync is not configured", func() {
policy := &MetadataSyncPolicy{}

Expect(policy.AutoSyncEnabled()).To(BeTrue())
})

It("returns true when auto sync is explicitly enabled", func() {
enabled := true
policy := &MetadataSyncPolicy{AutoSync: &enabled}

Expect(policy.AutoSyncEnabled()).To(BeTrue())
})

It("returns false when auto sync is explicitly disabled", func() {
disabled := false
policy := &MetadataSyncPolicy{AutoSync: &disabled}

Expect(policy.AutoSyncEnabled()).To(BeFalse())
})
})

})
39 changes: 13 additions & 26 deletions api/v1alpha1/container_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,18 @@ limitations under the License.

package v1alpha1

import "testing"
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestIsHostNetwork(t *testing.T) {
testCases := map[string]struct {
n NetworkMode
want bool
}{
"test host network case 1": {
n: HostNetworkMode,
want: true,
var _ = Describe("IsHostNetwork", func() {
DescribeTable("reports whether the network mode uses the host network",
func(mode NetworkMode, expected bool) {
Expect(IsHostNetwork(mode)).To(Equal(expected))
},
"test host network case 2": {
n: "",
want: true,
},
"test container network case 1": {
n: ContainerNetworkMode,
want: false,
},
}

for k, v := range testCases {
got := IsHostNetwork(v.n)
if v.want != got {
t.Errorf("check %s failure, got:%t,want:%t", k, got, v.want)
}
}
}
Entry("host network mode", HostNetworkMode, true),
Entry("default network mode", DefaultNetworkMode, true),
Entry("container network mode", ContainerNetworkMode, false),
)
})
269 changes: 111 additions & 158 deletions api/v1alpha1/dataset_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,171 +17,124 @@
package v1alpha1

import (
"testing"
"github.com/fluid-cloudnative/fluid/pkg/common"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
const (
dataLoadOperation = "DataLoad"
dataMigrateOperation = "DataMigrate"
)

func TestDataset_RemoveDataOperationInProgress(t *testing.T) {
type fields struct {
TypeMeta v1.TypeMeta
ObjectMeta v1.ObjectMeta
Spec DatasetSpec
Status DatasetStatus
}
type args struct {
operationType string
name string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "test1",
fields: fields{
Status: DatasetStatus{
OperationRef: map[string]string{
"DataLoad": "test1",
},
},
},
args: args{
operationType: "DataLoad",
name: "test1",
},
want: "",
},
{
name: "test2",
fields: fields{
Status: DatasetStatus{
OperationRef: map[string]string{
"DataLoad": "test1,test2",
},
},
},
args: args{
operationType: "DataLoad",
name: "test1",
var _ = Describe("Dataset methods", func() {
Describe("RemoveDataOperationInProgress", func() {
DescribeTable("removes the target operation reference",
func(dataset *Dataset, operationType string, name string, expectedRemoved string, expectedCurrent string) {
Expect(dataset.RemoveDataOperationInProgress(operationType, name)).To(Equal(expectedRemoved))
Expect(dataset.GetDataOperationInProgress(operationType)).To(Equal(expectedCurrent))
},
Comment on lines +32 to 36
Comment on lines +33 to 36
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The expectedRemoved and expectedCurrent parameters in this table-driven test are redundant because RemoveDataOperationInProgress returns the updated state of the operation reference, which is exactly what GetDataOperationInProgress retrieves. Simplifying this to a single expected parameter would improve test maintainability.

Suggested change
func(dataset *Dataset, operationType string, name string, expectedRemoved string, expectedCurrent string) {
Expect(dataset.RemoveDataOperationInProgress(operationType, name)).To(Equal(expectedRemoved))
Expect(dataset.GetDataOperationInProgress(operationType)).To(Equal(expectedCurrent))
},
func(dataset *Dataset, operationType string, name string, expected string) {
Expect(dataset.RemoveDataOperationInProgress(operationType, name)).To(Equal(expected))
Expect(dataset.GetDataOperationInProgress(operationType)).To(Equal(expected))
},

want: "test2",
},
{
name: "test3",
fields: fields{
Status: DatasetStatus{},
},
args: args{
operationType: "DataLoad",
name: "test1",
Entry("removes the only in-progress operation",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1"}}},
dataLoadOperation,
"test1",
"",
"",
),
Entry("removes one operation from a list",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1,test2"}}},
dataLoadOperation,
"test1",
"test2",
"test2",
),
Entry("returns empty when no operation refs are recorded",
&Dataset{Status: DatasetStatus{}},
dataLoadOperation,
"test1",
"",
"",
),
Comment on lines +51 to +57
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Adding a test case for the scenario where the operation to be removed is not present in the OperationRef list would improve the coverage of edge cases for RemoveDataOperationInProgress.

			Entry("returns empty when no operation refs are recorded",
				&Dataset{Status: DatasetStatus{}},
				dataLoadOperation,
				"test1",
				"",
				"",
			),
			Entry("returns original list when operation is not found",
				&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1,test2"}}},
				dataLoadOperation,
				"test3",
				"test1,test2",
				"test1,test2",
			),

Entry("keeps the current refs when the target operation is not found",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1,test2"}}},
dataLoadOperation,
"missing",
"test1,test2",
"test1,test2",
),
)
})

Describe("SetDataOperationInProgress", func() {
DescribeTable("tracks the operation reference for the requested operation type",
func(dataset *Dataset, operationType string, name string, expected string) {
dataset.SetDataOperationInProgress(operationType, name)

Expect(dataset.GetDataOperationInProgress(operationType)).To(Equal(expected))
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataset := &Dataset{
TypeMeta: tt.fields.TypeMeta,
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
if got := dataset.RemoveDataOperationInProgress(tt.args.operationType, tt.args.name); got != tt.want {
t.Errorf("RemoveDataOperationInProgress() = %v, want %v", got, tt.want)
}
Entry("creates the first operation ref",
&Dataset{Status: DatasetStatus{}},
dataLoadOperation,
"test1",
"test1",
),
Entry("appends a new operation ref for the same type",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1"}}},
dataLoadOperation,
"test2",
"test1,test2",
),
Entry("records a different operation type independently",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test1"}}},
dataMigrateOperation,
"test",
"test",
),
Entry("keeps an existing operation ref without duplication",
&Dataset{Status: DatasetStatus{OperationRef: map[string]string{dataLoadOperation: "test"}}},
dataLoadOperation,
"test",
"test",
),
)
})

Describe("CanbeBound", func() {
It("returns true when no runtime is recorded", func() {
dataset := &Dataset{}

Expect(dataset.CanbeBound("runtime", "fluid", common.AccelerateCategory)).To(BeTrue())
})
}
}

func TestDataset_SetDataOperationInProgress(t *testing.T) {
type fields struct {
TypeMeta v1.TypeMeta
ObjectMeta v1.ObjectMeta
Spec DatasetSpec
Status DatasetStatus
}
type args struct {
operationType string
name string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "test1",
fields: fields{
Status: DatasetStatus{},
},
args: args{
operationType: "DataLoad",
name: "test1",
},
want: "test1",
},
{
name: "test2",
fields: fields{
Status: DatasetStatus{
OperationRef: map[string]string{
"DataLoad": "test1",
},
},
},
args: args{
operationType: "DataLoad",
name: "test2",
},
want: "test1,test2",
},
{
name: "test3",
fields: fields{
Status: DatasetStatus{
OperationRef: map[string]string{
"DataLoad": "test1",
DescribeTable("matches the runtime identity",
func(name, namespace string, category common.Category, expected bool) {
dataset := &Dataset{
Status: DatasetStatus{
Runtimes: []Runtime{
{Name: "target", Namespace: "fluid", Category: common.AccelerateCategory},
},
},
},
},
args: args{
operationType: "DataMigrate",
name: "test",
},
want: "test",
},
{
name: "test4",
fields: fields{
Status: DatasetStatus{
OperationRef: map[string]string{
"DataLoad": "test",
},
},
}

Expect(dataset.CanbeBound(name, namespace, category)).To(Equal(expected))
},
args: args{
operationType: "DataLoad",
name: "test",
Entry("matching identity", "target", "fluid", common.AccelerateCategory, true),
Entry("name mismatch", "other", "fluid", common.AccelerateCategory, false),
Entry("namespace mismatch", "target", "other", common.AccelerateCategory, false),
Entry("category mismatch", "target", "fluid", common.Category("other"), false),
)
})

Describe("IsExclusiveMode", func() {
DescribeTable("reports whether the placement mode is exclusive",
func(mode PlacementMode, expected bool) {
dataset := &Dataset{Spec: DatasetSpec{PlacementMode: mode}}

Expect(dataset.IsExclusiveMode()).To(Equal(expected))
},
want: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataset := &Dataset{
TypeMeta: tt.fields.TypeMeta,
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
dataset.SetDataOperationInProgress(tt.args.operationType, tt.args.name)
if got := dataset.GetDataOperationInProgress(tt.args.operationType); got != tt.want {
t.Errorf("SetDataOperationInProgress() = %v, want %v", got, tt.want)
}
})
}
}
Entry("default placement mode", DefaultMode, true),
Entry("exclusive placement mode", ExclusiveMode, true),
Entry("shared placement mode", ShareMode, false),
)
})
})
Loading
Loading