Skip to content

Commit df67adf

Browse files
authored
test(thin): migrate transform_config_test.go to Ginkgo/Gomega (#5627)
Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent ee4a80f commit df67adf

1 file changed

Lines changed: 91 additions & 115 deletions

File tree

pkg/ddc/thin/transform_config_test.go

Lines changed: 91 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -17,146 +17,122 @@
1717
package thin
1818

1919
import (
20-
"reflect"
21-
"testing"
22-
2320
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
2423
corev1 "k8s.io/api/core/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
)
2726

28-
func TestThinEngine_extractVolumeInfo(t *testing.T) {
29-
pvc := &corev1.PersistentVolumeClaim{
30-
ObjectMeta: metav1.ObjectMeta{
31-
Name: "test-pvc",
32-
Namespace: "fluid",
33-
},
34-
Spec: corev1.PersistentVolumeClaimSpec{
35-
VolumeName: "test-pv",
36-
},
37-
Status: corev1.PersistentVolumeClaimStatus{
38-
Phase: corev1.ClaimBound,
39-
},
40-
}
27+
var _ = Describe("ThinEngine extractVolumeInfo", func() {
28+
var engine ThinEngine
4129

42-
pv := &corev1.PersistentVolume{
43-
ObjectMeta: metav1.ObjectMeta{
44-
Name: "test-pv",
45-
},
46-
Spec: corev1.PersistentVolumeSpec{
47-
MountOptions: []string{"rw", "noexec"},
48-
PersistentVolumeSource: corev1.PersistentVolumeSource{
49-
CSI: &corev1.CSIPersistentVolumeSource{
50-
NodePublishSecretRef: &corev1.SecretReference{
51-
Name: "my-secret",
52-
Namespace: "node-publish-secrets",
53-
},
54-
VolumeHandle: "test-pv",
55-
VolumeAttributes: map[string]string{
56-
"test-attr": "true",
57-
"test-attr2": "foobar",
30+
BeforeEach(func() {
31+
pvc := &corev1.PersistentVolumeClaim{
32+
ObjectMeta: metav1.ObjectMeta{
33+
Name: "test-pvc",
34+
Namespace: "fluid",
35+
},
36+
Spec: corev1.PersistentVolumeClaimSpec{
37+
VolumeName: "test-pv",
38+
},
39+
Status: corev1.PersistentVolumeClaimStatus{
40+
Phase: corev1.ClaimBound,
41+
},
42+
}
43+
44+
pv := &corev1.PersistentVolume{
45+
ObjectMeta: metav1.ObjectMeta{
46+
Name: "test-pv",
47+
},
48+
Spec: corev1.PersistentVolumeSpec{
49+
MountOptions: []string{"rw", "noexec"},
50+
PersistentVolumeSource: corev1.PersistentVolumeSource{
51+
CSI: &corev1.CSIPersistentVolumeSource{
52+
NodePublishSecretRef: &corev1.SecretReference{
53+
Name: "my-secret",
54+
Namespace: "node-publish-secrets",
55+
},
56+
VolumeHandle: "test-pv",
57+
VolumeAttributes: map[string]string{
58+
"test-attr": "true",
59+
"test-attr2": "foobar",
60+
},
5861
},
5962
},
6063
},
61-
},
62-
}
64+
}
6365

64-
client := fake.NewFakeClientWithScheme(testScheme, pvc, pv)
66+
client := fake.NewFakeClientWithScheme(testScheme, pvc, pv)
6567

66-
engine := ThinEngine{
67-
name: "thin-test",
68-
namespace: "fluid",
69-
Client: client,
70-
Log: fake.NullLogger(),
71-
}
68+
engine = ThinEngine{
69+
name: "thin-test",
70+
namespace: "fluid",
71+
Client: client,
72+
Log: fake.NullLogger(),
73+
}
74+
})
7275

73-
tests := []struct {
74-
name string
75-
pvcName string
76-
wantCsiInfo *corev1.CSIPersistentVolumeSource
77-
wantMountOptions []string
78-
wantErr bool
79-
}{
80-
{
81-
name: "testExtractVolumeInfo",
82-
pvcName: "test-pvc",
83-
wantCsiInfo: &corev1.CSIPersistentVolumeSource{
84-
NodePublishSecretRef: &corev1.SecretReference{
85-
Name: "my-secret",
86-
Namespace: "node-publish-secrets",
87-
},
88-
VolumeHandle: "test-pv",
89-
VolumeAttributes: map[string]string{
90-
"test-attr": "true",
91-
"test-attr2": "foobar",
92-
},
76+
It("should extract volume info correctly", func() {
77+
wantCsiInfo := &corev1.CSIPersistentVolumeSource{
78+
NodePublishSecretRef: &corev1.SecretReference{
79+
Name: "my-secret",
80+
Namespace: "node-publish-secrets",
9381
},
94-
wantMountOptions: []string{"rw", "noexec"},
95-
wantErr: false,
96-
},
97-
}
98-
for _, tt := range tests {
99-
t.Run(tt.name, func(t *testing.T) {
100-
gotCsiInfo, gotMountOptions, err := engine.extractVolumeInfo(tt.pvcName)
101-
if (err != nil) != tt.wantErr {
102-
t.Errorf("ThinEngine.extractVolumeInfo() error = %v, wantErr %v", err, tt.wantErr)
103-
return
104-
}
105-
if !reflect.DeepEqual(gotCsiInfo, tt.wantCsiInfo) {
106-
t.Errorf("ThinEngine.extractVolumeInfo() gotCsiInfo = %v, want %v", gotCsiInfo, tt.wantCsiInfo)
107-
}
108-
if !reflect.DeepEqual(gotMountOptions, tt.wantMountOptions) {
109-
t.Errorf("ThinEngine.extractVolumeInfo() gotMountOptions = %v, want %v", gotMountOptions, tt.wantMountOptions)
110-
}
111-
})
112-
}
113-
}
82+
VolumeHandle: "test-pv",
83+
VolumeAttributes: map[string]string{
84+
"test-attr": "true",
85+
"test-attr2": "foobar",
86+
},
87+
}
88+
wantMountOptions := []string{"rw", "noexec"}
89+
90+
gotCsiInfo, gotMountOptions, err := engine.extractVolumeInfo("test-pvc")
91+
Expect(err).NotTo(HaveOccurred())
92+
Expect(gotCsiInfo).To(Equal(wantCsiInfo))
93+
Expect(gotMountOptions).To(Equal(wantMountOptions))
94+
})
95+
})
96+
97+
var _ = Describe("ThinEngine extractVolumeMountOptions", func() {
98+
var engine ThinEngine
11499

115-
func TestThinEngine_extractVolumeMountOptions(t *testing.T) {
116-
engine := ThinEngine{}
100+
BeforeEach(func() {
101+
engine = ThinEngine{}
102+
})
117103

118-
tests := []struct {
119-
name string
120-
pv *corev1.PersistentVolume
121-
wantMountOptions []string
122-
wantErr bool
123-
}{
124-
{
125-
name: "test_mount_options_in_annotation",
126-
pv: &corev1.PersistentVolume{
104+
DescribeTable("extracting mount options from PV",
105+
func(pv *corev1.PersistentVolume, wantMountOptions []string, wantErr bool) {
106+
gotMountOptions, err := engine.extractVolumeMountOptions(pv)
107+
if wantErr {
108+
Expect(err).To(HaveOccurred())
109+
} else {
110+
Expect(err).NotTo(HaveOccurred())
111+
Expect(gotMountOptions).To(Equal(wantMountOptions))
112+
}
113+
},
114+
Entry("mount options in annotation",
115+
&corev1.PersistentVolume{
127116
ObjectMeta: metav1.ObjectMeta{
128117
Annotations: map[string]string{
129118
corev1.MountOptionAnnotation: "rw,noexec,testOpts",
130119
},
131120
},
132121
},
133-
wantMountOptions: []string{"rw", "noexec", "testOpts"},
134-
wantErr: false,
135-
},
136-
{
137-
name: "test_mount_options_in_proerty",
138-
pv: &corev1.PersistentVolume{
122+
[]string{"rw", "noexec", "testOpts"},
123+
false,
124+
),
125+
Entry("mount options in property",
126+
&corev1.PersistentVolume{
139127
ObjectMeta: metav1.ObjectMeta{
140128
Annotations: map[string]string{},
141129
},
142130
Spec: corev1.PersistentVolumeSpec{
143131
MountOptions: []string{"ro", "noexec"},
144132
},
145133
},
146-
wantMountOptions: []string{"ro", "noexec"},
147-
wantErr: false,
148-
},
149-
}
150-
for _, tt := range tests {
151-
t.Run(tt.name, func(t *testing.T) {
152-
gotMountOptions, err := engine.extractVolumeMountOptions(tt.pv)
153-
if (err != nil) != tt.wantErr {
154-
t.Errorf("ThinEngine.extractVolumeMountOptions() error = %v, wantErr %v", err, tt.wantErr)
155-
return
156-
}
157-
if !reflect.DeepEqual(gotMountOptions, tt.wantMountOptions) {
158-
t.Errorf("ThinEngine.extractVolumeMountOptions() = %v, want %v", gotMountOptions, tt.wantMountOptions)
159-
}
160-
})
161-
}
162-
}
134+
[]string{"ro", "noexec"},
135+
false,
136+
),
137+
)
138+
})

0 commit comments

Comments
 (0)