@@ -20,6 +20,7 @@ describe('Metalk8sLocalVolumeProvider', () => {
2020 const mockCustomObjectsApi = {
2121 listClusterCustomObject : jest . fn ( ) ,
2222 deleteClusterCustomObject : jest . fn ( ) ,
23+ getClusterCustomObject : jest . fn ( ) ,
2324 } as unknown as CustomObjectsApi ;
2425
2526 const mockCoreV1Api = {
@@ -160,31 +161,22 @@ describe('Metalk8sLocalVolumeProvider', () => {
160161 } ) ;
161162 } ) ;
162163
163- describe ( 'detachVolumes ' , ( ) => {
164- it ( 'should detach hardware volumes and virtual volumes ' , async ( ) => {
164+ describe ( 'detachVolume ' , ( ) => {
165+ it ( 'should detach volume ' , async ( ) => {
165166 //S
166167 (
167168 mockCustomObjectsApi . deleteClusterCustomObject as jest . Mock
168169 ) . mockResolvedValue ( {
169170 body : { } ,
170171 } ) ;
171172 //E
172- await provider . detachVolumes ( [
173- {
174- IP : '192.168.1.100' ,
175- devicePath : '/dev/sda' ,
176- volumeType : VolumeType . Hardware ,
177- nodeName : 'test-node' ,
178- metadata : { name : 'test-volume' } ,
179- } ,
180- {
181- IP : '192.168.1.100' ,
182- devicePath : 'test-lvm' ,
183- volumeType : VolumeType . Virtual ,
184- nodeName : 'test-node' ,
185- metadata : { name : 'test-lvm' } ,
186- } ,
187- ] ) ;
173+ await provider . detachVolume ( {
174+ IP : '192.168.1.100' ,
175+ devicePath : '/dev/sda' ,
176+ volumeType : VolumeType . Hardware ,
177+ nodeName : 'test-node' ,
178+ metadata : { name : 'test-volume' } ,
179+ } ) ;
188180 //V
189181 expect (
190182 mockCustomObjectsApi . deleteClusterCustomObject ,
@@ -195,15 +187,6 @@ describe('Metalk8sLocalVolumeProvider', () => {
195187 'test-volume' ,
196188 { } ,
197189 ) ;
198- expect (
199- mockCustomObjectsApi . deleteClusterCustomObject ,
200- ) . toHaveBeenCalledWith (
201- MOCK_GROUP ,
202- MOCK_VERSION ,
203- MOCK_PLURAL ,
204- 'test-lvm' ,
205- { } ,
206- ) ;
207190 } ) ;
208191
209192 it ( 'should raise an error if metalk8s volume deletion fails' , async ( ) => {
@@ -213,18 +196,86 @@ describe('Metalk8sLocalVolumeProvider', () => {
213196 ) . mockRejectedValue ( new Error ( 'Failed to delete metalk8s volume' ) ) ;
214197 //E+V
215198 await expect (
216- provider . detachVolumes ( [
217- {
218- IP : '192.168.1.100' ,
219- devicePath : '/dev/sda' ,
220- volumeType : VolumeType . Hardware ,
221- nodeName : 'test-node' ,
222- metadata : { name : 'test-volume' } ,
223- } ,
224- ] ) ,
199+ provider . detachVolume ( {
200+ IP : '192.168.1.100' ,
201+ devicePath : '/dev/sda' ,
202+ volumeType : VolumeType . Hardware ,
203+ nodeName : 'test-node' ,
204+ metadata : { name : 'test-volume' } ,
205+ } ) ,
225206 ) . rejects . toThrow (
226207 'Failed to delete MetalK8s volume test-volume: Failed to delete metalk8s volume' ,
227208 ) ;
228209 } ) ;
229210 } ) ;
211+
212+ describe ( 'isVolumeProvisioned' , ( ) => {
213+ it ( 'should return false if the volume is not yet provisioned' , async ( ) => {
214+ //S
215+ (
216+ mockCustomObjectsApi . getClusterCustomObject as jest . Mock
217+ ) . mockResolvedValue ( {
218+ status : { conditions : [ { type : 'Ready' , status : 'Unknown' } ] } ,
219+ } ) ;
220+
221+ //E
222+ const result = await provider . isVolumeProvisioned ( {
223+ IP : '192.168.1.100' ,
224+ devicePath : '/dev/sda' ,
225+ volumeType : VolumeType . Hardware ,
226+ nodeName : 'test-node' ,
227+ volumeName : 'test-volume' ,
228+ } ) ;
229+ //V
230+ expect ( result ) . toBe ( false ) ;
231+ } ) ;
232+
233+ it ( 'should return true if the volume is provisioned' , async ( ) => {
234+ //S
235+ (
236+ mockCustomObjectsApi . getClusterCustomObject as jest . Mock
237+ ) . mockResolvedValue ( {
238+ status : { conditions : [ { type : 'Ready' , status : 'True' } ] } ,
239+ } ) ;
240+ //E
241+ const result = await provider . isVolumeProvisioned ( {
242+ IP : '192.168.1.100' ,
243+ devicePath : '/dev/sda' ,
244+ volumeType : VolumeType . Hardware ,
245+ nodeName : 'test-node' ,
246+ volumeName : 'test-volume' ,
247+ } ) ;
248+ //V
249+ expect ( result ) . toBe ( true ) ;
250+ } ) ;
251+
252+ it ( 'should raise an error if the volume is failed to provisioned' , async ( ) => {
253+ //S
254+ (
255+ mockCustomObjectsApi . getClusterCustomObject as jest . Mock
256+ ) . mockResolvedValue ( {
257+ status : {
258+ conditions : [
259+ {
260+ type : 'Ready' ,
261+ status : 'False' ,
262+ reason : 'Volume is not provisioned' ,
263+ } ,
264+ ] ,
265+ } ,
266+ } ) ;
267+ //E+V
268+ await expect (
269+ provider . isVolumeProvisioned ( {
270+ IP : '192.168.1.100' ,
271+ devicePath : '/dev/sda' ,
272+ volumeType : VolumeType . Hardware ,
273+ nodeName : 'test-node' ,
274+ volumeName : 'test-volume' ,
275+ } ) ,
276+ ) . rejects . toThrow (
277+ 'Volume test-volume failed to provisioned: Volume is not provisioned' ,
278+ ) ;
279+ } ) ;
280+ } ) ;
230281} ) ;
0 commit comments