@@ -17,120 +17,169 @@ limitations under the License.
1717package juicefs
1818
1919import (
20- "testing "
20+ base "github.com/fluid-cloudnative/fluid/pkg/ddc/base "
2121
2222 datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
23- "github.com/fluid-cloudnative/fluid/pkg/common"
2423 cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
2524 "github.com/fluid-cloudnative/fluid/pkg/utils/fake"
26- appsv1 "k8s.io/api/apps/v1 "
27- v1 "k8s.io/api/core/v1 "
25+ . "github.com/onsi/ginkgo/v2 "
26+ . "github.com/onsi/gomega "
2827 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29- "k8s.io/apimachinery/pkg/runtime"
3028 "k8s.io/apimachinery/pkg/types"
3129)
3230
33- func TestBuild (t * testing.T ) {
34- var namespace = v1.Namespace {
35- ObjectMeta : metav1.ObjectMeta {
36- Name : "fluid" ,
37- },
38- }
39- testObjs := []runtime.Object {}
40- testObjs = append (testObjs , namespace .DeepCopy ())
41-
42- var dataset = datav1alpha1.Dataset {
43- ObjectMeta : metav1.ObjectMeta {
44- Name : "hbase" ,
45- Namespace : "fluid" ,
46- },
47- }
48- testObjs = append (testObjs , dataset .DeepCopy ())
49-
50- var runtime = datav1alpha1.JuiceFSRuntime {
51- ObjectMeta : metav1.ObjectMeta {
52- Name : "hbase" ,
53- Namespace : "fluid" ,
54- },
55- Spec : datav1alpha1.JuiceFSRuntimeSpec {
56- Fuse : datav1alpha1.JuiceFSFuseSpec {},
57- },
58- Status : datav1alpha1.RuntimeStatus {
59- CacheStates : map [common.CacheStateName ]string {
60- common .Cached : "true" ,
31+ var _ = Describe ("JuiceFS engine" , func () {
32+ const (
33+ runtimeName = "hbase"
34+ runtimeNamespace = "fluid"
35+ )
36+
37+ var (
38+ key types.NamespacedName
39+ runtime * datav1alpha1.JuiceFSRuntime
40+ )
41+
42+ BeforeEach (func () {
43+ key = types.NamespacedName {
44+ Name : runtimeName ,
45+ Namespace : runtimeNamespace ,
46+ }
47+ runtime = & datav1alpha1.JuiceFSRuntime {
48+ ObjectMeta : metav1.ObjectMeta {
49+ Name : key .Name ,
50+ Namespace : key .Namespace ,
6151 },
62- },
63- }
64- testObjs = append (testObjs , runtime .DeepCopy ())
65- var runtime2 = datav1alpha1.JuiceFSRuntime {
66- ObjectMeta : metav1.ObjectMeta {
67- Name : "test" ,
68- Namespace : "fluid" ,
69- },
70- Spec : datav1alpha1.JuiceFSRuntimeSpec {
71- Fuse : datav1alpha1.JuiceFSFuseSpec {},
72- },
73- Status : datav1alpha1.RuntimeStatus {
74- CacheStates : map [common.CacheStateName ]string {
75- common .Cached : "true" ,
76- },
77- },
78- }
79-
80- var sts = appsv1.StatefulSet {
81- ObjectMeta : metav1.ObjectMeta {
82- Name : "hbase-worker" ,
83- Namespace : "fluid" ,
84- },
85- }
86- testObjs = append (testObjs , sts .DeepCopy ())
87- client := fake .NewFakeClientWithScheme (testScheme , testObjs ... )
88-
89- var ctx = cruntime.ReconcileRequestContext {
90- NamespacedName : types.NamespacedName {
91- Name : "hbase" ,
92- Namespace : "fluid" ,
93- },
94- Client : client ,
95- Log : fake .NullLogger (),
96- RuntimeType : "juicefs" ,
97- Runtime : & runtime ,
98- }
99-
100- engine , err := Build ("testId" , ctx )
101- if err != nil || engine == nil {
102- t .Errorf ("fail to exec the build function with the eror %v" , err )
103- }
104-
105- var errCtx = cruntime.ReconcileRequestContext {
106- NamespacedName : types.NamespacedName {
107- Name : "hbase" ,
108- Namespace : "fluid" ,
109- },
110- Client : client ,
111- Log : fake .NullLogger (),
112- RuntimeType : "juicefs" ,
113- Runtime : nil ,
114- }
115-
116- got , err := Build ("testId" , errCtx )
117- if err == nil {
118- t .Errorf ("expect err, but no err got %v" , got )
119- }
120-
121- var errrCtx = cruntime.ReconcileRequestContext {
122- NamespacedName : types.NamespacedName {
123- Name : "test" ,
124- Namespace : "fluid" ,
125- },
126- Client : client ,
127- Log : fake .NullLogger (),
128- RuntimeType : "juicefs" ,
129- Runtime : & runtime2 ,
130- }
131-
132- gott , err := Build ("testId" , errrCtx )
133- if err == nil {
134- t .Errorf ("expect err, but no err got %v" , gott )
135- }
136- }
52+ }
53+ })
54+
55+ Describe ("Build" , func () {
56+ When ("the runtime exists and the context runtime is valid" , func () {
57+ It ("builds a template engine" , func () {
58+ ctx := cruntime.ReconcileRequestContext {
59+ NamespacedName : key ,
60+ Client : fake .NewFakeClientWithScheme (testScheme , runtime .DeepCopy ()),
61+ Log : fake .NullLogger (),
62+ RuntimeType : "juicefs" ,
63+ Runtime : runtime .DeepCopy (),
64+ }
65+
66+ engine , err := Build ("test-id" , ctx )
67+
68+ Expect (err ).NotTo (HaveOccurred ())
69+ Expect (engine ).NotTo (BeNil ())
70+ Expect (engine ).To (BeAssignableToTypeOf (& base.TemplateEngine {}))
71+ })
72+ })
73+
74+ When ("the context runtime is nil" , func () {
75+ It ("returns a parse error" , func () {
76+ ctx := cruntime.ReconcileRequestContext {
77+ NamespacedName : key ,
78+ Client : fake .NewFakeClientWithScheme (testScheme , runtime .DeepCopy ()),
79+ Log : fake .NullLogger (),
80+ RuntimeType : "juicefs" ,
81+ }
82+
83+ engine , err := Build ("test-id" , ctx )
84+
85+ Expect (err ).To (HaveOccurred ())
86+ Expect (err .Error ()).To (ContainSubstring ("failed to parse" ))
87+ Expect (engine ).To (BeNil ())
88+ })
89+ })
90+
91+ When ("the context runtime has the wrong concrete type" , func () {
92+ It ("returns a parse error" , func () {
93+ ctx := cruntime.ReconcileRequestContext {
94+ NamespacedName : key ,
95+ Client : fake .NewFakeClientWithScheme (testScheme , runtime .DeepCopy ()),
96+ Log : fake .NullLogger (),
97+ RuntimeType : "juicefs" ,
98+ Runtime : & datav1alpha1.AlluxioRuntime {
99+ ObjectMeta : metav1.ObjectMeta {
100+ Name : key .Name ,
101+ Namespace : key .Namespace ,
102+ },
103+ },
104+ }
105+
106+ engine , err := Build ("test-id" , ctx )
107+
108+ Expect (err ).To (HaveOccurred ())
109+ Expect (err .Error ()).To (ContainSubstring ("failed to parse" ))
110+ Expect (engine ).To (BeNil ())
111+ })
112+ })
113+
114+ When ("runtime info cannot be loaded from the client" , func () {
115+ It ("returns a runtime info error" , func () {
116+ ctx := cruntime.ReconcileRequestContext {
117+ NamespacedName : key ,
118+ Client : fake .NewFakeClientWithScheme (testScheme ),
119+ Log : fake .NullLogger (),
120+ RuntimeType : "juicefs" ,
121+ Runtime : runtime .DeepCopy (),
122+ }
123+
124+ engine , err := Build ("test-id" , ctx )
125+
126+ Expect (err ).To (HaveOccurred ())
127+ Expect (err .Error ()).To (ContainSubstring ("failed to get runtime info" ))
128+ Expect (engine ).To (BeNil ())
129+ })
130+ })
131+
132+ When ("the context runtime is runtime2 named test but the client is missing that runtime" , func () {
133+ It ("returns a runtime info error for the old runtime2 build scenario" , func () {
134+ runtime2Key := types.NamespacedName {
135+ Name : "test" ,
136+ Namespace : runtimeNamespace ,
137+ }
138+ runtime2 := & datav1alpha1.JuiceFSRuntime {
139+ ObjectMeta : metav1.ObjectMeta {
140+ Name : runtime2Key .Name ,
141+ Namespace : runtime2Key .Namespace ,
142+ },
143+ }
144+
145+ ctx := cruntime.ReconcileRequestContext {
146+ NamespacedName : runtime2Key ,
147+ Client : fake .NewFakeClientWithScheme (testScheme , runtime .DeepCopy ()),
148+ Log : fake .NullLogger (),
149+ RuntimeType : "juicefs" ,
150+ Runtime : runtime2 ,
151+ }
152+
153+ engine , err := Build ("test-id" , ctx )
154+
155+ Expect (err ).To (HaveOccurred ())
156+ Expect (err .Error ()).To (ContainSubstring ("failed to get runtime info" ))
157+ Expect (engine ).To (BeNil ())
158+ })
159+ })
160+ })
161+
162+ Describe ("Precheck" , func () {
163+ When ("the runtime exists" , func () {
164+ It ("returns found" , func () {
165+ client := fake .NewFakeClientWithScheme (testScheme , runtime .DeepCopy ())
166+
167+ found , err := Precheck (client , key )
168+
169+ Expect (err ).NotTo (HaveOccurred ())
170+ Expect (found ).To (BeTrue ())
171+ })
172+ })
173+
174+ When ("the runtime does not exist" , func () {
175+ It ("returns not found without error" , func () {
176+ client := fake .NewFakeClientWithScheme (testScheme )
177+
178+ found , err := Precheck (client , key )
179+
180+ Expect (err ).NotTo (HaveOccurred ())
181+ Expect (found ).To (BeFalse ())
182+ })
183+ })
184+ })
185+ })
0 commit comments