|
17 | 17 | package thin |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "reflect" |
21 | | - "testing" |
22 | | - |
23 | 20 | "github.com/fluid-cloudnative/fluid/pkg/utils/fake" |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
24 | 23 | corev1 "k8s.io/api/core/v1" |
25 | 24 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
26 | 25 | ) |
27 | 26 |
|
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 |
41 | 29 |
|
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 | + }, |
58 | 61 | }, |
59 | 62 | }, |
60 | 63 | }, |
61 | | - }, |
62 | | - } |
| 64 | + } |
63 | 65 |
|
64 | | - client := fake.NewFakeClientWithScheme(testScheme, pvc, pv) |
| 66 | + client := fake.NewFakeClientWithScheme(testScheme, pvc, pv) |
65 | 67 |
|
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 | + }) |
72 | 75 |
|
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", |
93 | 81 | }, |
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 |
114 | 99 |
|
115 | | -func TestThinEngine_extractVolumeMountOptions(t *testing.T) { |
116 | | - engine := ThinEngine{} |
| 100 | + BeforeEach(func() { |
| 101 | + engine = ThinEngine{} |
| 102 | + }) |
117 | 103 |
|
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{ |
127 | 116 | ObjectMeta: metav1.ObjectMeta{ |
128 | 117 | Annotations: map[string]string{ |
129 | 118 | corev1.MountOptionAnnotation: "rw,noexec,testOpts", |
130 | 119 | }, |
131 | 120 | }, |
132 | 121 | }, |
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{ |
139 | 127 | ObjectMeta: metav1.ObjectMeta{ |
140 | 128 | Annotations: map[string]string{}, |
141 | 129 | }, |
142 | 130 | Spec: corev1.PersistentVolumeSpec{ |
143 | 131 | MountOptions: []string{"ro", "noexec"}, |
144 | 132 | }, |
145 | 133 | }, |
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