Skip to content

Commit 4e1642c

Browse files
authored
test: add EFC Ginkgo package coverage (#5823)
* test: add efc ginkgo package coverage Signed-off-by: Harsh <harshmastic@gmail.com> * test: use DeferCleanup for EFC value file Signed-off-by: Harsh <harshmastic@gmail.com> * test: tighten EFC metadata and shutdown checks Signed-off-by: Harsh <harshmastic@gmail.com> * test: restore EFC build helper fixture parity Signed-off-by: Harsh <harshmastic@gmail.com> * test: remove EFC metadata test seam Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 70d9752 commit 4e1642c

4 files changed

Lines changed: 657 additions & 506 deletions

File tree

pkg/ddc/efc/engine_test.go

Lines changed: 110 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@
1717
package efc
1818

1919
import (
20-
"testing"
20+
"context"
2121

2222
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2323
"github.com/fluid-cloudnative/fluid/pkg/common"
2424
cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
2525
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
26+
. "github.com/onsi/ginkgo/v2"
27+
. "github.com/onsi/gomega"
2628
appsv1 "k8s.io/api/apps/v1"
2729
v1 "k8s.io/api/core/v1"
2830
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931
"k8s.io/apimachinery/pkg/runtime"
3032
"k8s.io/apimachinery/pkg/types"
33+
"sigs.k8s.io/controller-runtime/pkg/client"
3134
)
3235

3336
var (
@@ -41,27 +44,17 @@ func init() {
4144
_ = appsv1.AddToScheme(testScheme)
4245
}
4346

44-
func TestBuild(t *testing.T) {
45-
var namespace = v1.Namespace{
46-
ObjectMeta: metav1.ObjectMeta{
47-
Name: "fluid",
48-
},
49-
}
50-
testObjs := []runtime.Object{}
51-
testObjs = append(testObjs, namespace.DeepCopy())
52-
53-
var dataset = datav1alpha1.Dataset{
54-
ObjectMeta: metav1.ObjectMeta{
55-
Name: "hbase",
56-
Namespace: "fluid",
57-
},
58-
}
59-
testObjs = append(testObjs, dataset.DeepCopy())
47+
const (
48+
engineTestNamespace = "fluid"
49+
engineTestName = "hbase"
50+
engineTestID = "testId"
51+
)
6052

61-
var runtime = datav1alpha1.EFCRuntime{
53+
func newEngineRuntime() *datav1alpha1.EFCRuntime {
54+
return &datav1alpha1.EFCRuntime{
6255
ObjectMeta: metav1.ObjectMeta{
63-
Name: "hbase",
64-
Namespace: "fluid",
56+
Name: engineTestName,
57+
Namespace: engineTestNamespace,
6558
},
6659
Spec: datav1alpha1.EFCRuntimeSpec{
6760
Fuse: datav1alpha1.EFCFuseSpec{
@@ -74,78 +67,121 @@ func TestBuild(t *testing.T) {
7467
},
7568
},
7669
}
77-
testObjs = append(testObjs, runtime.DeepCopy())
70+
}
7871

79-
var sts = appsv1.StatefulSet{
72+
func newEngineNamespace() *v1.Namespace {
73+
return &v1.Namespace{
8074
ObjectMeta: metav1.ObjectMeta{
81-
Name: "hbase-worker",
82-
Namespace: "fluid",
75+
Name: engineTestNamespace,
8376
},
8477
}
85-
testObjs = append(testObjs, sts.DeepCopy())
86-
client := fake.NewFakeClientWithScheme(testScheme, testObjs...)
78+
}
8779

88-
var ctx = cruntime.ReconcileRequestContext{
89-
NamespacedName: types.NamespacedName{
90-
Name: "hbase",
91-
Namespace: "fluid",
80+
func newEngineDataset() *datav1alpha1.Dataset {
81+
return &datav1alpha1.Dataset{
82+
ObjectMeta: metav1.ObjectMeta{
83+
Name: engineTestName,
84+
Namespace: engineTestNamespace,
9285
},
93-
Client: client,
94-
Log: fake.NullLogger(),
95-
RuntimeType: common.EFCRuntime,
96-
Runtime: &runtime,
97-
}
98-
99-
engine, err := Build("testId", ctx)
100-
if err != nil || engine == nil {
101-
t.Errorf("fail to exec the build function with the eror %v", err)
10286
}
87+
}
10388

104-
var errCtx = cruntime.ReconcileRequestContext{
105-
NamespacedName: types.NamespacedName{
106-
Name: "hbase",
107-
Namespace: "fluid",
89+
func newEngineWorkerStatefulSet() *appsv1.StatefulSet {
90+
return &appsv1.StatefulSet{
91+
ObjectMeta: metav1.ObjectMeta{
92+
Name: engineTestName + "-worker",
93+
Namespace: engineTestNamespace,
10894
},
109-
Client: client,
110-
Log: fake.NullLogger(),
111-
RuntimeType: common.EFCRuntime,
112-
Runtime: nil,
11395
}
96+
}
11497

115-
got, err := Build("testId", errCtx)
116-
if err == nil {
117-
t.Errorf("expect err, but no err got %v", got)
98+
func newEngineContext(clientObjs ...runtime.Object) cruntime.ReconcileRequestContext {
99+
runtime := newEngineRuntime()
100+
if len(clientObjs) == 0 {
101+
clientObjs = append(
102+
clientObjs,
103+
newEngineNamespace().DeepCopy(),
104+
newEngineDataset().DeepCopy(),
105+
runtime.DeepCopy(),
106+
newEngineWorkerStatefulSet().DeepCopy(),
107+
)
118108
}
119109

120-
var errCtx2 = cruntime.ReconcileRequestContext{
110+
return cruntime.ReconcileRequestContext{
121111
NamespacedName: types.NamespacedName{
122-
Name: "hbase2",
123-
Namespace: "fluid",
112+
Name: engineTestName,
113+
Namespace: engineTestNamespace,
124114
},
125-
Client: client,
115+
Client: fake.NewFakeClientWithScheme(testScheme, clientObjs...),
126116
Log: fake.NullLogger(),
127117
RuntimeType: common.EFCRuntime,
128-
Runtime: &runtime,
118+
Runtime: runtime,
129119
}
120+
}
130121

131-
got2, err2 := Build("testId", errCtx2)
132-
if err2 == nil {
133-
t.Errorf("expect err, but no err got %v", got2)
134-
}
122+
var _ = Describe("EFCEngine", func() {
123+
Describe("Build", func() {
124+
It("seeds the legacy default client objects used by the original build test", func() {
125+
ctx := newEngineContext()
135126

136-
var errCtx3 = cruntime.ReconcileRequestContext{
137-
NamespacedName: types.NamespacedName{
138-
Name: "hbase",
139-
Namespace: "fluid",
140-
},
141-
Client: client,
142-
Log: fake.NullLogger(),
143-
RuntimeType: common.EFCRuntime,
144-
Runtime: &datav1alpha1.JindoRuntime{},
145-
}
127+
namespace := &v1.Namespace{}
128+
Expect(ctx.Client.Get(context.TODO(), types.NamespacedName{Name: engineTestNamespace}, namespace)).To(Succeed())
146129

147-
got3, err3 := Build("testId", errCtx3)
148-
if err3 == nil {
149-
t.Errorf("expect err, but no err got %v", got3)
150-
}
151-
}
130+
dataset := &datav1alpha1.Dataset{}
131+
Expect(ctx.Client.Get(context.TODO(), types.NamespacedName{Name: engineTestName, Namespace: engineTestNamespace}, dataset)).To(Succeed())
132+
133+
runtimeObj := &datav1alpha1.EFCRuntime{}
134+
Expect(ctx.Client.Get(context.TODO(), types.NamespacedName{Name: engineTestName, Namespace: engineTestNamespace}, runtimeObj)).To(Succeed())
135+
136+
worker := &appsv1.StatefulSet{}
137+
Expect(ctx.Client.Get(context.TODO(), types.NamespacedName{Name: engineTestName + "-worker", Namespace: engineTestNamespace}, worker)).To(Succeed())
138+
})
139+
140+
It("builds an engine when the runtime can be resolved", func() {
141+
engine, err := Build(engineTestID, newEngineContext())
142+
143+
Expect(err).NotTo(HaveOccurred())
144+
Expect(engine).NotTo(BeNil())
145+
})
146+
147+
DescribeTable("returns an error for invalid runtime input",
148+
func(runtimeObj client.Object, expectedError string) {
149+
ctx := newEngineContext()
150+
ctx.Runtime = runtimeObj
151+
152+
engine, err := Build(engineTestID, ctx)
153+
154+
Expect(err).To(HaveOccurred())
155+
Expect(err.Error()).To(Equal(expectedError))
156+
Expect(engine).To(BeNil())
157+
},
158+
Entry("when runtime is nil", nil, "engine hbase is failed to parse"),
159+
Entry("when runtime has the wrong type", &datav1alpha1.JindoRuntime{}, "engine hbase is failed to parse"),
160+
)
161+
162+
It("returns an error when runtime info cannot be resolved", func() {
163+
ctx := newEngineContext()
164+
ctx.Client = fake.NewFakeClientWithScheme(testScheme)
165+
166+
engine, err := Build(engineTestID, ctx)
167+
168+
Expect(err).To(HaveOccurred())
169+
Expect(err.Error()).To(Equal("engine hbase failed to get runtime info"))
170+
Expect(engine).To(BeNil())
171+
})
172+
})
173+
174+
DescribeTable("Precheck",
175+
func(clientObjs []runtime.Object, expectedFound bool) {
176+
found, err := Precheck(
177+
fake.NewFakeClientWithScheme(testScheme, clientObjs...),
178+
types.NamespacedName{Name: engineTestName, Namespace: engineTestNamespace},
179+
)
180+
181+
Expect(err).NotTo(HaveOccurred())
182+
Expect(found).To(Equal(expectedFound))
183+
},
184+
Entry("returns true when the runtime exists", []runtime.Object{newEngineRuntime().DeepCopy()}, true),
185+
Entry("returns false when the runtime does not exist", []runtime.Object{}, false),
186+
)
187+
})

0 commit comments

Comments
 (0)