Skip to content

Commit 7269b08

Browse files
authored
test(base): migrate runtime_test.go to Ginkgo/Gomega (#5602)
* test(base): migrate runtime_test.go to Ginkgo/Gomega Signed-off-by: Harsh <harshmastic@gmail.com> * fix: address Copilot review comments - Add base_internal_suite_test.go for package base tests - Use DeferCleanup with proper env var save/restore - Fix os.Setenv to preserve previous values Signed-off-by: Harsh <harshmastic@gmail.com> * fix: resolve dual Ginkgo suite conflict and flaky test - Remove base_internal_suite_test.go (duplicate suite causing 'RunSpecs more than once') - Convert base_suite_test.go to internal package (package base) - Update runtime_conventions_test.go to use internal package - Fix flaky PermitSync test by setting explicit syncRetryDuration before assertions Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 3d05929 commit 7269b08

3 files changed

Lines changed: 124 additions & 111 deletions

File tree

pkg/ddc/base/base_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package base_test
17+
package base
1818

1919
import (
2020
"testing"

pkg/ddc/base/runtime_conventions_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package base_test
17+
package base
1818

1919
import (
2020
"github.com/fluid-cloudnative/fluid/pkg/common"
21-
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
2221
. "github.com/onsi/ginkgo/v2"
2322
. "github.com/onsi/gomega"
2423
)
@@ -28,7 +27,7 @@ const testNamespace = "default"
2827
var _ = Describe("RuntimeInfo.GetWorkerStatefulsetName", func() {
2928
DescribeTable("returns correct statefulset name",
3029
func(runtimeName, runtimeType, suffix string) {
31-
info, err := base.BuildRuntimeInfo(runtimeName, testNamespace, runtimeType)
30+
info, err := BuildRuntimeInfo(runtimeName, testNamespace, runtimeType)
3231
Expect(err).NotTo(HaveOccurred())
3332
Expect(info.GetWorkerStatefulsetName()).To(Equal(runtimeName + suffix))
3433
},

pkg/ddc/base/runtime_test.go

Lines changed: 121 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ limitations under the License.
1717
package base
1818

1919
import (
20-
"reflect"
21-
"testing"
20+
"os"
2221
"time"
2322

23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
2426
"github.com/fluid-cloudnative/fluid/api/v1alpha1"
2527
"github.com/fluid-cloudnative/fluid/pkg/common"
2628
cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
@@ -34,7 +36,7 @@ import (
3436
"sigs.k8s.io/controller-runtime/pkg/client"
3537
)
3638

37-
func Test_convertToTieredstoreInfo(t *testing.T) {
39+
var _ = Describe("convertToTieredstoreInfo", func() {
3840
type args struct {
3941
tieredstore v1alpha1.TieredStore
4042
}
@@ -122,20 +124,19 @@ func Test_convertToTieredstoreInfo(t *testing.T) {
122124
},
123125
}
124126
for _, tt := range tests {
125-
t.Run(tt.name, func(t *testing.T) {
127+
It(tt.name, func() {
126128
got, err := convertToTieredstoreInfo(tt.args.tieredstore)
127-
if (err != nil) != tt.wantErr {
128-
t.Errorf("convertToTieredstoreInfo() error = %v, wantErr %v", err, tt.wantErr)
129-
return
130-
}
131-
if !reflect.DeepEqual(got, tt.want) {
132-
t.Errorf("convertToTieredstoreInfo() got = %v, want %v", got, tt.want)
129+
if tt.wantErr {
130+
Expect(err).To(HaveOccurred())
131+
} else {
132+
Expect(err).NotTo(HaveOccurred())
133+
Expect(got).To(Equal(tt.want))
133134
}
134135
})
135136
}
136-
}
137+
})
137138

138-
func TestBuildRuntimeInfo(t *testing.T) {
139+
var _ = Describe("BuildRuntimeInfo", func() {
139140
const runtimetype = "alluxio"
140141
type args struct {
141142
name string
@@ -191,30 +192,26 @@ func TestBuildRuntimeInfo(t *testing.T) {
191192
},
192193
}
193194
for _, tt := range tests {
194-
t.Run(tt.name, func(t *testing.T) {
195+
It(tt.name, func() {
195196
gotRuntime, err := BuildRuntimeInfo(tt.args.name, tt.args.namespace, tt.args.runtimeType, WithTieredStore(tt.args.tieredstore))
196-
if (err != nil) != tt.wantErr {
197-
t.Errorf("BuildRuntimeInfo() error = %v, wantErr %v", err, tt.wantErr)
198-
return
199-
}
200-
201-
tieredstoreInfo, err := convertToTieredstoreInfo(tieredstore)
202-
if (err != nil) != tt.wantErr {
203-
t.Errorf("convertToTieredstoreInfo() error = %v, wantErr %v", err, tt.wantErr)
197+
if tt.wantErr {
198+
Expect(err).To(HaveOccurred())
199+
} else {
200+
Expect(err).NotTo(HaveOccurred())
201+
202+
tieredstoreInfo, err := convertToTieredstoreInfo(tieredstore)
203+
Expect(err).NotTo(HaveOccurred())
204+
205+
Expect(gotRuntime.GetName()).To(Equal(tt.wantRuntime.GetName()))
206+
Expect(gotRuntime.GetNamespace()).To(Equal(tt.wantRuntime.GetNamespace()))
207+
Expect(gotRuntime.GetRuntimeType()).To(Equal(tt.wantRuntime.GetRuntimeType()))
208+
Expect(gotRuntime.GetTieredStoreInfo()).To(Equal(tieredstoreInfo))
204209
}
205-
206-
if gotRuntime.GetName() != tt.wantRuntime.GetName() ||
207-
gotRuntime.GetNamespace() != tt.wantRuntime.GetNamespace() ||
208-
gotRuntime.GetRuntimeType() != tt.wantRuntime.GetRuntimeType() ||
209-
!reflect.DeepEqual(gotRuntime.GetTieredStoreInfo(), tieredstoreInfo) {
210-
t.Errorf("BuildRuntimeInfo() gotRuntime = %v, want %v", gotRuntime, tt.wantRuntime)
211-
}
212-
213210
})
214211
}
215-
}
212+
})
216213

217-
func TestCleanPolicy(t *testing.T) {
214+
var _ = Describe("CleanPolicy", func() {
218215
s := runtime.NewScheme()
219216

220217
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.AlluxioRuntime{})
@@ -793,21 +790,20 @@ func TestCleanPolicy(t *testing.T) {
793790
}
794791

795792
for _, tt := range tests {
796-
t.Run(tt.name, func(t *testing.T) {
793+
It(tt.name, func() {
797794
// SetupFuseCleanPolicy will be called in GetRuntimeInfo()
798795
got, err := GetRuntimeInfo(tt.args.client, tt.args.name, tt.args.namespace)
799-
if (err != nil) != tt.wantErr {
800-
t.Errorf("GetRuntimeInfo() error = %v, wantErr %v", err, tt.wantErr)
801-
return
802-
}
803-
if !tt.wantErr && !reflect.DeepEqual(got.GetFuseCleanPolicy(), tt.want.GetFuseCleanPolicy()) {
804-
t.Errorf("GetRuntimeInfo() = %#v, want %#v", got, tt.want)
796+
if tt.wantErr {
797+
Expect(err).To(HaveOccurred())
798+
} else {
799+
Expect(err).NotTo(HaveOccurred())
800+
Expect(got.GetFuseCleanPolicy()).To(Equal(tt.want.GetFuseCleanPolicy()))
805801
}
806802
})
807803
}
808-
}
804+
})
809805

810-
func TestGetRuntimeInfo(t *testing.T) {
806+
var _ = Describe("GetRuntimeInfo", func() {
811807
s := runtime.NewScheme()
812808

813809
alluxioRuntime := v1alpha1.AlluxioRuntime{
@@ -1142,11 +1138,12 @@ func TestGetRuntimeInfo(t *testing.T) {
11421138
},
11431139
}
11441140
for _, tt := range tests {
1145-
t.Run(tt.name, func(t *testing.T) {
1141+
It(tt.name, func() {
11461142
got, err := GetRuntimeInfo(tt.args.client, tt.args.name, tt.args.namespace)
1147-
if (err != nil) != tt.wantErr {
1148-
t.Errorf("GetRuntimeInfo() error = %v, wantErr %v", err, tt.wantErr)
1149-
return
1143+
if tt.wantErr {
1144+
Expect(err).To(HaveOccurred())
1145+
} else {
1146+
Expect(err).NotTo(HaveOccurred())
11501147
}
11511148
if got != nil {
11521149
got.SetAPIReader(nil)
@@ -1156,14 +1153,14 @@ func TestGetRuntimeInfo(t *testing.T) {
11561153
tt.want.SetAPIReader(nil)
11571154
}
11581155

1159-
if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
1160-
t.Errorf("GetRuntimeInfo() = %#v\n, want %#v", got, tt.want)
1156+
if !tt.wantErr {
1157+
Expect(got).To(Equal(tt.want))
11611158
}
11621159
})
11631160
}
1164-
}
1161+
})
11651162

1166-
func TestGetRuntimeStatus(t *testing.T) {
1163+
var _ = Describe("GetRuntimeStatus", func() {
11671164
s := runtime.NewScheme()
11681165

11691166
alluxioRuntime := v1alpha1.AlluxioRuntime{
@@ -1373,68 +1370,85 @@ func TestGetRuntimeStatus(t *testing.T) {
13731370
},
13741371
}
13751372
for _, tt := range tests {
1376-
t.Run(tt.name, func(t *testing.T) {
1373+
It(tt.name, func() {
13771374
_, err := GetRuntimeStatus(tt.args.client, tt.args.runtimeType, tt.args.name, tt.args.namespace)
1378-
if (err != nil) != tt.wantErr {
1379-
t.Errorf("GetRuntimeInfo() error = %v, wantErr %v", err, tt.wantErr)
1380-
return
1375+
if tt.wantErr {
1376+
Expect(err).To(HaveOccurred())
1377+
} else {
1378+
Expect(err).NotTo(HaveOccurred())
13811379
}
13821380
})
13831381
}
1384-
}
1385-
1386-
func TestGetSyncRetryDuration(t *testing.T) {
1387-
1388-
_, err := getSyncRetryDuration()
1389-
if err != nil {
1390-
t.Errorf("Failed to getSyncRetryDuration %v", err)
1391-
}
1392-
1393-
t.Setenv(syncRetryDurationEnv, "s")
1394-
_, err = getSyncRetryDuration()
1395-
if err == nil {
1396-
t.Errorf("Expect to get err, but got nil")
1397-
}
1398-
1399-
t.Setenv(syncRetryDurationEnv, "3s")
1400-
d, err := getSyncRetryDuration()
1401-
if err != nil {
1402-
t.Errorf("Failed to getSyncRetryDuration %v", err)
1403-
}
1404-
if d == nil {
1405-
t.Errorf("Failed to set the duration, expect %v, got %v", time.Duration(3*time.Second), d)
1406-
}
1407-
}
1408-
1409-
func TestPermitSync(t *testing.T) {
1410-
1411-
id := "test id"
1412-
ctx := cruntime.ReconcileRequestContext{
1413-
NamespacedName: types.NamespacedName{
1414-
Name: "hbase",
1415-
Namespace: "fluid",
1416-
},
1417-
Log: fakeutils.NullLogger(),
1418-
Runtime: &v1alpha1.AlluxioRuntime{},
1419-
}
1420-
1421-
templateEngine := NewTemplateEngine(nil, id, ctx)
1422-
permit := templateEngine.permitSync(types.NamespacedName{Namespace: ctx.Namespace, Name: ctx.Namespace})
1423-
if !permit {
1424-
t.Errorf("expect permit, but got %v", permit)
1425-
}
1382+
})
1383+
1384+
var _ = Describe("GetSyncRetryDuration", func() {
1385+
It("should get default sync retry duration", func() {
1386+
_, err := getSyncRetryDuration()
1387+
Expect(err).NotTo(HaveOccurred())
1388+
})
1389+
1390+
It("should fail with invalid duration format", func() {
1391+
oldVal, wasSet := os.LookupEnv(syncRetryDurationEnv)
1392+
err := os.Setenv(syncRetryDurationEnv, "s")
1393+
Expect(err).NotTo(HaveOccurred())
1394+
DeferCleanup(func() {
1395+
if wasSet {
1396+
_ = os.Setenv(syncRetryDurationEnv, oldVal)
1397+
} else {
1398+
_ = os.Unsetenv(syncRetryDurationEnv)
1399+
}
1400+
})
14261401

1427-
templateEngine.setTimeOfLastSync()
1428-
permit = templateEngine.permitSync(types.NamespacedName{Namespace: ctx.Namespace, Name: ctx.Namespace})
1429-
if permit {
1430-
t.Errorf("expect not permit, but got %v", permit)
1431-
}
1402+
_, err = getSyncRetryDuration()
1403+
Expect(err).To(HaveOccurred())
1404+
})
1405+
1406+
It("should successfully parse valid duration", func() {
1407+
oldVal, wasSet := os.LookupEnv(syncRetryDurationEnv)
1408+
err := os.Setenv(syncRetryDurationEnv, "3s")
1409+
Expect(err).NotTo(HaveOccurred())
1410+
DeferCleanup(func() {
1411+
if wasSet {
1412+
_ = os.Setenv(syncRetryDurationEnv, oldVal)
1413+
} else {
1414+
_ = os.Unsetenv(syncRetryDurationEnv)
1415+
}
1416+
})
14321417

1433-
templateEngine.setTimeOfLastSync()
1434-
templateEngine.syncRetryDuration = 1 * time.Microsecond
1435-
time.Sleep(1 * time.Second)
1436-
permit = templateEngine.permitSync(types.NamespacedName{Namespace: ctx.Namespace, Name: ctx.Namespace})
1437-
if !permit {
1438-
t.Errorf("expect permit, but got %v", permit)
1439-
}
1440-
}
1418+
d, err := getSyncRetryDuration()
1419+
Expect(err).NotTo(HaveOccurred())
1420+
Expect(d).NotTo(BeNil())
1421+
Expect(*d).To(Equal(time.Duration(3 * time.Second)))
1422+
})
1423+
})
1424+
1425+
var _ = Describe("PermitSync", func() {
1426+
It("should control sync permission based on time", func() {
1427+
id := "test id"
1428+
ctx := cruntime.ReconcileRequestContext{
1429+
NamespacedName: types.NamespacedName{
1430+
Name: "hbase",
1431+
Namespace: "fluid",
1432+
},
1433+
Log: fakeutils.NullLogger(),
1434+
Runtime: &v1alpha1.AlluxioRuntime{},
1435+
}
1436+
1437+
templateEngine := NewTemplateEngine(nil, id, ctx)
1438+
permit := templateEngine.permitSync(ctx.NamespacedName)
1439+
Expect(permit).To(BeTrue(), "expect permit initially")
1440+
1441+
// Set a long syncRetryDuration to ensure the test is not flaky
1442+
templateEngine.syncRetryDuration = 1 * time.Hour
1443+
templateEngine.setTimeOfLastSync()
1444+
permit = templateEngine.permitSync(ctx.NamespacedName)
1445+
Expect(permit).To(BeFalse(), "expect not permit immediately after sync")
1446+
1447+
// Now set a very short duration and verify permit is granted after waiting
1448+
templateEngine.setTimeOfLastSync()
1449+
templateEngine.syncRetryDuration = 1 * time.Microsecond
1450+
time.Sleep(10 * time.Millisecond) // Wait longer than syncRetryDuration
1451+
permit = templateEngine.permitSync(ctx.NamespacedName)
1452+
Expect(permit).To(BeTrue(), "expect permit after retry duration elapsed")
1453+
})
1454+
})

0 commit comments

Comments
 (0)