Skip to content
Merged
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
164 changes: 122 additions & 42 deletions pkg/ddc/vineyard/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ limitations under the License.
package vineyard

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

datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
Expand All @@ -23,31 +24,137 @@ import (
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestBuild(t *testing.T) {
var namespace = v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "fluid",
const (
engineTestNamespace = "fluid"
engineTestName = "vineyard"
engineTestID = "testId"
)

var _ = Describe("VineyardEngine", Label("vineyard"), func() {
Describe("Build", func() {
Context("when runtime is a valid VineyardRuntime", func() {
It("should build the template engine", func() {
vineyardRuntime := newEngineTestVineyardRuntime()
fakeClient := fake.NewFakeClientWithScheme(testScheme, newEngineTestObjects(vineyardRuntime)...)

engine, err := Build(engineTestID, newEngineTestContext(fakeClient, vineyardRuntime))

Expect(err).NotTo(HaveOccurred())
Expect(engine).NotTo(BeNil())
})
})

Context("when runtime is nil", func() {
It("should return a parse error", func() {
fakeClient := fake.NewFakeClientWithScheme(testScheme)

engine, err := Build(engineTestID, newEngineTestContext(fakeClient, nil))

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("engine vineyard is failed to parse"))
Expect(engine).To(BeNil())
})
})

Context("when runtime has the wrong type", func() {
It("should return a parse error", func() {
dataset := newEngineTestDataset()
fakeClient := fake.NewFakeClientWithScheme(testScheme)

engine, err := Build(engineTestID, newEngineTestContext(fakeClient, dataset))

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("engine vineyard is failed to parse"))
Expect(engine).To(BeNil())
})
})

Context("when runtime info cannot be loaded", func() {
It("should return a runtime info error", func() {
vineyardRuntime := newEngineTestVineyardRuntime()
fakeClient := fake.NewFakeClientWithScheme(testScheme)

engine, err := Build(engineTestID, newEngineTestContext(fakeClient, vineyardRuntime))

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("engine vineyard failed to get runtime info"))
Expect(engine).To(BeNil())
})
})
})

Describe("Precheck", func() {
Context("when the VineyardRuntime exists", func() {
It("should return found true without error", func() {
vineyardRuntime := newEngineTestVineyardRuntime()
fakeClient := fake.NewFakeClientWithScheme(testScheme, vineyardRuntime)

found, err := Precheck(fakeClient, types.NamespacedName{Name: engineTestName, Namespace: engineTestNamespace})

Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeTrue())
})
})

Context("when the VineyardRuntime does not exist", func() {
It("should return found false without error", func() {
fakeClient := fake.NewFakeClientWithScheme(testScheme)

found, err := Precheck(fakeClient, types.NamespacedName{Name: engineTestName, Namespace: engineTestNamespace})

Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeFalse())
})
})
})
})

func newEngineTestContext(fakeClient client.Client, runtime client.Object) cruntime.ReconcileRequestContext {
return cruntime.ReconcileRequestContext{
NamespacedName: types.NamespacedName{
Name: engineTestName,
Namespace: engineTestNamespace,
},
Client: fakeClient,
Log: fake.NullLogger(),
RuntimeType: "vineyard",
Runtime: runtime,
}
testObjs := []runtime.Object{}
testObjs = append(testObjs, namespace.DeepCopy())
}
Comment on lines +117 to +128
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 ReconcileRequestContext is initialized without the Context and Recorder fields. While the current tests for Build and Precheck do not appear to use these fields, they are fundamental parts of the request context. Missing them could lead to nil pointer dereferences if the code under test is updated to emit events or use the context for API calls. It is recommended to initialize them with context.TODO() and a fake recorder (e.g., record.NewFakeRecorder(1)). Additionally, consider using the engineTestName constant for the RuntimeType field to maintain consistency.

Suggested change
func newEngineTestContext(fakeClient client.Client, runtime client.Object) cruntime.ReconcileRequestContext {
return cruntime.ReconcileRequestContext{
NamespacedName: types.NamespacedName{
Name: engineTestName,
Namespace: engineTestNamespace,
},
Client: fakeClient,
Log: fake.NullLogger(),
RuntimeType: "vineyard",
Runtime: runtime,
}
testObjs := []runtime.Object{}
testObjs = append(testObjs, namespace.DeepCopy())
}
func newEngineTestContext(fakeClient client.Client, runtime client.Object) cruntime.ReconcileRequestContext {
return cruntime.ReconcileRequestContext{
NamespacedName: types.NamespacedName{
Name: engineTestName,
Namespace: engineTestNamespace,
},
Client: fakeClient,
Log: fake.NullLogger(),
RuntimeType: engineTestName,
Runtime: runtime,
}
}


var dataset = datav1alpha1.Dataset{
func newEngineTestObjects(vineyardRuntime *datav1alpha1.VineyardRuntime) []k8sruntime.Object {
return []k8sruntime.Object{
&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: engineTestNamespace}},
newEngineTestDataset(),
vineyardRuntime,
&appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: engineTestName + "-worker",
Namespace: engineTestNamespace,
},
},
}
}

func newEngineTestDataset() *datav1alpha1.Dataset {
return &datav1alpha1.Dataset{
ObjectMeta: metav1.ObjectMeta{
Name: "hbase",
Namespace: "fluid",
Name: engineTestName,
Namespace: engineTestNamespace,
},
}
testObjs = append(testObjs, dataset.DeepCopy())
}

var runtime = datav1alpha1.VineyardRuntime{
func newEngineTestVineyardRuntime() *datav1alpha1.VineyardRuntime {
return &datav1alpha1.VineyardRuntime{
ObjectMeta: metav1.ObjectMeta{
Name: "hbase",
Namespace: "fluid",
Name: engineTestName,
Namespace: engineTestNamespace,
},
Spec: datav1alpha1.VineyardRuntimeSpec{
Master: datav1alpha1.MasterSpec{
Expand All @@ -62,31 +169,4 @@ func TestBuild(t *testing.T) {
},
},
}
testObjs = append(testObjs, runtime.DeepCopy())

var daemonset = appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "hbase-worker",
Namespace: "fluid",
},
}
testObjs = append(testObjs, daemonset.DeepCopy())
client := fake.NewFakeClientWithScheme(testScheme, testObjs...)

var ctx = cruntime.ReconcileRequestContext{
NamespacedName: types.NamespacedName{
Name: "hbase",
Namespace: "fluid",
},
Client: client,
Log: fake.NullLogger(),
RuntimeType: "vineyard",
Runtime: &runtime,
}

engine, err := Build("testId", ctx)
if err != nil || engine == nil {
t.Errorf("fail to exec the build function with the eror %v", err)
}

}
Loading