diff --git a/api/v1alpha1/common_test.go b/api/v1alpha1/common_test.go new file mode 100644 index 00000000000..65ec17f8feb --- /dev/null +++ b/api/v1alpha1/common_test.go @@ -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()) + }) + }) + +}) diff --git a/api/v1alpha1/container_network_test.go b/api/v1alpha1/container_network_test.go index 256ed1faa1f..791fd7acbd3 100644 --- a/api/v1alpha1/container_network_test.go +++ b/api/v1alpha1/container_network_test.go @@ -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), + ) +}) diff --git a/api/v1alpha1/dataset_types_test.go b/api/v1alpha1/dataset_types_test.go index 8a567e49857..e7bcfc611d4 100644 --- a/api/v1alpha1/dataset_types_test.go +++ b/api/v1alpha1/dataset_types_test.go @@ -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)) }, - 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", + "", + "", + ), + 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), + ) + }) +}) diff --git a/api/v1alpha1/suite_test.go b/api/v1alpha1/suite_test.go new file mode 100644 index 00000000000..b1b8238fb62 --- /dev/null +++ b/api/v1alpha1/suite_test.go @@ -0,0 +1,29 @@ +/* +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 ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestV1alpha1(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "API V1alpha1 Suite") +}