@@ -14,7 +14,8 @@ limitations under the License.
1414package vineyard
1515
1616import (
17- "testing"
17+ . "github.com/onsi/ginkgo/v2"
18+ . "github.com/onsi/gomega"
1819
1920 datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2021 "github.com/fluid-cloudnative/fluid/pkg/common"
@@ -23,31 +24,137 @@ import (
2324 appsv1 "k8s.io/api/apps/v1"
2425 v1 "k8s.io/api/core/v1"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26- "k8s.io/apimachinery/pkg/runtime"
27+ k8sruntime "k8s.io/apimachinery/pkg/runtime"
2728 "k8s.io/apimachinery/pkg/types"
29+ "sigs.k8s.io/controller-runtime/pkg/client"
2830)
2931
30- func TestBuild (t * testing.T ) {
31- var namespace = v1.Namespace {
32- ObjectMeta : metav1.ObjectMeta {
33- Name : "fluid" ,
32+ const (
33+ engineTestNamespace = "fluid"
34+ engineTestName = "vineyard"
35+ engineTestID = "testId"
36+ )
37+
38+ var _ = Describe ("VineyardEngine" , Label ("vineyard" ), func () {
39+ Describe ("Build" , func () {
40+ Context ("when runtime is a valid VineyardRuntime" , func () {
41+ It ("should build the template engine" , func () {
42+ vineyardRuntime := newEngineTestVineyardRuntime ()
43+ fakeClient := fake .NewFakeClientWithScheme (testScheme , newEngineTestObjects (vineyardRuntime )... )
44+
45+ engine , err := Build (engineTestID , newEngineTestContext (fakeClient , vineyardRuntime ))
46+
47+ Expect (err ).NotTo (HaveOccurred ())
48+ Expect (engine ).NotTo (BeNil ())
49+ })
50+ })
51+
52+ Context ("when runtime is nil" , func () {
53+ It ("should return a parse error" , func () {
54+ fakeClient := fake .NewFakeClientWithScheme (testScheme )
55+
56+ engine , err := Build (engineTestID , newEngineTestContext (fakeClient , nil ))
57+
58+ Expect (err ).To (HaveOccurred ())
59+ Expect (err .Error ()).To (Equal ("engine vineyard is failed to parse" ))
60+ Expect (engine ).To (BeNil ())
61+ })
62+ })
63+
64+ Context ("when runtime has the wrong type" , func () {
65+ It ("should return a parse error" , func () {
66+ dataset := newEngineTestDataset ()
67+ fakeClient := fake .NewFakeClientWithScheme (testScheme )
68+
69+ engine , err := Build (engineTestID , newEngineTestContext (fakeClient , dataset ))
70+
71+ Expect (err ).To (HaveOccurred ())
72+ Expect (err .Error ()).To (Equal ("engine vineyard is failed to parse" ))
73+ Expect (engine ).To (BeNil ())
74+ })
75+ })
76+
77+ Context ("when runtime info cannot be loaded" , func () {
78+ It ("should return a runtime info error" , func () {
79+ vineyardRuntime := newEngineTestVineyardRuntime ()
80+ fakeClient := fake .NewFakeClientWithScheme (testScheme )
81+
82+ engine , err := Build (engineTestID , newEngineTestContext (fakeClient , vineyardRuntime ))
83+
84+ Expect (err ).To (HaveOccurred ())
85+ Expect (err .Error ()).To (ContainSubstring ("engine vineyard failed to get runtime info" ))
86+ Expect (engine ).To (BeNil ())
87+ })
88+ })
89+ })
90+
91+ Describe ("Precheck" , func () {
92+ Context ("when the VineyardRuntime exists" , func () {
93+ It ("should return found true without error" , func () {
94+ vineyardRuntime := newEngineTestVineyardRuntime ()
95+ fakeClient := fake .NewFakeClientWithScheme (testScheme , vineyardRuntime )
96+
97+ found , err := Precheck (fakeClient , types.NamespacedName {Name : engineTestName , Namespace : engineTestNamespace })
98+
99+ Expect (err ).NotTo (HaveOccurred ())
100+ Expect (found ).To (BeTrue ())
101+ })
102+ })
103+
104+ Context ("when the VineyardRuntime does not exist" , func () {
105+ It ("should return found false without error" , func () {
106+ fakeClient := fake .NewFakeClientWithScheme (testScheme )
107+
108+ found , err := Precheck (fakeClient , types.NamespacedName {Name : engineTestName , Namespace : engineTestNamespace })
109+
110+ Expect (err ).NotTo (HaveOccurred ())
111+ Expect (found ).To (BeFalse ())
112+ })
113+ })
114+ })
115+ })
116+
117+ func newEngineTestContext (fakeClient client.Client , runtime client.Object ) cruntime.ReconcileRequestContext {
118+ return cruntime.ReconcileRequestContext {
119+ NamespacedName : types.NamespacedName {
120+ Name : engineTestName ,
121+ Namespace : engineTestNamespace ,
34122 },
123+ Client : fakeClient ,
124+ Log : fake .NullLogger (),
125+ RuntimeType : "vineyard" ,
126+ Runtime : runtime ,
35127 }
36- testObjs := []runtime.Object {}
37- testObjs = append (testObjs , namespace .DeepCopy ())
128+ }
38129
39- var dataset = datav1alpha1.Dataset {
130+ func newEngineTestObjects (vineyardRuntime * datav1alpha1.VineyardRuntime ) []k8sruntime.Object {
131+ return []k8sruntime.Object {
132+ & v1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : engineTestNamespace }},
133+ newEngineTestDataset (),
134+ vineyardRuntime ,
135+ & appsv1.DaemonSet {
136+ ObjectMeta : metav1.ObjectMeta {
137+ Name : engineTestName + "-worker" ,
138+ Namespace : engineTestNamespace ,
139+ },
140+ },
141+ }
142+ }
143+
144+ func newEngineTestDataset () * datav1alpha1.Dataset {
145+ return & datav1alpha1.Dataset {
40146 ObjectMeta : metav1.ObjectMeta {
41- Name : "hbase" ,
42- Namespace : "fluid" ,
147+ Name : engineTestName ,
148+ Namespace : engineTestNamespace ,
43149 },
44150 }
45- testObjs = append ( testObjs , dataset . DeepCopy ())
151+ }
46152
47- var runtime = datav1alpha1.VineyardRuntime {
153+ func newEngineTestVineyardRuntime () * datav1alpha1.VineyardRuntime {
154+ return & datav1alpha1.VineyardRuntime {
48155 ObjectMeta : metav1.ObjectMeta {
49- Name : "hbase" ,
50- Namespace : "fluid" ,
156+ Name : engineTestName ,
157+ Namespace : engineTestNamespace ,
51158 },
52159 Spec : datav1alpha1.VineyardRuntimeSpec {
53160 Master : datav1alpha1.MasterSpec {
@@ -62,31 +169,4 @@ func TestBuild(t *testing.T) {
62169 },
63170 },
64171 }
65- testObjs = append (testObjs , runtime .DeepCopy ())
66-
67- var daemonset = appsv1.DaemonSet {
68- ObjectMeta : metav1.ObjectMeta {
69- Name : "hbase-worker" ,
70- Namespace : "fluid" ,
71- },
72- }
73- testObjs = append (testObjs , daemonset .DeepCopy ())
74- client := fake .NewFakeClientWithScheme (testScheme , testObjs ... )
75-
76- var ctx = cruntime.ReconcileRequestContext {
77- NamespacedName : types.NamespacedName {
78- Name : "hbase" ,
79- Namespace : "fluid" ,
80- },
81- Client : client ,
82- Log : fake .NullLogger (),
83- RuntimeType : "vineyard" ,
84- Runtime : & runtime ,
85- }
86-
87- engine , err := Build ("testId" , ctx )
88- if err != nil || engine == nil {
89- t .Errorf ("fail to exec the build function with the eror %v" , err )
90- }
91-
92172}
0 commit comments